Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(apiWatch): support passing the number type for deep to control the watch depth #9572

Merged
merged 25 commits into from
Aug 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
2c3524c
feat(apiWatch): control watch observation depth
Alfred-Skyblue Nov 8, 2023
36e7d41
feat(apiWatch): introducing the `depth` option
Alfred-Skyblue Nov 20, 2023
6e50040
chore: merge conflicts
Alfred-Skyblue Nov 20, 2023
1741978
test(apiWatch): add array test
Alfred-Skyblue Nov 21, 2023
a124d24
Merge branch 'minor' into feat/apiWatch/depth
Alfred-Skyblue Nov 30, 2023
6488588
Merge branch 'minor' into feat/apiWatch/depth
Alfred-Skyblue Dec 4, 2023
71d7575
Merge branch 'minor' into feat/apiWatch/depth
Alfred-Skyblue Dec 11, 2023
f49feac
Merge branch 'minor' into feat/apiWatch/depth
Alfred-Skyblue Dec 20, 2023
bf84dc6
fix(apiWatch): only monitor the first layer changes of shallowReactive
Alfred-Skyblue Dec 27, 2023
b43c8b4
Merge branch 'minor' into feat/apiWatch/depth
Alfred-Skyblue Dec 27, 2023
26a640c
[autofix.ci] apply automated fixes
autofix-ci[bot] Dec 27, 2023
a067f4f
fix(apiWatch): allows passing deep: false to listen to the first layer
Alfred-Skyblue Dec 27, 2023
6b97c56
test: add test case
Alfred-Skyblue Dec 28, 2023
d06f113
Merge branch 'minor' into feat/apiWatch/depth
Alfred-Skyblue Dec 28, 2023
16e0e7d
Merge branch 'minor' into feat/apiWatch/depth
Alfred-Skyblue Dec 30, 2023
b242550
feat(apiWatch): support deep passing number type watching depth
Alfred-Skyblue Dec 31, 2023
4f73fd0
Merge branch 'minor' into feat/apiWatch/depth
Alfred-Skyblue Jan 16, 2024
5d79da2
Merge branch 'minor' into feat/apiWatch/depth
Alfred-Skyblue Feb 27, 2024
ede33bc
Merge branch 'minor' into feat/apiWatch/depth
Alfred-Skyblue May 8, 2024
e745173
Merge branch 'minor' into feat/apiWatch/depth
Alfred-Skyblue Jun 13, 2024
fee8b75
Merge branch 'minor' into feat/apiWatch/depth
Alfred-Skyblue Jun 23, 2024
22b3c7b
Merge branch 'minor' into feat/apiWatch/depth
Alfred-Skyblue Jun 26, 2024
68b0e72
Merge branch 'minor' into feat/apiWatch/depth
Alfred-Skyblue Jul 31, 2024
6f1da6e
Merge branch 'minor' into feat/apiWatch/depth
Alfred-Skyblue Jul 31, 2024
4d1d15c
refactor: separate deep and depth logic
yyx990803 Aug 2, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
141 changes: 141 additions & 0 deletions packages/runtime-core/__tests__/apiWatch.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1532,6 +1532,147 @@ describe('api: watch', () => {
expect(spy2).toHaveBeenCalledTimes(1)
})

it('watching reactive depth', async () => {
const state = reactive({
a: {
b: {
c: {
d: {
e: 1,
},
},
},
},
})

const cb = vi.fn()

watch(state, cb, { deep: 2 })

state.a.b = { c: { d: { e: 2 } } }
await nextTick()
expect(cb).toHaveBeenCalledTimes(1)

state.a.b.c = { d: { e: 3 } }

await nextTick()
expect(cb).toHaveBeenCalledTimes(1)

state.a.b = { c: { d: { e: 4 } } }

await nextTick()
expect(cb).toHaveBeenCalledTimes(2)
})

