Skip to content

Commit aac74c0

Browse files
authored
fix: add null checks for appRecord and appRecord.instanceMap, fix #1892 (#2122)
1 parent 6ccd508 commit aac74c0

File tree

4 files changed

+38
-21
lines changed

4 files changed

+38
-21
lines changed

packages/app-backend-core/src/index.ts

+34-21
Original file line numberDiff line numberDiff line change
@@ -174,12 +174,14 @@ async function connect () {
174174
if (component.__VUE_DEVTOOLS_UID__ == null) {
175175
component.__VUE_DEVTOOLS_UID__ = id
176176
}
177-
if (!appRecord.instanceMap.has(id)) {
178-
appRecord.instanceMap.set(id, component)
177+
if (appRecord?.instanceMap) {
178+
if (!appRecord.instanceMap.has(id)) {
179+
appRecord.instanceMap.set(id, component)
180+
}
179181
}
180182
}
181183

182-
if (parentUid != null) {
184+
if (parentUid != null && appRecord?.instanceMap) {
183185
const parentInstances = await appRecord.backend.api.walkComponentParents(component)
184186
if (parentInstances.length) {
185187
// Check two parents level to update `hasChildren
@@ -222,14 +224,18 @@ async function connect () {
222224
try {
223225
if (!app || (typeof uid !== 'number' && !uid) || !component) return
224226
const appRecord = await getAppRecord(app, ctx)
225-
if (parentUid != null) {
227+
if (parentUid != null && appRecord) {
226228
const parentInstances = await appRecord.backend.api.walkComponentParents(component)
227229
if (parentInstances.length) {
228230
const parentId = await getComponentId(app, parentUid, parentInstances[0], ctx)
229231
if (isSubscribed(BridgeSubscriptions.COMPONENT_TREE, sub => sub.payload.instanceId === parentId)) {
230232
raf(async () => {
231233
try {
232-
sendComponentTreeData(await getAppRecord(app, ctx), parentId, appRecord.componentFilter, null, false, ctx)
234+
const appRecord = await getAppRecord(app, ctx)
235+
236+
if (appRecord) {
237+
sendComponentTreeData(appRecord, parentId, appRecord.componentFilter, null, false, ctx)
238+
}
233239
} catch (e) {
234240
if (SharedData.debugInfo) {
235241
console.error(e)
@@ -244,7 +250,10 @@ async function connect () {
244250
if (isSubscribed(BridgeSubscriptions.SELECTED_COMPONENT_DATA, sub => sub.payload.instanceId === id)) {
245251
await sendEmptyComponentData(id, ctx)
246252
}
247-
appRecord.instanceMap.delete(id)
253+
254+
if (appRecord) {
255+
appRecord.instanceMap.delete(id)
256+
}
248257

249258
await refreshComponentTreeSearch(ctx)
250259
} catch (e) {
@@ -288,13 +297,15 @@ async function connect () {
288297

289298
hook.on(HookEvents.TIMELINE_LAYER_ADDED, async (options: TimelineLayerOptions, plugin: Plugin) => {
290299
const appRecord = await getAppRecord(plugin.descriptor.app, ctx)
291-
ctx.timelineLayers.push({
292-
...options,
293-
appRecord,
294-
plugin,
295-
events: [],
296-
})
297-
ctx.bridge.send(BridgeEvents.TO_FRONT_TIMELINE_LAYER_ADD, {})
300+
if (appRecord) {
301+
ctx.timelineLayers.push({
302+
...options,
303+
appRecord,
304+
plugin,
305+
events: [],
306+
})
307+
ctx.bridge.send(BridgeEvents.TO_FRONT_TIMELINE_LAYER_ADD, {})
308+
}
298309
})
299310

300311
hook.on(HookEvents.TIMELINE_EVENT_ADDED, async (options: TimelineEventOptions, plugin: Plugin) => {
@@ -305,14 +316,16 @@ async function connect () {
305316

306317
hook.on(HookEvents.CUSTOM_INSPECTOR_ADD, async (options: CustomInspectorOptions, plugin: Plugin) => {
307318
const appRecord = await getAppRecord(plugin.descriptor.app, ctx)
308-
ctx.customInspectors.push({
309-
...options,
310-
appRecord,
311-
plugin,
312-
treeFilter: '',
313-
selectedNodeId: null,
314-
})
315-
ctx.bridge.send(BridgeEvents.TO_FRONT_CUSTOM_INSPECTOR_ADD, {})
319+
if (appRecord) {
320+
ctx.customInspectors.push({
321+
...options,
322+
appRecord,
323+
plugin,
324+
treeFilter: '',
325+
selectedNodeId: null,
326+
})
327+
ctx.bridge.send(BridgeEvents.TO_FRONT_CUSTOM_INSPECTOR_ADD, {})
328+
}
316329
})
317330

318331
hook.on(HookEvents.CUSTOM_INSPECTOR_SEND_TREE, async (inspectorId: string, plugin: Plugin) => {

packages/app-backend-core/src/perf.ts

+2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export async function performanceMarkStart (
1717
try {
1818
if (!SharedData.performanceMonitoringEnabled) return
1919
const appRecord = await getAppRecord(app, ctx)
20+
if (!appRecord) return
2021
const componentName = await appRecord.backend.api.getComponentName(instance)
2122
const groupId = ctx.perfUniqueGroupId++
2223
const groupKey = `${uid}-${type}`
@@ -80,6 +81,7 @@ export async function performanceMarkEnd (
8081
try {
8182
if (!SharedData.performanceMonitoringEnabled) return
8283
const appRecord = await getAppRecord(app, ctx)
84+
if (!appRecord) return
8385
const componentName = await appRecord.backend.api.getComponentName(instance)
8486
const groupKey = `${uid}-${type}`
8587
const groupInfo = appRecord.perfGroupIds.get(groupKey)

packages/app-backend-core/src/plugin.ts

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export async function addPlugin (pluginQueueItem: PluginQueueItem, ctx: BackendC
1414
ctx.currentPlugin = plugin
1515
try {
1616
const appRecord = await getAppRecord(plugin.descriptor.app, ctx)
17+
if (!appRecord) return
1718
const api = new DevtoolsPluginApiInstance(plugin, appRecord, ctx)
1819
if (pluginQueueItem.proxy) {
1920
await pluginQueueItem.proxy.setRealTarget(api)

packages/app-backend-core/src/timeline.ts

+1
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ function setupBuiltinLayers (ctx: BackendContext) {
7373
if (!SharedData.componentEventsEnabled) return
7474

7575
const appRecord = await getAppRecord(app, ctx)
76+
if (!appRecord) return
7677
const componentId = `${appRecord.id}:${instance.uid}`
7778
const componentDisplay = (await appRecord.backend.api.getComponentName(instance)) || '<i>Unknown Component</i>'
7879

0 commit comments

Comments
 (0)