Maintains a collection of items, and keeps the registered indexes up to date.

Example

import { Collection } from "composable-indexes";
type Person = { name: string, age: number };
const collection = new Collection<Readonly<Person>>();

Type Parameters

  • T

    The type of the items in the collection. It is recommended to use a read-only type here.

Hierarchy

  • Collection

Constructors

Properties

Methods

Mutations

Queries

Constructors

Properties

indexes: Index<T, T>[] = []
last: Id = ...
store: IdMap<T> = ...

Methods

  • Registers an UnregisteredIndex to a collection, returning the Index that can be used to query the collection.

    You likely want to use this before you populate the collection, as it needs to iterate over all the existing items in the collection to build the index.

    Complexity: O(n) where n is the number of items already in the collection.

    Type Parameters

    • Ix extends Index<T, T, Ix>

    Parameters

    Returns Ix

Mutations

  • Complexity: O(1)

    Parameters

    • value: T

      Value to add the collection

    Returns Id

    An Id that can be used to refer to the added value.

  • Updates a value in the collection, if it exists.

    Complexity: O(1)

    Parameters

    • id: Id
    • f: ((pre) => T)
        • (pre): T
        • Parameters

          • pre: T

          Returns T

    Returns void

  • Most generic way to update an item in the collection.

    Complexity: O(1)

    Type Parameters

    • Ret

    Parameters

    • id: Id
    • f: ((pre) => [undefined | T, Ret])

      Takes either the existing value, or undefined if it doesn't exist, and returns a tuple of the new value and the return value. If the new value is undefined, existing item is deleted.

        • (pre): [undefined | T, Ret]
        • Parameters

          • pre: undefined | T

          Returns [undefined | T, Ret]

    Returns Ret

    Throws

    ConflictException if the index invariant is violated

    Throws

    ConditionFailedException if the precondition fails

  • Complexity: O(1)

    Parameters

    Returns undefined | T

    The deleted value, or undefined if doesn't exist.

  • Creates or updates a item in the collection.

    Complexity: O(1)

    Parameters

    • id: Id
    • newValue: T

    Returns void

Queries

  • Parameters

    • f: ((value, id) => void)
        • (value, id): void
        • Parameters

          • value: T
          • id: Id

          Returns void

    Returns void

Generated using TypeDoc