Options
All
  • Public
  • Public/Protected
  • All
Menu

An ObservableStore maintains data of generic type STORE_TYPE, providing methods to set the fields, in turn emitting values on requested Observables. The ObservableStore is itself an Observable of type STORE_TYPE, acting like a BehaviorSubject, maintaining a single value and replaying it to new subscribers.

Type parameters

  • STORE_TYPE

Hierarchy

  • Observable<STORE_TYPE>
    • ObservableStore

Index

Constructors

constructor

  • new ObservableStore<STORE_TYPE>(initialValue: STORE_TYPE): ObservableStore<STORE_TYPE>
  • Type parameters

    • STORE_TYPE

    Parameters

    • initialValue: STORE_TYPE

      A valid instance of type STORE_TYPE, serving as the initial value of the data.

    Returns ObservableStore<STORE_TYPE>

Methods

apply

  • apply<KEY_IN_STORE>(updateFunction: (storeData: STORE_TYPE) => Pick<STORE_TYPE, KEY_IN_STORE>): Pick<STORE_TYPE, KEY_IN_STORE>
  • apply<KEY_IN_STORE>(updateFunction: (storeData: STORE_TYPE) => Promise<Pick<STORE_TYPE, KEY_IN_STORE>>): Promise<Pick<STORE_TYPE, KEY_IN_STORE>>
  • Updates the ObservableStore based on the current values in the store. This is useful if you need functionality such as "add an item to an existing array in the store". (Remember that the store values should generally be immutable, so in this example, you should .concat rather than .push).

    Type parameters

    • KEY_IN_STORE: string | number | symbol

    Parameters

    • updateFunction: (storeData: STORE_TYPE) => Pick<STORE_TYPE, KEY_IN_STORE>

      A function which takes the current data of the store and returns an object with a subset (Pick) of the fields. The calculated fields will be updated in the store, and any Observables will be triggered as if the values had been set with set.

        • (storeData: STORE_TYPE): Pick<STORE_TYPE, KEY_IN_STORE>
        • Parameters

          • storeData: STORE_TYPE

          Returns Pick<STORE_TYPE, KEY_IN_STORE>

    Returns Pick<STORE_TYPE, KEY_IN_STORE>

    The result of the update function; i.e. the same object which has been set in the store.

  • Updates the ObservableStore asynchronously based on the current values in the store. This is useful if you need functionality such as "add an item to an existing array in the store". (Remember that the store values should generally be immutable, so in this example, you should .concat rather than .push).

    Type parameters

    • KEY_IN_STORE: string | number | symbol

    Parameters

    • updateFunction: (storeData: STORE_TYPE) => Promise<Pick<STORE_TYPE, KEY_IN_STORE>>

      A function which takes the current data of the store and returns a Promise of a subset (Pick) of the fields. Once the Promise fulfills, the calculated fields will be updated in the store, and any Observables will be triggered as if the values had been set with set.

        • (storeData: STORE_TYPE): Promise<Pick<STORE_TYPE, KEY_IN_STORE>>
        • Parameters

          • storeData: STORE_TYPE

          Returns Promise<Pick<STORE_TYPE, KEY_IN_STORE>>

    Returns Promise<Pick<STORE_TYPE, KEY_IN_STORE>>

    The result of the update function; i.e. the same Promised object which will be set in the store.

getValue

  • getValue(): STORE_TYPE
  • Returns the current value of the internal data. This is a shallow copy, and should not be used to manipulate the fields of the store.

    Returns STORE_TYPE

observe

  • observe<KEY_IN_STORE>(key: KEY_IN_STORE): Observable<STORE_TYPE[KEY_IN_STORE]>
  • observe<KEY_IN_STORE>(key: KEY_IN_STORE, ...additionalKeys: KEY_IN_STORE[]): Observable<Pick<STORE_TYPE, KEY_IN_STORE>>
  • observe<KEY_IN_STORE>(keys: KEY_IN_STORE[]): Observable<Pick<STORE_TYPE, KEY_IN_STORE>>
  • Returns an Observable which emits the values for the given key as they are updated. This is essentially a shorthand for

    observableStore.pipe(
    map(data => data.key),
    distinctUntilChanged((a, b) => deepEqual(a, b))
    );

    Type parameters

    • KEY_IN_STORE: string | number | symbol

    Parameters

    • key: KEY_IN_STORE

    Returns Observable<STORE_TYPE[KEY_IN_STORE]>

  • Returns an Observable of an object consisting of the specified fields. This is essentially a shorthand for

    observableStore.pipe(
    map(data => ({
    k1: data.k1,
    k2: data.k2
    })),
    distinctUntilChanged((a, b) => deepEqual(a, b))
    );

    Type parameters

    • KEY_IN_STORE: string | number | symbol

    Parameters

    • key: KEY_IN_STORE
    • Rest ...additionalKeys: KEY_IN_STORE[]

    Returns Observable<Pick<STORE_TYPE, KEY_IN_STORE>>

  • Returns an Observable of an object consisting of the specified fields. This is equivalent to

    observableStore.pipe(
    map(data => ({
    k1: data.k1,
    k2: data.k2
    })),
    distinctUntilChanged((a, b) => deepEqual(a, b))
    );

    Type parameters

    • KEY_IN_STORE: string | number | symbol

    Parameters

    • keys: KEY_IN_STORE[]

    Returns Observable<Pick<STORE_TYPE, KEY_IN_STORE>>

set

  • set<KEY_IN_STORE>(key: KEY_IN_STORE, value: STORE_TYPE[KEY_IN_STORE]): void
  • set<KEY_IN_STORE>(keyValueObject: Pick<STORE_TYPE, KEY_IN_STORE>): void
  • Sets a given field to a given value. If the new value is not equal to the current value, it will trigger a notification in the relevant Observables. (See observe)

    Type parameters

    • KEY_IN_STORE: string | number | symbol

    Parameters

    • key: KEY_IN_STORE

      The name of the field. Must be a field in type T

    • value: STORE_TYPE[KEY_IN_STORE]

      The new value. Must be of the appropriate type.

    Returns void

  • Sets several fields at once. If any of the new values are not equal to the current value, it will trigger a notification in the relevant Observables. (See observe)

    Type parameters

    • KEY_IN_STORE: string | number | symbol

    Parameters

    • keyValueObject: Pick<STORE_TYPE, KEY_IN_STORE>

      An object containing key/value pairs to set. The object must be a subset (Pick) of type T

    Returns void