Events
Subscription
The EventDispatcher is a static class that may be used to register to Events.
import { EventDispatcher, REPOSITORY_EVENTS } from '@adactive/adsum-client-api';
const subscriber = (data) => {
console.log(data);
};
// Subscribe to all CREATE events fire by the CategoryRepository
EventDispatcher.subscribe(em.getRepository("Category"), REPOSITORY_EVENTS.CREATE, subscriber);
// Unsubscribe using the subscriber
EventDispatcher.unsubscribe(em.getRepository("Category"), REPOSITORY_EVENTS.CREATE, subscriber);
CREATE Event
import { EventDispatcher, REPOSITORY_EVENTS } from '@adactive/adsum-client-api';
const subscriber = (createdEntity) => {};
EventDispatcher.subscribe(em.getRepository("Category"), REPOSITORY_EVENTS.CREATE, subscriber);
UPDATE Event
import { EventDispatcher, REPOSITORY_EVENTS } from '@adactive/adsum-client-api';
const subscriber = ([updatedEntity, previousEntity, changes]) => {};
EventDispatcher.subscribe(em.getRepository("Category"), REPOSITORY_EVENTS.UPDATE, subscriber);
REMOVE Event
import { EventDispatcher, REPOSITORY_EVENTS } from '@adactive/adsum-client-api';
const subscriber = (removedEntity) => {};
EventDispatcher.subscribe(em.getRepository("Category"), REPOSITORY_EVENTS.REMOVE, subscriber);
IDENTIFIER_WILL_CHANGE Event
This event is fired during a flush (i.e. the action to send all your pending modifications to the API), BEFORE the object identifier is updated according to the remote server.
Usage: When a new entity is created, the EntityManager will assign a Symbol() as id. When flushing, the entity is saved on the remote server which will send it's real id.
import { EventDispatcher, REPOSITORY_EVENTS } from '@adactive/adsum-client-api';
const subscriber = ([newId, previousId]) => {};
EventDispatcher.subscribe(em.getRepository("Category"), REPOSITORY_EVENTS.IDENTIFIER_WILL_CHANGE, subscriber);
IDENTIFIER_DID_CHANGE Event
This event is fired during a flush, AFTER the object identifier is updated according to the remote server.
Usage: When a new entity is created, the EntityManager will assign a Symbol() as id. When flushing, the entity is saved on the remote server which will send it's real id.
import { EventDispatcher, REPOSITORY_EVENTS } from '@adactive/adsum-client-api';
const subscriber = ([newId, previousId]) => {};
EventDispatcher.subscribe(em.getRepository("Category"), REPOSITORY_EVENTS.IDENTIFIER_DID_CHANGE, subscriber);
Default listeners
There are many default listeners, used to keep model integrity on changes. For example, if you add a category to the poi entity then the category need to be added to the category entity.
Listeners aimed to be changed according to Adsum model. So each default listeners update will introduce a patch fix (as it's an internal process). To prevent any issue with those updates, just register to events as it will be difficult to guess the cascade modifications.