Skip to content

Feature storage sync #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 26, 2024

Conversation

bohoffi
Copy link
Contributor

@bohoffi bohoffi commented Dec 31, 2023

PR was created at the suggestion of @rainerhahnekamp in ngrx/plattform#4180.

PR Checklist

Please check if your PR fulfills the following requirements:

  • Tests for the changes have been added (for bug fixes / features)
  • Documentation has been added / updated (for bug fixes / features)

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 new behavior?

  1. Adding storage (localstorage/sessionstorage) synchronization to the store:
const SyncStore = signalStore(
  withStorageSync('synced'),
);
  1. Adding methods for manual synchronization
const SyncStore = signalStore(
  withStorageSync('synced'),
);

const syncStore = inject(SyncStore);

syncStore.clearStorage(); // clears the stores item in storage
syncStore.readFromStorage(); // reads the stores item from storage and patches the state
syncStore.writeToStorage(); // writes the current state to storage
  1. Offering customization options
const SyncStore = signalStore(
  withStorageSync<User>({
    key: 'synced',
    autoSync: false, // read from storage on init and write on state changes - `true` by default
    select: (state: User) => Partial<User>, // projection to keep specific slices in sync
    parse: (stateString: string) => State, // custom parsing from storage - `JSON.parse` by default
    stringify: (state: User) => string, // custom stringification - `JSON.stringify` by default
    storage: () => sessionstorage, // factory to select storage to sync with
  }),
);

Does this PR introduce a breaking change?

[ ] Yes
[x] No

@rainerhahnekamp rainerhahnekamp merged commit ef46f75 into angular-architects:main Feb 26, 2024
@bohoffi
Copy link
Contributor Author

bohoffi commented Feb 27, 2024

@rainerhahnekamp the build on main failed due to the bundle budget - should I open a new PR for increasing the budget?

@rainerhahnekamp
Copy link
Collaborator

Hi @bohoffi, first of all, thanks for your contribution. The build pipeline is not something I'd call stable at the moment. So, work is still in progress 😃

Every bit of improvement is more than welcomed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants