Skip to content

Commit 36e7d41

Browse files
feat(apiWatch): introducing the depth option
1 parent 2c3524c commit 36e7d41

File tree

2 files changed

+19
-11
lines changed

2 files changed

+19
-11
lines changed

packages/runtime-core/__tests__/apiWatch.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1221,7 +1221,7 @@ describe('api: watch', () => {
12211221

12221222
const cb = vi.fn()
12231223

1224-
watch(state, cb, { deep: 2 })
1224+
watch(state, cb, { deep: true, depth: 2 })
12251225

12261226
state.a.b = { c: { d: { e: 2 } } }
12271227
await nextTick()

packages/runtime-core/src/apiWatch.ts

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ export interface WatchOptionsBase extends DebuggerOptions {
7474

7575
export interface WatchOptions<Immediate = boolean> extends WatchOptionsBase {
7676
immediate?: Immediate
77-
deep?: boolean | number
77+
deep?: boolean
78+
depth?: number
7879
}
7980

8081
export type WatchStopHandle = () => void
@@ -172,7 +173,14 @@ export function watch<T = any, Immediate extends Readonly<boolean> = false>(
172173
function doWatch(
173174
source: WatchSource | WatchSource[] | WatchEffect | object,
174175
cb: WatchCallback | null,
175-
{ immediate, deep, flush, onTrack, onTrigger }: WatchOptions = EMPTY_OBJ
176+
{
177+
immediate,
178+
deep,
179+
flush,
180+
depth,
181+
onTrack,
182+
onTrigger
183+
}: WatchOptions = EMPTY_OBJ
176184
): WatchStopHandle {
177185
if (__DEV__ && !cb) {
178186
if (immediate !== undefined) {
@@ -270,7 +278,7 @@ function doWatch(
270278

271279
if (cb && deep) {
272280
const baseGetter = getter
273-
getter = () => traverse(baseGetter(), deep)
281+
getter = () => traverse(baseGetter(), depth)
274282
}
275283

276284
let cleanup: () => void
@@ -439,16 +447,16 @@ export function createPathGetter(ctx: any, path: string) {
439447

440448
export function traverse(
441449
value: unknown,
442-
deep?: boolean | number,
450+
depth?: number,
443451
currentDepth = 0,
444452
seen?: Set<unknown>
445453
) {
446454
if (!isObject(value) || (value as any)[ReactiveFlags.SKIP]) {
447455
return value
448456
}
449457

450-
if (typeof deep === 'number') {
451-
if (currentDepth >= deep) {
458+
if (depth && depth > 0) {
459+
if (currentDepth >= depth) {
452460
return value
453461
}
454462
currentDepth++
@@ -460,18 +468,18 @@ export function traverse(
460468
}
461469
seen.add(value)
462470
if (isRef(value)) {
463-
traverse(value.value, deep, currentDepth, seen)
471+
traverse(value.value, depth, currentDepth, seen)
464472
} else if (isArray(value)) {
465473
for (let i = 0; i < value.length; i++) {
466-
traverse(value[i], deep, currentDepth, seen)
474+
traverse(value[i], depth, currentDepth, seen)
467475
}
468476
} else if (isSet(value) || isMap(value)) {
469477
value.forEach((v: any) => {
470-
traverse(v, deep, currentDepth, seen)
478+
traverse(v, depth, currentDepth, seen)
471479
})
472480
} else if (isPlainObject(value)) {
473481
for (const key in value) {
474-
traverse(value[key], deep, currentDepth, seen)
482+
traverse(value[key], depth, currentDepth, seen)
475483
}
476484
}
477485
return value

0 commit comments

Comments
 (0)