Options
All
  • Public
  • Public/Protected
  • All
Menu

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

Type parameters

  • StoreDataT

Hierarchy

  • Observable<StoreDataT>
    • ObservableStore

Index

Constructors

constructor

  • new ObservableStore<StoreDataT>(initialValue: StoreDataT, fieldsToPersist?: { [ K in string | number | symbol]?: boolean | { deserializer?: any; serializer?: any } }): ObservableStore<StoreDataT>
  • example
    new ObservableStore<TestObject>(
    {a: 'A', b: 1},
    {
    a: {serializer: v => JSON.stringify(v), deserializer: json => JSON.parse(json)},
    b: true
    }
    );

    Type parameters

    • StoreDataT

    Parameters

    • initialValue: StoreDataT

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

    • Optional fieldsToPersist: { [ K in string | number | symbol]?: boolean | { deserializer?: any; serializer?: any } }

      An object describing which fields in the store data to persist in localStorage. The object consists of keys matching the keys in `StoreDataT´, with either a boolean value (true if the value should be persisted as standard JSOB), or an object with custom serializer/deserializer for the field.

    Returns ObservableStore<StoreDataT>

Methods

apply

  • apply<KeyInStoreT>(updateFunction: (storeData: StoreDataT) => Pick<StoreDataT, KeyInStoreT>): Pick<StoreDataT, KeyInStoreT>
  • apply<KeyInStoreT>(updateFunction: (storeData: StoreDataT) => Promise<Pick<StoreDataT, KeyInStoreT>>): Promise<Pick<StoreDataT, KeyInStoreT>>
  • 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

    • KeyInStoreT: string | number | symbol

    Parameters

    • updateFunction: (storeData: StoreDataT) => Pick<StoreDataT, KeyInStoreT>

      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: StoreDataT): Pick<StoreDataT, KeyInStoreT>
        • Parameters

          • storeData: StoreDataT

          Returns Pick<StoreDataT, KeyInStoreT>

    Returns Pick<StoreDataT, KeyInStoreT>

    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

    • KeyInStoreT: string | number | symbol

    Parameters

    • updateFunction: (storeData: StoreDataT) => Promise<Pick<StoreDataT, KeyInStoreT>>

      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: StoreDataT): Promise<Pick<StoreDataT, KeyInStoreT>>
        • Parameters

          • storeData: StoreDataT

          Returns Promise<Pick<StoreDataT, KeyInStoreT>>

    Returns Promise<Pick<StoreDataT, KeyInStoreT>>

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

getValue

  • getValue(): StoreDataT
  • 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 StoreDataT

observe

  • observe<KeyInStoreT>(key: KeyInStoreT): Observable<StoreDataT[KeyInStoreT]>
  • observe<KeyInStoreT>(key: KeyInStoreT, ...additionalKeys: KeyInStoreT[]): Observable<Pick<StoreDataT, KeyInStoreT>>
  • observe<KeyInStoreT>(keys: KeyInStoreT[]): Observable<Pick<StoreDataT, KeyInStoreT>>
  • 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

    • KeyInStoreT: string | number | symbol

    Parameters

    • key: KeyInStoreT

    Returns Observable<StoreDataT[KeyInStoreT]>

  • 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

    • KeyInStoreT: string | number | symbol

    Parameters

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

    Returns Observable<Pick<StoreDataT, KeyInStoreT>>

  • 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

    • KeyInStoreT: string | number | symbol

    Parameters

    • keys: KeyInStoreT[]

    Returns Observable<Pick<StoreDataT, KeyInStoreT>>

set

  • set<KeyInStoreT>(key: KeyInStoreT, value: StoreDataT[KeyInStoreT]): void
  • set<KeyInStoreT>(keyValueObject: Pick<StoreDataT, KeyInStoreT>): 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

    • KeyInStoreT: string | number | symbol

    Parameters

    • key: KeyInStoreT

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

    • value: StoreDataT[KeyInStoreT]

      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

    • KeyInStoreT: string | number | symbol

    Parameters

    • keyValueObject: Pick<StoreDataT, KeyInStoreT>

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

    Returns void