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 } 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' ;
11
- import { FORMS_MANAGER_STORAGE } from './injection-tokens' ;
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 '@ngneat/storage' ;
12
+ import { wrapIntoObservable } from '@ngneat/storage/lib/utils' ;
12
13
13
14
const NO_DEBOUNCE = Symbol ( 'NO_DEBOUNCE' ) ;
14
15
@@ -19,12 +20,10 @@ export class NgFormsManager<FormsState = any> {
19
20
private valueChanges$$ : Map < keyof FormsState , Subscription > = new Map ( ) ;
20
21
private instances$$ : Map < keyof FormsState , AbstractControl > = new Map ( ) ;
21
22
private initialValues$$ : Map < keyof FormsState , any > = new Map ( ) ;
23
+ private persistManager = new LocalStorageManager ( ) ;
22
24
private destroy$$ = new Subject ( ) ;
23
25
24
- constructor (
25
- @Optional ( ) @Inject ( NG_FORMS_MANAGER_CONFIG ) private config : NgFormsManagerConfig ,
26
- @Inject ( FORMS_MANAGER_STORAGE ) private readonly browserStorage ?: Storage
27
- ) {
26
+ constructor ( @Optional ( ) @Inject ( NG_FORMS_MANAGER_CONFIG ) private config : NgFormsManagerConfig ) {
28
27
this . store = new FormsStore ( { } as FormsState ) ;
29
28
}
30
29
@@ -495,7 +494,7 @@ export class NgFormsManager<FormsState = any> {
495
494
*
496
495
* @example
497
496
*
498
- * Removes the control from the store and from browser storage
497
+ * Removes the control from the store and from given PersistStorageManager
499
498
*
500
499
* manager.clear('login');
501
500
*
@@ -540,13 +539,20 @@ export class NgFormsManager<FormsState = any> {
540
539
this . setInitialValue ( name , control . value ) ;
541
540
}
542
541
543
- if ( isBrowser ( ) && config . persistState && this . hasControl ( name ) === false ) {
544
- const storageValue = this . getFromStorage ( mergedConfig . storage . key ) ;
545
- if ( storageValue [ name ] ) {
546
- this . store . update ( {
547
- [ name ] : mergeDeep ( toStore ( name , control ) , storageValue [ name ] ) ,
548
- } as Partial < FormsState > ) ;
549
- }
542
+ if (
543
+ ( isBrowser ( ) || ! ( config . persistManager instanceof LocalStorageManager ) ) &&
544
+ config . persistState &&
545
+ this . hasControl ( name ) === false
546
+ ) {
547
+ this . persistManager = config . persistManager || this . persistManager ;
548
+ this . getFromStorage ( mergedConfig . storage . key ) . subscribe ( value => {
549
+ const storageValue = value ;
550
+ if ( storageValue [ name ] ) {
551
+ this . store . update ( {
552
+ [ name ] : mergeDeep ( toStore ( name , control ) , storageValue [ name ] ) ,
553
+ } as Partial < FormsState > ) ;
554
+ }
555
+ } ) ;
550
556
}
551
557
552
558
/** If the control already exist, patch the control with the store value */
@@ -598,22 +604,29 @@ export class NgFormsManager<FormsState = any> {
598
604
}
599
605
600
606
private removeFromStorage ( ) {
601
- this . browserStorage ?. setItem (
602
- this . config . merge ( ) . storage . key ,
603
- JSON . stringify ( this . store . getValue ( ) )
604
- ) ;
607
+ wrapIntoObservable (
608
+ this . persistManager . setValue ( this . config . merge ( ) . storage . key , this . store . getValue ( ) )
609
+ )
610
+ . pipe ( first ( ) )
611
+ . subscribe ( ) ;
605
612
}
606
613
607
614
private updateStorage ( name : keyof FormsState , value : any , config ) {
608
615
if ( isBrowser ( ) && config . persistState ) {
609
- const storageValue = this . getFromStorage ( config . storage . key ) ;
610
- storageValue [ name ] = filterControlKeys ( value ) ;
611
- this . browserStorage ?. setItem ( config . storage . key , JSON . stringify ( storageValue ) ) ;
616
+ this . getFromStorage ( config . storage . key )
617
+ . pipe ( first ( ) )
618
+ . subscribe ( valueFromStorage => {
619
+ const storageValue = valueFromStorage ;
620
+ storageValue [ name ] = filterControlKeys ( value ) ;
621
+ wrapIntoObservable ( this . persistManager . setValue ( config . storage . key , storageValue ) )
622
+ . pipe ( first ( ) )
623
+ . subscribe ( ) ;
624
+ } ) ;
612
625
}
613
626
}
614
627
615
628
private getFromStorage ( key : string ) {
616
- return JSON . parse ( this . browserStorage ?. getItem ( key ) || '{}' ) ;
629
+ return wrapIntoObservable ( this . persistManager . getValue ( key ) ) . pipe ( take ( 1 ) ) ;
617
630
}
618
631
619
632
private deleteControl ( name : FormKeys < FormsState > ) {
0 commit comments