Skip to content

Commit

Permalink
fix: delay the cleanup of unused namespaces
Browse files Browse the repository at this point in the history
  • Loading branch information
BowlingX committed May 14, 2024
1 parent 1dbbcd0 commit e7c5fef
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@
}
]
},
"setupFiles": ["./test.setup.ts"],
"moduleNameMapper": {
"^(\\.{1,2}/.*)\\.js$": "$1"
},
Expand Down
14 changes: 10 additions & 4 deletions src/lib/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export interface NamespaceValues<ValueState extends object> {
mappedConfig: MappedConfig
config: Config
query: Record<string, string>
unsubscribe: () => boolean
unsubscribe: () => void
}

export type PushStateFunction<T> = (
Expand All @@ -51,7 +51,7 @@ export type InnerNamespace<V extends Record<string, unknown>> = Record<
>

interface RegistryPayload<ValueState> {
unsubscribe: () => boolean
unsubscribe: () => void
values: ValueState
initialValues: ValueState
}
Expand Down Expand Up @@ -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<V, T>)((inner) => {
if (inner[ns]?.subscribers === 0) {
delete inner[ns]
}
}, HistoryEventType.REGISTER)
})
}
}, HistoryEventType.REGISTER)
return !get().namespaces[ns]
}
state.mappedConfig = mappedConfig
state.config = config
Expand Down
4 changes: 4 additions & 0 deletions test.setup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// @ts-ignore
window.requestIdleCallback = jest.fn().mockImplementation((cb: () => unknown) => {
return setTimeout(cb, 15)
})

0 comments on commit e7c5fef

Please sign in to comment.