From db5c98a70c4894c57580f11efda9851f277e9fd8 Mon Sep 17 00:00:00 2001 From: Micah Zoltu Date: Thu, 14 Dec 2023 01:11:35 +0800 Subject: [PATCH 1/3] Changes void to unknown for callback definitions. A callback like `() => void` means that the caller must provide a function that is explicitly void returning. This means one cannot pass a function as a callback that may have a return value that is ignored. By changing the required return type to `unknown`, it allows the caller to provide either a void returning function, or a function that returns a value. This is especially valuable when you have callback functions like `() => condition && doThing()`. Such a function returns a boolean value, which makes it incompatible with `() => void` callback requirements. You can work around this by wrapping the body with curly braces (to throw away the result of the expression), but it is better to be permissive in what is accepted instead. --- packages/preact/src/index.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/preact/src/index.ts b/packages/preact/src/index.ts index caf2c525a..c7d214d3c 100644 --- a/packages/preact/src/index.ts +++ b/packages/preact/src/index.ts @@ -40,7 +40,7 @@ function hook(hookName: T, hookFn: HookFn) { } let currentComponent: AugmentedComponent | undefined; -let finishUpdate: (() => void) | undefined; +let finishUpdate: (() => unknown) | undefined; function setCurrentUpdater(updater?: Effect) { // end tracking for the current update: @@ -49,7 +49,7 @@ function setCurrentUpdater(updater?: Effect) { finishUpdate = updater && updater._start(); } -function createUpdater(update: () => void) { +function createUpdater(update: () => unknown) { let updater!: Effect; effect(function (this: Effect) { updater = this; @@ -356,7 +356,7 @@ export function useComputed(compute: () => T) { return useMemo(() => computed(() => $compute.current()), []); } -export function useSignalEffect(cb: () => void | (() => void)) { +export function useSignalEffect(cb: () => unknown | (() => unknown)) { const callback = useRef(cb); callback.current = cb; From d52dbb957f7318758a903747f4dc92fd7034b1cf Mon Sep 17 00:00:00 2001 From: Micah Zoltu Date: Thu, 14 Dec 2023 10:16:36 +0000 Subject: [PATCH 2/3] Ran pnpm changeset --- .changeset/mean-flies-grow.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/mean-flies-grow.md diff --git a/.changeset/mean-flies-grow.md b/.changeset/mean-flies-grow.md new file mode 100644 index 000000000..cef2089fd --- /dev/null +++ b/.changeset/mean-flies-grow.md @@ -0,0 +1,5 @@ +--- +"@preact/signals": patch +--- + +Made useSignalEffect more permissive in the functions it accepts. From 378c56c1874db8fe3ddf57625fdfd8ccf9c85f33 Mon Sep 17 00:00:00 2001 From: Micah Zoltu Date: Tue, 19 Dec 2023 15:17:45 +0800 Subject: [PATCH 3/3] Changed function passed to `effect` to be void returning. --- packages/preact/src/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/preact/src/index.ts b/packages/preact/src/index.ts index c7d214d3c..1f2efa00c 100644 --- a/packages/preact/src/index.ts +++ b/packages/preact/src/index.ts @@ -361,7 +361,7 @@ export function useSignalEffect(cb: () => unknown | (() => unknown)) { callback.current = cb; useEffect(() => { - return effect(() => callback.current()); + return effect(() => { callback.current() }); }, []); }