diff --git a/package.json b/package.json index 219e7b0..7a69625 100644 --- a/package.json +++ b/package.json @@ -206,6 +206,7 @@ } ] }, + "setupFiles": ["./test.setup.ts"], "moduleNameMapper": { "^(\\.{1,2}/.*)\\.js$": "$1" }, diff --git a/src/lib/middleware.ts b/src/lib/middleware.ts index 04e1e28..8945209 100644 --- a/src/lib/middleware.ts +++ b/src/lib/middleware.ts @@ -31,7 +31,7 @@ export interface NamespaceValues { mappedConfig: MappedConfig config: Config query: Record - unsubscribe: () => boolean + unsubscribe: () => void } export type PushStateFunction = ( @@ -51,7 +51,7 @@ export type InnerNamespace> = Record< > interface RegistryPayload { - unsubscribe: () => boolean + unsubscribe: () => void values: ValueState initialValues: ValueState } @@ -476,10 +476,16 @@ export const converter = } thisState[ns].subscribers = thisState[ns].subscribers - 1 if (thisState[ns].subscribers === 0) { - delete thisState[ns] + // Delay the removal of subscribers, as we may have direct remounting components after + requestIdleCallback(() => { + ;(set as GenericConverter)((inner) => { + if (inner[ns]?.subscribers === 0) { + delete inner[ns] + } + }, HistoryEventType.REGISTER) + }) } }, HistoryEventType.REGISTER) - return !get().namespaces[ns] } state.mappedConfig = mappedConfig state.config = config diff --git a/test.setup.ts b/test.setup.ts new file mode 100644 index 0000000..b8d0aa8 --- /dev/null +++ b/test.setup.ts @@ -0,0 +1,4 @@ +// @ts-ignore +window.requestIdleCallback = jest.fn().mockImplementation((cb: () => unknown) => { + return setTimeout(cb, 15) +})