Skip to content

Commit a1fea2e

Browse files
dai-shichungyo
andauthored
fix(devtools): preserve shared connections during store cleanup (#3580)
* fix(devtools): preserve shared connections during store cleanup * test(devtools): cover cleanup review edge cases Cover independent and shared connections using the same name, and verify that repeated cleanup preserves replacement registrations. Use a curly block for the cleanup guard. * fix(devtools): check store identity during cleanup Pass the store API to tracked connection cleanup so an older store cannot remove a replacement registration. Remove the cleanup flag and cover replacement before the first cleanup call. --------- Co-authored-by: chungyo <[email protected]>
1 parent d867e6f commit a1fea2e

2 files changed

Lines changed: 142 additions & 4 deletions

File tree

‎src/middleware/devtools.ts‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,10 +156,11 @@ const extractConnectionInformation = (
156156
const removeStoreFromTrackedConnections = (
157157
name: string | undefined,
158158
store: string | undefined,
159+
api: StoreInformation,
159160
) => {
160161
if (store === undefined) return
161162
const connectionInfo = trackedConnections.get(name)
162-
if (!connectionInfo) return
163+
if (connectionInfo?.stores[store] !== api) return
163164
delete connectionInfo.stores[store]
164165
if (Object.keys(connectionInfo.stores).length === 0) {
165166
trackedConnections.delete(name)
@@ -243,15 +244,19 @@ const devtoolsImpl: DevtoolsImpl =
243244
)
244245
return r
245246
}) as NamedSet<S>
247+
let unsubscribeFromStore: (() => void) | undefined
246248
;(api as StoreApi<S> & StoreDevtools<S>).devtools = {
247249
cleanup: () => {
250+
unsubscribeFromStore?.()
251+
unsubscribeFromStore = undefined
252+
removeStoreFromTrackedConnections(options.name, store, api)
248253
if (
254+
(store === undefined || !trackedConnections.has(options.name)) &&
249255
connection &&
250256
typeof (connection as any).unsubscribe === 'function'
251257
) {
252258
;(connection as any).unsubscribe()
253259
}
254-
removeStoreFromTrackedConnections(options.name, store)
255260
},
256261
}
257262

@@ -298,7 +303,7 @@ const devtoolsImpl: DevtoolsImpl =
298303
}
299304
}
300305

