Options
All
  • Public
  • Public/Protected
  • All
Menu

Class CSet<C, AddArgs>

A collaborative set with 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 Set<C>, you do not add values directly. Instead, you use the pattern described in collections of Collabs: one user calls add with AddArgs; each replica passes those AddArgs to its valueConstructor; and valueConstructor returns the local copy of the new value Collab.

When a value is deleted with delete, it is deleted permanently and can no longer be used; future and concurrent operations on that value are ignored.

You can also treat a CSet<C> as a "factory" for Collabs of type C: add is like "new"/"malloc" and delete is like "free", but replicated across all devices.

See also: CValueSet.

Type Parameters

  • C extends Collab

    The value type, which is a Collab.

  • AddArgs extends unknown[]

    The type of arguments to add.

Hierarchy

  • AbstractSet_Collab<C, AddArgs>
    • CSet

Implements

  • IParent

Index

Constructors

  • new CSet<C, AddArgs>(init: InitToken, valueConstructor: ((valueInit: InitToken, ...args: AddArgs) => C), options?: { argsSerializer?: Serializer<AddArgs> }): CSet<C, AddArgs>
  • Constructs a CSet with the given valueConstructor.

    Type Parameters

    • C extends Collab<CollabEventsRecord, C>

    • AddArgs extends unknown[]

    Parameters

    • init: InitToken
    • valueConstructor: ((valueInit: InitToken, ...args: AddArgs) => C)

      Callback used to construct a value Collab with the given [[InitToken]] and arguments to add. See collections of Collabs for example usage.

        • (valueInit: InitToken, ...args: AddArgs): C
        • Parameters

          • valueInit: InitToken
          • Rest ...args: AddArgs

          Returns C

    • options: { argsSerializer?: Serializer<AddArgs> } = {}
      • Optional argsSerializer?: Serializer<AddArgs>

        A serializer for AddArgs as an array. Defaults to [[DefaultSerializer]].

    Returns CSet<C, AddArgs>

Properties

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<C>
  • Returns IterableIterator<C>

  • add(...args: AddArgs): C
  • Adds a value to the set 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

    • Rest ...args: AddArgs

    Returns C

    The added value.

  • canGC(): 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(value: C): void
  • Deletes the given value, making it no longer present in this set.

    value is deleted permanently and can no longer be used; future and concurrent operations on that value are ignored. Local operations will succeed but will not affect remote replicas. The value can perform cleanup in its [[Collab.finalize]] method.

    Parameters

    • value: C

    Returns void

  • emit<K>(eventName: K, event: SetEventsRecord<C>[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 SetEventsRecord<C>

    Parameters

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

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

    Returns void

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

    Called by this Collab's parent when it has been deleted from a collection on the local replica and can no longer be used (e.g., due to CSet.delete on this or an ancestor). A Collab implementation can implement this method to clean up external resources, e.g., associated DOM elements.

    finalize has no relation to the JavaScript garbage collector or canGC.

    By default, this method does nothing.

    Returns void

  • forEach(callbackfn: ((value: C, value2: C, set: CSet<C, AddArgs>) => void), thisArg?: any): void
  • Parameters

    • callbackfn: ((value: C, value2: C, set: CSet<C, AddArgs>) => void)
        • (value: C, value2: C, set: CSet<C, AddArgs>): void
        • Parameters

          • value: C
          • value2: C
          • set: CSet<C, AddArgs>

          Returns void

    • Optional thisArg: any

    Returns void

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

    Specifically, given a [[CollabID]] returned by idOf on some replica of this CSet, returns this replica's copy of the original descendant.

    If the original descendant has been deleted from this set, this method will usually return undefined. The exception is if descendant was just deleted from this set. In that case, this method will still return the original descendant until the end of the deleting update. Thus event handlers within the same transaction can still get the deleted value.

    Type Parameters

    • D extends Collab<CollabEventsRecord, D>

    Parameters

    • id: CollabID<D>

      A CollabID from idOf.

    • startIndex: number = 0

      Internal (parent) use only. If provided, treat id.collabIDPath as if it starts at startIndex instead of 0.

    Returns undefined | D

  • getArgs(value: C): AddArgs
  • Returns the AddArgs used to add value.

    throws

    if !this.has(value).

    Parameters

    • value: C

    Returns AddArgs

  • has(value: C): boolean
  • idOf<D>(descendant: D): CollabID<D>
  • Type Parameters

    • D extends Collab<CollabEventsRecord, D>

    Parameters

    • descendant: D

    Returns CollabID<D>

  • load(savedStateTree: null | SavedStateTree, meta: SavedStateMeta): void
  • Parameters

    • savedStateTree: null | SavedStateTree
    • meta: SavedStateMeta

    Returns void

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

    Type Parameters

    • K extends keyof SetEventsRecord<C>

    Parameters

    • eventName: K

      Name of the event to listen on.

    • handler: ((event: SetEventsRecord<C>[K], caller: CSet<C, AddArgs>) => void)

      Callback that handles the event.

        • (event: SetEventsRecord<C>[K], caller: CSet<C, AddArgs>): void
        • Parameters

          • event: SetEventsRecord<C>[K]
          • caller: CSet<C, AddArgs>

          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

  • save(): 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

  • toString(): string
  • Returns string

  • values(): IterableIterator<C>

Generated using TypeDoc