1
1
import { AbstractAssignmentCache } from '@eppo/js-client-sdk-common' ;
2
2
3
- import { hasWindowLocalStorage } from '../configuration-factory' ;
4
-
5
3
import { BulkReadAssignmentCache , BulkWriteAssignmentCache } from './hybrid-assignment-cache' ;
4
+ import { LocalStorageAssignmentShim } from './local-storage-assignment-shim' ;
6
5
7
6
export class LocalStorageAssignmentCache
8
7
extends AbstractAssignmentCache < LocalStorageAssignmentShim >
@@ -24,74 +23,3 @@ export class LocalStorageAssignmentCache
24
23
return Promise . resolve ( Array . from ( this . entries ( ) ) ) ;
25
24
}
26
25
}
27
-
28
- // noinspection JSUnusedGlobalSymbols (methods are used by common repository)
29
- class LocalStorageAssignmentShim implements Map < string , string > {
30
- private readonly localStorageKey : string ;
31
-
32
- public constructor ( storageKeySuffix : string ) {
33
- if ( ! hasWindowLocalStorage ( ) ) {
34
- throw new Error ( 'LocalStorage is not available' ) ;
35
- }
36
- const keySuffix = storageKeySuffix ? `-${ storageKeySuffix } ` : '' ;
37
- this . localStorageKey = `eppo-assignment${ keySuffix } ` ;
38
- }
39
-
40
- clear ( ) : void {
41
- this . getCache ( ) . clear ( ) ;
42
- }
43
-
44
- delete ( key : string ) : boolean {
45
- return this . getCache ( ) . delete ( key ) ;
46
- }
47
-
48
- forEach (
49
- callbackfn : ( value : string , key : string , map : Map < string , string > ) => void ,
50
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
51
- thisArg ?: any ,
52
- ) : void {
53
- this . getCache ( ) . forEach ( callbackfn , thisArg ) ;
54
- }
55
-
56
- size : number ;
57
-
58
- entries ( ) : IterableIterator < [ string , string ] > {
59
- return this . getCache ( ) . entries ( ) ;
60
- }
61
-
62
- keys ( ) : IterableIterator < string > {
63
- return this . getCache ( ) . keys ( ) ;
64
- }
65
-
66
- values ( ) : IterableIterator < string > {
67
- return this . getCache ( ) . values ( ) ;
68
- }
69
-
70
- [ Symbol . iterator ] ( ) : IterableIterator < [ string , string ] > {
71
- return this . getCache ( ) [ Symbol . iterator ] ( ) ;
72
- }
73
-
74
- [ Symbol . toStringTag ] : string ;
75
-
76
- public has ( key : string ) : boolean {
77
- return this . getCache ( ) . has ( key ) ;
78
- }
79
-
80
- public get ( key : string ) : string | undefined {
81
- return this . getCache ( ) . get ( key ) ?? undefined ;
82
- }
83
-
84
- public set ( key : string , value : string ) : this {
85
- return this . setCache ( this . getCache ( ) . set ( key , value ) ) ;
86
- }
87
-
88
- private getCache ( ) : Map < string , string > {
89
- const cache = window . localStorage . getItem ( this . localStorageKey ) ;
90
- return cache ? new Map ( JSON . parse ( cache ) ) : new Map ( ) ;
91
- }
92
-
93
- private setCache ( cache : Map < string , string > ) : this {
94
- window . localStorage . setItem ( this . localStorageKey , JSON . stringify ( Array . from ( cache . entries ( ) ) ) ) ;
95
- return this ;
96
- }
97
- }
0 commit comments