-
Notifications
You must be signed in to change notification settings - Fork 205
Expand file tree
/
Copy pathiframe-client.ts
More file actions
62 lines (50 loc) · 1.42 KB
/
iframe-client.ts
File metadata and controls
62 lines (50 loc) · 1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import type { Ref } from 'vue'
import type { NuxtDevtoolsIframeClient } from '../types'
import { shallowRef, triggerRef } from 'vue'
let clientRef: Ref<NuxtDevtoolsIframeClient | undefined> | undefined
const hasSetup = false
const fns = [] as ((client: NuxtDevtoolsIframeClient) => void)[]
export function onDevtoolsClientConnected(fn: (client: NuxtDevtoolsIframeClient) => void) {
fns.push(fn)
if (hasSetup)
return
if (typeof window === 'undefined')
return
// eslint-disable-next-line ts/ban-ts-comment
// @ts-ignore injection
if (window.__NUXT_DEVTOOLS__) {
// eslint-disable-next-line ts/ban-ts-comment
// @ts-ignore injection
fns.forEach(fn => fn(window.__NUXT_DEVTOOLS__))
}
Object.defineProperty(window, '__NUXT_DEVTOOLS__', {
set(value) {
if (value)
fns.forEach(fn => fn(value))
},
get() {
return clientRef!.value
},
configurable: true,
})
return () => {
fns.splice(fns.indexOf(fn), 1)
}
}
export function useDevtoolsClient() {
if (!clientRef) {
clientRef = shallowRef<NuxtDevtoolsIframeClient | undefined>()
onDevtoolsClientConnected(setup)
}
function onUpdateReactivity() {
if (clientRef) {
triggerRef(clientRef)
}
}
function setup(client: NuxtDevtoolsIframeClient) {
clientRef!.value = client
if (client.host)
client.host.hooks.hook('host:update:reactivity', onUpdateReactivity)
}
return clientRef
}