Persist state plugin

Plugin is used for storing state to external storages like localStorage . Due its async nature it supports also storages like IndexDB

To do this you need to access store either from actions either injected to component and do following:

from actions

this.store.storage.save()

from component

this.store.select(['state-to-save-path']).storage.save()

Storage API:

  • save - saves state to storage.

    • PersistStateParams params can be passed.

    • Returns: Observable<PersistStateItem> with saved key and object

  • load - loads and applies state from storage (state is deleted from storage afterwards at least keepEntry is passed as true).

    • PersistStateParams params can be passed.

    • Returns: Observable<PersistStateItem> with saved key and object

  • removeItem - removes item from storage.

    • PersistStateParams params can be passed.

    • Returns Observable<string> with key that was removed.

  • clear - clears all state items from storage (clears items that keys starts with state::).

    • Returns Observable<string[]> with keys that was removed.

Each method returns observable. Subscription is optional but once subscribed it gets notified once action is completed.

PersistStateParams:

  • custom key

  • custom storage config

  • custom deserialize function

  • custom serialize function

Demo

Last updated