11import { Inject , Injectable , Optional } from '@angular/core' ;
2- import { AbstractControl , FormArray , FormGroup } from '@angular/forms' ;
2+ import { AbstractControl , FormGroup , FormArray } from '@angular/forms' ;
3+ import { coerceArray , filterControlKeys , filterNil , isBrowser , mergeDeep , wrapIntoObservable } from './utils' ;
34import { EMPTY , merge , Observable , Subject , Subscription , timer } from 'rxjs' ;
4- import { debounce , distinctUntilChanged , filter , map , mapTo } from 'rxjs/operators' ;
5- import { deleteControl , findControl , handleFormArray , toStore } from './builders' ;
6- import { Config , NgFormsManagerConfig , NG_FORMS_MANAGER_CONFIG } from './config' ;
5+ import { debounce , distinctUntilChanged , filter , first , map , mapTo , take } from 'rxjs/operators' ;
76import { FormsStore } from './forms-manager.store' ;
8- import { isEqual } from './isEqual' ;
97import { Control , ControlFactory , FormKeys , HashMap , UpsertConfig } from './types' ;
10- import { coerceArray , filterControlKeys , filterNil , isBrowser , mergeDeep } from './utils' ;
8+ import { Config , NG_FORMS_MANAGER_CONFIG , NgFormsManagerConfig } from './config' ;
9+ import { isEqual } from './isEqual' ;
10+ import { deleteControl , findControl , handleFormArray , toStore } from './builders' ;
11+ import { LocalStorageManager } from "./localStorageManager" ;
1112
1213const NO_DEBOUNCE = Symbol ( 'NO_DEBOUNCE' ) ;
1314
@@ -17,6 +18,7 @@ export class NgFormsManager<FormsState = any> {
1718 private valueChanges$$ : Map < keyof FormsState , Subscription > = new Map ( ) ;
1819 private instances$$ : Map < keyof FormsState , AbstractControl > = new Map ( ) ;
1920 private initialValues$$ : Map < keyof FormsState , any > = new Map ( ) ;
21+ private persistManager = new LocalStorageManager ( ) ;
2022 private destroy$$ = new Subject ( ) ;
2123
2224 constructor ( @Optional ( ) @Inject ( NG_FORMS_MANAGER_CONFIG ) private config : NgFormsManagerConfig ) {
@@ -266,28 +268,6 @@ export class NgFormsManager<FormsState = any> {
266268 }
267269 }
268270
269- /**
270- *
271- * @example
272- *
273- * A proxy to the original `reset` method
274- *
275- * manager.reset('login', { email: '' });
276- *
277- */
278- reset < T extends keyof FormsState > (
279- name : T ,
280- value ?: Partial < FormsState [ T ] > ,
281- options ?: {
282- onlySelf ?: boolean ;
283- emitEvent ?: boolean ;
284- }
285- ) {
286- if ( this . instances$$ . has ( name ) ) {
287- this . instances$$ . get ( name ) . reset ( value , options ) ;
288- }
289- }
290-
291271 /**
292272 *
293273 * Sets the initial value for a control
@@ -490,7 +470,7 @@ export class NgFormsManager<FormsState = any> {
490470 *
491471 * @example
492472 *
493- * Removes the control from the store and from LocalStorage
473+ * Removes the control from the store and from given PersistStorageManager
494474 *
495475 * manager.clear('login');
496476 *
@@ -535,13 +515,16 @@ export class NgFormsManager<FormsState = any> {
535515 this . setInitialValue ( name , control . value ) ;
536516 }
537517
538- if ( isBrowser ( ) && config . persistState && this . hasControl ( name ) === false ) {
539- const storageValue = this . getFromStorage ( mergedConfig . storage . key ) ;
540- if ( storageValue [ name ] ) {
541- this . store . update ( {
542- [ name ] : mergeDeep ( toStore ( name , control ) , storageValue [ name ] ) ,
543- } as Partial < FormsState > ) ;
544- }
518+ if ( ( isBrowser ( ) || ! ( config . persistManager instanceof LocalStorageManager ) ) && config . persistState && this . hasControl ( name ) === false ) {
519+ this . persistManager = config . persistManager || this . persistManager ;
520+ this . getFromStorage ( mergedConfig . storage . key ) . subscribe ( value => {
521+ const storageValue = value ;
522+ if ( storageValue [ name ] ) {
523+ this . store . update ( {
524+ [ name ] : mergeDeep ( toStore ( name , control ) , storageValue [ name ] ) ,
525+ } as Partial < FormsState > ) ;
526+ }
527+ } ) ;
545528 }
546529
547530 /** If the control already exist, patch the control with the store value */
@@ -593,19 +576,26 @@ export class NgFormsManager<FormsState = any> {
593576 }
594577
595578 private removeFromStorage ( ) {
596- localStorage . setItem ( this . config . merge ( ) . storage . key , JSON . stringify ( this . store . getValue ( ) ) ) ;
579+ wrapIntoObservable ( this . persistManager . setValue (
580+ this . config . merge ( ) . storage . key ,
581+ this . store . getValue ( )
582+ ) ) . pipe ( first ( ) ) . subscribe ( )
597583 }
598584
599585 private updateStorage ( name : keyof FormsState , value : any , config ) {
600586 if ( isBrowser ( ) && config . persistState ) {
601- const storageValue = this . getFromStorage ( config . storage . key ) ;
602- storageValue [ name ] = filterControlKeys ( value ) ;
603- localStorage . setItem ( config . storage . key , JSON . stringify ( storageValue ) ) ;
587+ this . getFromStorage ( config . storage . key ) . pipe ( first ( ) ) . subscribe ( valueFromStorage => {
588+ const storageValue = valueFromStorage ;
589+ storageValue [ name ] = filterControlKeys ( value ) ;
590+ wrapIntoObservable ( this . persistManager . setValue ( config . storage . key , storageValue ) ) . pipe ( first ( ) ) . subscribe ( ) ;
591+ } ) ;
604592 }
605593 }
606594
607595 private getFromStorage ( key : string ) {
608- return JSON . parse ( localStorage . getItem ( key ) || '{}' ) ;
596+ return wrapIntoObservable ( this . persistManager . getValue ( key ) ) . pipe (
597+ take ( 1 ) ,
598+ ) ;
609599 }
610600
611601 private deleteControl ( name : FormKeys < FormsState > ) {
0 commit comments