301-
;(
306+
unsubscribeFromStore = (
302307
connection as unknown as {
303308
// FIXME https://github.com/reduxjs/redux-devtools/issues/1097
304309
subscribe: (

‎tests/devtools.test.tsx‎

Lines changed: 134 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,12 @@ const extensionConnector = {
9696
const api: Connection['api'] = {
9797
subscribe: vi.fn((f: (m: unknown) => void) => {
9898
subscribers.push(f)
99-
return () => {}
99+
return () => {
100+
subscribers.splice(subscribers.indexOf(f), 1)
101+
}
100102
}),
101103
unsubscribe: vi.fn(() => {
104+
subscribers.splice(0)
102105
connectionMap.delete(
103106
areNameUndefinedMapsNeeded ? options.testConnectionId : key,
104107
)
@@ -2687,6 +2690,136 @@ describe('when create devtools was called multiple times with `name` and `store`
26872690
})
26882691

26892692
describe('cleanup', () => {
2693+
it('should preserve a replacement store when the original store is cleaned up', () => {
2694+
const options = {
2695+
name: 'replacement-before-cleanup',
2696+
store: 'shared',
2697+
enabled: true,
2698+
}
2699+
const original = createStore(devtools(() => ({ count: 0 }), options))
2700+
const replacement = createStore(devtools(() => ({ count: 0 }), options))
2701+
const [connection] = getNamedConnectionApis(options.name)
2702+
2703+
original.devtools.cleanup()
2704+
2705+
expect(connection.unsubscribe).not.toHaveBeenCalled()
2706+
const [subscriber] = getNamedConnectionSubscribers(options.name)
2707+
subscriber({
2708+
type: 'DISPATCH',
2709+
payload: { type: 'JUMP_TO_STATE' },
2710+
state: JSON.stringify({ shared: { count: 20 } }),
2711+
})
2712+
expect(replacement.getState()).toEqual({ count: 20 })
2713+
expect(original.getState()).toEqual({ count: 0 })
2714+
2715+
original.devtools.cleanup()
2716+
expect(connection.unsubscribe).not.toHaveBeenCalled()
2717+
replacement.devtools.cleanup()
2718+
expect(connection.unsubscribe).toHaveBeenCalledTimes(1)
2719+
})
2720+
2721+
it.each([1, 2])(
2722+
'should preserve other stores on a shared connection after %i cleanup calls',
2723+
(cleanupCalls) => {
2724+
const name = `shared-cleanup-${cleanupCalls}`
2725+
const store1 = createStore(
2726+
devtools(() => ({ count: 0 }), { name, store: 'first', enabled: true }),
2727+
)
2728+
const store2 = createStore(
2729+
devtools(() => ({ count: 0 }), {
2730+
name,
2731+
store: 'second',
2732+
enabled: true,
2733+
}),
2734+
)
2735+
const subscribers = namedConnections.get(name)!.subscribers
2736+
2737+
for (let i = 0; i < cleanupCalls; i++) {
2738+
store1.devtools.cleanup()
2739+
}
2740+
subscribers.forEach((subscriber) =>
2741+
subscriber({
2742+
type: 'DISPATCH',
2743+
payload: { type: 'JUMP_TO_STATE' },
2744+
state: JSON.stringify({
2745+
first: { count: 10 },
2746+
second: { count: 20 },
2747+
}),
2748+
}),
2749+
)
2750+
2751+
expect(store1.getState()).toEqual({ count: 0 })
2752+
expect(store2.getState()).toEqual({ count: 20 })
2753+
store2.devtools.cleanup()
2754+
},
2755+
)
2756+
2757+
it('should close a shared connection only after its last store is cleaned up', () => {
2758+
const name = 'last-store-cleanup'
2759+
const store1 = createStore(
2760+
devtools(() => ({ count: 0 }), { name, store: 'first', enabled: true }),
2761+
)
2762+
const store2 = createStore(
2763+
devtools(() => ({ count: 0 }), { name, store: 'second', enabled: true }),
2764+
)
2765+
const [connection] = getNamedConnectionApis(name)
2766+
2767+
store1.devtools.cleanup()
2768+
expect(connection.unsubscribe).not.toHaveBeenCalled()
2769+
store2.devtools.cleanup()
2770+
expect(connection.unsubscribe).toHaveBeenCalledTimes(1)
2771+
2772+
const replacement = createStore(
2773+
devtools(() => ({ count: 0 }), { name, store: 'first', enabled: true }),
2774+
)
2775+
const [subscriber] = getNamedConnectionSubscribers(name)
2776+
store1.devtools.cleanup()
2777+
subscriber({
2778+
type: 'DISPATCH',
2779+
payload: { type: 'JUMP_TO_STATE' },
2780+
state: JSON.stringify({ first: { count: 30 } }),
2781+
})
2782+
2783+
expect(replacement.getState()).toEqual({ count: 30 })
2784+
const connectCalls = extensionConnector.connect.mock.calls.length
2785+
const sibling = createStore(
2786+
devtools(() => ({ count: 0 }), { name, store: 'second', enabled: true }),
2787+
)
2788+
expect(extensionConnector.connect).toHaveBeenCalledTimes(connectCalls)
2789+
2790+
sibling.devtools.cleanup()
2791+
replacement.devtools.cleanup()
2792+
})
2793+
2794+
it('should close an untracked connection while a tracked connection uses the same name', () => {
2795+
const name = 'mixed-connection-cleanup'
2796+
const options = { name, testStore: 'untracked', enabled: true }
2797+
const untrackedStore = createStore(devtools(() => ({ count: 0 }), options))
2798+
const trackedStore = createStore(
2799+
devtools(() => ({ count: 0 }), { name, store: 'tracked', enabled: true }),
2800+
)
2801+
const [untrackedConnection, trackedConnection] = getNamedConnectionApis(
2802+
`${name}|untracked`,
2803+
name,
2804+
)
2805+
2806+
untrackedStore.devtools.cleanup()
2807+
2808+
expect(untrackedConnection.unsubscribe).toHaveBeenCalledTimes(1)
2809+
expect(trackedConnection.unsubscribe).not.toHaveBeenCalled()
2810+
const [subscriber] = getNamedConnectionSubscribers(name)
2811+
subscriber({
2812+
type: 'DISPATCH',
2813+
payload: { type: 'JUMP_TO_STATE' },
2814+
state: JSON.stringify({ tracked: { count: 20 } }),
2815+
})
2816+
expect(trackedStore.getState()).toEqual({ count: 20 })
2817+
expect(untrackedStore.getState()).toEqual({ count: 0 })
2818+
2819+
trackedStore.devtools.cleanup()
2820+
expect(trackedConnection.unsubscribe).toHaveBeenCalledTimes(1)
2821+
})
2822+
26902823
it('should unsubscribe from devtools when cleanup is called', async () => {
26912824
const options = { name: 'test' }
26922825
const store = createStore(devtools(() => ({ count: 0 }), options))

0 commit comments

Comments
 (0)