Skip to content

Commit b002d1a

Browse files
committed
Changes per review
1 parent 3b6abde commit b002d1a

File tree

2 files changed

+26
-15
lines changed

2 files changed

+26
-15
lines changed

packages/block-editor/src/store/reducer.js

+21-15
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,19 @@ const withPostMetaUpdateCacheReset = ( reducer ) => ( state, action ) => {
218218
return newState;
219219
};
220220

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+
221234
/**
222235
* Higher-order reducer intended to compute a cache key for each block in the post.
223236
* 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 ) => {
235248
}
236249
newState.cache = state.cache ? state.cache : {};
237250

238-
const addParentBlocks = ( clientIds ) => {
251+
const getBlocksWithParentsClientIds = ( clientIds ) => {
239252
return clientIds.reduce( ( result, clientId ) => {
240253
let current = clientId;
241254
do {
@@ -246,13 +259,6 @@ const withBlockCache = ( reducer ) => ( state = {}, action ) => {
246259
}, [] );
247260
};
248261

249-
const fillKeysWithEmptyObject = ( clientIds ) => {
250-
return clientIds.reduce( ( result, key ) => {
251-
result[ key ] = {};
252-
return result;
253-
}, {} );
254-
};
255-
256262
switch ( action.type ) {
257263
case 'RESET_BLOCKS':
258264
newState.cache = mapValues( flattenBlocks( action.blocks ), () => ( {} ) );
@@ -266,7 +272,7 @@ const withBlockCache = ( reducer ) => ( state = {}, action ) => {
266272
newState.cache = {
267273
...newState.cache,
268274
...fillKeysWithEmptyObject(
269-
addParentBlocks( updatedBlockUids ),
275+
getBlocksWithParentsClientIds( updatedBlockUids ),
270276
),
271277
};
272278
break;
@@ -276,23 +282,23 @@ const withBlockCache = ( reducer ) => ( state = {}, action ) => {
276282
newState.cache = {
277283
...newState.cache,
278284
...fillKeysWithEmptyObject(
279-
addParentBlocks( [ action.clientId ] ),
285+
getBlocksWithParentsClientIds( [ action.clientId ] ),
280286
),
281287
};
282288
break;
283289
case 'REPLACE_BLOCKS':
284290
newState.cache = {
285291
...omit( newState.cache, action.replacedClientIds ),
286292
...fillKeysWithEmptyObject(
287-
addParentBlocks( keys( flattenBlocks( action.blocks ) ) ),
293+
getBlocksWithParentsClientIds( keys( flattenBlocks( action.blocks ) ) ),
288294
),
289295
};
290296
break;
291297
case 'REMOVE_BLOCKS':
292298
newState.cache = {
293299
...omit( newState.cache, action.removedClientIds ),
294300
...fillKeysWithEmptyObject(
295-
difference( addParentBlocks( action.clientIds ), action.clientIds ),
301+
difference( getBlocksWithParentsClientIds( action.clientIds ), action.clientIds ),
296302
),
297303
};
298304
break;
@@ -307,7 +313,7 @@ const withBlockCache = ( reducer ) => ( state = {}, action ) => {
307313
newState.cache = {
308314
...newState.cache,
309315
...fillKeysWithEmptyObject(
310-
addParentBlocks( updatedBlockUids )
316+
getBlocksWithParentsClientIds( updatedBlockUids )
311317
),
312318
};
313319
break;
@@ -321,7 +327,7 @@ const withBlockCache = ( reducer ) => ( state = {}, action ) => {
321327
newState.cache = {
322328
...newState.cache,
323329
...fillKeysWithEmptyObject(
324-
addParentBlocks( updatedBlockUids )
330+
getBlocksWithParentsClientIds( updatedBlockUids )
325331
),
326332
};
327333
break;
@@ -334,7 +340,7 @@ const withBlockCache = ( reducer ) => ( state = {}, action ) => {
334340
newState.cache = {
335341
...newState.cache,
336342
...fillKeysWithEmptyObject(
337-
addParentBlocks( updatedBlockUids )
343+
getBlocksWithParentsClientIds( updatedBlockUids )
338344
),
339345
};
340346
}

packages/block-editor/src/store/selectors.js

+5
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,11 @@ export const getBlock = createSelector(
170170
};
171171
},
172172
( state, clientId ) => [
173+
// Normally, we'd have both `getBlockAttributes` dependancies and
174+
// `getBlocks` (children) dependancies here but for performance reasons
175+
// we use a denormalized cache key computed in the reducer that takes both
176+
// the attributes and inner blocks into account. The value of the cache key
177+
// is being changed whenever one of these dependencies is out of date.
173178
state.blocks.cache[ clientId ],
174179
]
175180
);

0 commit comments

Comments
 (0)