Skip to content

Commit

Permalink
fix: lint
Browse files Browse the repository at this point in the history
  • Loading branch information
LittleSound committed Dec 27, 2023
1 parent 97179ed commit d1f001b
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 63 deletions.
56 changes: 30 additions & 26 deletions packages/reactivity/src/baseWatch.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,33 @@
import {
EMPTY_OBJ,
isObject,
NOOP,
hasChanged,
isArray,
isFunction,
hasChanged,
NOOP,
isMap,
isSet,
isObject,
isPlainObject,
isPromise
isPromise,
isSet,
} from '@vue/shared'
import { warn } from './warning'
import { ComputedRef } from './computed'
import type { ComputedRef } from './computed'
import { ReactiveFlags } from './constants'
import { DebuggerOptions, ReactiveEffect, EffectScheduler } from './effect'
import { isShallow, isReactive } from './reactive'
import { Ref, isRef } from './ref'
import {
type DebuggerOptions,
type EffectScheduler,
ReactiveEffect,
} from './effect'
import { isReactive, isShallow } from './reactive'
import { type Ref, isRef } from './ref'
import { getCurrentScope } from './effectScope'

// contexts where user provided function may be executed, in addition to
// lifecycle hooks.
export enum BaseWatchErrorCodes {
WATCH_GETTER = 'BaseWatchErrorCodes_WATCH_GETTER',
WATCH_CALLBACK = 'BaseWatchErrorCodes_WATCH_CALLBACK',
WATCH_CLEANUP = 'BaseWatchErrorCodes_WATCH_CLEANUP'
WATCH_CLEANUP = 'BaseWatchErrorCodes_WATCH_CLEANUP',
}

// TODO move to a scheduler package
Expand Down Expand Up @@ -57,7 +61,7 @@ export type WatchSource<T = any> = Ref<T> | ComputedRef<T> | (() => T)
export type WatchCallback<V = any, OV = any> = (
value: V,
oldValue: OV,
onCleanup: OnCleanup
onCleanup: OnCleanup,
) => any

type OnCleanup = (cleanupFn: () => void) => void
Expand Down Expand Up @@ -119,15 +123,15 @@ export function baseWatch(
onTrigger,
scheduler = DEFAULT_SCHEDULER,
handleError: handleError = DEFAULT_HANDLE_ERROR,
handleWarn: handleWarn = warn
}: BaseWatchOptions = EMPTY_OBJ
handleWarn: handleWarn = warn,
}: BaseWatchOptions = EMPTY_OBJ,
): WatchInstance {
const warnInvalidSource = (s: unknown) => {
handleWarn(
`Invalid watch source: `,
s,
`A watch source can only be a getter/effect function, a ref, ` +
`a reactive object, or an array of these types.`
`a reactive object, or an array of these types.`,
)
}

Expand Down Expand Up @@ -156,7 +160,7 @@ export function baseWatch(
return callWithErrorHandling(
s,
handleError,
BaseWatchErrorCodes.WATCH_GETTER
BaseWatchErrorCodes.WATCH_GETTER,
)
} else {
__DEV__ && warnInvalidSource(s)
Expand All @@ -169,7 +173,7 @@ export function baseWatch(
callWithErrorHandling(
source,
handleError,
BaseWatchErrorCodes.WATCH_GETTER
BaseWatchErrorCodes.WATCH_GETTER,
)
} else {
// no cb -> simple effect
Expand All @@ -184,7 +188,7 @@ export function baseWatch(
source,
handleError,
BaseWatchErrorCodes.WATCH_CALLBACK,
[onEffectCleanup]
[onEffectCleanup],
)
} finally {
activeEffect = currentEffect
Expand Down Expand Up @@ -215,7 +219,7 @@ export function baseWatch(
cb,
handleError,
BaseWatchErrorCodes.WATCH_CALLBACK,
[getter(), isMultiSource ? [] : undefined, onEffectCleanup]
[getter(), isMultiSource ? [] : undefined, onEffectCleanup],
)
return NOOP
}
Expand Down Expand Up @@ -262,8 +266,8 @@ export function baseWatch(
: isMultiSource && oldValue[0] === INITIAL_WATCHER_VALUE
? []
: oldValue,
onEffectCleanup
]
onEffectCleanup,
],
)
oldValue = newValue
} finally {
Expand All @@ -284,7 +288,7 @@ export function baseWatch(
scheduler({
effect,
job,
isInit: false
isInit: false,
})

effect = new ReactiveEffect(getter, NOOP, effectScheduler)
Expand All @@ -296,8 +300,8 @@ export function baseWatch(
callWithErrorHandling(
cleanup,
handleError,
BaseWatchErrorCodes.WATCH_CLEANUP
)
BaseWatchErrorCodes.WATCH_CLEANUP,
),
)
cleanupMap.delete(effect)
}
Expand All @@ -324,7 +328,7 @@ export function baseWatch(
scheduler({
effect,
job,
isInit: true
isInit: true,
})
}

