@@ -17,52 +17,7 @@ import { getMatching } from '../../utils/key';
1717import { validateCache } from './validateCache' ;
1818import { ILogger } from '../../logger/types' ;
1919import SplitIO from '../../../types/splitio' ;
20-
21- function isTillKey ( key : string ) {
22- return key . endsWith ( '.till' ) ;
23- }
24-
25- export function storageAdapter ( log : ILogger , prefix : string , wrapper : SplitIO . StorageWrapper ) : StorageAdapter {
26- let cache : Record < string , string > = { } ;
27-
28- let connectPromise : Promise < void > | undefined ;
29- let disconnectPromise = Promise . resolve ( ) ;
30-
31- return {
32- load ( ) {
33- return connectPromise || ( connectPromise = Promise . resolve ( wrapper . getItem ( prefix ) ) . then ( ( storedCache ) => {
34- cache = JSON . parse ( storedCache || '{}' ) ;
35- } ) . catch ( ( e ) => {
36- log . error ( LOG_PREFIX + 'Rejected promise calling storage getItem, with error: ' + e ) ;
37- } ) ) ;
38- } ,
39- save ( ) {
40- return disconnectPromise = disconnectPromise . then ( ( ) => {
41- return Promise . resolve ( wrapper . setItem ( prefix , JSON . stringify ( cache ) ) ) . catch ( ( e ) => {
42- log . error ( LOG_PREFIX + 'Rejected promise calling storage setItem, with error: ' + e ) ;
43- } ) ;
44- } ) ;
45- } ,
46-
47- get length ( ) {
48- return Object . keys ( cache ) . length ;
49- } ,
50- getItem ( key : string ) {
51- return cache [ key ] || null ;
52- } ,
53- key ( index : number ) {
54- return Object . keys ( cache ) [ index ] || null ;
55- } ,
56- removeItem ( key : string ) {
57- delete cache [ key ] ;
58- if ( isTillKey ( key ) ) this . save ! ( ) ;
59- } ,
60- setItem ( key : string , value : string ) {
61- cache [ key ] = value ;
62- if ( isTillKey ( key ) ) this . save ! ( ) ;
63- }
64- } ;
65- }
20+ import { storageAdapter } from './storageAdapter' ;
6621
6722function validateStorage ( log : ILogger , prefix : string , wrapper ?: SplitIO . StorageWrapper ) : StorageAdapter | undefined {
6823 if ( wrapper ) {
0 commit comments