Events

What is events?

Events let use listen to database changes, like changing the field or entity, adding or removing entities.

How to use?

Add your listeners, like this

    private void AddListeners()
    {
        var events = BGRepo.I.Events;

        //listen to particular entity (row)
        events.AddEntityUpdatedListener(new BGId("some-entity-id"), EntityUpdated);
        events.AddEntityDeletedListener(new BGId("some-entity-id"), EntityDeleted);

        //listen to all entities (rows)
        events.AddAnyEntityAddedListener(new BGId("some-meta-id"), SomeEntityAdded);
        events.AddAnyEntityDeletedListener(new BGId("some-meta-id"), SomeEntityDeleted);
        events.AddAnyEntityUpdatedListener(new BGId("some-meta-id"), SomeEntityUpdated);

        //listen to some field change
        events.AddFieldListener(new BGId("some-field-id"), FieldChanged);
    }

    //------------------------------------------------
    //            Field changed
    //------------------------------------------------
    private void FieldChanged(object sender, BGEventArgsField e)
    {
        Debug.Log("The Field is changed " + e.Entity.Name);
    }

    //------------------------------------------------
    //             any entity (row)
    //------------------------------------------------
    private void SomeEntityUpdated(object sender, BGEventArgsAnyEntityUpdated e)
    {
        Debug.Log("Some Entity Updated " + e.Entity.Name);
    }

    private void SomeEntityDeleted(object sender, BGEventArgsAnyEntity e)
    {
        Debug.Log("Some Entity Deleted " + e.Entity.Id);
    }

    private void SomeEntityAdded(object sender, BGEventArgsAnyEntity e)
    {
        //note, fields are not available here
        Debug.Log("Some Entity Added " + e.Entity.Id);
    }

    //------------------------------------------------
    //             particular entity (row)
    //------------------------------------------------
    private void EntityDeleted(object sender, BGEventArgsEntity e)
    {
        Debug.Log("The Entity Deleted " + e.Entity.Id);
    }

    private void EntityUpdated(object sender, BGEventArgsEntityUpdated e)
    {
        Debug.Log("The Entity Updated " + e.Entity.Name);
    }

Do not forget to remove listeners, as soon as they are not needed. For example in you MonoBehaviour classes, use OnDestroy method to remove listeners.

Use corresponding events.RemoveXXX method to remove a listener.