refactor: `baseWatch` always return effect

This commit is contained in:
三咲智子 Kevin Deng 2024-08-09 00:41:31 +08:00
parent 16ea5ff2d6
commit 5632e2fe68
No known key found for this signature in database
3 changed files with 16 additions and 30 deletions

View File

@ -123,7 +123,7 @@ export function baseWatch(
onTrack, onTrack,
onTrigger, onTrigger,
}: BaseWatchOptions = EMPTY_OBJ, }: BaseWatchOptions = EMPTY_OBJ,
): ReactiveEffect | undefined { ): ReactiveEffect {
const warnInvalidSource = (s: unknown) => { const warnInvalidSource = (s: unknown) => {
onWarn( onWarn(
`Invalid watch source: `, `Invalid watch source: `,

View File

@ -224,17 +224,14 @@ function doWatch(
handleErrorWithInstance(err, instance, type) handleErrorWithInstance(err, instance, type)
extendOptions.scheduler = getScheduler(flush)(instance) extendOptions.scheduler = getScheduler(flush)(instance)
let effect = baseWatch(source, cb, extend({}, options, extendOptions)) const effect = baseWatch(source, cb, extend({}, options, extendOptions))
const scope = getCurrentScope() const scope = getCurrentScope()
const unwatch = !effect const unwatch = () => {
? NOOP effect!.stop()
: () => { if (scope) {
effect!.stop() remove(scope.effects, effect)
if (scope) { }
remove(scope.effects, effect) }
}
}
if (__SSR__ && ssrCleanup) ssrCleanup.push(unwatch) if (__SSR__ && ssrCleanup) ssrCleanup.push(unwatch)
return unwatch return unwatch

View File

@ -7,7 +7,7 @@ import {
baseWatch, baseWatch,
getCurrentScope, getCurrentScope,
} from '@vue/reactivity' } from '@vue/reactivity'
import { EMPTY_OBJ, NOOP, extend, isFunction, remove } from '@vue/shared' import { EMPTY_OBJ, extend, isFunction, remove } from '@vue/shared'
import { currentInstance } from './component' import { currentInstance } from './component'
import { import {
type SchedulerFactory, type SchedulerFactory,
@ -159,14 +159,6 @@ function doWatch(
): WatchStopHandle { ): WatchStopHandle {
const { immediate, deep, flush, once } = options const { immediate, deep, flush, once } = options
// 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 (__DEV__ && !cb) {
if (immediate !== undefined) { if (immediate !== undefined) {
warn( warn(
@ -213,17 +205,14 @@ function doWatch(
handleErrorWithInstance(err, instance, type) handleErrorWithInstance(err, instance, type)
extendOptions.scheduler = getScheduler(flush)(instance) extendOptions.scheduler = getScheduler(flush)(instance)
let effect = baseWatch(source, cb, extend({}, options, extendOptions)) const effect = baseWatch(source, cb, extend({}, options, extendOptions))
const scope = getCurrentScope() const scope = getCurrentScope()
const unwatch = !effect const unwatch = () => {
? NOOP effect!.stop()
: () => { if (scope) {
effect!.stop() remove(scope.effects, effect)
if (scope) { }
remove(scope.effects, effect) }
}
}
if (__SSR__ && ssrCleanup) ssrCleanup.push(unwatch) if (__SSR__ && ssrCleanup) ssrCleanup.push(unwatch)
return unwatch return unwatch