Options
All
  • Public
  • Public/Protected
  • All
Menu

Class CMap<K, C, SetArgs>

A collaborative map with keys of type K and mutable values of type C.

Values are internally mutable. Specifically, each value is its own Collab, and operations on that Collab are collaborative as usual.

Unlike a normal Map<K, C>, you do not set values directly. Instead, you use the pattern described in collections of Collabs: one user calls set with SetArgs; each replica passes those SetArgs to its valueConstructor; and valueConstructor returns the local copy of the new value Collab.

If multiple users set the value at a key concurrently, one of the value Collabs is picked arbitrarily; operations on the others are ignored. To instead "merge" concurrently-created values - applying all of their operations to the same underlying Collab - use CLazyMap.

See also: CValueMap, a simpler map for immutable values.

Type Parameters

  • K

    The key type.

  • C extends Collab

    The value type, which is a Collab.

  • SetArgs extends unknown[]

    The type of arguments to set.

Hierarchy

Index

Constructors

Properties

children: Map<string, Collab<CollabEventsRecord>>

The children (registered Collab properties), keyed by name.

This map should only be read, not mutated. It is exposed to subclasses as a convenience for methods that loop over all children.

name: string

Internal (this/parent) use only.

This Collab's name, which distinguishes it among its siblings in the tree of Collabs.

parent: Parent

Internal (this/parent) use only.

This Collab's parent in the tree of Collabs.

runtime: IRuntime

The ambient IRuntime.

Use this to access utilities like IRuntime.replicaID.

Accessors

  • get size(): number
  • The number of present keys in the map.

    Returns number

