@@ -123,32 +123,35 @@ export default class PersistentDataStoreWrapper implements LDFeatureStore {
123123
124124 init ( allData : LDFeatureStoreDataStorage , callback : ( ) => void ) : void {
125125 this . _queue . enqueue ( ( cb ) => {
126- const afterStoreInit = ( ) => {
127- this . _isInitialized = true ;
128- if ( this . _itemCache ) {
129- this . _itemCache . clear ( ) ;
130- this . _allItemsCache ! . clear ( ) ;
126+ this . _internalInit ( allData , cb ) ;
127+ } , callback ) ;
128+ }
129+ private _internalInit ( allData : LDFeatureStoreDataStorage , callback : ( ) => void ) : void {
130+ const afterStoreInit = ( ) => {
131+ this . _isInitialized = true ;
132+ if ( this . _itemCache ) {
133+ this . _itemCache . clear ( ) ;
134+ this . _allItemsCache ! . clear ( ) ;
131135
132- Object . keys ( allData ) . forEach ( ( kindNamespace ) => {
133- const kind = persistentStoreKinds [ kindNamespace ] ;
134- const items = allData [ kindNamespace ] ;
135- this . _allItemsCache ! . set ( allForKindCacheKey ( kind ) , items ) ;
136- Object . keys ( items ) . forEach ( ( key ) => {
137- const itemForKey = items [ key ] ;
136+ Object . keys ( allData ) . forEach ( ( kindNamespace ) => {
137+ const kind = persistentStoreKinds [ kindNamespace ] ;
138+ const items = allData [ kindNamespace ] ;
139+ this . _allItemsCache ! . set ( allForKindCacheKey ( kind ) , items ) ;
140+ Object . keys ( items ) . forEach ( ( key ) => {
141+ const itemForKey = items [ key ] ;
138142
139- const itemDescriptor : ItemDescriptor = {
140- version : itemForKey . version ,
141- item : itemForKey ,
142- } ;
143- this . _itemCache ! . set ( cacheKey ( kind , key ) , itemDescriptor ) ;
144- } ) ;
143+ const itemDescriptor : ItemDescriptor = {
144+ version : itemForKey . version ,
145+ item : itemForKey ,
146+ } ;
147+ this . _itemCache ! . set ( cacheKey ( kind , key ) , itemDescriptor ) ;
145148 } ) ;
146- }
147- cb ( ) ;
148- } ;
149+ } ) ;
150+ }
151+ callback ( ) ;
152+ } ;
149153
150- this . _core . init ( sortDataSet ( allData ) , afterStoreInit ) ;
151- } , callback ) ;
154+ this . _core . init ( sortDataSet ( allData ) , afterStoreInit ) ;
152155 }
153156
154157 get ( kind : DataKind , key : string , callback : ( res : LDFeatureStoreItem | null ) => void ) : void {
@@ -218,59 +221,73 @@ export default class PersistentDataStoreWrapper implements LDFeatureStore {
218221
219222 upsert ( kind : DataKind , data : LDKeyedFeatureStoreItem , callback : ( ) => void ) : void {
220223 this . _queue . enqueue ( ( cb ) => {
221- // Clear the caches which contain all the values of a specific kind.
222- if ( this . _allItemsCache ) {
223- this . _allItemsCache . clear ( ) ;
224- }
224+ this . _internalUpsert ( kind , data , cb ) ;
225+ } , callback ) ;
226+ }
227+
228+ private _internalUpsert (
229+ kind : DataKind ,
230+ data : LDKeyedFeatureStoreItem ,
231+ callback : ( ) => void ,
232+ ) : void {
233+ // Clear the caches which contain all the values of a specific kind.
234+ if ( this . _allItemsCache ) {
235+ this . _allItemsCache . clear ( ) ;
236+ }
225237
226- const persistKind = persistentStoreKinds [ kind . namespace ] ;
227- this . _core . upsert (
228- persistKind ,
229- data . key ,
230- persistKind . serialize ( data ) ,
231- ( err , updatedDescriptor ) => {
232- if ( ! err && updatedDescriptor ) {
233- if ( updatedDescriptor . serializedItem ) {
234- const value = deserialize ( persistKind , updatedDescriptor ) ;
235- this . _itemCache ?. set ( cacheKey ( kind , data . key ) , value ) ;
236- } else if ( updatedDescriptor . deleted ) {
237- // Deleted and there was not a serialized representation.
238- this . _itemCache ?. set ( data . key , {
239- key : data . key ,
240- version : updatedDescriptor . version ,
241- deleted : true ,
242- } ) ;
243- }
238+ const persistKind = persistentStoreKinds [ kind . namespace ] ;
239+ this . _core . upsert (
240+ persistKind ,
241+ data . key ,
242+ persistKind . serialize ( data ) ,
243+ ( err , updatedDescriptor ) => {
244+ if ( ! err && updatedDescriptor ) {
245+ if ( updatedDescriptor . serializedItem ) {
246+ const value = deserialize ( persistKind , updatedDescriptor ) ;
247+ this . _itemCache ?. set ( cacheKey ( kind , data . key ) , value ) ;
248+ } else if ( updatedDescriptor . deleted ) {
249+ // Deleted and there was not a serialized representation.
250+ this . _itemCache ?. set ( data . key , {
251+ key : data . key ,
252+ version : updatedDescriptor . version ,
253+ deleted : true ,
254+ } ) ;
244255 }
245- cb ( ) ;
246- } ,
247- ) ;
248- } , callback ) ;
256+ }
257+ callback ( ) ;
258+ } ,
259+ ) ;
249260 }
250261
251262 delete ( kind : DataKind , key : string , version : number , callback : ( ) => void ) : void {
252263 this . upsert ( kind , { key, version, deleted : true } , callback ) ;
253264 }
254265
255266 applyChanges (
256- _basis : boolean ,
257- _data : LDFeatureStoreDataStorage ,
258- _selector : String | undefined ,
259- _callback : ( ) => void ,
267+ basis : boolean ,
268+ data : LDFeatureStoreDataStorage ,
269+ selector : String | undefined , // TODO: SDK-1044 - Utilize selector
270+ callback : ( ) => void ,
260271 ) : void {
261- // TODO: SDK-1029 - Transactional persistent store - update this to not iterate over items and instead send data to underlying PersistentDataStore
262- // no need for queue at the moment as init and upsert handle that, but as part of SDK-1029, queue may be needed
263- if ( _basis ) {
264- this . init ( _data , _callback ) ;
265- } else {
266- Object . entries ( _data ) . forEach ( ( [ namespace , items ] ) => {
267- Object . keys ( items || { } ) . forEach ( ( key ) => {
268- const item = items [ key ] ;
269- this . upsert ( { namespace } , { key, ...item } , ( ) => { } ) ;
272+ this . _queue . enqueue ( ( cb ) => {
273+ if ( basis ) {
274+ this . _internalInit ( data , cb ) ;
275+ } else {
276+ const promises : Promise < void > [ ] = [ ] ;
277+ Object . entries ( data ) . forEach ( ( [ namespace , items ] ) => {
278+ Object . keys ( items || { } ) . forEach ( ( key ) => {
279+ promises . push (
280+ new Promise < void > ( ( resolve , _ ) => {
281+ const item = items [ key ] ;
282+ this . _internalUpsert ( { namespace } , { key, ...item } , resolve ) ; // callback intentionally not passed
283+ } ) ,
284+ ) ;
285+ } ) ;
270286 } ) ;
271- } ) ;
272- _callback ( ) ;
273- }
287+ // invoke callback after all operations complete
288+ Promise . all ( promises ) . then ( cb ) ;
289+ }
290+ } , callback ) ;
274291 }
275292
276293 close ( ) : void {
0 commit comments