it('watching ref depth', async () => {
const state = ref({
a: {
b: 2,
},
})

const cb = vi.fn()

watch(state, cb, { deep: 1 })

state.value.a.b = 3
await nextTick()
expect(cb).toHaveBeenCalledTimes(0)

state.value.a = { b: 3 }
await nextTick()
expect(cb).toHaveBeenCalledTimes(1)
})

it('watching array depth', async () => {
const arr = ref([
{
a: {
b: 2,
},
},
{
a: {
b: 3,
},
},
])
const cb = vi.fn()
watch(arr, cb, { deep: 2 })

arr.value[0].a.b = 3
await nextTick()
expect(cb).toHaveBeenCalledTimes(0)

arr.value[0].a = { b: 3 }
await nextTick()
expect(cb).toHaveBeenCalledTimes(1)

arr.value[1].a = { b: 4 }
await nextTick()
expect(cb).toHaveBeenCalledTimes(2)

arr.value.push({ a: { b: 5 } })
await nextTick()
expect(cb).toHaveBeenCalledTimes(3)

arr.value.pop()
await nextTick()
expect(cb).toHaveBeenCalledTimes(4)
})

it('shallowReactive', async () => {
const state = shallowReactive({
msg: ref('hello'),
foo: {
a: ref(1),
b: 2,
},
bar: 'bar',
})

const spy = vi.fn()

watch(state, spy)

state.msg.value = 'hi'
await nextTick()
expect(spy).not.toHaveBeenCalled()

state.bar = 'bar2'
await nextTick()
expect(spy).toHaveBeenCalledTimes(1)

state.foo.a.value++
state.foo.b++
await nextTick()
expect(spy).toHaveBeenCalledTimes(1)

state.bar = 'bar3'
await nextTick()
expect(spy).toHaveBeenCalledTimes(2)
})
it('watching reactive with deep: false', async () => {
const state = reactive({
foo: {
a: 2,
},
bar: 'bar',
})

const spy = vi.fn()

watch(state, spy, { deep: false })

state.foo.a++
await nextTick()
expect(spy).toHaveBeenCalledTimes(0)

state.bar = 'bar2'
await nextTick()
expect(spy).toHaveBeenCalledTimes(1)
})

test("effect should be removed from scope's effects after it is stopped", () => {
const num = ref(0)
let unwatch: () => void
Expand Down
27 changes: 12 additions & 15 deletions packages/runtime-core/src/apiWatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export interface WatchOptionsBase extends DebuggerOptions {

export interface WatchOptions<Immediate = boolean> extends WatchOptionsBase {
immediate?: Immediate
deep?: boolean
deep?: boolean | number
once?: boolean
}

Expand Down Expand Up @@ -189,14 +189,6 @@ function doWatch(
}
}

// TODO remove in 3.5
if (__DEV__ && deep !== void 0 && typeof deep === 'number') {
warn(
`watch() "deep" option with number value will be used as watch depth in future versions. ` +
`Please use a boolean instead to avoid potential breakage.`,
)
}

if (__DEV__ && !cb) {
if (immediate !== undefined) {
warn(
Expand Down Expand Up @@ -228,11 +220,15 @@ function doWatch(
}

const instance = currentInstance
const reactiveGetter = (source: object) =>
deep === true
? source // traverse will happen in wrapped getter below
: // for deep: false, only traverse root-level properties
traverse(source, deep === false ? 1 : undefined)
const reactiveGetter = (source: object) => {
// traverse will happen in wrapped getter below
if (deep) return source
// for `deep: false | 0` or shallow reactive, only traverse root-level properties
if (isShallow(source) || deep === false || deep === 0)
return traverse(source, 1)
// for `deep: undefined` on a reactive object, deeply traverse all properties
return traverse(source)
}

let getter: () => any
let forceTrigger = false
Expand Down Expand Up @@ -300,7 +296,8 @@ function doWatch(

if (cb && deep) {
const baseGetter = getter
getter = () => traverse(baseGetter())
const depth = deep === true ? Infinity : deep
getter = () => traverse(baseGetter(), depth)
}

let cleanup: (() => void) | undefined
Expand Down