Closed nickhh76 closed 2 years ago
Thanks for the PR. I'm wondering why not use one token:
export const FORMS_MANAGER_STORAGE = new InjectionToken<Storage | undefined>('FORMS_MANAGER_STORAGE', {
providedIn: 'root',
factory: () => (isPlatformBrowser(inject(PLATFORM_ID)) ? localStorage : undefined),
});
Now if someone wants to use sessionStorage
they can define it as follows:
{
provide: FORMS_MANAGER_STORAGE,
useValue: sessionStorage
}
We can also export it from the library:
export const FORMS_MANAGER_SESSION_STORAGE = {
provide: FORMS_MANAGER_STORAGE,
....
}
Thanks for your feedback! I agree with your suggested approach. It's simpler and more flexible.
I have updated the PR accordingly. Please let me know if you have any further comments.
Thanks, Nick
Adds ability to use session storage for persistence, configurable through NgFormsManagerConfig
PR Checklist
Please check if your PR fulfills the following requirements:
PR Type
What kind of change does this PR introduce?
What is the current behavior?
Currently, when
persistState
is set totrue
inUpsertConfig
, state is always persisted to local storage.Issue Number: N/A
What is the new behavior?
Through configuration using the
NG_FORMS_MANAGER_CONFIG
token, the user can now choose to use session storage instead. Projects using the library that don't use the new configuration option will continue to use local storage by default.Configuration example:
Does this PR introduce a breaking change?
Other information
This provides a simple solution to the common requirement of having to delete any user data when the browser session closes. This solution is backward-compatible, requires minimal configuration and is fully tested and documented.