Skip to content

Commit d867e6f

Browse files
dai-shikoreahghg
andauthored
fix(devtools): apply serialize.reviver when parsing devtools messages (#3568)
connect() forwards `serialize` to the extension for outgoing state, but incoming ACTION/ROLLBACK/JUMP_TO_STATE/JUMP_TO_ACTION messages were parsed with a bare JSON.parse, so a configured reviver never ran. Types like Immutable.js collections came back as plain objects after using devtools' Jump/Revert/Rollback controls. Fixes #2561 Co-authored-by: koreahghg <[email protected]>
1 parent dac22e1 commit d867e6f

2 files changed

Lines changed: 70 additions & 4 deletions

File tree

‎src/middleware/devtools.ts‎

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,10 @@ const devtoolsImpl: DevtoolsImpl =
306306
) => (() => void) | undefined
307307
}
308308
).subscribe((message) => {
309+
const reviver =
310+
typeof options.serialize === 'object'
311+
? options.serialize.reviver
312+
: undefined
309313
switch (message.type) {
310314
case 'ACTION':
311315
if (typeof message.payload !== 'string') {
@@ -316,6 +320,7 @@ const devtoolsImpl: DevtoolsImpl =
316320
}
317321
return parseJsonThen<{ type: unknown; state?: PartialState }>(
318322
message.payload,
323+
reviver,
319324
(action) => {
320325
if (action.type === '__setState') {
321326
if (store === undefined) {
@@ -370,7 +375,7 @@ const devtoolsImpl: DevtoolsImpl =
370375
return connection?.init(getTrackedConnectionState(options.name))
371376

372377
case 'ROLLBACK':
373-
return parseJsonThen<S>(message.state, (state) => {
378+
return parseJsonThen<S>(message.state, reviver, (state) => {
374379
if (store === undefined) {
375380
setStateFromDevtools(state)
376381
connection?.init(api.getState())
@@ -382,7 +387,7 @@ const devtoolsImpl: DevtoolsImpl =
382387

383388
case 'JUMP_TO_STATE':
384389
case 'JUMP_TO_ACTION':
385-
return parseJsonThen<S>(message.state, (state) => {
390+
return parseJsonThen<S>(message.state, reviver, (state) => {
386391
if (store === undefined) {
387392
setStateFromDevtools(state)
388393
return
@@ -423,10 +428,14 @@ const devtoolsImpl: DevtoolsImpl =
423428
}
424429
export const devtools = devtoolsImpl as unknown as Devtools
425430

426-
const parseJsonThen = <T>(stringified: string, fn: (parsed: T) => void) => {
431+
const parseJsonThen = <T>(
432+
stringified: string,
433+
reviver: ((key: string, value: unknown) => unknown) | undefined,
434+
fn: (parsed: T) => void,
435+
) => {
427436
let parsed: T | undefined
428437
try {
429-
parsed = JSON.parse(stringified)
438+
parsed = JSON.parse(stringified, reviver)
430439
} catch (e) {
431440
console.error(
432441
'[zustand devtools middleware] Could not parse the received json',

‎tests/devtools.test.tsx‎

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -736,6 +736,63 @@ describe('when it receives a message of type...', () => {
736736
)
737737
})
738738
})
739+
740+
describe('serialize.reviver option...', () => {
741+
const reviver = (key: string, value: unknown) =>
742+
key === 'count' ? Number(value) * 10 : value
743+
744+
it('applies reviver to ACTION `__setState` payload', async () => {
745+
const initialState = { count: 0 }
746+
const api = createStore(
747+
devtools(() => initialState, { enabled: true, serialize: { reviver } }),
748+
)
749+
750+
const [connectionSubscriber] = getNamedConnectionSubscribers(undefined)
751+
connectionSubscriber({
752+
type: 'ACTION',
753+
payload: '{ "type": "__setState", "state": { "count": 5 } }',
754+
})
755+
756+
expect(api.getState()).toStrictEqual({ ...initialState, count: 50 })
757+
})
758+
759+
it('applies reviver to `message.state` on JUMP_TO_STATE', async () => {
760+
const initialState = { count: 0 }
761+
const api = createStore(
762+
devtools(() => initialState, { enabled: true, serialize: { reviver } }),
763+
)
764+
const newState = { count: 5 }
765+
766+
const [connection] = getNamedConnectionApis(undefined)
767+
connection.send.mockClear()
768+
const [connectionSubscriber] = getNamedConnectionSubscribers(undefined)
769+
connectionSubscriber({
770+
type: 'DISPATCH',
771+
payload: { type: 'JUMP_TO_STATE' },
772+
state: JSON.stringify(newState),
773+
})
774+
775+
expect(api.getState()).toStrictEqual({ ...initialState, count: 50 })
776+
expect(connection.send).not.toBeCalled()
777+
})
778+
779+
it('applies reviver to `message.state` on ROLLBACK', async () => {
780+
const initialState = { count: 0 }
781+
const api = createStore(
782+
devtools(() => initialState, { enabled: true, serialize: { reviver } }),
783+
)
784+
const newState = { count: 5 }
785+
786+
const [connectionSubscriber] = getNamedConnectionSubscribers(undefined)
787+
connectionSubscriber({
788+
type: 'DISPATCH',
789+
payload: { type: 'ROLLBACK' },
790+
state: JSON.stringify(newState),
791+
})
792+
793+
expect(api.getState()).toStrictEqual({ ...initialState, count: 50 })
794+
})
795+
})
739796
})
740797

741798
describe('with redux middleware', () => {

0 commit comments

Comments
 (0)