Options
All
  • Public
  • Public/Protected
  • All
Menu

Class BroadcastingSet<ELEMENTTYPE, IDTYPE>

A BroadcastingSet maintains a set of elements identified by an ID, and exposes Observables signaling changes to the elements.

Note: If the ID of an object is changed after it is inserted into a BroadcastingSet, the behavior of the set is undefined.

The methods in this class typically return the same object which is also emitted in changes. In most cases, you should not use this, as relying on the synchronous return value in a specific part of the code rather defeats the purpose of the BroadcastingSet in the first place.

The return types may seem a little inconsistent, sometimes being Arrays and sometimes being IterableIterator. This is because the class internally uses Maps and typically simply exposes IterableIterators directly from those maps. In many cases, an iterator is enough for the purpose, so converting it to an Array for "niceness" would be an unnecessary performance hit. Conversely, sometimes the class needs to work with an array internally, and in those cases exposes it as such.

Type parameters

  • ELEMENTTYPE

  • IDTYPE

Hierarchy

  • BroadcastingSet

Index

Constructors

constructor

  • new BroadcastingSet<ELEMENTTYPE, IDTYPE>(idExtractor: (element: ELEMENTTYPE) => IDTYPE, equality?: (a: ELEMENTTYPE, b: ELEMENTTYPE) => boolean): BroadcastingSet<ELEMENTTYPE, IDTYPE>
  • Type parameters

    • ELEMENTTYPE

    • IDTYPE

    Parameters

    • idExtractor: (element: ELEMENTTYPE) => IDTYPE

      A function which converts an element to its id. The types of this function implicitly determines the type of the BroadcastingSet.

        • (element: ELEMENTTYPE): IDTYPE
        • Parameters

          • element: ELEMENTTYPE

          Returns IDTYPE

    • equality: (a: ELEMENTTYPE, b: ELEMENTTYPE) => boolean = ...

      When putting elements into the set, changes will only be reported if the elements actually differ. By default, elements will be compared using fast-deep-equal, but in some cases, the elements may already carry, say, a revision number or change date which can be used for fast identification. In such cases, provide an equality function leveraging that.

        • (a: ELEMENTTYPE, b: ELEMENTTYPE): boolean
        • Parameters

          • a: ELEMENTTYPE
          • b: ELEMENTTYPE

          Returns boolean

    Returns BroadcastingSet<ELEMENTTYPE, IDTYPE>

Properties

Readonly changes

changes: Observable<ElementChange<ELEMENTTYPE, IDTYPE>> = ...

A stream of ElementChanges describing the changes to the BroadcastingSet as they happen. A change message is triggered after the BroadcastingSet has been updated, before changes are signaled in elements. If a bulk operations (putAll or replaceAll) does multiple updates to the same element ID, only one change will be reported, ignoring the intermediate updates and reporting the initial and final value.

Readonly elements

elements: Observable<IterableIterator<ELEMENTTYPE>> = ...

An Observable of the items in the BroadcastingSet, emitting every time the values change. A change message will only be emitted once for each of the bulk operations, putAll, replaceAll and clear. The value is an {@link IterableIterator} which can be stepped through using the for...of construct. If you need it as an array, simply use Array.from(...).

Methods

clear

  • Removes all elements from the set, emitting delete messages for each.

    Returns ElementChange<ELEMENTTYPE, IDTYPE>[]

put

  • put(element: ELEMENTTYPE): undefined | ElementChange<ELEMENTTYPE, IDTYPE>
  • Inserts an element into the set, adding it if the ID does not already exist, or updating it if it does. This will trigger a change message in changes (if the new object differs from the existing, per the equality function given to the {@link BroadcastingSet:constructor}.

    Parameters

    • element: ELEMENTTYPE

    Returns undefined | ElementChange<ELEMENTTYPE, IDTYPE>

putAll

  • putAll(elements: Iterable<ELEMENTTYPE>): IterableIterator<ElementChange<ELEMENTTYPE, IDTYPE>>
  • Inserts multiple elements. Change messages will be emitted for each in changes, but the elements will only emit once (if the insertions resulted in any actual changes).

    Parameters

    • elements: Iterable<ELEMENTTYPE>

    Returns IterableIterator<ElementChange<ELEMENTTYPE, IDTYPE>>

remove

  • remove(id: IDTYPE): undefined | ElementChange<ELEMENTTYPE, IDTYPE>
  • Removes a specific element from the set, emitting a delete message if the element existed.

    Parameters

    • id: IDTYPE

    Returns undefined | ElementChange<ELEMENTTYPE, IDTYPE>

replaceAll

  • replaceAll(elements: Iterable<ELEMENTTYPE>): ElementChange<ELEMENTTYPE, IDTYPE>[]
  • Replaces all elements in the set with those given, emitting delete, create and update messages as appropriate, depending on what was already in the set.

    Parameters

    • elements: Iterable<ELEMENTTYPE>

    Returns ElementChange<ELEMENTTYPE, IDTYPE>[]