1
1
import { 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' ;
3
4
import { 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' ;
7
6
import { FormsStore } from './forms-manager.store' ;
8
- import { isEqual } from './isEqual' ;
9
7
import { 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" ;
11
12
12
13
const NO_DEBOUNCE = Symbol ( 'NO_DEBOUNCE' ) ;
13
14
@@ -17,6 +18,7 @@ export class NgFormsManager<FormsState = any> {
17
18
private valueChanges$$ : Map < keyof FormsState , Subscription > = new Map ( ) ;
18
19
private instances$$ : Map < keyof FormsState , AbstractControl > = new Map ( ) ;
19
20
private initialValues$$ : Map < keyof FormsState , any > = new Map ( ) ;
21
+ private persistManager = new LocalStorageManager ( ) ;
20
22
private destroy$$ = new Subject ( ) ;
21
23
22
24
constructor ( @Optional ( ) @Inject ( NG_FORMS_MANAGER_CONFIG ) private config : NgFormsManagerConfig ) {
@@ -490,7 +492,7 @@ export class NgFormsManager<FormsState = any> {
490
492
*
491
493
* @example
492
494
*
493
- * Removes the control from the store and from LocalStorage
495
+ * Removes the control from the store and from given PersistStorageManager
494
496
*
495
497
* manager.clear('login');
496
498
*
@@ -535,13 +537,16 @@ export class NgFormsManager<FormsState = any> {
535
537
this . setInitialValue ( name , control . value ) ;
536
538
}
537
539
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
+ } ) ;
545
550
}
546
551
547
552
/** If the control already exist, patch the control with the store value */
@@ -593,19 +598,26 @@ export class NgFormsManager<FormsState = any> {
593
598
}
594
599
595
600
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 ( )
597
605
}
598
606
599
607
private updateStorage ( name : keyof FormsState , value : any , config ) {
600
608
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
+ } ) ;
604
614
}
605
615
}
606
616
607
617
private getFromStorage ( key : string ) {
608
- return JSON . parse ( localStorage . getItem ( key ) || '{}' ) ;
618
+ return wrapIntoObservable ( this . persistManager . getValue ( key ) ) . pipe (
619
+ take ( 1 ) ,
620
+ ) ;
609
621
}
610
622
611
623
private deleteControl ( name : FormKeys < FormsState > ) {
0 commit comments