Skip to content
Source
Sign inSign up
TanStack/devtools
Open History to see the last commit

devtools.svelte.ts

117 lines3.1 KB

1import { mount, unmount } from 'svelte'
2import { PLUGIN_CONTAINER_ID, TanStackDevtoolsCore } from '@tanstack/devtools'
3import type { Component } from 'svelte'
4import type { TanStackDevtoolsPlugin } from '@tanstack/devtools'
5import type {
6 TanStackDevtoolsSvelteInit,
7 TanStackDevtoolsSveltePlugin,
8} from './types'
9​
10type MountedComponent = ReturnType<typeof mount>
11​
12export class TanStackDevtoolsSvelteAdapter {
13 private devtools: TanStackDevtoolsCore | null = null
14 private mountedComponents: Array<{
15 instance: MountedComponent
16 containerId: string
17 }> = []
18​
19 mount(target: HTMLElement, init: TanStackDevtoolsSvelteInit) {
20 const pluginsMap = this.getPluginsMap(init.plugins)
21​
22 this.devtools = new TanStackDevtoolsCore({
23 config: init.config,
24 eventBusConfig: init.eventBusConfig,
25 plugins: pluginsMap,
26 })
27​
28 this.devtools.mount(target)
29 }
30​
31 update(init: TanStackDevtoolsSvelteInit) {
32 if (this.devtools) {
33 // Tear down the previously mounted plugin components before re-applying
34 // config. The core re-invokes `render`/`name` for the new plugin set, so
35 // without this the old Svelte instances are orphaned and leak.
36 this.destroyAllComponents()
37 this.devtools.setConfig({
38 config: init.config,
39 eventBusConfig: init.eventBusConfig,
40 plugins: this.getPluginsMap(init.plugins),
41 })
42 }
43 }
44​
45 destroy() {
46 this.destroyAllComponents()
47 if (this.devtools) {
48 this.devtools.unmount()
49 this.devtools = null
50 }
51 }
52​
53 private getPluginsMap(
54 plugins?: Array<TanStackDevtoolsSveltePlugin>,
55 ): Array<TanStackDevtoolsPlugin> {
56 if (!plugins) return []
57 return plugins.map((plugin) => this.convertPlugin(plugin))
58 }
59​
60 private convertPlugin(
61 plugin: TanStackDevtoolsSveltePlugin,
62 ): TanStackDevtoolsPlugin {
63 return {
64 id: plugin.id,
65 defaultOpen: plugin.defaultOpen,
66 name:
67 typeof plugin.name === 'string'
68 ? plugin.name
69 : (el, theme) => {
70 this.renderComponent(plugin.name as Component<any>, el, {
71 theme,
72 ...(plugin.props ?? {}),
73 })
74 },
75 render: (el, theme) => {
76 this.renderComponent(plugin.component, el, {
77 theme,
78 ...(plugin.props ?? {}),
79 })
80 },
81 destroy: (pluginId) => {
82 this.destroyComponentsInContainer(`${PLUGIN_CONTAINER_ID}-${pluginId}`)
83 },
84 }
85 }
86​
87 private renderComponent(
88 component: Component<any>,
89 container: HTMLElement,
90 props: Record<string, unknown>,
91 ) {
92 const instance = mount(component, {
93 target: container,
94 props,
95 })
96​
97 const containerId = container.id
98 this.mountedComponents.push({ instance, containerId })
99 }
100​
101 private destroyComponentsInContainer(containerId: string) {
102 this.mountedComponents = this.mountedComponents.filter((entry) => {
103 if (entry.containerId === containerId) {
104 unmount(entry.instance)
105 return false
106 }
107 return true
108 })
109 }
110​
111 private destroyAllComponents() {
112 for (const entry of this.mountedComponents) {
113 unmount(entry.instance)
114 }
115 this.mountedComponents = []
116 }