Skip to content

Commit 2213634

Browse files
committed
refactor: simplify unwatch implementation
1 parent f44ef0b commit 2213634

File tree

4 files changed

+16
-26
lines changed

4 files changed

+16
-26
lines changed

packages/reactivity/__tests__/baseWatch.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ describe('baseWatch', () => {
6464
)
6565

6666
const source = ref(0)
67-
const stop = baseWatch(
67+
const effect = baseWatch(
6868
source,
6969
() => {
7070
onEffectCleanup(() => {
@@ -88,7 +88,7 @@ describe('baseWatch', () => {
8888
BaseWatchErrorCodes.WATCH_CALLBACK,
8989
])
9090

91-
stop()
91+
effect!.stop()
9292
source.value++
9393
expect(onError.mock.calls.length).toBe(3)
9494
expect(onError.mock.calls[2]).toMatchObject([

packages/reactivity/src/baseWatch.ts

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,6 @@ export interface BaseWatchOptions<Immediate = boolean> extends DebuggerOptions {
7777
onWarn?: HandleWarn
7878
}
7979

80-
type WatchStopHandle = () => void
81-
82-
export interface WatchInstance extends WatchStopHandle {
83-
effect?: ReactiveEffect
84-
}
85-
8680
// initial value for watchers to trigger on undefined initial values
8781
const INITIAL_WATCHER_VALUE = {}
8882

@@ -146,7 +140,7 @@ export function baseWatch(
146140
onTrack,
147141
onTrigger,
148142
}: BaseWatchOptions = EMPTY_OBJ,
149-
): WatchInstance {
143+
): ReactiveEffect | undefined {
150144
const warnInvalidSource = (s: unknown) => {
151145
onWarn(
152146
`Invalid watch source: `,
@@ -232,7 +226,7 @@ export function baseWatch(
232226
// onEffectCleanup need use effect as a key
233227
getCurrentScope()?.effects.push((effect = {} as any))
234228
getter()
235-
return NOOP
229+
return
236230
}
237231
if (immediate) {
238232
// onEffectCleanup need use effect as a key
@@ -243,12 +237,12 @@ export function baseWatch(
243237
BaseWatchErrorCodes.WATCH_CALLBACK,
244238
[getter(), isMultiSource ? [] : undefined, onEffectCleanup],
245239
)
246-
return NOOP
240+
return
247241
}
248242
const _cb = cb
249243
cb = (...args) => {
250244
_cb(...args)
251-
unwatch()
245+
effect?.stop()
252246
}
253247
}
254248

@@ -329,11 +323,6 @@ export function baseWatch(
329323
}
330324
}
331325

332-
const unwatch: WatchInstance = () => {
333-
effect.stop()
334-
}
335-
unwatch.effect = effect
336-
337326
if (__DEV__) {
338327
effect.onTrack = onTrack
339328
effect.onTrigger = onTrigger
@@ -354,7 +343,7 @@ export function baseWatch(
354343
})
355344
}
356345

357-
return unwatch
346+
return effect
358347
}
359348

360349
export function traverse(value: unknown, seen?: Set<unknown>) {

packages/reactivity/src/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,5 +76,4 @@ export {
7676
traverse,
7777
type BaseWatchOptions,
7878
type Scheduler,
79-
type WatchInstance,
8079
} from './baseWatch'

packages/runtime-core/src/apiWatch.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -223,14 +223,16 @@ function doWatch(
223223
const scheduler = getSchedulerByFlushMode(flush)({ instance })
224224
extendOptions.scheduler = scheduler
225225

226-
let baseUnwatch = baseWatch(source, cb, extend({}, options, extendOptions))
226+
let effect = baseWatch(source, cb, extend({}, options, extendOptions))
227227

228-
const unwatch = () => {
229-
baseUnwatch()
230-
if (instance && instance.scope) {
231-
remove(instance.scope.effects!, baseUnwatch.effect)
232-
}
233-
}
228+
const unwatch = !effect
229+
? NOOP
230+
: () => {
231+
effect!.stop()
232+
if (instance && instance.scope) {
233+
remove(instance.scope.effects!, effect)
234+
}
235+
}
234236

235237
if (__SSR__ && ssrCleanup) ssrCleanup.push(unwatch)
236238
return unwatch

0 commit comments

Comments
 (0)