Expand Down Expand Up @@ -362,7 +366,7 @@ export function callWithErrorHandling(
fn: Function,
handleError: HandleError,
type: BaseWatchErrorCodes,
args?: unknown[]
args?: unknown[],
) {
let res
try {
Expand All @@ -377,7 +381,7 @@ export function callWithAsyncErrorHandling(
fn: Function | Function[],
handleError: HandleError,
type: BaseWatchErrorCodes,
args?: unknown[]
args?: unknown[],
): any[] {
if (isFunction(fn)) {
const res = callWithErrorHandling(fn, handleError, type, args)
Expand Down
74 changes: 37 additions & 37 deletions packages/runtime-core/src/apiWatch.ts
Original file line number Diff line number Diff line change
@@ -1,43 +1,43 @@
import {
isRef,
type Ref,
type BaseWatchErrorCodes,
type BaseWatchOptions,
type ComputedRef,
ReactiveFlags,
type DebuggerOptions,
getCurrentScope,
BaseWatchErrorCodes,
ReactiveFlags,
type Ref,
baseWatch,
type BaseWatchOptions
getCurrentScope,
isRef,
} from '@vue/reactivity'
import {
type SchedulerJob,
usePreScheduler,
useSyncScheduler
useSyncScheduler,
} from './scheduler'
import {
EMPTY_OBJ,
isObject,
NOOP,
extend,
isArray,
isFunction,
isString,
NOOP,
remove,
isMap,
isSet,
isObject,
isPlainObject,
extend
isSet,
isString,
remove,
} from '@vue/shared'
import {
currentInstance,
type ComponentInternalInstance,
currentInstance,
isInSSRComponentSetup,
setCurrentInstance,
unsetCurrentInstance
unsetCurrentInstance,
} from './component'
import { handleError as handleErrorWithInstance } from './errorHandling'
import { usePostRenderScheduler } from './renderer'
import { warn } from './warning'
import { type ObjectWatchOptionItem } from './componentOptions'
import type { ObjectWatchOptionItem } from './componentOptions'
import { useSSRContext } from '@vue/runtime-core'

export type WatchEffect = (onCleanup: OnCleanup) => void
Expand All @@ -47,7 +47,7 @@ export type WatchSource<T = any> = Ref<T> | ComputedRef<T> | (() => T)
export type WatchCallback<V = any, OV = any> = (
value: V,
oldValue: OV,
onCleanup: OnCleanup
onCleanup: OnCleanup,
) => any

type MapSources<T, Immediate> = {
Expand Down Expand Up @@ -79,30 +79,30 @@ export type WatchStopHandle = () => void
// Simple effect.
export function watchEffect(
effect: WatchEffect,
options?: WatchOptionsBase
options?: WatchOptionsBase,
): WatchStopHandle {
return doWatch(effect, null, options)
}

export function watchPostEffect(
effect: WatchEffect,
options?: DebuggerOptions
options?: DebuggerOptions,
) {
return doWatch(
effect,
null,
__DEV__ ? extend({}, options as any, { flush: 'post' }) : { flush: 'post' }
__DEV__ ? extend({}, options as any, { flush: 'post' }) : { flush: 'post' },
)
}

export function watchSyncEffect(
effect: WatchEffect,
options?: DebuggerOptions
options?: DebuggerOptions,
) {
return doWatch(
effect,
null,
__DEV__ ? extend({}, options as any, { flush: 'sync' }) : { flush: 'sync' }
__DEV__ ? extend({}, options as any, { flush: 'sync' }) : { flush: 'sync' },
)
}

Expand All @@ -111,60 +111,60 @@ type MultiWatchSources = (WatchSource<unknown> | object)[]
// overload: array of multiple sources + cb
export function watch<
T extends MultiWatchSources,
Immediate extends Readonly<boolean> = false
Immediate extends Readonly<boolean> = false,
>(
sources: [...T],
cb: WatchCallback<MapSources<T, false>, MapSources<T, Immediate>>,
options?: WatchOptions<Immediate>
options?: WatchOptions<Immediate>,
): WatchStopHandle

// overload: multiple sources w/ `as const`
// watch([foo, bar] as const, () => {})
// somehow [...T] breaks when the type is readonly
export function watch<
T extends Readonly<MultiWatchSources>,
Immediate extends Readonly<boolean> = false
Immediate extends Readonly<boolean> = false,
>(
source: T,
cb: WatchCallback<MapSources<T, false>, MapSources<T, Immediate>>,
options?: WatchOptions<Immediate>
options?: WatchOptions<Immediate>,
): WatchStopHandle

// overload: single source + cb
export function watch<T, Immediate extends Readonly<boolean> = false>(
source: WatchSource<T>,
cb: WatchCallback<T, Immediate extends true ? T | undefined : T>,
options?: WatchOptions<Immediate>
options?: WatchOptions<Immediate>,
): WatchStopHandle

// overload: watching reactive object w/ cb
export function watch<
T extends object,
Immediate extends Readonly<boolean> = false
Immediate extends Readonly<boolean> = false,
>(
source: T,
cb: WatchCallback<T, Immediate extends true ? T | undefined : T>,
options?: WatchOptions<Immediate>
options?: WatchOptions<Immediate>,
): WatchStopHandle

// implementation
export function watch<T = any, Immediate extends Readonly<boolean> = false>(
source: T | WatchSource<T>,
cb: any,
options?: WatchOptions<Immediate>
options?: WatchOptions<Immediate>,
): WatchStopHandle {
if (__DEV__ && !isFunction(cb)) {
warn(
`\`watch(fn, options?)\` signature has been moved to a separate API. ` +
`Use \`watchEffect(fn, options?)\` instead. \`watch\` now only ` +
`supports \`watch(source, cb, options?) signature.`
`supports \`watch(source, cb, options?) signature.`,
)
}
return doWatch(source as any, cb, options)
}

function getSchedulerByFlushMode(
flush: WatchOptionsBase['flush']
flush: WatchOptionsBase['flush'],
): SchedulerJob {
if (flush === 'post') {
return usePostRenderScheduler
Expand All @@ -179,26 +179,26 @@ function getSchedulerByFlushMode(
function doWatch(
source: WatchSource | WatchSource[] | WatchEffect | object,
cb: WatchCallback | null,
options: WatchOptions = EMPTY_OBJ
options: WatchOptions = EMPTY_OBJ,
): WatchStopHandle {
const { immediate, deep, flush, once } = options
if (__DEV__ && !cb) {
if (immediate !== undefined) {
warn(
`watch() "immediate" option is only respected when using the ` +
`watch(source, callback, options?) signature.`
`watch(source, callback, options?) signature.`,
)
}
if (deep !== undefined) {
warn(
`watch() "deep" option is only respected when using the ` +
`watch(source, callback, options?) signature.`
`watch(source, callback, options?) signature.`,
)
}
if (once !== undefined) {
warn(
`watch() "once" option is only respected when using the ` +
`watch(source, callback, options?) signature.`
`watch(source, callback, options?) signature.`,
)
}
}
Expand Down Expand Up @@ -246,7 +246,7 @@ export function instanceWatch(
this: ComponentInternalInstance,
source: string | Function,
value: WatchCallback | ObjectWatchOptionItem,
options?: WatchOptions
options?: WatchOptions,
): WatchStopHandle {
const publicThis = this.proxy as any
const getter = isString(source)
Expand Down

0 comments on commit d1f001b

Please sign in to comment.