Skip to content

Commit

Permalink
refactor: simplify traverse() for deep watchers (#10795)
Browse files Browse the repository at this point in the history
  • Loading branch information
skirtles-code authored Apr 29, 2024
1 parent b01be66 commit 2d56816
Showing 1 changed file with 7 additions and 14 deletions.
21 changes: 7 additions & 14 deletions packages/runtime-core/src/apiWatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -466,39 +466,32 @@ export function createPathGetter(ctx: any, path: string) {

export function traverse(
value: unknown,
depth?: number,
currentDepth = 0,
depth = Infinity,
seen?: Set<unknown>,
) {
if (!isObject(value) || (value as any)[ReactiveFlags.SKIP]) {
if (depth <= 0 || !isObject(value) || (value as any)[ReactiveFlags.SKIP]) {
return value
}

if (depth && depth > 0) {
if (currentDepth >= depth) {
return value
}
currentDepth++
}

seen = seen || new Set()
if (seen.has(value)) {
return value
}
seen.add(value)
depth--
if (isRef(value)) {
traverse(value.value, depth, currentDepth, seen)
traverse(value.value, depth, seen)
} else if (isArray(value)) {
for (let i = 0; i < value.length; i++) {
traverse(value[i], depth, currentDepth, seen)
traverse(value[i], depth, seen)
}
} else if (isSet(value) || isMap(value)) {
value.forEach((v: any) => {
traverse(v, depth, currentDepth, seen)
traverse(v, depth, seen)
})
} else if (isPlainObject(value)) {
for (const key in value) {
traverse(value[key], depth, currentDepth, seen)
traverse(value[key], depth, seen)
}
}
return value
Expand Down

0 comments on commit 2d56816

Please sign in to comment.