Options
All
  • Public
  • Public/Protected
  • All
Menu

Class Store

A store class.

The store can be used to contain the states of an app.

One can add/remove data inside the store using the [[register]]/unregister method.

It is possible to set the value of a data using the update method or using the updater$s subject.

It is possible to subscribe to a value using the select$ observable.

The content of the store can be displayed using the dump() method and emptied using reset().

The store as an history that support rollback and can be flushed usinng commit.

Hierarchy

  • Store

Index

Constructors

constructor

  • Example:

    This example will create ...

    const store = new Store()
    store.register$1('val1', newVal => { console.log("*" + newVal) }).subscribe()
    store.select$('val1').subscribe(val => { if (val) console.log(val) })
    store.update('val1', "Hello,")
    store.update('val1', "World!")

    Returns Store

Properties

commit$s

commit$s: Subject<void> = new Subject<void>()

Subject for compressing the history. See commit() for more information regarding the effect of the function. Example:

store.commit$s.next()

flush$s

flush$s: Subject<void> = new Subject<void>()

Private history

history: State<StoreHistory> = new State<StoreHistory>([])

Private latest$

latest$: BehaviorSubject<StoreState> = new BehaviorSubject({})

rollback$s

rollback$s: Subject<void> = new Subject<void>()

Subject for rollbacking the history. See rollback() for more information regarding the effect of the function. Example:

store.rollback$s.next()

Private subscribers

subscribers: Subscribers = new Subscribers()

updater$s

updater$s: Subject<object> = new Subject<StoreUpdate>()

Subject for updating the content of a data stored Example:

store.updater$s.next({ name: 'val1', data: 3})

Methods

commit

  • commit(): void
  • Returns void

dump

  • dump(): void
  • Display the content of the store

    Returns void

flush

  • flush(): void
  • Remove any data in the store (not the subscription)

    Returns void

register$1

  • register$1<T>(name: string, cb: function): Observable<boolean>
  • Register an new value in the store Any select$ to the same name made before the registration will be hooked to this value. It is forbiden to register two values with the same name.

    Type parameters

    • T

    Parameters

    • name: string

      The namee of the value

    • cb: function

      Callback that will be called whenever the value changes because of the store (rollback...). The callback parameter is the new value.

        • (data: T): void
        • Parameters

          • data: T

          Returns void

    Returns Observable<boolean>

    An observable that is true when the value is subscribed

reset

  • reset(): void
  • Returns void

rollback

  • rollback(): void
  • Return to the previous state in the history Going back to a previous state will trigger the callback function provided during the registration register$1 of any impacted values.

    Returns void

select$

  • select$<T>(name: string): Observable<T>
  • subscribe to a registered value in the store

    Type parameters

    • T

    Parameters

    • name: string

      the name of the value to subscribe to

    Returns Observable<T>

    an observable on the value. (The observable will never terminate) The subscribtion will remains if the value is unregistered from the store:

    const store = new Store()
    store.select$('val1').subscribe(val => { if (val) console.log(val) })
    store.register$1('val1', newVal => { console.log("*" + newVal) }).subscribe()
    store.update('val1', "Hello,")
    store.unregister('val1')
    store.register$1('val1', newVal => { console.log("*" + newVal) }).subscribe()
    store.update('val1', "World!")

    The returned observable only trigger if the stored value changes.

Private storeStateUpdate

  • Parameters

    Returns void

unregister

  • unregister(name: string): void
  • Unregister a value from the store Unregister value will not trigger any select$ to be closed.

    Parameters

    • name: string

      The namee of the value.

    Returns void

update

  • update<T>(name: string, data: T): void
  • Update a registered value in the store

    Type parameters

    • T

    Parameters

    • name: string

      The value to update

    • data: T

      The new data to set

    Returns void

Generated using TypeDoc