diff --git a/lib/reducers/thread-reducer.js b/lib/reducers/thread-reducer.js --- a/lib/reducers/thread-reducer.js +++ b/lib/reducers/thread-reducer.js @@ -59,19 +59,28 @@ const { processStoreOperations: processThreadStoreOperations } = threadStoreOpsHandlers; -function generateOpsForThreadUpdates( - threadInfos: RawThreadInfos, +function generateOpsAndProcessThreadUpdates( + threadStore: ThreadStore, newUpdates: $ReadOnlyArray, -): $ReadOnlyArray { - return newUpdates - .map(update => - updateSpecs[update.type].generateOpsForThreadUpdates?.( - threadInfos, - update, - ), - ) - .filter(Boolean) - .flat(); +): { + +threadStoreOperations: $ReadOnlyArray, + +updatedThreadStore: ThreadStore, +} { + const operations: Array = []; + let store = threadStore; + for (const update of newUpdates) { + const ops = updateSpecs[update.type].generateOpsForThreadUpdates?.( + store.threadInfos, + update, + ); + if (!ops || ops.length === 0) { + continue; + } + + operations.push(...ops); + store = processThreadStoreOperations(store, ops); + } + return { threadStoreOperations: operations, updatedThreadStore: store }; } type ReduceThreadInfosResult = { @@ -261,14 +270,8 @@ threadStoreOperations: [], }; } - const threadStoreOperations = generateOpsForThreadUpdates( - state.threadInfos, - newUpdates, - ); - const updatedThreadStore = processThreadStoreOperations( - state, - threadStoreOperations, - ); + const { threadStoreOperations, updatedThreadStore } = + generateOpsAndProcessThreadUpdates(state, newUpdates); return { threadStore: updatedThreadStore, newThreadInconsistencies: [], @@ -286,14 +289,8 @@ threadStoreOperations: [], }; } - const threadStoreOperations = generateOpsForThreadUpdates( - state.threadInfos, - newUpdates, - ); - const updatedThreadStore = processThreadStoreOperations( - state, - threadStoreOperations, - ); + const { threadStoreOperations, updatedThreadStore } = + generateOpsAndProcessThreadUpdates(state, newUpdates); return { threadStore: updatedThreadStore, newThreadInconsistencies: [], @@ -544,14 +541,8 @@ threadStoreOperations: [], }; } - const threadStoreOperations = generateOpsForThreadUpdates( - state.threadInfos, - updateInfos, - ); - const updatedThreadStore = processThreadStoreOperations( - state, - threadStoreOperations, - ); + const { threadStoreOperations, updatedThreadStore } = + generateOpsAndProcessThreadUpdates(state, updateInfos); return { threadStore: updatedThreadStore, newThreadInconsistencies: [],