Skip to content

Conversation

@nickhh76
Copy link

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?

[ ] 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?

Currently, when persistState is set to true in UpsertConfig, 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:

import { NG_FORMS_MANAGER_CONFIG, NgFormsManagerConfig } from '@ngneat/forms-manager';

@NgModule({
  declarations: [AppComponent],
  imports: [ReactiveFormsModule],
  providers: [
    {
      provide: NG_FORMS_MANAGER_CONFIG,
      useValue: new NgFormsManagerConfig({
        storage: {
          type: 'SessionStorage', // accepts 'LocalStorage' or 'SessionStorage', can be omitted to default to local storage
          key: 'myCustomKey',
        },
      }),
    },
  ],
  bootstrap: [AppComponent],
})
export class AppModule {}

Does this PR introduce a breaking change?

[ ] Yes
[X] No

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.

Adds ability to use session storage for persistence, configurable through NgFormsManagerConfig
@NetanelBasal
Copy link
Member

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,
  ....
}

Adds ability to use sessionStorage or other custom storage solution for persistence.
@nickhh76
Copy link
Author

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

constructor(@Optional() @Inject(NG_FORMS_MANAGER_CONFIG) private config: NgFormsManagerConfig) {
constructor(
@Optional() @Inject(NG_FORMS_MANAGER_CONFIG) private config: NgFormsManagerConfig,
@Optional() @Inject(FORMS_MANAGER_STORAGE) private readonly browserStorage?: Storage
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The default is localStorage so why do we need @optional?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, @optional is not necessary. I have removed it.

BTW, @optional is also not necessary for the NG_FORMS_MANAGER_CONFIG token because a default is provided.


describe('FORMS_MANAGER_SESSION_STORAGE_PROVIDER', () => {
it('should provide SESSION_STORAGE_TOKEN', () => {
TestBed.configureTestingModule({ providers: [FORMS_MANAGER_SESSION_STORAGE_PROVIDER] });
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please test that it uses sessionStorage in the manager?

Copy link
Author

@nickhh76 nickhh76 Oct 17, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think, this is already tested through the test in injection-tokens.spec.ts.

describe('FORMS_MANAGER_SESSION_STORAGE_PROVIDER', () => {
  it('should provide SESSION_STORAGE_TOKEN', () => {
    TestBed.configureTestingModule({ providers: [FORMS_MANAGER_SESSION_STORAGE_PROVIDER] });
    expect(TestBed.inject(FORMS_MANAGER_STORAGE)).toBe(TestBed.inject(SESSION_STORAGE_TOKEN));
  });
});

However, I have added a test to forms-manager.spec.ts as requested.

Adds ability to use sessionStorage or other custom storage solution for persistence.
@NetanelBasal NetanelBasal merged commit c5400ba into ngneat:master Oct 18, 2021
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