Skip to content

Commit

Permalink
refactor: simplify unwatch implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
LittleSound committed Dec 31, 2023
1 parent f44ef0b commit 2213634
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 26 deletions.
4 changes: 2 additions & 2 deletions packages/reactivity/__tests__/baseWatch.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ describe('baseWatch', () => {
)

const source = ref(0)
const stop = baseWatch(
const effect = baseWatch(
source,
() => {
onEffectCleanup(() => {
Expand All @@ -88,7 +88,7 @@ describe('baseWatch', () => {
BaseWatchErrorCodes.WATCH_CALLBACK,
])

stop()
effect!.stop()
source.value++
expect(onError.mock.calls.length).toBe(3)
expect(onError.mock.calls[2]).toMatchObject([
Expand Down
21 changes: 5 additions & 16 deletions packages/reactivity/src/baseWatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,6 @@ export interface BaseWatchOptions<Immediate = boolean> extends DebuggerOptions {
onWarn?: HandleWarn
}

type WatchStopHandle = () => void

export interface WatchInstance extends WatchStopHandle {
effect?: ReactiveEffect
}

// initial value for watchers to trigger on undefined initial values
const INITIAL_WATCHER_VALUE = {}

Expand Down Expand Up @@ -146,7 +140,7 @@ export function baseWatch(
onTrack,
onTrigger,
}: BaseWatchOptions = EMPTY_OBJ,
): WatchInstance {
): ReactiveEffect | undefined {
const warnInvalidSource = (s: unknown) => {
onWarn(
`Invalid watch source: `,
Expand Down Expand Up @@ -232,7 +226,7 @@ export function baseWatch(
// onEffectCleanup need use effect as a key
getCurrentScope()?.effects.push((effect = {} as any))
getter()
return NOOP
return
}
if (immediate) {
// onEffectCleanup need use effect as a key
Expand All @@ -243,12 +237,12 @@ export function baseWatch(
BaseWatchErrorCodes.WATCH_CALLBACK,
[getter(), isMultiSource ? [] : undefined, onEffectCleanup],
)
return NOOP
return
}
const _cb = cb
cb = (...args) => {
_cb(...args)
unwatch()
effect?.stop()
}
}

Expand Down Expand Up @@ -329,11 +323,6 @@ export function baseWatch(
}
}

const unwatch: WatchInstance = () => {
effect.stop()
}
unwatch.effect = effect

if (__DEV__) {
effect.onTrack = onTrack
effect.onTrigger = onTrigger
Expand All @@ -354,7 +343,7 @@ export function baseWatch(
})
}

return unwatch
return effect
}

export function traverse(value: unknown, seen?: Set<unknown>) {
Expand Down
1 change: 0 additions & 1 deletion packages/reactivity/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,5 +76,4 @@ export {
traverse,
type BaseWatchOptions,
type Scheduler,
type WatchInstance,
} from './baseWatch'
16 changes: 9 additions & 7 deletions packages/runtime-core/src/apiWatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,14 +223,16 @@ function doWatch(
const scheduler = getSchedulerByFlushMode(flush)({ instance })
extendOptions.scheduler = scheduler

let baseUnwatch = baseWatch(source, cb, extend({}, options, extendOptions))
let effect = baseWatch(source, cb, extend({}, options, extendOptions))

const unwatch = () => {
baseUnwatch()
if (instance && instance.scope) {
remove(instance.scope.effects!, baseUnwatch.effect)
}
}
const unwatch = !effect
? NOOP
: () => {
effect!.stop()
if (instance && instance.scope) {
remove(instance.scope.effects!, effect)
}
}

if (__SSR__ && ssrCleanup) ssrCleanup.push(unwatch)
return unwatch
Expand Down

0 comments on commit 2213634

Please sign in to comment.