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 ) {
@@ -266,28 +268,6 @@ export class NgFormsManager<FormsState = any> {
266
268
}
267
269
}
268
270
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
-
291
271
/**
292
272
*
293
273
* Sets the initial value for a control
@@ -490,7 +470,7 @@ export class NgFormsManager<FormsState = any> {
490
470
*
491
471
* @example
492
472
*
493
- * Removes the control from the store and from LocalStorage
473
+ * Removes the control from the store and from given PersistStorageManager
494
474
*
495
475
* manager.clear('login');
496
476
*
@@ -535,13 +515,16 @@ export class NgFormsManager<FormsState = any> {
535
515
this . setInitialValue ( name , control . value ) ;
536
516
}
537
517
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
+ } ) ;
545
528
}
546
529
547
530
/** If the control already exist, patch the control with the store value */
@@ -593,19 +576,26 @@ export class NgFormsManager<FormsState = any> {
593
576
}
594
577
595
578
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 ( )
597
583
}
598
584
599
585
private updateStorage ( name : keyof FormsState , value : any , config ) {
600
586
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
+ } ) ;
604
592
}
605
593
}
606
594
607
595
private getFromStorage ( key : string ) {
608
- return JSON . parse ( localStorage . getItem ( key ) || '{}' ) ;
596
+ return wrapIntoObservable ( this . persistManager . getValue ( key ) ) . pipe (
597
+ take ( 1 ) ,
598
+ ) ;
609
599
}
610
600
611
601
private deleteControl ( name : FormKeys < FormsState > ) {
0 commit comments