File size: 4,280 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import type { Bridge } from '@vue-devtools/shared-utils'
import { BridgeEvents, parse } from '@vue-devtools/shared-utils'
import { getApps } from '@front/features/apps'
import type {
  MarkerFromBackend,
  TimelineEvent,
  TimelineMarker,
} from './store'
import {
  inspectedEvent,
  inspectedEventData,
  inspectedEventPendingId,
  markersAllApps,
  markersPerApp,
} from './store'
import { fetchLayers, getLayers, layerFactory } from './layers'
import { addEvent } from './events'
import { resetTimeline } from './reset'

const pendingEvents: Record<string, TimelineEvent[]> = {}

export function setupTimelineBridgeEvents(bridge: Bridge) {
  resetTimeline(false)

  bridge.on(BridgeEvents.TO_FRONT_TIMELINE_EVENT, ({ appId, layerId, event }) => {
    const appIds = appId === 'all' ? getApps().map(app => app.id) : [appId]
    for (const appId of appIds) {
      const layer = getLayers(appId).find(l => l.id === layerId)
      if (!layer) {
        // Layer not found
        const pendingKey = `${appId}:${layerId}`
        pendingEvents[pendingKey] = pendingEvents[pendingKey] ?? []
        pendingEvents[pendingKey].push(event)
        return
      }

      addEvent(appId, event, layer)
    }
  })

  bridge.on(BridgeEvents.TO_FRONT_TIMELINE_LAYER_ADD, () => {
    fetchLayers()
  })

  bridge.on(BridgeEvents.TO_FRONT_TIMELINE_LAYER_LIST, ({ layers }) => {
    for (const layer of layers) {
      const appIds = layer.appId != null ? [layer.appId] : getApps().map(app => app.id)
      for (const appId of appIds) {
        const existingLayers = getLayers(appId)
        if (!existingLayers.some(l => l.id === layer.id)) {
          existingLayers.push(layerFactory(layer))

          // Add pending events
          const pendingKey = `${appId}:${layer.id}`
          if (pendingEvents[pendingKey] && pendingEvents[pendingKey].length) {
            for (const event of pendingEvents[pendingKey]) {
              addEvent(appId, event, getLayers(appId).find(l => l.id === layer.id))
            }
            pendingEvents[pendingKey] = []
          }

          // Load existing events that may not have been catched
          bridge.send(BridgeEvents.TO_BACK_TIMELINE_LAYER_LOAD_EVENTS, { layerId: layer.id, appId })
        }
      }
    }
  })

  bridge.on(BridgeEvents.TO_FRONT_TIMELINE_EVENT_DATA, ({ eventId, data }) => {
    if (inspectedEvent.value) {
      if (inspectedEvent.value.id === eventId) {
        inspectedEventData.value = parse(data)

        if (data === null) {
          inspectedEvent.value = null
        }
      }
      if (eventId === inspectedEventPendingId.value) {
        inspectedEventPendingId.value = null
      }
    }
  })

  bridge.on(BridgeEvents.TO_FRONT_TIMELINE_LAYER_LOAD_EVENTS, ({ appId, layerId, events }) => {
    const layer = getLayers(appId).find(l => l.id === layerId)
    if (!layer) {
      // Layer not found
      const pendingKey = `${appId}:${layerId}`
      pendingEvents[pendingKey] = pendingEvents[pendingKey] ?? []
      pendingEvents[pendingKey].push(...events)
      return
    }

    for (const event of events) {
      addEvent(appId, event, layer)
    }
  })

  bridge.on(BridgeEvents.TO_FRONT_TIMELINE_LOAD_MARKERS, ({ markers, appId }: { markers: MarkerFromBackend[], appId: string }) => {
    const allList: TimelineMarker[] = []
    const appList: TimelineMarker[] = []

    for (const marker of markers) {
      const result = {
        ...marker,
        x: 0,
      }
      if (marker.all) {
        allList.push(result)
      }
      else {
        appList.push(result)
      }
    }

    markersAllApps.value = allList
    markersPerApp.value[appId] = appList
  })

  bridge.on(BridgeEvents.TO_FRONT_TIMELINE_MARKER, ({ marker, appId }: { marker: MarkerFromBackend, appId: string }) => {
    let targetList: TimelineMarker[]
    if (marker.all) {
      targetList = markersAllApps.value
    }
    else {
      if (!markersPerApp.value[appId]) {
        markersPerApp.value[appId] = []
      }
      targetList = markersPerApp.value[appId]
    }

    const result = {
      ...marker,
      x: 0,
    }

    const index = targetList.findIndex(m => m.id === marker.id)
    if (index !== -1) {
      targetList.splice(index, 1, result)
    }
    else {
      targetList.push(result)
    }
  })
}