File size: 2,539 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
84
85
86
87
import type { Bridge } from '@vue-devtools/shared-utils'
import { BridgeEvents, getStorage, parse } from '@vue-devtools/shared-utils'
import { putError } from '@front/features/error'
import {
  addUpdateTrackingEvent,
  ensureComponentsMapData,
  getAppIdFromComponentId,
  isComponentOpen,
  lastSelectedComponentId,
  loadComponent,
  requestComponentTree,
  requestedComponentTree,
  rootInstances,
  selectedComponentData,
  selectedComponentId,
  selectedComponentPendingId,
  setComponentOpen,
} from './components'

export function setupComponentsBridgeEvents(bridge: Bridge) {
  selectedComponentPendingId.value = null

  bridge.on(BridgeEvents.TO_FRONT_COMPONENT_TREE, ({ instanceId, treeData, notFound }) => {
    requestedComponentTree.delete(instanceId)

    const isRoot = instanceId.endsWith('root')

    // Not supported
    if (!treeData) {
      if (isRoot && !notFound) {
        putError('Component tree not supported')
      }
      return
    }

    // Handle tree data
    const data = parse(treeData)
    if (isRoot) {
      rootInstances.value = data.map(i => ensureComponentsMapData(i))
    }
    else {
      for (const child of data) {
        ensureComponentsMapData(child)
      }
    }

    // Try to load selected component again
    if (isRoot && selectedComponentId.value && !selectedComponentData.value && !selectedComponentPendingId.value
      && getAppIdFromComponentId(selectedComponentId.value) === getAppIdFromComponentId(instanceId)) {
      loadComponent(selectedComponentId.value)
    }
  })

  bridge.on(BridgeEvents.TO_FRONT_COMPONENT_SELECTED_DATA, ({ instanceId, data, parentIds }) => {
    if (instanceId === selectedComponentId.value) {
      selectedComponentData.value = parse(data)
    }
    if (instanceId === selectedComponentPendingId.value) {
      selectedComponentPendingId.value = null
    }
    if (parentIds) {
      parentIds.reverse().forEach((id) => {
        // Ignore root
        if (id.endsWith('root')) {
          return
        }
        if (!isComponentOpen(id)) {
          setComponentOpen(id, true)
          requestComponentTree(id)
        }
      })
    }
  })

  bridge.on(BridgeEvents.TO_FRONT_COMPONENT_INSPECT_DOM, () => {
    chrome.devtools.inspectedWindow.eval('inspect(window.__VUE_DEVTOOLS_INSPECT_TARGET__)')
  })

  bridge.on(BridgeEvents.TO_FRONT_COMPONENT_UPDATED, ({ instanceId, time }) => {
    addUpdateTrackingEvent(instanceId, time)
  })

  // Persistance

  Object.assign(lastSelectedComponentId, getStorage('lastSelectedComponentId', {}))
}