Open History to see the last commit
1import type { TanStackDevtoolsPluginProps } from '@tanstack/devtools'
2import type { JSX } from 'solid-js'
3β
4/**
5 * Constructs the core class for the Devtools.
6 * This utility is used to construct a lazy loaded Solid component for the Devtools.
7 * It returns a tuple containing the main DevtoolsCore class and a NoOpDevtoolsCore class.
8 * The NoOpDevtoolsCore class is a no-op implementation that can be used for production if you want to explicitly exclude
9 * the Devtools from your application.
10 * @param importFn A function that returns a dynamic import of the Solid component
11 * @returns Tuple containing the DevtoolsCore class and a NoOpDevtoolsCore class
12 */
13export function constructCoreClass(
14 importFn: () => Promise<{
15 default: (props: TanStackDevtoolsPluginProps) => JSX.Element
16 }>,
17) {
18 class DevtoolsCore {
19 #isMounted = false
20 #isMounting = false
21 #abortMount = false
22 #dispose?: () => void
23β
24 constructor() {}
25β
26 async mount<T extends HTMLElement>(
27 el: T,
28 props: TanStackDevtoolsPluginProps,
29 ) {
30 if (this.#isMounted || this.#isMounting) {
31 throw new Error('Devtools is already mounted')
32 }
33 this.#isMounting = true
34 this.#abortMount = false
35β
36 try {
37 const { __mountComponent } = await import('./class-mount-impl')
38 // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition -- can be set by unmount() during await
39 if (this.#abortMount) {
40 this.#isMounting = false
41 return
42 }
43β
44 this.#dispose = __mountComponent(el, props, importFn)
45 this.#isMounted = true
46 this.#isMounting = false
47 } catch (err) {
48 this.#isMounting = false
49 console.error('[TanStack Devtools] Failed to load:', err)
50 }
51 }
52β
53 unmount() {
54 if (!this.#isMounted && !this.#isMounting) {
55 throw new Error('Devtools is not mounted')
56 }
57β
58 if (this.#isMounting) {
59 this.#abortMount = true
60 this.#isMounting = false
61 return
62 }
63β
64 this.#dispose?.()
65 this.#isMounted = false
66 }
67 }
68β
69 class NoOpDevtoolsCore extends DevtoolsCore {
70 constructor() {
71 super()
72 }
73 async mount<T extends HTMLElement>(
74 _el: T,
75 _props: TanStackDevtoolsPluginProps,
76 ) {}
77 unmount() {}
78 }
79β
80 return [DevtoolsCore, NoOpDevtoolsCore] as const
81}
82β
83export type ClassType = ReturnType<typeof constructCoreClass>[0]