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 ) {
@@ -490,7 +492,7 @@ export class NgFormsManager<FormsState = any> {
490492 *
491493 * @example
492494 *
493- * Removes the control from the store and from LocalStorage
495+ * Removes the control from the store and from given PersistStorageManager
494496 *
495497 * manager.clear('login');
496498 *
@@ -535,13 +537,16 @@ export class NgFormsManager<FormsState = any> {
535537 this . setInitialValue ( name , control . value ) ;
536538 }
537539
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- }
540+ if ( ( isBrowser ( ) || ! ( config . persistManager instanceof LocalStorageManager ) ) && config . persistState && this . hasControl ( name ) === false ) {
541+ this . persistManager = config . persistManager || this . persistManager ;
542+ this . getFromStorage ( mergedConfig . storage . key ) . subscribe ( value => {
543+ const storageValue = value ;
544+ if ( storageValue [ name ] ) {
545+ this . store . update ( {
546+ [ name ] : mergeDeep ( toStore ( name , control ) , storageValue [ name ] ) ,
547+ } as Partial < FormsState > ) ;
548+ }
549+ } ) ;
545550 }
546551
547552 /** If the control already exist, patch the control with the store value */
@@ -593,19 +598,26 @@ export class NgFormsManager<FormsState = any> {
593598 }
594599
595600 private removeFromStorage ( ) {
596- localStorage . setItem ( this . config . merge ( ) . storage . key , JSON . stringify ( this . store . getValue ( ) ) ) ;
601+ wrapIntoObservable ( this . persistManager . setValue (
602+ this . config . merge ( ) . storage . key ,
603+ this . store . getValue ( )
604+ ) ) . pipe ( first ( ) ) . subscribe ( )
597605 }
598606
599607 private updateStorage ( name : keyof FormsState , value : any , config ) {
600608 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 ) ) ;
609+ this . getFromStorage ( config . storage . key ) . pipe ( first ( ) ) . subscribe ( valueFromStorage => {
610+ const storageValue = valueFromStorage ;
611+ storageValue [ name ] = filterControlKeys ( value ) ;
612+ wrapIntoObservable ( this . persistManager . setValue ( config . storage . key , storageValue ) ) . pipe ( first ( ) ) . subscribe ( ) ;
613+ } ) ;
604614 }
605615 }
606616
607617 private getFromStorage ( key : string ) {
608- return JSON . parse ( localStorage . getItem ( key ) || '{}' ) ;
618+ return wrapIntoObservable ( this . persistManager . getValue ( key ) ) . pipe (
619+ take ( 1 ) ,
620+ ) ;
609621 }
610622
611623 private deleteControl ( name : FormKeys < FormsState > ) {
0 commit comments