Skip to content

Commit

Permalink
createPersistor: Separate serializing from writing.
Browse files Browse the repository at this point in the history
One thing that should help mitigate zulip#4841 is to narrow the window
during which we're propagating a Redux state change to persistent
storage. This does quite a bit of that, by not having the `setItem`
calls separated by (a) the CPU time to do the serialization, or (b)
yields, as Greg points out [1].

[1] zulip#4694 (comment)
  • Loading branch information
chrisbobbe authored and gnprice committed Sep 15, 2021
1 parent b54737f commit 7faa494
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/third/redux-persist/createPersistor.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,18 @@ export default function createPersistor (store, config) {
updatedSubstates.push([key, state[key]])
});

const writes = []
while (updatedSubstates.length > 0) {
await new Promise(r => setTimeout(r, 0));

const [key, substate] = updatedSubstates.shift()
const storageKey = createStorageKey(key)
storage.setItem(storageKey, serializer(substate)).catch(warnIfSetError(key))
writes.push([key, serializer(substate)])
}

writes.forEach(([key, serializedSubstate]) => {
storage.setItem(createStorageKey(key), serializedSubstate).catch(warnIfSetError(key))
})

writeInProgress = false
lastState = state
})()
Expand Down

0 comments on commit 7faa494

Please sign in to comment.