mirror of
https://github.com/element-plus/element-plus.git
synced 2026-03-13 07:51:17 +08:00
perf(components): [tree] resolve memory leak occurring after data update (#23055)
This commit is contained in:
@@ -1,11 +1,11 @@
|
||||
import { inject, provide } from 'vue'
|
||||
import { inject, onBeforeUnmount, provide } from 'vue'
|
||||
import { TREE_NODE_MAP_INJECTION_KEY } from '../tokens'
|
||||
|
||||
import type Node from '../model/node'
|
||||
|
||||
interface NodeMap {
|
||||
treeNodeExpand(node?: Node): void
|
||||
children: NodeMap[]
|
||||
children: Set<NodeMap>
|
||||
}
|
||||
|
||||
interface Props {
|
||||
@@ -18,25 +18,32 @@ export function useNodeExpandEventBroadcast(props: Props) {
|
||||
TREE_NODE_MAP_INJECTION_KEY,
|
||||
null
|
||||
) as NodeMap | null
|
||||
const currentNodeMap: NodeMap = {
|
||||
let currentNodeMap: NodeMap | null = {
|
||||
treeNodeExpand: (node) => {
|
||||
if (props.node !== node) {
|
||||
props.node?.collapse()
|
||||
}
|
||||
},
|
||||
children: [],
|
||||
children: new Set(),
|
||||
}
|
||||
|
||||
if (parentNodeMap) {
|
||||
parentNodeMap.children.push(currentNodeMap)
|
||||
parentNodeMap.children.add(currentNodeMap)
|
||||
}
|
||||
|
||||
onBeforeUnmount(() => {
|
||||
if (parentNodeMap) {
|
||||
parentNodeMap.children.delete(currentNodeMap!)
|
||||
}
|
||||
currentNodeMap = null
|
||||
})
|
||||
|
||||
provide(TREE_NODE_MAP_INJECTION_KEY, currentNodeMap)
|
||||
|
||||
return {
|
||||
broadcastExpanded: (node?: Node): void => {
|
||||
if (!props.accordion) return
|
||||
for (const childNode of currentNodeMap.children) {
|
||||
for (const childNode of currentNodeMap!.children) {
|
||||
childNode.treeNodeExpand(node)
|
||||
}
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user