Skip to main content

Changes API: How to Subscribe to Index Changes

Subscribe to index changes using ForIndex or ForAllIndexes.

The Subscribe method on the observable these methods return accepts only an IObserver object by default.
To instead subscribe with a delegate, like the change => ... lambda used in the examples below, add the System.Reactive package to your project.
This adds Subscribe overloads that take delegates, including ones with error and completion handlers.

ForIndex​

Index changes for one index can be observed using ForIndex method.

Syntax​

IChangesObservable<IndexChange> ForIndex(string indexName);

Parameters
indexNamestringName 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​

IDisposable subscription = store
.Changes()
.ForIndex("Orders/All")
.Subscribe(
change =>
{
switch (change.Type)
{
case IndexChangeTypes.None:
//Do someting
break;
case IndexChangeTypes.BatchCompleted:
//Do someting
break;
case IndexChangeTypes.IndexAdded:
//Do someting
break;
case IndexChangeTypes.IndexRemoved:
//Do someting
break;
case IndexChangeTypes.IndexDemotedToIdle:
//Do someting
break;
case IndexChangeTypes.IndexPromotedFromIdle:
//Do someting
break;
case IndexChangeTypes.IndexDemotedToDisabled:
//Do someting
break;
case IndexChangeTypes.IndexMarkedAsErrored:
//Do someting
break;
case IndexChangeTypes.SideBySideReplace:
//Do someting
break;
case IndexChangeTypes.IndexPaused:
//Do someting
break;
case IndexChangeTypes.LockModeChanged:
//Do someting
break;
case IndexChangeTypes.PriorityChanged:
//Do someting
break;
default:
throw new ArgumentOutOfRangeException();
}
});

ForAllIndexes​

Index changes for all indexex can be observed using ForAllIndexes method.

Syntax​

IChangesObservable<IndexChange> ForAllIndexes();

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

Example​

IDisposable subscription = store
.Changes()
.ForAllIndexes()
.Subscribe(change => Console.WriteLine("{0} on index {1}", change.Type, change.Name));

IndexChange and IndexChangeTypes syntax​

The notification object delivered to subscribers when an index changes.

class IndexChange
{
IndexChangeTypes Type
string Name
}

PropertyTypeDescription
TypeIndexChangeTypesChange type
NamestringIndex name

In this article