Skip to content
This repository was archived by the owner on Jul 19, 2025. It is now read-only.

Commit 28d0549

Browse files
committed
fix: sync code changes according to the review in PR #82
1 parent c4db74c commit 28d0549

File tree

3 files changed

+37
-45
lines changed

3 files changed

+37
-45
lines changed

packages/runtime-vapor/src/apiWatch.ts

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ import {
1010
import { EMPTY_OBJ, NOOP, extend, isFunction, remove } from '@vue/shared'
1111
import { currentInstance } from './component'
1212
import {
13-
type SchedulerJob,
14-
useVaporPostScheduler,
15-
useVaporPreScheduler,
16-
useVaporSyncScheduler,
13+
type CreateScheduler,
14+
createVaporPostScheduler,
15+
createVaporPreScheduler,
16+
createVaporSyncScheduler,
1717
} from './scheduler'
1818
import { handleError as handleErrorWithInstance } from './errorHandling'
1919
import { warn } from './warning'
@@ -143,15 +143,15 @@ export function watch<T = any, Immediate extends Readonly<boolean> = false>(
143143

144144
function getSchedulerByFlushMode(
145145
flush: WatchOptionsBase['flush'],
146-
): SchedulerJob {
146+
): CreateScheduler {
147147
if (flush === 'post') {
148-
return useVaporPostScheduler
148+
return createVaporPostScheduler
149149
}
150150
if (flush === 'sync') {
151-
return useVaporSyncScheduler
151+
return createVaporSyncScheduler
152152
}
153153
// default: 'pre'
154-
return useVaporPreScheduler
154+
return createVaporPreScheduler
155155
}
156156

157157
function doWatch(
@@ -160,6 +160,15 @@ function doWatch(
160160
options: WatchOptions = EMPTY_OBJ,
161161
): WatchStopHandle {
162162
const { immediate, deep, flush, once } = options
163+
164+
// TODO remove in 3.5
165+
if (__DEV__ && deep !== void 0 && typeof deep === 'number') {
166+
warn(
167+
`watch() "deep" option with number value will be used as watch depth in future versions. ` +
168+
`Please use a boolean instead to avoid potential breakage.`,
169+
)
170+
}
171+
163172
if (__DEV__ && !cb) {
164173
if (immediate !== undefined) {
165174
warn(
@@ -200,23 +209,23 @@ function doWatch(
200209
// }
201210
// }
202211

203-
const instance =
204-
getCurrentScope() === currentInstance?.scope ? currentInstance : null
212+
const instance = currentInstance
205213

206214
extendOptions.onError = (err: unknown, type: BaseWatchErrorCodes) =>
207215
handleErrorWithInstance(err, instance, type)
208216

209-
const scheduler = getSchedulerByFlushMode(flush)({ instance })
217+
const scheduler = getSchedulerByFlushMode(flush)(instance)
210218
extendOptions.scheduler = scheduler
211219

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

222+
const scope = getCurrentScope()
214223
const unwatch = !effect
215224
? NOOP
216225
: () => {
217226
effect!.stop()
218-
if (instance && instance.scope) {
219-
remove(instance.scope.effects!, effect)
227+
if (scope) {
228+
remove(scope.effects, effect)
220229
}
221230
}
222231

packages/runtime-vapor/src/renderWatch.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
} from '@vue/reactivity'
77
import { NOOP, remove } from '@vue/shared'
88
import { currentInstance } from './component'
9-
import { useVaporRenderingScheduler } from './scheduler'
9+
import { createVaporRenderingScheduler } from './scheduler'
1010
import { handleError as handleErrorWithInstance } from './errorHandling'
1111
import { warn } from './warning'
1212

@@ -40,7 +40,7 @@ function doWatch(source: any, cb?: any): WatchStopHandle {
4040
extendOptions.onError = (err: unknown, type: BaseWatchErrorCodes) =>
4141
handleErrorWithInstance(err, instance, type)
4242

43-
const scheduler = useVaporRenderingScheduler({ instance })
43+
const scheduler = createVaporRenderingScheduler(instance)
4444
extendOptions.scheduler = scheduler
4545

4646
let effect = baseWatch(source, cb, extendOptions)

packages/runtime-vapor/src/scheduler.ts

Lines changed: 13 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { ReactiveEffect } from '@vue/reactivity'
1+
import type { Scheduler } from '@vue/reactivity'
22
import type { ComponentInternalInstance } from './component'
33

44
export interface SchedulerJob extends Function {
@@ -197,33 +197,21 @@ const comparator = (a: SchedulerJob, b: SchedulerJob): number => {
197197
return diff
198198
}
199199

200-
export type Scheduler = (options: {
201-
instance: ComponentInternalInstance | null
202-
}) => (context: {
203-
effect: ReactiveEffect
204-
job: SchedulerJob
205-
isInit: boolean
206-
}) => void
207-
208-
export const useVaporSyncScheduler: Scheduler =
209-
({ instance }) =>
210-
({ isInit, effect, job }) => {
211-
if (instance && instance.isUnmounted) {
212-
return
213-
}
200+
export type CreateScheduler = (
201+
instance: ComponentInternalInstance | null,
202+
) => Scheduler
203+
204+
export const createVaporSyncScheduler: CreateScheduler =
205+
(instance) => (job, effect, isInit) => {
214206
if (isInit) {
215207
effect.run()
216208
} else {
217209
job()
218210
}
219211
}
220212

221-
export const useVaporPreScheduler: Scheduler =
222-
({ instance }) =>
223-
({ isInit, effect, job }) => {
224-
if (instance && instance.isUnmounted) {
225-
return
226-
}
213+
export const createVaporPreScheduler: CreateScheduler =
214+
(instance) => (job, effect, isInit) => {
227215
if (isInit) {
228216
effect.run()
229217
} else {
@@ -233,12 +221,8 @@ export const useVaporPreScheduler: Scheduler =
233221
}
234222
}
235223

236-
export const useVaporRenderingScheduler: Scheduler =
237-
({ instance }) =>
238-
({ isInit, effect, job }) => {
239-
if (instance && instance.isUnmounted) {
240-
return
241-
}
224+
export const createVaporRenderingScheduler: CreateScheduler =
225+
(instance) => (job, effect, isInit) => {
242226
if (isInit) {
243227
effect.run()
244228
} else {
@@ -248,9 +232,8 @@ export const useVaporRenderingScheduler: Scheduler =
248232
}
249233
}
250234

251-
export const useVaporPostScheduler: Scheduler =
252-
() =>
253-
({ isInit, effect, job }) => {
235+
export const createVaporPostScheduler: CreateScheduler =
236+
(instance) => (job, effect, isInit) => {
254237
if (isInit) {
255238
queuePostRenderEffect(effect.run.bind(effect))
256239
} else {

0 commit comments

Comments
 (0)