Skip to main content
Module

x/automerge/types.ts>Counter

A JSON-like data structure (a CRDT) that can be modified concurrently by different users, and merged again automatically.
Go to Latest
class Counter
Re-export
import { Counter } from "https://deno.land/x/automerge@2.2.0/types.ts";

The most basic CRDT: an integer value that can be changed only by incrementing and decrementing. Since addition of integers is commutative, the value trivially converges.

Constructors

new
Counter(value?: number)

Properties

value: number

Methods

decrement(_delta: number): number

Decreases the value of the counter by delta. If delta is not given, decreases the value of the counter by 1.

Will throw an error if used outside of a change callback.

increment(_delta: number): number

Increases the value of the counter by delta. If delta is not given, increases the value of the counter by 1.

Will throw an error if used outside of a change callback.

toJSON(): number

Returns the counter value, so that a JSON serialization of an Automerge document represents the counter simply as an integer.

toString(): string

Returns the counter value as a decimal string. If x is a counter object, this method is called e.g. when you do ['value: ', x].join('') or when you use string interpolation: value: ${x}.

valueOf(): number

A peculiar JavaScript language feature from its early days: if the object x has a valueOf() method that returns a number, you can use numerical operators on the object x directly, such as x + 1 or x < 4. This method is also called when coercing a value to a string by concatenating it with another string, as in x + ''. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/valueOf