Options
All
  • Public
  • Public/Protected
  • All
Menu

Interface IList<T, InsertArgs, Events>

Interface for a collaborative list with values of type T.

For implementations, see [[CList]] and [[CValueList]]. [[CText]] is similar but not an actual IList implementation.

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

This interface permits insert to accept arbitrary InsertArgs instead of just the value to insert. That is useful when the value is not serializable, e.g., a dynamically- created Collab.

IList lets you address a list entry by its immutable position, in addition to its mutable index; see Position.

Type Parameters

  • T

    The value type.

  • InsertArgs extends unknown[] = [T]

    The type of arguments to insert. Defaults to [T], i.e., insert inputs the actual value.

  • Events extends ListEventsRecord<T> = ListEventsRecord<T>

    Events record.

Hierarchy

Implemented by

Index

Properties

length: number

The length of the list.

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.

Methods

  • [iterator](): IterableIterator<T>
  • Returns an iterator for values in the list, in list order.

    Returns IterableIterator<T>

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

    If this Collab is in its initial, post-constructor state, then this method may (but is not required to) return true; otherwise, it returns false.

    By default, this method always returns false; override to change.

    If this method returns true:

    1. The parent may choose to weakly reference this object to save memory (e.g., CLazyMap does so). If this becomes garbage collected, then is needed later, the parent will recreate it using the same constructor call.
    2. The parent may skip calling save during saving. When loading the resulting saved state, the parent will call load(null, meta). load should process this as if called with the output of save from a garbage-collectable state. For a nontrivial example, see [[CMultiValueMap.load]]'s implementation.

    Returns boolean

  • clear(): void
  • delete(index: number, count?: number): void
  • Delete count values starting at index, i.e., values [index, index + count - 1).

    All later values shift to the left, decreasing their indices by count.

    throws

    if index < 0 or index + count >= this.length.

    Parameters

    • index: number
    • Optional count: number

      The number of values to delete. Defaults to 1 (delete the value at index only).

    Returns void

  • emit<K>(eventName: K, event: Events[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 string | number | symbol

    Parameters

    • eventName: K
    • event: Events[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]>
  • Returns an iterator of [index, value, position] tuples for every value in the list, in list order.

    Returns IterableIterator<[index: number, value: T, position: string]>

  • 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: T, index: number, list: IList<T, InsertArgs, Events>) => void), thisArg?: any): void
  • Performs the specified action for each element in this list.

    Parameters

    • callbackfn: ((value: T, index: number, list: IList<T, InsertArgs, Events>) => void)

      A function that accepts up to three arguments. forEach calls the callbackfn function one time for each element in the list.

        • (value: T, index: number, list: IList<T, InsertArgs, Events>): void
        • Parameters

          • value: T
          • index: number
          • list: IList<T, InsertArgs, Events>

          Returns void

    • Optional thisArg: any

      An object to which the this keyword can refer in the callbackfn function. If thisArg is omitted, undefined is used as the this value.

    Returns void

  • get(index: number): T
  • Returns the value currently at index.

    throws

    If index is not in [0, this.length). Note that this differs from an ordinary Array, which would instead return undefined.

    Parameters

    • index: number

    Returns T

  • getByPosition(position: string): undefined | T
  • Returns the value at position, or undefined if it is not currently present (hasPosition returns false).

    Parameters

    • position: string

    Returns undefined | T

  • getPosition(index: number): string
  • hasPosition(position: string): boolean
  • Returns whether position is currently present in the list, i.e., its value is present.

    Parameters

    • position: string

    Returns boolean

  • indexOf(searchElement: T, fromIndex?: number): number
  • Returns the index of the first occurrence of a value in this list, or -1 if it is not present.

    Parameters

    • searchElement: T

      The value to locate in this list.

    • Optional fromIndex: number

      The index to start the search at. If the index is greater than or equal to the list's length, -1 is returned, which means the list will not be searched. If the provided index value is a negative number, it is taken as the offset from the end of the list. Note: if the provided index is negative, the list is still searched from front to back. If the provided index is 0, then the whole list will be searched. Default: 0 (entire list is searched).

    Returns number

  • indexOfPosition(position: string, searchDir?: "none" | "left" | "right"): number
  • Returns the current index of position.

    If position is not currently present in the list (hasPosition returns false), then the result depends on searchDir:

    • "none" (default): Returns -1.
    • "left": Returns the next index to the left of position. If there are no values to the left of position, returns -1.
    • "right": Returns the next index to the right of position. If there are no values to the right of position, returns length.

    To find the index where a position would be if present, use searchDir = "right".

    Parameters

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

    Returns number

  • insert(index: number, ...args: InsertArgs): undefined | T
  • Inserts a value at the given index using args.

    All values currently at or after index shift to the right, incrementing their indices.

    Typically, args are broadcast to all replicas in serialized form. Every replica then uses them to contruct the actual value of type T.

    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 value is appended to the end of the list.

    • Rest ...args: InsertArgs

    Returns undefined | T

    The inserted value, or undefined if it is not constructed immediately.

  • Internal (parent) use only.

    Called by this Collab's parent to load saved state. You may assume that the saved state was generated by save on some replica of this Collab, possibly in a different collaboration session, with guarantees set by the runtime.

    This method may also be called with savedStateTree = null; you should ignore such calls (i.e., return immediately) unless you override canGC. If you do override canGC, see that method's docs for instructions.

    Parameters

    • savedStateTree: null | SavedStateTree

      The saved state to load, in the same format as in save, or null as described above.

    • meta: SavedStateMeta

      Metadata attached to this saved state by the runtime. It incorporates all possible metadata requests. Note that meta.updateType is always "savedState".

    Returns void

  • map<U>(callbackfn: ((value: T, index: number, list: IList<T, InsertArgs, Events>) => U), thisArg?: any): U[]
  • Calls a defined callback function on each element of this list, and returns an array that contains the results.

    Type Parameters

    • U

    Parameters

    • callbackfn: ((value: T, index: number, list: IList<T, InsertArgs, Events>) => U)

      A function that accepts up to three arguments. The map method calls the callbackfn function one time for each element in the list.

        • (value: T, index: number, list: IList<T, InsertArgs, Events>): U
        • Parameters

          • value: T
          • index: number
          • list: IList<T, InsertArgs, Events>

          Returns U

    • Optional thisArg: any

      An object to which the this keyword can refer in the callbackfn function. If thisArg is omitted, undefined is used as the this value.

    Returns U[]

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

    Type Parameters

    • K extends string | number | symbol

    Parameters

    • eventName: K

      Name of the event to listen on.

    • handler: ((event: Events[K], caller: IList<T, InsertArgs, Events>) => void)

      Callback that handles the event.

        • (event: Events[K], caller: IList<T, InsertArgs, Events>): void
        • Parameters

          • event: Events[K]
          • caller: IList<T, InsertArgs, Events>

          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
  • Returns the position of the first occurrence of searchElement, or undefined if there are no occurrences.

    Compare to indexOf.

    Parameters

    • searchElement: T

    Returns undefined | string

  • positions(): IterableIterator<string>
  • Returns an iterator for present positions, in list order.

    Returns IterableIterator<string>

  • push(...args: InsertArgs): undefined | T
  • Inserts a value at the end of the list using args. Equivalent to this.insert(this.length, ...args).

    Parameters

    • Rest ...args: InsertArgs

    Returns undefined | T

    The inserted value, or undefined if it is not constructed immediately.

  • 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

    • messageStack: (string | Uint8Array)[]

      The message to receive, in the same format as in send. This method may mutate messageStack in-place (e.g., calling pop).

    • meta: MessageMeta

      Metadata attached to this message by the runtime. It incorporates metadata requests made in send. Note that meta.updateType is always "message".

    Returns void

  • Internal (parent) use only.

    Returns saved state describing the current state of this Collab.

    The saved state may later be passed to load on a replica of this Collab, possibly in a different collaboration session, with rules set by the runtime. For example, [[CRuntime]] allows load at any time; it must then act as a merge operation (like a state-based CRDT), applying all updates that the saved replica had applied before saving, ignoring duplicates.

    save may be called at any time, possibly many times while an app is running. Calling save should not affect this Collab's user-visible state.

    For convenience, the saved state may be expressed as a tree of Uint8Arrays instead of just a single Uint8Array; see [[SaveStateTree]]'s docs.

    Returns SavedStateTree

    The saved state.

  • 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[]
  • Returns a copy of a section of this list, as an array. For both start and end, a negative index can be used to indicate an offset from the end of the list. For example, -2 refers to the second to last element of the list.

    Parameters

    • Optional start: number

      The beginning index of the specified portion of the list. If start is undefined, then the slice begins at index 0.

    • Optional end: number

      The end index of the specified portion of the list. This is exclusive of the element at the index 'end'. If end is undefined, then the slice extends to the end of the list.

    Returns T[]

  • unshift(...args: InsertArgs): undefined | T
  • Inserts a value at the start of the list using args. Equivalent to this.insert(0, ...args).

    Parameters

    • Rest ...args: InsertArgs

    Returns undefined | T

    The inserted value, or undefined if it is not constructed immediately.

  • values(): IterableIterator<T>
  • Returns an iterator for values in the list, in list order.

    Returns IterableIterator<T>

Generated using TypeDoc