From a078ad11c4cd865318c70493d261922fdba974c4 Mon Sep 17 00:00:00 2001 From: Rizumu Ayaka Date: Thu, 28 Dec 2023 21:28:28 +0800 Subject: [PATCH] chore: rename handleWarn to onWarn --- .../reactivity/__tests__/baseWatch.spec.ts | 18 ++++++------ packages/reactivity/src/baseWatch.ts | 28 ++++++++----------- packages/runtime-core/src/apiWatch.ts | 4 +-- 3 files changed, 23 insertions(+), 27 deletions(-) diff --git a/packages/reactivity/__tests__/baseWatch.spec.ts b/packages/reactivity/__tests__/baseWatch.spec.ts index 4b5a3dddd37..53db9522bce 100644 --- a/packages/reactivity/__tests__/baseWatch.spec.ts +++ b/packages/reactivity/__tests__/baseWatch.spec.ts @@ -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) @@ -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, ]) diff --git a/packages/reactivity/src/baseWatch.ts b/packages/reactivity/src/baseWatch.ts index 5a04ed4ae23..72679633efb 100644 --- a/packages/reactivity/src/baseWatch.ts +++ b/packages/reactivity/src/baseWatch.ts @@ -73,8 +73,8 @@ export interface BaseWatchOptions extends DebuggerOptions { deep?: boolean once?: boolean scheduler?: Scheduler - handleError?: HandleError - handleWarn?: HandleWarn + onError?: HandleError + onWarn?: HandleWarn } type WatchStopHandle = () => void @@ -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, ` + @@ -161,7 +161,7 @@ export function baseWatch( } else if (isFunction(s)) { return callWithErrorHandling( s, - handleError, + onError, BaseWatchErrorCodes.WATCH_GETTER, ) } else { @@ -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 = () => { @@ -193,7 +189,7 @@ export function baseWatch( try { return callWithAsyncErrorHandling( source, - handleError, + onError, BaseWatchErrorCodes.WATCH_CALLBACK, [onEffectCleanup], ) @@ -224,7 +220,7 @@ export function baseWatch( getCurrentScope()?.effects.push((effect = {} as any)) callWithAsyncErrorHandling( cb, - handleError, + onError, BaseWatchErrorCodes.WATCH_CALLBACK, [getter(), isMultiSource ? [] : undefined, onEffectCleanup], ) @@ -263,7 +259,7 @@ export function baseWatch( try { callWithAsyncErrorHandling( cb, - handleError, + onError, BaseWatchErrorCodes.WATCH_CALLBACK, [ newValue, @@ -306,7 +302,7 @@ export function baseWatch( cleanups.forEach(cleanup => callWithErrorHandling( cleanup, - handleError, + onError, BaseWatchErrorCodes.WATCH_CLEANUP, ), ) diff --git a/packages/runtime-core/src/apiWatch.ts b/packages/runtime-core/src/apiWatch.ts index 08cdca36280..e5f58891802 100644 --- a/packages/runtime-core/src/apiWatch.ts +++ b/packages/runtime-core/src/apiWatch.ts @@ -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) { @@ -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 })