SharedCounter Class

Packages > @fluidframework/counter > SharedCounter

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

Signature:

export declare class SharedCounter extends SharedObject<ISharedCounterEvents> implements ISharedCounter 

Extends: SharedObject<ISharedCounterEvents>

Implements: ISharedCounter

Remarks

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); // 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

List of properties for this class
Property Modifiers Type Description
value number The counter value.

Methods

List of methods on this class
Method Modifiers Description
applyStashedOp()
create(runtime, id) static Create a new shared counter
getFactory() static Get a factory for SharedCounter to register with the data store.
increment(incrementAmount) Increments or decrements the value. Must only increment or decrement by a whole number value.
loadCore(storage) Allows the distributed data type to perform custom loading
onDisconnect() Call back on disconnect
processCore(message, local, localOpMetadata) Process a counter operation
registerCore()
snapshotCore(serializer) Create a snapshot for the counter

Properties

value

The counter value.

Signature:

get value(): number;

Methods

applyStashedOp

Signature:

protected applyStashedOp(): void;

Returns:

void

create

Create a new shared counter

Signature:

static create(runtime: IFluidDataStoreRuntime, id?: string): SharedCounter;

Parameters

List of parameters
Parameter Type Description
runtime IFluidDataStoreRuntime data store runtime the new shared counter belongs to
id string optional name of the shared counter

Returns:

SharedCounter

newly create shared counter (but not attached yet)

getFactory

Get a factory for SharedCounter to register with the data store.

Signature:

static getFactory(): IChannelFactory;

Returns:

IChannelFactory

a factory that creates and load SharedCounter

increment

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

Signature:

increment(incrementAmount: number): void;

Parameters

List of parameters
Parameter Type Description
incrementAmount number a whole number to increment or decrement by

Returns:

void

loadCore

Allows the distributed data type to perform custom loading

Signature:

protected loadCore(storage: IChannelStorageService): Promise<void>;

Parameters

List of parameters
Parameter Type Description
storage IChannelStorageService

Returns:

Promise<void>

onDisconnect

Call back on disconnect

Signature:

protected onDisconnect(): void;

Returns:

void

processCore

Process a counter operation

Signature:

protected processCore(message: ISequencedDocumentMessage, local: boolean, localOpMetadata: unknown): void;

Parameters

List of parameters
Parameter Type Description
message ISequencedDocumentMessage the message to prepare
local boolean whether the message was sent by the local client
localOpMetadata unknown For local client messages, this is the metadata that was submitted with the message. For messages from a remote client, this will be undefined.

Returns:

void

registerCore

Signature:

protected registerCore(): void;

Returns:

void

snapshotCore

Create a snapshot for the counter

Signature:

protected snapshotCore(serializer: IFluidSerializer): ITree;

Parameters

List of parameters
Parameter Type Description
serializer IFluidSerializer

Returns:

ITree

the snapshot of the current state of the counter