@fluidframework/counter
A SharedCounter
is a shared object which holds a number that can be incremented or decremented.
Creation
To create a SharedCounter
, get the factory and call create with a runtime and string ID:
const factory = SharedCounter.getFactory();
const counter = factory.create(this.runtime, id) as SharedCounter;
Usage
Once created, you can call increment
to modify the value with either a positive or negative number:
counter.increment(10);
counter.increment(-5);
To observe changes to the value (including those from remote clients), register for the "incremented"
event:
counter.on("incremented", (incrementAmount, newValue) => {
console.log(`The counter incremented by ${incrementAmount} and now has a value of ${newValue}`);
});