-
-
Notifications
You must be signed in to change notification settings - Fork 99
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
Proposal: use signal effect #91
Conversation
🦋 Changeset detectedLatest commit: 183000d The changes in this PR will be included in the next version bump. This PR includes changesets to release 2 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
✅ Deploy Preview for preact-signals-demo ready!
To edit notification comments on pull requests, go to your Netlify site settings. |
Size Change: +431 B (+1%) Total Size: 67.9 kB
ℹ️ View Unchanged
|
packages/preact/src/index.ts
Outdated
@@ -283,6 +283,15 @@ export function useComputed<T>(compute: () => T) { | |||
return useMemo(() => computed<T>(() => $compute.current()), []); | |||
} | |||
|
|||
export function useSignalEffect(cb: () => void) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's add this hook to the react adapter too 👍
Co-authored-by: Jason Miller <developit@users.noreply.github.com>
af1e4df
to
d13a7aa
Compare
Currently running effects is a bit of a pitfall imho, or we run them globally outside of the function-scope, or we have to wrap them with a
useMemo
/useEffect
/... This lead me on the path of creating a small utility function that abstracts this. When people want to use a globalsignal
/effect
they can import from thecore
package, however when they want a component-specific signal they leverageuseSignal
. It's here where I saw a discrepancy, we have a utility to help provide a signal but no utility for the effect.Here comes the introduction of
useSignalEffect
it basically wraps aneffect
withuseEffect
and holds a stable ref to the callback being passed to theuseSignalEffect
so we don't have to bother adding dependencies to the array of the useduseEffect
. Currently the only short-coming I may be foreseeing is the lack of anonInvalidate
function, i.e. what happens if an effect updates that is for example still fetching, we could be introducing a race-condition there.Usage: