Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changes void to unknown for callback definitions. #462

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/mean-flies-grow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@preact/signals": patch
---

Made useSignalEffect more permissive in the functions it accepts.
8 changes: 4 additions & 4 deletions packages/preact/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ function hook<T extends OptionsTypes>(hookName: T, hookFn: HookFn<T>) {
}

let currentComponent: AugmentedComponent | undefined;
let finishUpdate: (() => void) | undefined;
let finishUpdate: (() => unknown) | undefined;

function setCurrentUpdater(updater?: Effect) {
// end tracking for the current update:
Expand All @@ -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;
Expand Down Expand Up @@ -356,12 +356,12 @@ export function useComputed<T>(compute: () => T) {
return useMemo(() => computed<T>(() => $compute.current()), []);
}

export function useSignalEffect(cb: () => void | (() => void)) {
export function useSignalEffect(cb: () => unknown | (() => unknown)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the type of the callback passed to effect? Should this type match that since this callback is invoked inside of an effect and its return value is passed to effect? IIRC, effect accepts a clean up function

Copy link
Author

@MicahZoltu MicahZoltu Dec 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, good point. We could make the change from

useEffect(() => {
	return effect(() => callback.current());
}, []);

to

useEffect(() => {
	return effect(() => { callback.current() });
}, []);

This would ensure that effect is getting a void returning function. Alternatively, we could change the definition of effect, but I think that is in a different repository?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I went ahead and made the above change to this PR, can revert if another solution is desired.

const callback = useRef(cb);
callback.current = cb;

useEffect(() => {
return effect(() => callback.current());
return effect(() => { callback.current() });
}, []);
}

Expand Down