11import { Injectable } from '@angular/core' ;
22import { Observable , of , throwError } from 'rxjs' ;
33import { map , switchMap } from 'rxjs/operators' ;
4+ import { AbstractStorage } from '../utilities/browser/storage/abstract-storage' ;
45import { LocalStorage } from '../utilities/browser/storage/local-storage' ;
6+ import { SessionStorage } from '../utilities/browser/storage/session-storage' ;
57import { BooleanCoercer } from '../utilities/coercers/boolean-coercer' ;
68import { NumberCoercer } from '../utilities/coercers/number-coercer' ;
79
10+ export const enum StorageType {
11+ Local = 'local' ,
12+ Session = 'session'
13+ }
14+
815@Injectable ( {
916 providedIn : 'root'
1017} )
1118export class PreferenceService {
19+ private static readonly DEFAULT_STORAGE_TYPE : StorageType = StorageType . Local ;
20+
1221 private static readonly PREFERENCE_STORAGE_NAMESPACE : string = 'preference' ;
1322 private static readonly SEPARATOR_CHAR : string = '.' ;
1423 private static readonly SEPARATOR_REGEX : RegExp = / \. ( .+ ) / ;
1524 private readonly numberCoercer : NumberCoercer = new NumberCoercer ( ) ;
1625 private readonly booleanCoercer : BooleanCoercer = new BooleanCoercer ( ) ;
1726
18- public constructor ( private readonly preferenceStorage : LocalStorage ) { }
27+ public constructor (
28+ private readonly localStorage : LocalStorage ,
29+ private readonly sessionStorage : SessionStorage
30+ ) { }
1931
2032 /**
2133 * Returns the current storage value if defined, else the default value. The observable
2234 * will continue to emit as the preference is updated, reverting to default value if the
2335 * preference becomes unset. If default value is not provided, the observable will
2436 * throw in the case the preference is unset.
2537 */
26- public get < T extends PreferenceValue > ( key : PreferenceKey , defaultValue ?: T ) : Observable < T > {
27- return this . preferenceStorage . watch ( this . asStorageKey ( key ) ) . pipe (
38+ public get < T extends PreferenceValue > (
39+ key : PreferenceKey ,
40+ defaultValue ?: T ,
41+ type : StorageType = PreferenceService . DEFAULT_STORAGE_TYPE
42+ ) : Observable < T > {
43+ return this . preferenceStorage ( type ) . watch ( this . asStorageKey ( key ) ) . pipe (
2844 map ( storedValue => this . fromStorageValue < T > ( storedValue ) ?? defaultValue ) ,
2945 switchMap ( value =>
3046 value === undefined
@@ -34,9 +50,13 @@ export class PreferenceService {
3450 ) ;
3551 }
3652
37- public set ( key : PreferenceKey , value : PreferenceValue ) : void {
53+ public set (
54+ key : PreferenceKey ,
55+ value : PreferenceValue ,
56+ type : StorageType = PreferenceService . DEFAULT_STORAGE_TYPE
57+ ) : void {
3858 const val = this . asStorageValue ( value ) ;
39- this . preferenceStorage . set ( this . asStorageKey ( key ) , val ) ;
59+ this . preferenceStorage ( type ) . set ( this . asStorageKey ( key ) , val ) ;
4060 }
4161
4262 private asStorageKey ( key : PreferenceKey ) : PreferenceStorageKey {
@@ -70,6 +90,16 @@ export class PreferenceService {
7090 return undefined ;
7191 }
7292 }
93+
94+ private preferenceStorage ( type : StorageType ) : AbstractStorage {
95+ switch ( type ) {
96+ case StorageType . Session :
97+ return this . sessionStorage ;
98+ case StorageType . Local :
99+ default :
100+ return this . localStorage ;
101+ }
102+ }
73103}
74104
75105type PreferenceStorageKey = string ;
0 commit comments