Skip to content

Commit

Permalink
chore: rename handleWarn to onWarn
Browse files Browse the repository at this point in the history
  • Loading branch information
LittleSound committed Dec 28, 2023
1 parent 90fd005 commit a078ad1
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 27 deletions.
18 changes: 9 additions & 9 deletions packages/reactivity/__tests__/baseWatch.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,14 @@ describe('baseWatch', () => {
})

test('custom error handler', () => {
const handleError = vi.fn()
const onError = vi.fn()

baseWatch(
() => {
throw 'oops in effect'
},
null,
{ handleError },
{ onError },
)

const source = ref(0)
Expand All @@ -72,26 +72,26 @@ describe('baseWatch', () => {
})
throw 'oops in watch'
},
{ handleError },
{ onError },
)

expect(handleError.mock.calls.length).toBe(1)
expect(handleError.mock.calls[0]).toMatchObject([
expect(onError.mock.calls.length).toBe(1)
expect(onError.mock.calls[0]).toMatchObject([
'oops in effect',
BaseWatchErrorCodes.WATCH_CALLBACK,
])

source.value++
expect(handleError.mock.calls.length).toBe(2)
expect(handleError.mock.calls[1]).toMatchObject([
expect(onError.mock.calls.length).toBe(2)
expect(onError.mock.calls[1]).toMatchObject([
'oops in watch',
BaseWatchErrorCodes.WATCH_CALLBACK,
])

stop()
source.value++
expect(handleError.mock.calls.length).toBe(3)
expect(handleError.mock.calls[2]).toMatchObject([
expect(onError.mock.calls.length).toBe(3)
expect(onError.mock.calls[2]).toMatchObject([
'oops in cleanup',
BaseWatchErrorCodes.WATCH_CLEANUP,
])
Expand Down
28 changes: 12 additions & 16 deletions packages/reactivity/src/baseWatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ export interface BaseWatchOptions<Immediate = boolean> extends DebuggerOptions {
deep?: boolean
once?: boolean
scheduler?: Scheduler
handleError?: HandleError
handleWarn?: HandleWarn
onError?: HandleError
onWarn?: HandleWarn
}

type WatchStopHandle = () => void
Expand Down Expand Up @@ -121,15 +121,15 @@ export function baseWatch(
immediate,
deep,
once,
scheduler = DEFAULT_SCHEDULER,
onWarn = __DEV__ ? warn : NOOP,
onError = DEFAULT_HANDLE_ERROR,
onTrack,
onTrigger,
scheduler = DEFAULT_SCHEDULER,
handleError: handleError = DEFAULT_HANDLE_ERROR,
handleWarn: handleWarn = __DEV__ ? warn : NOOP,
}: BaseWatchOptions = EMPTY_OBJ,
): WatchInstance {
const warnInvalidSource = (s: unknown) => {
handleWarn(
onWarn(
`Invalid watch source: `,
s,
`A watch source can only be a getter/effect function, a ref, ` +
Expand Down Expand Up @@ -161,7 +161,7 @@ export function baseWatch(
} else if (isFunction(s)) {
return callWithErrorHandling(
s,
handleError,
onError,
BaseWatchErrorCodes.WATCH_GETTER,
)
} else {
Expand All @@ -172,11 +172,7 @@ export function baseWatch(
if (cb) {
// getter with cb
getter = () =>
callWithErrorHandling(
source,
handleError,
BaseWatchErrorCodes.WATCH_GETTER,
)
callWithErrorHandling(source, onError, BaseWatchErrorCodes.WATCH_GETTER)
} else {
// no cb -> simple effect
getter = () => {
Expand All @@ -193,7 +189,7 @@ export function baseWatch(
try {
return callWithAsyncErrorHandling(
source,
handleError,
onError,
BaseWatchErrorCodes.WATCH_CALLBACK,
[onEffectCleanup],
)
Expand Down Expand Up @@ -224,7 +220,7 @@ export function baseWatch(
getCurrentScope()?.effects.push((effect = {} as any))
callWithAsyncErrorHandling(
cb,
handleError,
onError,
BaseWatchErrorCodes.WATCH_CALLBACK,
[getter(), isMultiSource ? [] : undefined, onEffectCleanup],
)
Expand Down Expand Up @@ -263,7 +259,7 @@ export function baseWatch(
try {
callWithAsyncErrorHandling(
cb,
handleError,
onError,
BaseWatchErrorCodes.WATCH_CALLBACK,
[
newValue,
Expand Down Expand Up @@ -306,7 +302,7 @@ export function baseWatch(
cleanups.forEach(cleanup =>
callWithErrorHandling(
cleanup,
handleError,
onError,
BaseWatchErrorCodes.WATCH_CLEANUP,
),
)
Expand Down
4 changes: 2 additions & 2 deletions packages/runtime-core/src/apiWatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ function doWatch(

const extendOptions: BaseWatchOptions = {}

if (__DEV__) extendOptions.handleWarn = warn
if (__DEV__) extendOptions.onWarn = warn

let ssrCleanup: (() => void)[] | undefined
if (__SSR__ && isInSSRComponentSetup) {
Expand All @@ -217,7 +217,7 @@ function doWatch(
const instance =
getCurrentScope() === currentInstance?.scope ? currentInstance : null

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

const scheduler = getSchedulerByFlushMode(flush)({ instance })
Expand Down

0 comments on commit a078ad1

Please sign in to comment.