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,
onTrigger,
}: BaseWatchOptions = EMPTY_OBJ,
): ReactiveEffect | undefined {
): ReactiveEffect {
const warnInvalidSource = (s: unknown) => {
onWarn(
`Invalid watch source: `,

View File

@ -224,17 +224,14 @@ function doWatch(
handleErrorWithInstance(err, instance, type)
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 unwatch = !effect
? NOOP
: () => {
effect!.stop()
if (scope) {
remove(scope.effects, effect)
}
}
const unwatch = () => {
effect!.stop()
if (scope) {
remove(scope.effects, effect)
}
}
if (__SSR__ && ssrCleanup) ssrCleanup.push(unwatch)
return unwatch

View File

@ -7,7 +7,7 @@ import {
baseWatch,
getCurrentScope,
} 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 {
type SchedulerFactory,
@ -159,14 +159,6 @@ function doWatch(
): WatchStopHandle {
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 (immediate !== undefined) {
warn(
@ -213,17 +205,14 @@ function doWatch(
handleErrorWithInstance(err, instance, type)
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 unwatch = !effect
? NOOP
: () => {
effect!.stop()
if (scope) {
remove(scope.effects, effect)
}
}
const unwatch = () => {
effect!.stop()
if (scope) {
remove(scope.effects, effect)
}
}
if (__SSR__ && ssrCleanup) ssrCleanup.push(unwatch)
return unwatch