Skip to content

Commit 770c21d

Browse files
committed
fix: sync code changes according to the review in PR vuejs/vue-vapor#82
1 parent a6eb043 commit 770c21d

File tree

8 files changed

+47
-67
lines changed

8 files changed

+47
-67
lines changed

packages/reactivity/__tests__/baseWatch.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
baseWatch,
77
onEffectCleanup,
88
ref,
9-
} from '../src/index'
9+
} from '../src'
1010

1111
const queue: SchedulerJob[] = []
1212

@@ -15,7 +15,7 @@ let isFlushPending = false
1515
const resolvedPromise = /*#__PURE__*/ Promise.resolve() as Promise<any>
1616
const nextTick = (fn?: () => any) =>
1717
fn ? resolvedPromise.then(fn) : resolvedPromise
18-
const scheduler: Scheduler = ({ job }) => {
18+
const scheduler: Scheduler = job => {
1919
queue.push(job)
2020
flushJobs()
2121
}

packages/reactivity/src/baseWatch.ts

Lines changed: 15 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,13 @@ import { isReactive, isShallow } from './reactive'
2424
import { type Ref, isRef } from './ref'
2525
import { getCurrentScope } from './effectScope'
2626

27-
// contexts where user provided function may be executed, in addition to
28-
// lifecycle hooks.
27+
// These errors were transferred from `packages/runtime-core/src/errorHandling.ts`
28+
// along with baseWatch to maintain code compatibility. Hence,
29+
// it is essential to keep these values unchanged.
2930
export enum BaseWatchErrorCodes {
30-
WATCH_GETTER = 'BaseWatchErrorCodes_WATCH_GETTER',
31-
WATCH_CALLBACK = 'BaseWatchErrorCodes_WATCH_CALLBACK',
32-
WATCH_CLEANUP = 'BaseWatchErrorCodes_WATCH_CLEANUP',
31+
WATCH_GETTER = 2,
32+
WATCH_CALLBACK,
33+
WATCH_CLEANUP,
3334
}
3435

3536
// TODO move to a scheduler package
@@ -57,15 +58,12 @@ export interface SchedulerJob extends Function {
5758
}
5859

5960
type WatchEffect = (onCleanup: OnCleanup) => void
60-
6161
type WatchSource<T = any> = Ref<T> | ComputedRef<T> | (() => T)
62-
6362
type WatchCallback<V = any, OV = any> = (
6463
value: V,
6564
oldValue: OV,
6665
onCleanup: OnCleanup,
6766
) => any
68-
6967
type OnCleanup = (cleanupFn: () => void) => void
7068

7169
export interface BaseWatchOptions<Immediate = boolean> extends DebuggerOptions {
@@ -80,22 +78,19 @@ export interface BaseWatchOptions<Immediate = boolean> extends DebuggerOptions {
8078
// initial value for watchers to trigger on undefined initial values
8179
const INITIAL_WATCHER_VALUE = {}
8280

83-
export type Scheduler = (context: {
84-
effect: ReactiveEffect
85-
job: SchedulerJob
86-
isInit: boolean
87-
}) => void
88-
89-
const DEFAULT_SCHEDULER: Scheduler = ({ job }) => job()
90-
81+
export type Scheduler = (
82+
job: SchedulerJob,
83+
effect: ReactiveEffect,
84+
isInit: boolean,
85+
) => void
9186
export type HandleError = (err: unknown, type: BaseWatchErrorCodes) => void
87+
export type HandleWarn = (msg: string, ...args: any[]) => void
9288

89+
const DEFAULT_SCHEDULER: Scheduler = job => job()
9390
const DEFAULT_HANDLE_ERROR: HandleError = (err: unknown) => {
9491
throw err
9592
}
9693

97-
export type HandleWarn = (msg: string, ...args: any[]) => void
98-
9994
const cleanupMap: WeakMap<ReactiveEffect, (() => void)[]> = new WeakMap()
10095
let activeEffect: ReactiveEffect | undefined = undefined
10196

@@ -306,12 +301,7 @@ export function baseWatch(
306301
// it is allowed to self-trigger (#1727)
307302
job.allowRecurse = !!cb
308303

309-
let effectScheduler: EffectScheduler = () =>
310-
scheduler({
311-
effect,
312-
job,
313-
isInit: false,
314-
})
304+
let effectScheduler: EffectScheduler = () => scheduler(job, effect, false)
315305

316306
effect = new ReactiveEffect(getter, NOOP, effectScheduler)
317307

@@ -342,11 +332,7 @@ export function baseWatch(
342332
oldValue = effect.run()
343333
}
344334
} else {
345-
scheduler({
346-
effect,
347-
job,
348-
isInit: true,
349-
})
335+
scheduler(job, effect, true)
350336
}
351337

352338
return effect

packages/reactivity/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ export { TrackOpTypes, TriggerOpTypes, ReactiveFlags } from './constants'
7272
export {
7373
baseWatch,
7474
onEffectCleanup,
75-
BaseWatchErrorCodes,
7675
traverse,
76+
BaseWatchErrorCodes,
7777
type BaseWatchOptions,
7878
type Scheduler,
7979
} from './baseWatch'

packages/runtime-core/src/apiWatch.ts

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ import {
88
getCurrentScope,
99
} from '@vue/reactivity'
1010
import {
11-
type SchedulerJob,
12-
usePreScheduler,
13-
useSyncScheduler,
11+
type SchedulerFactory,
12+
createPreScheduler,
13+
createSyncScheduler,
1414
} from './scheduler'
1515
import {
1616
EMPTY_OBJ,
@@ -28,7 +28,7 @@ import {
2828
unsetCurrentInstance,
2929
} from './component'
3030
import { handleError as handleErrorWithInstance } from './errorHandling'
31-
import { usePostRenderScheduler } from './renderer'
31+
import { createPostRenderScheduler } from './renderer'
3232
import { warn } from './warning'
3333
import type { ObjectWatchOptionItem } from './componentOptions'
3434
import { useSSRContext } from './helpers/useSsrContext'
@@ -156,17 +156,15 @@ export function watch<T = any, Immediate extends Readonly<boolean> = false>(
156156
return doWatch(source as any, cb, options)
157157
}
158158

159-
function getSchedulerByFlushMode(
160-
flush: WatchOptionsBase['flush'],
161-
): SchedulerJob {
159+
function getScheduler(flush: WatchOptionsBase['flush']): SchedulerFactory {
162160
if (flush === 'post') {
163-
return usePostRenderScheduler
161+
return createPostRenderScheduler
164162
}
165163
if (flush === 'sync') {
166-
return useSyncScheduler
164+
return createSyncScheduler
167165
}
168166
// default: 'pre'
169-
return usePreScheduler
167+
return createPreScheduler
170168
}
171169

172170
function doWatch(
@@ -224,12 +222,9 @@ function doWatch(
224222
}
225223

226224
const instance = currentInstance
227-
228225
extendOptions.onError = (err: unknown, type: BaseWatchErrorCodes) =>
229226
handleErrorWithInstance(err, instance, type)
230-
231-
const scheduler = getSchedulerByFlushMode(flush)({ instance })
232-
extendOptions.scheduler = scheduler
227+
extendOptions.scheduler = getScheduler(flush)(instance)
233228

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

packages/runtime-core/src/componentOptions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -942,7 +942,7 @@ export function createWatcher(
942942
? createPathGetter(publicThis, key)
943943
: () => (publicThis as any)[key]
944944

945-
const options: WatchOptions<false> = {}
945+
const options: WatchOptions = {}
946946
if (__COMPAT__) {
947947
const instance =
948948
getCurrentScope() === currentInstance?.scope ? currentInstance : null

packages/runtime-core/src/errorHandling.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,13 @@ import { BaseWatchErrorCodes } from '@vue/reactivity'
1010
export enum ErrorCodes {
1111
SETUP_FUNCTION,
1212
RENDER_FUNCTION,
13-
NATIVE_EVENT_HANDLER,
13+
// The error codes for the watch have been transferred to the reactivity
14+
// package along with baseWatch to maintain code compatibility. Hence,
15+
// it is essential to keep these values unchanged.
16+
// WATCH_GETTER,
17+
// WATCH_CALLBACK,
18+
// WATCH_CLEANUP,
19+
NATIVE_EVENT_HANDLER = 5,
1420
COMPONENT_EVENT_HANDLER,
1521
VNODE_HOOK,
1622
DIRECTIVE_HOOK,

packages/runtime-core/src/renderer.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ import {
3838
isReservedProp,
3939
} from '@vue/shared'
4040
import {
41-
type Scheduler,
41+
type SchedulerFactory,
4242
type SchedulerJob,
4343
flushPostFlushCbs,
4444
flushPreFlushCbs,
@@ -282,9 +282,8 @@ export const queuePostRenderEffect = __FEATURE_SUSPENSE__
282282
: queueEffectWithSuspense
283283
: queuePostFlushCb
284284

285-
export const usePostRenderScheduler: Scheduler =
286-
({ instance }) =>
287-
({ isInit, effect, job }) => {
285+
export const createPostRenderScheduler: SchedulerFactory =
286+
instance => (job, effect, isInit) => {
288287
if (isInit) {
289288
queuePostRenderEffect(
290289
effect.run.bind(effect),

packages/runtime-core/src/scheduler.ts

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { ErrorCodes, callWithErrorHandling, handleError } from './errorHandling'
22
import { type Awaited, NOOP, isArray } from '@vue/shared'
33
import { type ComponentInternalInstance, getComponentName } from './component'
4-
import type { ReactiveEffect } from '@vue/reactivity'
4+
import type { Scheduler } from '@vue/reactivity'
55

66
export interface SchedulerJob extends Function {
77
id?: number
@@ -289,27 +289,21 @@ function checkRecursiveUpdates(seen: CountMap, fn: SchedulerJob) {
289289
}
290290
}
291291

292-
export type Scheduler = (options: {
293-
instance: ComponentInternalInstance | null
294-
}) => (context: {
295-
effect: ReactiveEffect
296-
job: SchedulerJob
297-
isInit: boolean
298-
}) => void
299-
300-
export const useSyncScheduler: Scheduler =
301-
({ instance }) =>
302-
({ isInit, effect, job }) => {
292+
export type SchedulerFactory = (
293+
instance: ComponentInternalInstance | null,
294+
) => Scheduler
295+
296+
export const createSyncScheduler: SchedulerFactory =
297+
instance => (job, effect, isInit) => {
303298
if (isInit) {
304299
effect.run()
305300
} else {
306301
job()
307302
}
308303
}
309304

310-
export const usePreScheduler: Scheduler =
311-
({ instance }) =>
312-
({ isInit, effect, job }) => {
305+
export const createPreScheduler: SchedulerFactory =
306+
instance => (job, effect, isInit) => {
313307
if (isInit) {
314308
effect.run()
315309
} else {

0 commit comments

Comments
 (0)