diff --git a/README.md b/README.md index 4ee01b7..8c6b25a 100644 --- a/README.md +++ b/README.md @@ -250,7 +250,7 @@ In this example, the test-identifier string will appear as part of the console.e ### Custom Error Handlers -Both computed and effect signals can receive a custom `errorHandler` property, +Both computed and effect signals can receive a custom `onError` property, that allows developers to completely override the default functionality that logs and rethrows the error. #### Effect handlers @@ -275,7 +275,7 @@ $effect( throw new Error("test"); }, { - errorHandler: customErrorHandlerFn + onError: customErrorHandlerFn } ); ``` @@ -304,7 +304,7 @@ $computed( throw new Error("test"); }, { - errorHandler: customErrorHandlerFn + onError: customErrorHandlerFn } ); ``` diff --git a/force-app/lwc/signals/core.js b/force-app/lwc/signals/core.js index 66c752b..b07055a 100644 --- a/force-app/lwc/signals/core.js +++ b/force-app/lwc/signals/core.js @@ -10,7 +10,7 @@ const UNSET = Symbol("UNSET"); const COMPUTING = Symbol("COMPUTING"); const ERRORED = Symbol("ERRORED"); const READY = Symbol("READY"); -const defaultEffectProps = { +const defaultEffectOptions = { _fromComputed: false, identifier: Symbol() }; @@ -32,10 +32,10 @@ const defaultEffectProps = { * ``` * * @param fn The function to execute - * @param props Options to configure the effect + * @param options Options to configure the effect */ -function $effect(fn, props) { - const _props = { ...defaultEffectProps, ...props }; +function $effect(fn, options) { + const _optionsWithDefaults = { ...defaultEffectOptions, ...options }; const effectNode = { error: null, state: UNSET @@ -53,28 +53,28 @@ function $effect(fn, props) { } catch (error) { effectNode.state = ERRORED; effectNode.error = error; - _props.errorHandler - ? _props.errorHandler(error) - : handleEffectError(error, _props); + _optionsWithDefaults.onError + ? _optionsWithDefaults.onError(error) + : handleEffectError(error, _optionsWithDefaults); } finally { context.pop(); } }; execute(); return { - identifier: _props.identifier + identifier: _optionsWithDefaults.identifier }; } -function handleEffectError(error, props) { +function handleEffectError(error, options) { const errorTemplate = ` LWC Signals: An error occurred in a reactive function \n - Type: ${props._fromComputed ? "Computed" : "Effect"} \n - Identifier: ${props.identifier.toString()} + Type: ${options._fromComputed ? "Computed" : "Effect"} \n + Identifier: ${options.identifier.toString()} `.trim(); console.error(errorTemplate, error); throw error; } -const defaultComputedProps = { +const defaultComputedOptions = { identifier: Symbol() }; /** @@ -91,23 +91,23 @@ const defaultComputedProps = { * ``` * * @param fn The function that returns the computed value. - * @param props Options to configure the computed value. + * @param options Options to configure the computed value. */ -function $computed(fn, props) { - const _props = { ...defaultComputedProps, ...props }; +function $computed(fn, options) { + const _optionsWithDefaults = { ...defaultComputedOptions, ...options }; const computedSignal = $signal(undefined, { track: true }); $effect( () => { - if (props?.errorHandler) { - // If this computed has a custom errorHandler, then error + if (options?.onError) { + // If this computed has a custom error handler, then the // handling occurs in the computed function itself. try { computedSignal.value = fn(); } catch (error) { const previousValue = computedSignal.peek(); - computedSignal.value = props.errorHandler(error, previousValue); + computedSignal.value = options.onError(error, previousValue); } } else { // Otherwise, the error handling is done in the $effect @@ -116,11 +116,11 @@ function $computed(fn, props) { }, { _fromComputed: true, - identifier: _props.identifier + identifier: _optionsWithDefaults.identifier } ); const returnValue = computedSignal.readOnly; - returnValue.identifier = _props.identifier; + returnValue.identifier = _optionsWithDefaults.identifier; return returnValue; } class UntrackedState { diff --git a/src/lwc/signals/__tests__/computed.test.ts b/src/lwc/signals/__tests__/computed.test.ts index 8208fb7..9c39be0 100644 --- a/src/lwc/signals/__tests__/computed.test.ts +++ b/src/lwc/signals/__tests__/computed.test.ts @@ -114,7 +114,7 @@ describe("computed values", () => { $computed(() => { throw new Error("test"); }, { - errorHandler: customErrorHandlerFn + onError: customErrorHandlerFn }); expect(customErrorHandlerFn).toHaveBeenCalled(); @@ -128,7 +128,7 @@ describe("computed values", () => { const computed = $computed(() => { throw new Error("test"); }, { - errorHandler: customErrorHandlerFn + onError: customErrorHandlerFn }); expect(computed.value).toBe("fallback"); @@ -147,7 +147,7 @@ describe("computed values", () => { return signal.value; }, { - errorHandler: customErrorHandlerFn + onError: customErrorHandlerFn }); expect(computed.value).toBe(0); diff --git a/src/lwc/signals/__tests__/effect.test.ts b/src/lwc/signals/__tests__/effect.test.ts index e3ed483..3432f15 100644 --- a/src/lwc/signals/__tests__/effect.test.ts +++ b/src/lwc/signals/__tests__/effect.test.ts @@ -115,7 +115,7 @@ describe("effects", () => { $effect(() => { throw new Error("test"); }, { - errorHandler: customErrorHandlerFn + onError: customErrorHandlerFn }); expect(customErrorHandlerFn).toHaveBeenCalled(); diff --git a/src/lwc/signals/core.ts b/src/lwc/signals/core.ts index c1ff6dd..ecbfe0b 100644 --- a/src/lwc/signals/core.ts +++ b/src/lwc/signals/core.ts @@ -38,7 +38,7 @@ interface EffectNode { type EffectOptions = { _fromComputed: boolean; identifier: string | symbol; - errorHandler?: (error: unknown) => void; + onError?: (error: unknown) => void; }; const defaultEffectOptions: EffectOptions = { @@ -87,8 +87,8 @@ function $effect(fn: VoidFunction, options?: Partial): Effect { } catch (error) { effectNode.state = ERRORED; effectNode.error = error; - _optionsWithDefaults.errorHandler - ? _optionsWithDefaults.errorHandler(error) + _optionsWithDefaults.onError + ? _optionsWithDefaults.onError(error) : handleEffectError(error, _optionsWithDefaults); } finally { context.pop(); @@ -116,7 +116,7 @@ function handleEffectError(error: unknown, options: EffectOptions) { type ComputedFunction = () => T; type ComputedOptions = { identifier: string | symbol; - errorHandler?: ( + onError?: ( error: unknown, previousValue: T | undefined ) => T | undefined; @@ -156,14 +156,14 @@ function $computed( }); $effect( () => { - if (options?.errorHandler) { - // If this computed has a custom errorHandler, then error + if (options?.onError) { + // If this computed has a custom error handler, then the // handling occurs in the computed function itself. try { computedSignal.value = fn(); } catch (error) { const previousValue = computedSignal.peek(); - computedSignal.value = options.errorHandler(error, previousValue); + computedSignal.value = options.onError(error, previousValue); } } else { // Otherwise, the error handling is done in the $effect