Model Lifecycle of the JS SDK

To keep your application responsive, you should listen to one or more of the model lifecycle events described in this document and update your list of known model instances in your application accordingly.

Each of our models emit events during their lifetimes to help integrators react to model changes (addition, deletion, updates, etc.).

Lifecycle Methods

As a brief primer, all models emit the following events:

EventsDescription
beforeInjectEmitted before the model is created or before properties on the model change
afterInjectEmitted after the model has been created or after properties on the model change
afterCreateEmitted after the model is created
beforeEjectEmitted before the model is removed
afterEjectEmitted after the model is removed

To keep your application responsive, you should listen to one or more of these model lifecycle events and update your list of known models accordingly.

Model Added or Updated

We'll use an Organization model as an example.

The afterInject event can be used to both determine when your user has been added to an organization and when an organization changes.

Here is an example of listening to that event:

client.models.Organization.on('afterInject', (organizationStore, organization) => {
console.log('Organization added / changed:', organization.displayName);
});

In the case of receiving new or changed messages, you may see changes to the Message model.

client.models.Message.on('afterInject', (messageStore, message) => {
console.log('Message added / changed:', message.body);
});

Several cases may cause changes to the Conversation model model. The specific kind of change isn't specified in the event. Examples of changes: new message, message recalled, lastMessage changed, message status changed, unreadCount changed.

client.models.Conversation.on('afterInject', (conversationStore, conversation) => {
console.log('Conversation added / changed:', conversation);
});

Model Removed

The afterEject event can be used to determine when an organization has been deleted, and therefore should be removed from view.

Here is an example of listening to that event:

client.models.Organization.on('afterEject', (organizationStore, organization) => {
console.log('Organization deleted:', organization.displayName);
});

In the case of message expiration, you may see changes to the Message model:

client.models.Message.on('afterEject', (messageStore, message) => {
console.log('Message removed:', message.body);
});

In the case of being removed from a conversation, you may see changes to the Conversation model:

client.models.Conversation.on('afterEject', (conversationStore, conversation) => {
console.log('Conversation removed:', conversation);
});

Caveats

For Groups, because we receive these updates, it is unnecessary to continually refresh the list of groups.

When conversation is added, deleted, or updated, the $lastModified value can change as the result of some other action as well, such as a new message being received for this conversation.