File size: 2,628 Bytes
4d70170
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import type { PluginQueueItem } from '@vue/devtools-api'
import type { BackendContext, Plugin } from '@vue-devtools/app-backend-api'
import { DevtoolsPluginApiInstance } from '@vue-devtools/app-backend-api'
import { BridgeEvents, SharedData, target } from '@vue-devtools/shared-utils'
import { getAppRecord, getAppRecordId } from './app'

export async function addPlugin(pluginQueueItem: PluginQueueItem, ctx: BackendContext) {
  const { pluginDescriptor, setupFn } = pluginQueueItem

  const plugin: Plugin = {
    descriptor: pluginDescriptor,
    setupFn,
    error: null,
  }
  ctx.currentPlugin = plugin
  try {
    const appRecord = await getAppRecord(plugin.descriptor.app, ctx)
    if (!appRecord) {
      return
    }
    const api = new DevtoolsPluginApiInstance(plugin, appRecord, ctx)
    if (pluginQueueItem.proxy) {
      await pluginQueueItem.proxy.setRealTarget(api)
    }
    else {
      setupFn(api)
    }
  }
  catch (e) {
    plugin.error = e
    if (SharedData.debugInfo) {
      console.error(e)
    }
  }
  ctx.currentPlugin = null
  ctx.plugins.push(plugin)
  ctx.bridge.send(BridgeEvents.TO_FRONT_DEVTOOLS_PLUGIN_ADD, {
    plugin: await serializePlugin(plugin),
  })

  const targetList = target.__VUE_DEVTOOLS_REGISTERED_PLUGINS__ = target.__VUE_DEVTOOLS_REGISTERED_PLUGINS__ || []
  targetList.push({
    pluginDescriptor,
    setupFn,
  })
}

export async function addQueuedPlugins(ctx: BackendContext) {
  if (target.__VUE_DEVTOOLS_PLUGINS__ && Array.isArray(target.__VUE_DEVTOOLS_PLUGINS__)) {
    for (const queueItem of target.__VUE_DEVTOOLS_PLUGINS__) {
      await addPlugin(queueItem, ctx)
    }
    target.__VUE_DEVTOOLS_PLUGINS__ = null
  }
}

export async function addPreviouslyRegisteredPlugins(ctx: BackendContext) {
  if (target.__VUE_DEVTOOLS_REGISTERED_PLUGINS__ && Array.isArray(target.__VUE_DEVTOOLS_REGISTERED_PLUGINS__)) {
    for (const queueItem of target.__VUE_DEVTOOLS_REGISTERED_PLUGINS__) {
      await addPlugin(queueItem, ctx)
    }
  }
}

export async function sendPluginList(ctx: BackendContext) {
  ctx.bridge.send(BridgeEvents.TO_FRONT_DEVTOOLS_PLUGIN_LIST, {
    plugins: await Promise.all(ctx.plugins.map(p => serializePlugin(p))),
  })
}

export async function serializePlugin(plugin: Plugin) {
  return {
    id: plugin.descriptor.id,
    label: plugin.descriptor.label,
    appId: getAppRecordId(plugin.descriptor.app),
    packageName: plugin.descriptor.packageName,
    homepage: plugin.descriptor.homepage,
    logo: plugin.descriptor.logo,
    componentStateTypes: plugin.descriptor.componentStateTypes,
    settingsSchema: plugin.descriptor.settings,
  }
}