ngneat / elf

🧙‍♀️ A Reactive Store with Magical Powers
https://ngneat.github.io/elf/
MIT License
1.62k stars 93 forks source link

feat(persist-state): :fire: add callback methods for value formatting #514

Closed micobarac closed 8 months ago

micobarac commented 8 months ago

… (#513)

The preStoreValueInit methods allow the modification of state value before it is saved to store.

The preStorageValueUpdate methods allow the modification of state value before it is saved to storage.

Closes: #513

PR Checklist

Please check if your PR fulfils the following requirements:

PR Type

What kind of change does this PR introduce?

[ ] Bugfix
[x] Feature
[ ] Code style update (formatting, local variables)
[ ] Refactoring (no functional changes, no api changes)
[ ] Build related changes
[ ] CI related changes
[ ] Documentation content changes
[ ] Other... Please describe:

What is the current behavior?

There is no option to format the state / storage value while synchronising store with storage, while persisting in local storage in raw string format.

Issue Number: #513

What is the new behavior?

The following callback methods are added:

Does this PR introduce a breaking change?

[x] Yes
[ ] No

packages/persist-state/src/lib/storage.ts

export interface StateStorage {
  getItem<T extends Record<string, any> | string>(key: string): Async<T | null | undefined>;

  setItem(key: string, value: Record<string, any> | string): Async<any>;

  removeItem(key: string): Async<boolean | void>;
}

Other information

The added callback methods allow storing raw string values in local storage, allowing data encryption / decryption or compression / decompression value to be persisted and parsed.

stackblitz[bot] commented 8 months ago

Review PR in StackBlitz Codeflow Run & review this pull request in StackBlitz Codeflow.