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.
- In this article:
ForIndex
Index changes for one index can be observed using ForIndex method.
Syntax
IChangesObservable<IndexChange> ForIndex(string 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
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
- IndexChange
- IndexChangeTypes
The notification object delivered to subscribers when an index changes.
class IndexChange
{
IndexChangeTypes Type
string Name
}
| Property | Type | Description |
|---|---|---|
| Type | IndexChangeTypes | Change type |
| Name | string | Index name |
enum IndexChangeTypes
{
None,
BatchCompleted,
IndexAdded,
IndexRemoved,
IndexDemotedToIdle,
IndexPromotedFromIdle,
IndexDemotedToDisabled,
IndexMarkedAsErrored,
SideBySideReplace,
Renamed,
IndexPaused,
LockModeChanged,
PriorityChanged,
RollingIndexChanged,
}
| Name | Value |
|---|---|
| None | 0 |
| BatchCompleted | 1 |
| IndexAdded | 8 |
| IndexRemoved | 16 |
| IndexDemotedToIdle | 32 |
| IndexPromotedFromIdle | 64 |
| IndexDemotedToDisabled | 256 |
| IndexMarkedAsErrored | 512 |
| SideBySideReplace | 1024 |
| Renamed | 2048 |
| IndexPaused | 4096 |
| LockModeChanged | 8192 |
| PriorityChanged | 16384 |
| RollingIndexChanged | 65536 |