Methods

  • [iterator](): IterableIterator<[K, C]>
  • Returns an iterator for entries in the map.

    The iteration order is NOT eventually consistent: it may differ on replicas with the same state.

    Returns IterableIterator<[K, C]>

  • canGC(): boolean
  • Internal (parent) use only.

    By default, this method returns true if canGC returns true on every child. Override to change this behavior.

    See Collab.canGC.

    Returns boolean

  • clear(): void
  • Deletes every key in the map.

    Returns void

  • delete(key: K): void
  • Deletes the given key, making it no longer present in this map.

    Parameters

    • key: K

    Returns void

  • Emits an event, which triggers all the registered event handlers.

    See CollabEventsRecord for advice on what events to emit.

    This is a wrapper around EventEmitter.emit that forces events to extend CollabEvent and also emits an "Any" event.

    Type Parameters

    Parameters

    • eventName: K
    • event: MapEventsRecord<K, C>[K] & CollabEvent
    • Optional options: { skipAnyEvent?: boolean }
      • Optional skipAnyEvent?: boolean

        Set to true to skip emitting an "Any" event.

    Returns void

  • entries(): IterableIterator<[K, C]>
  • Returns an iterator of key, value pairs for every entry in the map.

    The iteration order is NOT eventually consistent: it may differ on replicas with the same state.

    Returns IterableIterator<[K, C]>

  • finalize(): void
  • Internal (parent) use only.

    By default, this methods calls finalize on every child. Override to change this behavior, e.g., to add your own finalization steps (but consider calling super.finalize()).

    Returns void

  • forEach(callbackfn: ((value: C, key: K, map: CMap<K, C, SetArgs>) => void), thisArg?: any): void
  • Executes a provided function once for each (key, value) pair in the map, in the same order as entries.

    Parameters

    • callbackfn: ((value: C, key: K, map: CMap<K, C, SetArgs>) => void)
        • (value: C, key: K, map: CMap<K, C, SetArgs>): void
        • Parameters

          • value: C
          • key: K
          • map: CMap<K, C, SetArgs>

          Returns void

    • Optional thisArg: any

    Returns void

  • fromID<C>(id: CollabID<C>, startIndex?: number): undefined | C
  • Inverse of idOf.

    Specifically, given a CollabID returned by idOf on some replica of this parent, returns this replica's copy of the original descendant. If that descendant does not exist (e.g., it was deleted or it is not present in this program version), returns undefined.

    Type Parameters

    Parameters

    • id: CollabID<C>
    • Optional startIndex: number

    Returns undefined | C

  • get(key: K): undefined | C
  • Returns the value associated to key, or undefined if key is not present.

    Parameters

    • key: K

    Returns undefined | C

  • has(key: K): boolean
  • Returns whether key is present in the map.

    Parameters

    • key: K

    Returns boolean

  • Returns a CollabID for the given strict descendant of this parent.

    The CollabID may be passed to fromID on any replica of this parent (but not other parents) to obtain that replica's copy of descendant.

    Type Parameters

    Parameters

    • descendant: C

    Returns CollabID<C>

  • keyOf(searchElement: C): undefined | K
  • Returns the unique key associated to a value in this map, in O(1) time.

    This method will succeed if value is the current value or a conflicting concurrently-set value (see getConflicts). Otherwise, it returns undefined.

    Parameters

    • searchElement: C

      The value to locate in this map.

    Returns undefined | K

  • keys(): IterableIterator<K>
  • Returns an iterator for keys in the map.

    The iteration order is NOT eventually consistent: it may differ on replicas with the same state.

    Returns IterableIterator<K>

  • Internal (parent) use only.

    Called by this Collab's parent to load saved state. See Collab.load.

    A CObject subclass may override this method to load additional state from Collab.save or to perform extra setup - e.g., refreshing functional views that were not automatically updated by children's load events. It is recommended to do so as follows:

    load(savedStateTree: SavedStateTree | null, meta: SavedStateMeta) {
    super.load(savedStateTree, meta);
    // Process your extra saved state from savedStateTree.self.
    const savedState = savedStateTree === null? null: savedStateTree.self!;
    ...
    // Perform extra setup as needed.
    ...
    }

    Parameters

    Returns void

  • on<K>(eventName: K, handler: ((event: MapEventsRecord<K, C>[K], caller: CMap<K, C, SetArgs>) => void), options?: { once?: boolean }): (() => void)
  • Registers an event handler that is triggered when the event happens.

    Type Parameters

    Parameters

    • eventName: K

      Name of the event to listen on.

    • handler: ((event: MapEventsRecord<K, C>[K], caller: CMap<K, C, SetArgs>) => void)

      Callback that handles the event.

    • Optional options: { once?: boolean }
      • Optional once?: boolean

        If true, the event handler is triggered at most once (the next time the event happens), then unsubscribed.

    Returns (() => void)

    An "off" function that removes the event handler when called.

      • (): void
      • Registers an event handler that is triggered when the event happens.

        Returns void

        An "off" function that removes the event handler when called.

  • receive(messageStack: (string | Uint8Array)[], meta: MessageMeta): void
  • Internal (parent) use only.

    Receives a message sent by send on a local or remote replica of this Collab.

    This method processes the message, changes the local state accordingly, and emits events describing the local changes.

    This method should make assumptions and ensure consistency guarantees appropriate to its use case. For example, CRDTs may assume eventual, exactly-once, causal-order message delivery, and they must ensure strong eventual consistency.

    Parameters

    Returns void

  • registerCollab<C>(name: string, collabCallback: ((init: InitToken) => C)): C
  • Registers a Collab property of this CObject with the given name, making it one of our children.

    Typically, you will call this method during the constructor in the style:

    this.foo = this.registerCollab("foo", (init) => new FooClass(init, constructor args...));
    

    where readonly foo: FooClass; is a Collab property. See Data Modeling for examples.

    Registrations must be identical across all replicas.

    See also: CRuntime.registerCollab.

    Type Parameters

    Parameters

    • name: string

      A name for this property, unique among this class's registerCollab calls. We recommend using the same name as the property, but you can also use short strings to reduce network usage ("", "0", "1", ...).

    • collabCallback: ((init: InitToken) => C)

      A callback that uses the given InitToken to construct the registered Collab.

    Returns C

    The registered Collab.

  • Internal (parent) use only.

    Returns saved state describing the current state of this CObject. See Collab.save.

    A CObject subclass may override this method to save additional state. It is recommended to do so as follows:

    save() {
    const ans = super.save();
    // Put your extra saved state in ans.self, which is otherwise unused.
    ans.self = <subclass's saved state>;
    return ans;
    }

    Returns SavedStateTree

  • send(messageStack: (string | Uint8Array)[], metaRequests: MetaRequest[]): void
  • Broadcasts a message to other replicas of this Collab. The message will be delivered to all replicas' receive, including locally.

    For convenience, the message may be expressed as a stack of (Uint8Array | string), instead of just a single Uint8Array. This is useful for parents sending messages on behalf of their children; see the implementation of CObject for an example.

    Parameters

    • messageStack: (string | Uint8Array)[]

      The message to send, in the form of a stack of Uint8Arrays. Note that this method may mutate it in-place.

    • metaRequests: MetaRequest[]

      A stack of metadata requests. The runtime will use the union of these when creating the MessageMeta for receive. Note that the stack need not align with messageStack, and this method may mutate it in place.

    Returns void

  • set(key: K, ...args: SetArgs): C
  • Sets the value at key using args.

    The args are broadcast to all replicas in serialized form. Every replica then passes them to valueConstructor to construct the actual value of type C, a new Collab that is collaborative as usual.

    Parameters

    • key: K
    • Rest ...args: SetArgs

    Returns C

    The set value.

  • toString(): string
  • values(): IterableIterator<C>
  • Returns an iterator for values in the map.

    The iteration order is NOT eventually consistent: it may differ on replicas with the same state.

    Returns IterableIterator<C>

Generated using TypeDoc