@@ -218,6 +218,19 @@ const withPostMetaUpdateCacheReset = ( reducer ) => ( state, action ) => {
218
218
return newState ;
219
219
} ;
220
220
221
+ /**
222
+ * Utility returning an object with an empty object value for each key.
223
+ *
224
+ * @param {Array } objectKeys Keys to fill.
225
+ * @return {Object } Object filled with empty object as values for each clientId.
226
+ */
227
+ const fillKeysWithEmptyObject = ( objectKeys ) => {
228
+ return objectKeys . reduce ( ( result , key ) => {
229
+ result [ key ] = { } ;
230
+ return result ;
231
+ } , { } ) ;
232
+ } ;
233
+
221
234
/**
222
235
* Higher-order reducer intended to compute a cache key for each block in the post.
223
236
* A new instance of the cache key (empty object) is created each time the block object
@@ -235,7 +248,7 @@ const withBlockCache = ( reducer ) => ( state = {}, action ) => {
235
248
}
236
249
newState . cache = state . cache ? state . cache : { } ;
237
250
238
- const addParentBlocks = ( clientIds ) => {
251
+ const getBlocksWithParentsClientIds = ( clientIds ) => {
239
252
return clientIds . reduce ( ( result , clientId ) => {
240
253
let current = clientId ;
241
254
do {
@@ -246,13 +259,6 @@ const withBlockCache = ( reducer ) => ( state = {}, action ) => {
246
259
} , [ ] ) ;
247
260
} ;
248
261
249
- const fillKeysWithEmptyObject = ( clientIds ) => {
250
- return clientIds . reduce ( ( result , key ) => {
251
- result [ key ] = { } ;
252
- return result ;
253
- } , { } ) ;
254
- } ;
255
-
256
262
switch ( action . type ) {
257
263
case 'RESET_BLOCKS' :
258
264
newState . cache = mapValues ( flattenBlocks ( action . blocks ) , ( ) => ( { } ) ) ;
@@ -266,7 +272,7 @@ const withBlockCache = ( reducer ) => ( state = {}, action ) => {
266
272
newState . cache = {
267
273
...newState . cache ,
268
274
...fillKeysWithEmptyObject (
269
- addParentBlocks ( updatedBlockUids ) ,
275
+ getBlocksWithParentsClientIds ( updatedBlockUids ) ,
270
276
) ,
271
277
} ;
272
278
break ;
@@ -276,23 +282,23 @@ const withBlockCache = ( reducer ) => ( state = {}, action ) => {
276
282
newState . cache = {
277
283
...newState . cache ,
278
284
...fillKeysWithEmptyObject (
279
- addParentBlocks ( [ action . clientId ] ) ,
285
+ getBlocksWithParentsClientIds ( [ action . clientId ] ) ,
280
286
) ,
281
287
} ;
282
288
break ;
283
289
case 'REPLACE_BLOCKS' :
284
290
newState . cache = {
285
291
...omit ( newState . cache , action . replacedClientIds ) ,
286
292
...fillKeysWithEmptyObject (
287
- addParentBlocks ( keys ( flattenBlocks ( action . blocks ) ) ) ,
293
+ getBlocksWithParentsClientIds ( keys ( flattenBlocks ( action . blocks ) ) ) ,
288
294
) ,
289
295
} ;
290
296
break ;
291
297
case 'REMOVE_BLOCKS' :
292
298
newState . cache = {
293
299
...omit ( newState . cache , action . removedClientIds ) ,
294
300
...fillKeysWithEmptyObject (
295
- difference ( addParentBlocks ( action . clientIds ) , action . clientIds ) ,
301
+ difference ( getBlocksWithParentsClientIds ( action . clientIds ) , action . clientIds ) ,
296
302
) ,
297
303
} ;
298
304
break ;
@@ -307,7 +313,7 @@ const withBlockCache = ( reducer ) => ( state = {}, action ) => {
307
313
newState . cache = {
308
314
...newState . cache ,
309
315
...fillKeysWithEmptyObject (
310
- addParentBlocks ( updatedBlockUids )
316
+ getBlocksWithParentsClientIds ( updatedBlockUids )
311
317
) ,
312
318
} ;
313
319
break ;
@@ -321,7 +327,7 @@ const withBlockCache = ( reducer ) => ( state = {}, action ) => {
321
327
newState . cache = {
322
328
...newState . cache ,
323
329
...fillKeysWithEmptyObject (
324
- addParentBlocks ( updatedBlockUids )
330
+ getBlocksWithParentsClientIds ( updatedBlockUids )
325
331
) ,
326
332
} ;
327
333
break ;
@@ -334,7 +340,7 @@ const withBlockCache = ( reducer ) => ( state = {}, action ) => {
334
340
newState . cache = {
335
341
...newState . cache ,
336
342
...fillKeysWithEmptyObject (
337
- addParentBlocks ( updatedBlockUids )
343
+ getBlocksWithParentsClientIds ( updatedBlockUids )
338
344
) ,
339
345
} ;
340
346
}
0 commit comments