Skip to content

Commit

Permalink
chore: update
Browse files Browse the repository at this point in the history
  • Loading branch information
sxzz committed Jan 4, 2024
1 parent 58ceb75 commit c1b3e7c
Show file tree
Hide file tree
Showing 7 changed files with 13 additions and 24 deletions.
2 changes: 1 addition & 1 deletion packages/reactivity/__tests__/baseWatch.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
baseWatch,
onEffectCleanup,
ref,
} from '../src/index'
} from '../src'

const queue: SchedulerJob[] = []

Expand Down
10 changes: 2 additions & 8 deletions packages/reactivity/src/baseWatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,12 @@ export interface SchedulerJob extends Function {
}

type WatchEffect = (onCleanup: OnCleanup) => void

type WatchSource<T = any> = Ref<T> | ComputedRef<T> | (() => T)

type WatchCallback<V = any, OV = any> = (
value: V,
oldValue: OV,
onCleanup: OnCleanup,
) => any

type OnCleanup = (cleanupFn: () => void) => void

export interface BaseWatchOptions<Immediate = boolean> extends DebuggerOptions {
Expand All @@ -86,17 +83,14 @@ export type Scheduler = (
effect: ReactiveEffect,
isInit: boolean,
) => void

const DEFAULT_SCHEDULER: Scheduler = job => job()

export type HandleError = (err: unknown, type: BaseWatchErrorCodes) => void
export type HandleWarn = (msg: string, ...args: any[]) => void

const DEFAULT_SCHEDULER: Scheduler = job => job()
const DEFAULT_HANDLE_ERROR: HandleError = (err: unknown) => {
throw err
}

export type HandleWarn = (msg: string, ...args: any[]) => void

const cleanupMap: WeakMap<ReactiveEffect, (() => void)[]> = new WeakMap()
let activeEffect: ReactiveEffect | undefined = undefined

Expand Down
2 changes: 1 addition & 1 deletion packages/reactivity/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ export { TrackOpTypes, TriggerOpTypes, ReactiveFlags } from './constants'
export {
baseWatch,
onEffectCleanup,
BaseWatchErrorCodes,
traverse,
BaseWatchErrorCodes,
type BaseWatchOptions,
type Scheduler,
} from './baseWatch'
11 changes: 3 additions & 8 deletions packages/runtime-core/src/apiWatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
getCurrentScope,
} from '@vue/reactivity'
import {
type CreateScheduler,
type SchedulerFactory,
createPreScheduler,
createSyncScheduler,
} from './scheduler'
Expand Down Expand Up @@ -156,9 +156,7 @@ export function watch<T = any, Immediate extends Readonly<boolean> = false>(
return doWatch(source as any, cb, options)
}

function getSchedulerByFlushMode(
flush: WatchOptionsBase['flush'],
): CreateScheduler {
function getScheduler(flush: WatchOptionsBase['flush']): SchedulerFactory {
if (flush === 'post') {
return createPostRenderScheduler
}
Expand Down Expand Up @@ -224,12 +222,9 @@ function doWatch(
}

const instance = currentInstance

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

const scheduler = getSchedulerByFlushMode(flush)(instance)
extendOptions.scheduler = scheduler
extendOptions.scheduler = getScheduler(flush)(instance)

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

Expand Down
2 changes: 1 addition & 1 deletion packages/runtime-core/src/componentOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -942,7 +942,7 @@ export function createWatcher(
? createPathGetter(publicThis, key)
: () => (publicThis as any)[key]

const options: WatchOptions<false> = {}
const options: WatchOptions = {}
if (__COMPAT__) {
const instance =
getCurrentScope() === currentInstance?.scope ? currentInstance : null
Expand Down
4 changes: 2 additions & 2 deletions packages/runtime-core/src/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import {
isReservedProp,
} from '@vue/shared'
import {
type CreateScheduler,
type SchedulerFactory,
type SchedulerJob,
flushPostFlushCbs,
flushPreFlushCbs,
Expand Down Expand Up @@ -282,7 +282,7 @@ export const queuePostRenderEffect = __FEATURE_SUSPENSE__
: queueEffectWithSuspense
: queuePostFlushCb

export const createPostRenderScheduler: CreateScheduler =
export const createPostRenderScheduler: SchedulerFactory =
instance => (job, effect, isInit) => {
if (isInit) {
queuePostRenderEffect(
Expand Down
6 changes: 3 additions & 3 deletions packages/runtime-core/src/scheduler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -289,11 +289,11 @@ function checkRecursiveUpdates(seen: CountMap, fn: SchedulerJob) {
}
}

export type CreateScheduler = (
export type SchedulerFactory = (
instance: ComponentInternalInstance | null,
) => Scheduler

export const createSyncScheduler: CreateScheduler =
export const createSyncScheduler: SchedulerFactory =
instance => (job, effect, isInit) => {
if (isInit) {
effect.run()
Expand All @@ -302,7 +302,7 @@ export const createSyncScheduler: CreateScheduler =
}
}

export const createPreScheduler: CreateScheduler =
export const createPreScheduler: SchedulerFactory =
instance => (job, effect, isInit) => {
if (isInit) {
effect.run()
Expand Down

0 comments on commit c1b3e7c

Please sign in to comment.