Changes API: How to Subscribe to Counter Changes
Subscribe to counter changes using ForCounter, ForCounterOfDocument,
ForCountersOfDocument, or ForAllCounters.
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.
ForCounter
Counter changes can be observed using ForCounter method. This will subscribe changes from all counters with a given name, no matter in what document counter was changed.
Syntax
IChangesObservable<CounterChange> ForCounter(string counterName);
| Parameters | ||
|---|---|---|
| counterName | string | Name of a counter to subscribe to. |
| Return Value | |
|---|---|
| IChangesObservable<CounterChange> | Observable that allows to add subscriptions to counter notifications. |
Example
IDisposable subscription = store
.Changes()
.ForCounter("Likes")
.Subscribe(
change =>
{
switch (change.Type)
{
case CounterChangeTypes.Increment:
// do something
break;
}
});
ForCounterOfDocument
Specific counter changes of a given document can be observed using ForCounterOfDocument method.
Syntax
IChangesObservable<CounterChange> ForCounterOfDocument(string documentId, string counterName);
| Parameters | ||
|---|---|---|
| documentId | string | ID of a document to subscribe to. |
| counterName | string | Name of a counter to subscribe to. |
| Return Value | |
|---|---|
| IChangesObservable<CounterChange> | Observable that allows to add subscriptions to counter notifications. |
Example
IDisposable subscription = store
.Changes()
.ForCounterOfDocument("companies/1-A", "Likes")
.Subscribe(
change =>
{
switch (change.Type)
{
case CounterChangeTypes.Increment:
// do something
break;
}
});
ForCountersOfDocument
Counter changes of a specified document can be observed using ForCountersOfDocument method.
Syntax
IChangesObservable<CounterChange> ForCountersOfDocument(string documentId);
| Parameters | ||
|---|---|---|
| documentId | string | ID of a document to subscribe to. |
| Return Value | |
|---|---|
| IChangesObservable<CounterChange> | Observable that allows to add subscriptions to counter notifications. |
Example
IDisposable subscription = store
.Changes()
.ForCountersOfDocument("companies/1-A")
.Subscribe(
change =>
{
switch (change.Type)
{
case CounterChangeTypes.Increment:
// do something
break;
}
});
ForAllCounters
Changes for all counters can be observed using ForAllCounters method.
Syntax
IChangesObservable<CounterChange> ForAllCounters();
| Return Value | |
|---|---|
| IChangesObservable<CounterChange> | Observable that allows to add subscriptions to counter notifications. |
Example
IDisposable subscription = store
.Changes()
.ForAllCounters()
.Subscribe(
change =>
{
switch (change.Type)
{
case CounterChangeTypes.Increment:
// do something
break;
}
});
CounterChange and CounterChangeTypes syntax
- CounterChange
- CounterChangeTypes
The notification object delivered to subscribers when a counter changes.
class CounterChange
{
CounterChangeTypes Type
string Name
long Value
string DocumentId
string CollectionName
string ChangeVector
}
| Property | Type | Description |
|---|---|---|
| Type | CounterChangeTypes | Counter change type enum |
| Name | string | Counter name |
| Value | long | Counter value after the change |
| DocumentId | string | Counter document identifier |
| CollectionName | string | Document's collection name |
| ChangeVector | string | Counter's ChangeVector |
enum CounterChangeTypes
{
None,
Put,
Delete,
Increment,
}
| Name | Value |
|---|---|
| None | 0 |
| Put | 1 |
| Delete | 2 |
| Increment | 4 |