Skip to content

Commit 56c87ec

Browse files
committed
test: baseWatch with onEffectCleanup
1 parent 7c5f05a commit 56c87ec

File tree

2 files changed

+110
-1
lines changed

2 files changed

+110
-1
lines changed
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
import {
2+
type Scheduler,
3+
type SchedulerJob,
4+
baseWatch,
5+
onEffectCleanup,
6+
} from '../src/baseWatch'
7+
import { EffectScope } from '../src/effectScope'
8+
import { type Ref, ref } from '../src/ref'
9+
10+
const queue: SchedulerJob[] = []
11+
12+
let isFlushPending = false
13+
const resolvedPromise = /*#__PURE__*/ Promise.resolve() as Promise<any>
14+
const nextTick = (fn?: () => any) =>
15+
fn ? resolvedPromise.then(fn) : resolvedPromise
16+
const scheduler: Scheduler = ({ job }) => {
17+
queue.push(job)
18+
flushJobs()
19+
}
20+
const flushJobs = () => {
21+
if (isFlushPending) return
22+
isFlushPending = true
23+
resolvedPromise.then(() => {
24+
queue.forEach(job => job())
25+
queue.length = 0
26+
isFlushPending = false
27+
})
28+
}
29+
30+
describe('baseWatch with onEffectCleanup', () => {
31+
test('basic', async () => {
32+
let dummy = 0
33+
let source: Ref<number>
34+
const scope = new EffectScope()
35+
36+
scope.run(() => {
37+
source = ref(0)
38+
baseWatch(onCleanup => {
39+
source.value
40+
41+
onCleanup(() => (dummy += 2))
42+
onEffectCleanup(() => (dummy += 3))
43+
onEffectCleanup(() => (dummy += 5))
44+
})
45+
})
46+
expect(dummy).toBe(0)
47+
48+
scope.run(() => {
49+
source.value++
50+
})
51+
expect(dummy).toBe(10)
52+
53+
scope.run(() => {
54+
source.value++
55+
})
56+
expect(dummy).toBe(20)
57+
58+
scope.stop()
59+
expect(dummy).toBe(30)
60+
})
61+
62+
test('nested call to baseWatch', async () => {
63+
let calls: string[] = []
64+
let source: Ref<number>
65+
let copyist: Ref<number>
66+
const scope = new EffectScope()
67+
68+
scope.run(() => {
69+
source = ref(0)
70+
copyist = ref(0)
71+
// sync by default
72+
baseWatch(
73+
() => {
74+
const current = (copyist.value = source.value)
75+
onEffectCleanup(() => calls.push(`sync ${current}`))
76+
},
77+
null,
78+
{},
79+
)
80+
// with scheduler
81+
baseWatch(
82+
() => {
83+
const current = copyist.value
84+
onEffectCleanup(() => calls.push(`post ${current}`))
85+
},
86+
null,
87+
{ scheduler },
88+
)
89+
})
90+
91+
await nextTick()
92+
expect(calls).toEqual([])
93+
94+
scope.run(() => source.value++)
95+
expect(calls).toEqual(['sync 0'])
96+
await nextTick()
97+
expect(calls).toEqual(['sync 0', 'post 0'])
98+
calls.length = 0
99+
100+
scope.run(() => source.value++)
101+
expect(calls).toEqual(['sync 1'])
102+
await nextTick()
103+
expect(calls).toEqual(['sync 1', 'post 1'])
104+
calls.length = 0
105+
106+
scope.stop()
107+
expect(calls).toEqual(['sync 2', 'post 2'])
108+
})
109+
})

packages/reactivity/src/baseWatch.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ export function onEffectCleanup(cleanupFn: () => void) {
116116

117117
export function baseWatch(
118118
source: WatchSource | WatchSource[] | WatchEffect | object,
119-
cb: WatchCallback | null,
119+
cb?: WatchCallback | null,
120120
{
121121
immediate,
122122
deep,

0 commit comments

Comments
 (0)