Changes API: How to Subscribe to Index Changes

Following methods allow you to subscribe to index changes:

forIndex

Index changes for one index can be observed using forIndex() method.

Syntax

store.changes().forIndex(indexName);
Parameters
indexName string Name of an index for which notifications will be processed.
Return value
IChangesObservable<IndexChange> Observable that allows to add subscriptions to notifications for index with given name.

Example

store.changes().forIndex("Orders/All")
    .on("data", change => {
        switch (change.type) {
            case "None":
                // do something
                break;
            case "BatchCompleted":
                // do something
                break;
            case "IndexAdded":
                // do something
                break;
            case "IndexRemoved":
                // do something
                break;
            case "IndexDemotedToIdle":
                // do something
                break;
            case "IndexPromotedFromIdle":
                // do something
                break;
            case "IndexDemotedToDisabled":
                // do something
                break;
            case "IndexMarkedAsErrored":
                // do something
                break;
            case "SideBySideReplace":
                // do something
                break;
            case "Renamed":
                // do something
                break;
            case "IndexPaused":
                // do something
                break;
            case "LockModeChanged":
                // do something
                break;
            case "PriorityChanged":
                // do something
                break;
            default:
                throw new Error("Not supported.");
        }
    });

forAllIndexes

Index changes for all indexex can be observed using forAllIndexes() method.

Return value
IChangesObservable<IndexChange> Observable that allows to add subscriptions to notifications for all indexes.

Syntax

store.changes().forAllIndexes();

Example

store.changes().forAllIndexes()
    .on("data", change => {
        console.log(change.type + " on index " + change.name);
    });

IndexChange

Properties

Name Type Description
type IndexChangeTypes Change type
name string Index name
etag number Index Etag

IndexChangeTypes

Name
None
BatchCompleted
IndexAdded
IndexRemoved
IndexDemotedToIdle
IndexPromotedToIdle
IndexDemotedToDisabled
IndexMarkedAsErrored
SideBySideReplace
Renamed
IndexPaused
LockModeChanged
PriorityChanged