Skip to content

Commit

Permalink
Renaming error handler callback property
Browse files Browse the repository at this point in the history
  • Loading branch information
cesarParra committed Dec 6, 2024
1 parent f64dc23 commit 3edd88d
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 34 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -275,7 +275,7 @@ $effect(
throw new Error("test");
},
{
errorHandler: customErrorHandlerFn
onError: customErrorHandlerFn
}
);
```
Expand Down Expand Up @@ -304,7 +304,7 @@ $computed(
throw new Error("test");
},
{
errorHandler: customErrorHandlerFn
onError: customErrorHandlerFn
}
);
```
Expand Down
40 changes: 20 additions & 20 deletions force-app/lwc/signals/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
};
Expand All @@ -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
Expand All @@ -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()
};
/**
Expand All @@ -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
Expand All @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions src/lwc/signals/__tests__/computed.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ describe("computed values", () => {
$computed(() => {
throw new Error("test");
}, {
errorHandler: customErrorHandlerFn
onError: customErrorHandlerFn
});

expect(customErrorHandlerFn).toHaveBeenCalled();
Expand All @@ -128,7 +128,7 @@ describe("computed values", () => {
const computed = $computed(() => {
throw new Error("test");
}, {
errorHandler: customErrorHandlerFn
onError: customErrorHandlerFn
});

expect(computed.value).toBe("fallback");
Expand All @@ -147,7 +147,7 @@ describe("computed values", () => {

return signal.value;
}, {
errorHandler: customErrorHandlerFn
onError: customErrorHandlerFn
});

expect(computed.value).toBe(0);
Expand Down
2 changes: 1 addition & 1 deletion src/lwc/signals/__tests__/effect.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ describe("effects", () => {
$effect(() => {
throw new Error("test");
}, {
errorHandler: customErrorHandlerFn
onError: customErrorHandlerFn
});

expect(customErrorHandlerFn).toHaveBeenCalled();
Expand Down
14 changes: 7 additions & 7 deletions src/lwc/signals/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ interface EffectNode {
type EffectOptions = {
_fromComputed: boolean;
identifier: string | symbol;
errorHandler?: (error: unknown) => void;
onError?: (error: unknown) => void;
};

const defaultEffectOptions: EffectOptions = {
Expand Down Expand Up @@ -87,8 +87,8 @@ function $effect(fn: VoidFunction, options?: Partial<EffectOptions>): 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();
Expand Down Expand Up @@ -116,7 +116,7 @@ function handleEffectError(error: unknown, options: EffectOptions) {
type ComputedFunction<T> = () => T;
type ComputedOptions<T> = {
identifier: string | symbol;
errorHandler?: (
onError?: (
error: unknown,
previousValue: T | undefined
) => T | undefined;
Expand Down Expand Up @@ -156,14 +156,14 @@ function $computed<T>(
});
$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
Expand Down

0 comments on commit 3edd88d

Please sign in to comment.