Skip to main content
Version: v2

ISharedCounter Interface

A shared object that holds a number that can be incremented or decremented.

This API is provided for existing users, but is not recommended for new users.

To use, import via @fluidframework/counter/legacy.

For more information about our API support guarantees, see here.

Signature​

export interface ISharedCounter extends ISharedObject<ISharedCounterEvents>

Extends: ISharedObject<ISharedCounterEvents>

Remarks​

Note that SharedCounter only operates on integer values. This is validated at runtime.

Usage​

Example 1​

Creating a SharedCounter

First, get the factory and call create(runtime, id) with a runtime and string ID:

const factory = SharedCounter.getFactory();
const counter = factory.create(this.runtime, id);

The initial value of a new SharedCounter is 0. If you wish to initialize the counter to a different value, you may call increment(incrementAmount) before attaching the Container, or before inserting it into an existing shared object.

Example 2​

Using the SharedCounter

Once created, you can call increment(incrementAmount) to modify the value with either a positive or negative number:

counter.increment(10); // add 10 to the counter value
counter.increment(-5); // subtract 5 from the counter value

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}`);
});

Properties​

PropertyAlertsTypeDescription
valueBetanumberThe counter value.

Methods​

MethodAlertsReturn TypeDescription
increment(incrementAmount)BetavoidIncrements or decrements the value. Must only increment or decrement by a whole number value.

Property Details​

value​

The counter value.

This API is provided for existing users, but is not recommended for new users.

For more information about our API support guarantees, see here.

Signature​

value: number;

Type: number

Remarks​

Must be a whole number.

Method Details​

increment​

Increments or decrements the value. Must only increment or decrement by a whole number value.

This API is provided for existing users, but is not recommended for new users.

For more information about our API support guarantees, see here.

Signature​

increment(incrementAmount: number): void;

Parameters​

ParameterTypeDescription
incrementAmountnumberA whole number to increment or decrement by.