forked from vuejs/devtools-v6
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
55 lines (48 loc) · 2.01 KB
/
index.ts
File metadata and controls
55 lines (48 loc) · 2.01 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
import { getTarget, getDevtoolsGlobalHook, isProxyAvailable } from './env.js'
import { HOOK_SETUP } from './const.js'
import type { DevtoolsPluginApi } from './api/index.js'
import { ApiProxy } from './proxy.js'
import type { PluginDescriptor, ExtractSettingsTypes, PluginSettingsItem } from './plugin.js'
export * from './api/index.js'
export * from './plugin.js'
export * from './time.js'
export { PluginQueueItem } from './env.js'
// https://github.com/microsoft/TypeScript/issues/30680#issuecomment-752725353
type Cast<A, B> = A extends B ? A : B
type Narrowable =
| string
| number
| bigint
| boolean
type Narrow<A> = Cast<A,
| []
| (A extends Narrowable ? A : never)
| ({ [K in keyof A]: Narrow<A[K]> })
>
// Prevent properties not in PluginDescriptor
// We need this because of the `extends` in the generic TDescriptor
type Exact<C, T> = {
[K in keyof C]: K extends keyof T ? T[K] : never
}
export type SetupFunction<TSettings = any> = (api: DevtoolsPluginApi<TSettings>) => void
export function setupDevtoolsPlugin<
TDescriptor extends Exact<TDescriptor, PluginDescriptor>,
TSettings = ExtractSettingsTypes<TDescriptor extends { settings : infer S } ? S extends Record<string, PluginSettingsItem> ? S : Record<string, PluginSettingsItem> : Record<string, PluginSettingsItem>>,
> (pluginDescriptor: Narrow<TDescriptor>, setupFn: SetupFunction<TSettings>) {
const descriptor = pluginDescriptor as unknown as PluginDescriptor
const target = getTarget()
const hook = getDevtoolsGlobalHook()
const enableProxy = isProxyAvailable && descriptor.enableEarlyProxy
if (hook && (target.__VUE_DEVTOOLS_PLUGIN_API_AVAILABLE__ || !enableProxy)) {
hook.emit(HOOK_SETUP, pluginDescriptor, setupFn)
} else {
const proxy = enableProxy ? new ApiProxy(descriptor, hook) : null
const list = target.__VUE_DEVTOOLS_PLUGINS__ = target.__VUE_DEVTOOLS_PLUGINS__ || []
list.push({
pluginDescriptor: descriptor,
setupFn,
proxy,
})
if (proxy) setupFn(proxy.proxiedTarget as DevtoolsPluginApi<TSettings>)
}
}