Options
All
  • Public
  • Public/Protected
  • All
Menu

Class CValueMap<K, V>

A collaborative map with keys of type K and values of type V.

The API is compatible* with Map<K, V> and can be used as a drop-in collaborative replacement when V is internally immutable. If multiple users set the value at a key concurrently, one of them is picked arbitrarily.

Values must be internally immutable; mutating a value internally will not change it on other replicas. If you need to mutate values internally, instead use a CMap or [[CLazyMap]].

(*) Except for the return value of set, which is the set value instead of this.

Type Parameters

  • K

    The key type.

  • V

    The value type.

Hierarchy

  • AbstractMap_CObject<K, V>
    • CValueMap

Implements

  • IMap<K, V>

Index

Constructors

  • new CValueMap<K, V>(init: InitToken, options?: { aggregator?: Aggregator<V>; keySerializer?: Serializer<K>; valueSerializer?: Serializer<V> }): CValueMap<K, V>
  • Constructs a CValueMap.

    Type Parameters

    • K

    • V

    Parameters

    • init: InitToken
    • options: { aggregator?: Aggregator<V>; keySerializer?: Serializer<K>; valueSerializer?: Serializer<V> } = {}
      • Optional aggregator?: Aggregator<V>

        If provided, used to aggregate concurrently-set values at the same key, instead of picking one arbitrarily.

      • Optional keySerializer?: Serializer<K>

        Serializer for keys. Defaults to [[DefaultSerializer]].

      • Optional valueSerializer?: Serializer<V>

        Serializer for values. Defaults to [[DefaultSerializer]].

    Returns CValueMap<K, V>

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

Methods

  • [iterator](): IterableIterator<[K, V]>
  • Returns IterableIterator<[K, V]>

  • 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

  • childSend(child: Collab<CollabEventsRecord>, messageStack: (string | Uint8Array)[], metaRequests: MetaRequest[]): void
  • Parameters

    • child: Collab<CollabEventsRecord>
    • messageStack: (string | Uint8Array)[]
    • metaRequests: MetaRequest[]

    Returns void

  • clear(): void
  • Returns void

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

    Parameters

    • key: K

    Returns boolean

    true if key was present and has been removed.

  • emit<K>(eventName: K, event: MapEventsRecord<K, V>[K] & CollabEvent, options?: { skipAnyEvent?: boolean }): 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

    • K extends keyof MapEventsRecord<K, V>

    Parameters

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

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

    Returns void

  • entries(): IterableIterator<[K, V]>
  • 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: V, key: K, map: CValueMap<K, V>) => void), thisArg?: any): void
  • Parameters

    • callbackfn: ((value: V, key: K, map: CValueMap<K, V>) => void)
        • (value: V, key: K, map: CValueMap<K, V>): void
        • Parameters

          Returns void

    • Optional thisArg: any

    Returns void

  • fromID<C>(id: CollabID<C>, startIndex?: number): undefined | C
  • Type Parameters

    • C extends Collab<CollabEventsRecord, C>

    Parameters

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

    Returns undefined | C

  • get(key: K): undefined | V
  • getConflicts(key: K): V[]
  • Returns all conflicting concurrently-set values at key. Their order is arbitrary but consistent across replicas.

    If the key is not present, this returns []. Otherwise, its first element is the set value.

    Parameters

    • key: K

    Returns V[]

  • has(key: K): boolean
  • idOf<C>(descendant: C): CollabID<C>
  • Type Parameters

    • C extends Collab<CollabEventsRecord, C>

    Parameters

    • descendant: C

    Returns CollabID<C>

  • keyOf(searchElement: V): undefined | K
  • Parameters

    • searchElement: V

    Returns undefined | K

  • keys(): IterableIterator<K>
  • Returns IterableIterator<K>

  • load(savedStateTree: null | SavedStateTree, meta: SavedStateMeta): void
  • 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

    • savedStateTree: null | SavedStateTree
    • meta: SavedStateMeta

    Returns void

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

    Type Parameters

    • K extends keyof MapEventsRecord<K, V>

    Parameters

    • eventName: K

      Name of the event to listen on.

    • handler: ((event: MapEventsRecord<K, V>[K], caller: CValueMap<K, V>) => void)

      Callback that handles the event.

        • (event: MapEventsRecord<K, V>[K], caller: CValueMap<K, V>): void
        • Parameters

          • event: MapEventsRecord<K, V>[K]
          • caller: CValueMap<K, V>

          Returns void

    • 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
  • Parameters

    • messageStack: (string | Uint8Array)[]
    • meta: MessageMeta

    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

    • C extends Collab<CollabEventsRecord, C>

    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]].

        • (init: InitToken): C
        • Parameters

          • init: InitToken

          Returns C

    Returns C

    The registered Collab.

  • save(): SavedStateTree
  • 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, value: V): V
  • Sets the value at key.

    Parameters

    • key: K
    • value: V

    Returns V

    value

  • toString(): string
  • Returns string

  • values(): IterableIterator<V>
  • Returns IterableIterator<V>

Generated using TypeDoc