Options
All
  • Public
  • Public/Protected
  • All
Menu

Class CValueList<T>

A collaborative list with values of type T.

CValueList<T> has a similar API to Array<T>, but it is mutated more like a linked list: instead of mutating existing values, you insert and delete list entries. Insertions and deletions shift later entries, changing their indices, like in collaborative text editing or Array.splice.

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

It is not safe to modify a CValueList while iterating over it. The iterator will attempt to throw an exception if it detects such modification, but this is not guaranteed.

See also: CList, CText, CRichText.

Type Parameters

  • T

    The value type.

Hierarchy

  • AbstractList_CObject<T, [T]>
    • CValueList

Index

Constructors

  • new CValueList<T>(init: InitToken, options?: { valueArraySerializer?: Serializer<T[]>; valueSerializer?: Serializer<T> }): CValueList<T>
  • Constructs a CValueList.

    Type Parameters

    • T

    Parameters

    • init: InitToken
    • options: { valueArraySerializer?: Serializer<T[]>; valueSerializer?: Serializer<T> } = {}
      • Optional valueArraySerializer?: Serializer<T[]>

        Serializer for an array of values, used for bulk operations and saved states. Defaults to using valueSerializer on each value.

      • Optional valueSerializer?: Serializer<T>

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

    Returns CValueList<T>

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

totalOrder: CTotalOrder

The abstract total order underlying this list CRDT.

Access this to construct separate LocalList views on top of the same total order.

valueArraySerializer: Serializer<T[]>
valueSerializer: Serializer<T>

Accessors

  • get length(): number

Methods

  • [iterator](): IterableIterator<T>
  • Returns IterableIterator<T>

  • 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(index: number, count?: number): void
  • emit<K>(eventName: K, event: ListEventsRecord<T>[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 ListEventsRecord<T>

    Parameters

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

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

    Returns void

  • entries(): IterableIterator<[index: number, value: T, position: string]>
  • 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: T, index: number, list: CValueList<T>) => void), thisArg?: any): void
  • Parameters

    • callbackfn: ((value: T, index: number, list: CValueList<T>) => void)
        • (value: T, index: number, list: CValueList<T>): 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(index: number): T
  • getByPosition(position: string): undefined | T
  • getPosition(index: number): string
  • hasPosition(position: string): boolean
  • idOf<C>(descendant: C): CollabID<C>
  • Type Parameters

    • C extends Collab<CollabEventsRecord, C>

    Parameters

    • descendant: C

    Returns CollabID<C>

  • indexOf(searchElement: T, fromIndex?: number): number
  • Parameters

    • searchElement: T
    • Optional fromIndex: number

    Returns number

  • indexOfPosition(position: string, searchDir?: "none" | "left" | "right"): number
  • Parameters

    • position: string
    • searchDir: "none" | "left" | "right" = "none"

    Returns number

  • insert(index: number, value: T): T
  • insert(index: number, ...values: T[]): undefined | T
  • Inserts values at the given index.

    All values currently at or after index shift to the right, increasing their indices by values.length.

    throws

    If index is not in [0, this.length].

    Parameters

    • index: number

      The insertion index in the range [0, this.length]. If this.length, the values are appended to the end of the list.

    • value: T

    Returns T

    The first inserted value, or undefined if there are no values.

  • Parameters

    • index: number
    • Rest ...values: T[]

    Returns undefined | T

  • load(savedStateTree: null | SavedStateTree, meta: SavedStateMeta): void
  • map<U>(callbackfn: ((value: T, index: number, list: CValueList<T>) => U), thisArg?: any): U[]
  • Type Parameters

    • U

    Parameters

    • callbackfn: ((value: T, index: number, list: CValueList<T>) => U)
        • Parameters

          Returns U

    • Optional thisArg: any

    Returns U[]

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

    Type Parameters

    • K extends keyof ListEventsRecord<T>

    Parameters

    • eventName: K

      Name of the event to listen on.

    • handler: ((event: ListEventsRecord<T>[K], caller: CValueList<T>) => void)

      Callback that handles the event.

        • (event: ListEventsRecord<T>[K], caller: CValueList<T>): void
        • Parameters

          • event: ListEventsRecord<T>[K]
          • caller: CValueList<T>

          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.

  • positionOf(searchElement: T): undefined | string
  • Parameters

    • searchElement: T

    Returns undefined | string

  • positions(): IterableIterator<string>
  • push(value: T): T
  • push(...values: T[]): undefined | T
  • 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
  • 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

  • slice(start?: number, end?: number): T[]
  • splice(start: number, deleteCount?: number, ...values: T[]): T[]
  • Deletes and inserts values like Array.splice.

    If deleteCount is provided, this method first deletes deleteCount values starting at start. Next, this method inserts values at start.

    All values currently at or after start + deleteCount shift to accommodate the change in length.

    Parameters

    • start: number
    • Optional deleteCount: number
    • Rest ...values: T[]

    Returns T[]

    The deleted values.

  • toString(): string
  • Returns string

  • unshift(value: T): T
  • unshift(...values: T[]): undefined | T
  • values(): IterableIterator<T>

Generated using TypeDoc