-
Notifications
You must be signed in to change notification settings - Fork 3
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
Recurse #29
Conversation
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. Latest deployment of this branch, based on commit 5d99d3d:
|
56718fa
to
d1805d1
Compare
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.
In general, I'm not a big fan of complicating atomEffect
.
If I were to do it, I'd create a separate atomRecurseEffect
.
Having that said, it's up to you.
|
||
const initAtom = atom(null, (get, set, mounted: boolean) => { | ||
const ref = get(refAtom) | ||
ref.mounted = mounted | ||
if (mounted) { | ||
ref.set = set |
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.
This is a hack that I would avoid as much as possible. It's a valid hack for now though.
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.
There was simply no other way. After the first recursion, the effectFn and setter must be run synchronous so that it can recurse down the call stack. So we have to use set instead of setSelf.
Yeah, its a bit complicated now. This is why I added so many additional tests to back this up. |
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.
update readme
warnSpy.mockRestore() | ||
}) | ||
|
||
// FIXME: is there a way to disallow asynchronous infinite loops in cleanup? |
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.
@dai-shi I couldn't find a way to detect and prohibit async recursion in the cleanup function.
atomEffect((get, set) => {
get(watchedAtom)
return () => {
setTimeout(() => {
set.recurse(watchedAtom, increment)
}, 1000)
}
})
IMO, recursion in the cleanup function is counter-intuitive and should not be allowed. The problem I ran into is how, technically, to distinguish it from async recursion in the effect function.
atomEffect((get, set) => {
get(watchedAtom)
setTimeout(() => {
set.recurse(watchedAtom, increment)
}, 1000)
})
I was wondering if you had any ideas.
Thanks for implementing this so quickly! What's the typical cadence for releases? Excited to try it out :) |
I'll release it today. Sorry for the delay. I was waiting on an unrelated bug fix in jotai core to release, but its taking longer than expected. |
Oh, no worries, thanks for the update! If you do want to wait, that's okay too |
jotai-effect 0.4.0 is now released. I also recommend upgrading to jotai
2.6.3 for the latest bug fixes.
Thank you for your patience.
…On Wed, Jan 24, 2024 at 4:33 PM QWu4xYV ***@***.***> wrote:
Oh, no worries, thanks for the update! If you do want to wait, that's okay
too
—
Reply to this email directly, view it on GitHub
<#29 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ABGNG6LEY3CHOB6Y562M6BTYQGR53AVCNFSM6AAAAABCEXIARCVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTSMBZGE2TCOBTG4>
.
You are receiving this because you modified the open/close state.Message
ID: ***@***.***>
|
Works perfectly for my use case, thanks so much! |
Related Issue: #26
Related Discussion: #16
This PR introduces additional api that allows the developer to opt-in for recursion.
✅ Synchronous recursive loops
✅ Asynchronous recursive loops. Both task and microtask delay is supported.
❌ Synchronous recursive loops in callback are disallowed (with warning)