Skip to content

proposal: withLocalStorage/withSessionStorage #11

Closed
@ducin

Description

@ducin

Hi Angular Architects!

Here's my proposal for a custom signalStoreFeature to be included within this library. I wanted to make my own library, but it makes much more sense to join forces. If you're open for contributions/PRs from outside...?

The idea behind this web storage synchronization is:

  • per store (granular, not global like)
  • redux-independent
  • only signals are taken into account, not computeds.
  • private withWebStorage implementation serves: withLocalStorage/withSessionStorage
withLocalStorage(key: string, {
  validatorFn: (obj) => void, // required validator will be less troublesome in the long run
  serializer?: (obj) => string, // defaults to JSON.stringify
  deserializer?: (string) => obj // defaults to JSON.parse
})

which does the following:

  • onInit -> deserialize the storage content if it exists, iterate over signal properties and set them
  • whenever an update happens on a signal, it's being serialized and dumped to web storage: withHooks / onInit / effect -> reads the state, serializes it and pushes. This establishes synchronization

Please let me know what you think. If the idea is ok, I'll work on the PR.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions