Skip to content

Commit 8840d8b

Browse files
committed
fix race condition
1 parent 604c96d commit 8840d8b

File tree

1 file changed

+12
-19
lines changed

1 file changed

+12
-19
lines changed

packages/devtools/src/core.ts

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,8 @@ export class TanStackDevtoolsCore {
4040
...initialState.settings,
4141
}
4242
#plugins: Array<TanStackDevtoolsPlugin> = []
43-
#isMounted = false
44-
#isMounting = false
45-
#abortMount = false
43+
#state: 'mounted' | 'mounting' | 'unmounted' = 'unmounted'
44+
#mountAbortController?: AbortController
4645
#dispose?: () => void
4746
#eventBus?: { stop: () => void }
4847
#eventBusConfig: ClientEventBusConfig | undefined
@@ -60,16 +59,15 @@ export class TanStackDevtoolsCore {
6059
mount<T extends HTMLElement>(el: T) {
6160
if (typeof document === 'undefined') return
6261

63-
if (this.#isMounted || this.#isMounting) {
62+
if (this.#state === 'mounted' || this.#state === 'mounting') {
6463
throw new Error('Devtools is already mounted')
6564
}
66-
this.#isMounting = true
67-
this.#abortMount = false
65+
this.#state = 'mounting'
66+
const { signal } = (this.#mountAbortController = new AbortController())
6867

6968
import('./mount-impl')
7069
.then(({ mountDevtools }) => {
71-
if (this.#abortMount) {
72-
this.#isMounting = false
70+
if (signal.aborted) {
7371
return
7472
}
7573

@@ -85,27 +83,22 @@ export class TanStackDevtoolsCore {
8583

8684
this.#dispose = result.dispose
8785
this.#eventBus = result.eventBus
88-
this.#isMounted = true
89-
this.#isMounting = false
86+
this.#state = 'mounted'
9087
})
9188
.catch((err) => {
92-
this.#isMounting = false
89+
this.#state = 'unmounted'
9390
console.error('[TanStack Devtools] Failed to load:', err)
9491
})
9592
}
9693

9794
unmount() {
98-
if (!this.#isMounted && !this.#isMounting) {
95+
if (this.#state === 'unmounted') {
9996
throw new Error('Devtools is not mounted')
10097
}
101-
if (this.#isMounting) {
102-
this.#abortMount = true
103-
this.#isMounting = false
104-
return
105-
}
98+
this.#mountAbortController?.abort()
10699
this.#eventBus?.stop()
107100
this.#dispose?.()
108-
this.#isMounted = false
101+
this.#state = 'unmounted'
109102
}
110103

111104
setConfig(config: Partial<TanStackDevtoolsInit>) {
@@ -116,7 +109,7 @@ export class TanStackDevtoolsCore {
116109
if (config.plugins) {
117110
this.#plugins = config.plugins
118111
// Update the reactive store if mounted
119-
if (this.#isMounted && this.#setPlugins) {
112+
if (this.#state === 'mounted' && this.#setPlugins) {
120113
this.#setPlugins(config.plugins)
121114
}
122115
}

0 commit comments

Comments
 (0)