Skip to content

Commit ce30047

Browse files
authored
feat: optimize suspense tag (#1595)
1 parent d375c50 commit ce30047

File tree

1 file changed

+21
-10
lines changed
  • packages/app-backend-vue3/src/components

1 file changed

+21
-10
lines changed

packages/app-backend-vue3/src/components/tree.ts

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -78,18 +78,19 @@ export class ComponentWalker {
7878
/**
7979
* Get children from a component instance.
8080
*/
81-
private getInternalInstanceChildren (subTree) {
81+
private getInternalInstanceChildren (subTree, suspense = null) {
8282
const list = []
8383
if (subTree.component) {
84-
list.push(subTree.component)
84+
!suspense ? list.push(subTree.component) : list.push({ ...subTree.component, suspense })
8585
} else if (subTree.suspense) {
86-
list.push(...this.getInternalInstanceChildren(subTree.suspense.activeBranch))
86+
const suspenseKey = !subTree.suspense.isInFallback ? 'default' : 'fallback'
87+
list.push(...this.getInternalInstanceChildren(subTree.suspense.activeBranch, { ...subTree.suspense, suspenseKey }))
8788
} else if (Array.isArray(subTree.children)) {
8889
subTree.children.forEach(childSubTree => {
8990
if (childSubTree.component) {
90-
list.push(childSubTree.component)
91+
!suspense ? list.push(childSubTree.component) : list.push({ ...childSubTree.component, suspense })
9192
} else {
92-
list.push(...this.getInternalInstanceChildren(childSubTree))
93+
list.push(...this.getInternalInstanceChildren(childSubTree, suspense))
9394
}
9495
})
9596
}
@@ -159,9 +160,10 @@ export class ComponentWalker {
159160
// keep-alive
160161
if (instance.type.__isKeepAlive && instance.__v_cache) {
161162
const cachedComponents = Array.from(instance.__v_cache.values()).map((vnode: any) => vnode.component).filter(Boolean)
162-
for (const child of cachedComponents) {
163-
if (!children.includes(child)) {
164-
const node = await this.capture({ ...child, isDeactivated: true }, null, depth + 1)
163+
const childrenIds = children.map(child => child.__VUE_DEVTOOLS_UID__)
164+
for (const cachedChild of cachedComponents) {
165+
if (!childrenIds.includes(cachedChild.__VUE_DEVTOOLS_UID__)) {
166+
const node = await this.capture({ ...cachedChild, isDeactivated: true }, null, depth + 1)
165167
if (node) {
166168
treeNode.children.push(node)
167169
}
@@ -193,6 +195,15 @@ export class ComponentWalker {
193195
textColor: 0xffffff,
194196
tooltip: 'Suspense',
195197
})
198+
if (instance.suspense.suspenseKey) {
199+
treeNode.tags.push({
200+
label: instance.suspense.suspenseKey,
201+
backgroundColor: 0xe492e4,
202+
textColor: 0xffffff,
203+
})
204+
// update instanceMap
205+
this.mark(instance, true)
206+
}
196207
}
197208

198209
return this.api.visitComponentTree(instance, treeNode, this.componentFilter.filter, this.ctx.currentAppRecord.options.app)
@@ -203,9 +214,9 @@ export class ComponentWalker {
203214
*
204215
* @param {Vue} instance
205216
*/
206-
private mark (instance) {
217+
private mark (instance, force = false) {
207218
const instanceMap = this.ctx.currentAppRecord.instanceMap
208-
if (!instanceMap.has(instance.__VUE_DEVTOOLS_UID__)) {
219+
if (force || !instanceMap.has(instance.__VUE_DEVTOOLS_UID__)) {
209220
instanceMap.set(instance.__VUE_DEVTOOLS_UID__, instance)
210221
}
211222
}

0 commit comments

Comments
 (0)