|
1 | 1 | import { Injectable } from '@angular/core'; |
2 | 2 | import { Observable, of, throwError } from 'rxjs'; |
3 | 3 | import { map, switchMap } from 'rxjs/operators'; |
| 4 | +import { AbstractStorage } from '../utilities/browser/storage/abstract-storage'; |
4 | 5 | import { LocalStorage } from '../utilities/browser/storage/local-storage'; |
| 6 | +import { SessionStorage } from '../utilities/browser/storage/session-storage'; |
5 | 7 | import { BooleanCoercer } from '../utilities/coercers/boolean-coercer'; |
6 | 8 | import { NumberCoercer } from '../utilities/coercers/number-coercer'; |
7 | 9 |
|
| 10 | +export const enum StorageType { |
| 11 | + Local = 'local', |
| 12 | + Session = 'session' |
| 13 | +} |
| 14 | + |
8 | 15 | @Injectable({ |
9 | 16 | providedIn: 'root' |
10 | 17 | }) |
11 | 18 | export class PreferenceService { |
| 19 | + private static readonly DEFAULT_STORAGE_TYPE: StorageType = StorageType.Local; |
| 20 | + |
12 | 21 | private static readonly PREFERENCE_STORAGE_NAMESPACE: string = 'preference'; |
13 | 22 | private static readonly SEPARATOR_CHAR: string = '.'; |
14 | 23 | private static readonly SEPARATOR_REGEX: RegExp = /\.(.+)/; |
15 | 24 | private readonly numberCoercer: NumberCoercer = new NumberCoercer(); |
16 | 25 | private readonly booleanCoercer: BooleanCoercer = new BooleanCoercer(); |
17 | 26 |
|
18 | | - public constructor(private readonly preferenceStorage: LocalStorage) {} |
| 27 | + public constructor(private readonly localStorage: LocalStorage, private readonly sessionStorage: SessionStorage) {} |
19 | 28 |
|
20 | 29 | /** |
21 | 30 | * Returns the current storage value if defined, else the default value. The observable |
22 | 31 | * will continue to emit as the preference is updated, reverting to default value if the |
23 | 32 | * preference becomes unset. If default value is not provided, the observable will |
24 | 33 | * throw in the case the preference is unset. |
25 | 34 | */ |
26 | | - public get<T extends PreferenceValue>(key: PreferenceKey, defaultValue?: T): Observable<T> { |
27 | | - return this.preferenceStorage.watch(this.asStorageKey(key)).pipe( |
28 | | - map(storedValue => this.fromStorageValue<T>(storedValue) ?? defaultValue), |
29 | | - switchMap(value => |
30 | | - value === undefined |
31 | | - ? throwError(Error(`No value found or default provided for preferenceKey: ${key}`)) |
32 | | - : of(value) |
33 | | - ) |
34 | | - ); |
| 35 | + public get<T extends PreferenceValue>( |
| 36 | + key: PreferenceKey, |
| 37 | + defaultValue?: T, |
| 38 | + type: StorageType = PreferenceService.DEFAULT_STORAGE_TYPE |
| 39 | + ): Observable<T> { |
| 40 | + return this.preferenceStorage(type) |
| 41 | + .watch(this.asStorageKey(key)) |
| 42 | + .pipe( |
| 43 | + map(storedValue => this.fromStorageValue<T>(storedValue) ?? defaultValue), |
| 44 | + switchMap(value => |
| 45 | + value === undefined |
| 46 | + ? throwError(Error(`No value found or default provided for preferenceKey: ${key}`)) |
| 47 | + : of(value) |
| 48 | + ) |
| 49 | + ); |
35 | 50 | } |
36 | 51 |
|
37 | | - public set(key: PreferenceKey, value: PreferenceValue): void { |
| 52 | + public set( |
| 53 | + key: PreferenceKey, |
| 54 | + value: PreferenceValue, |
| 55 | + type: StorageType = PreferenceService.DEFAULT_STORAGE_TYPE |
| 56 | + ): void { |
38 | 57 | const val = this.asStorageValue(value); |
39 | | - this.preferenceStorage.set(this.asStorageKey(key), val); |
| 58 | + this.preferenceStorage(type).set(this.asStorageKey(key), val); |
40 | 59 | } |
41 | 60 |
|
42 | 61 | private asStorageKey(key: PreferenceKey): PreferenceStorageKey { |
@@ -70,6 +89,16 @@ export class PreferenceService { |
70 | 89 | return undefined; |
71 | 90 | } |
72 | 91 | } |
| 92 | + |
| 93 | + private preferenceStorage(type: StorageType): AbstractStorage { |
| 94 | + switch (type) { |
| 95 | + case StorageType.Session: |
| 96 | + return this.sessionStorage; |
| 97 | + case StorageType.Local: |
| 98 | + default: |
| 99 | + return this.localStorage; |
| 100 | + } |
| 101 | + } |
73 | 102 | } |
74 | 103 |
|
75 | 104 | type PreferenceStorageKey = string; |
|
0 commit comments