-
Notifications
You must be signed in to change notification settings - Fork 12
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
Add batchedEffect() #69
Add batchedEffect() #69
Conversation
* Runs a function inside a batch, and calls all the effected batched effects | ||
* synchronously. | ||
*/ | ||
export const batch = (fn: () => 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.
why const arrow instead of function batch() {}
?
(is this personal preference, or some perf/debugging thing I don't know about? 😅 )
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.
Personal preference - I just use arrow functions for everything possible.
I supposed there's a slight theoretical perf benefit from not having a mutable binding, if the VM correctly elides the TDZ check for this case. Maybe some bundlers take advantage of this too? There's also of course a theoretical perf hit from un-elided TDZ checks.
I just personally think that arrow functions are the correct function semantics, and use the function
to highlight cases where this
might be non-lexical.
* The effect also runs asynchronously, on the microtask queue, if any of the | ||
* signals it depends on have been updated outside of a `batch()` call. | ||
*/ | ||
export const batchedEffect = (effectFn: () => 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.
Oops, we should return an unsubscribe function here.
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.
@NullVoxPopuli fyi
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.
womp, want to throw up another PR? <3
export const batch = (fn: () => void) => { | ||
batchDepth++; | ||
try { | ||
// Run the function to notifiy watchers |
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.
typo: notify
This adds a
batchedEffect(cb)
utility that synchronously runs its callback when dependencies are updated inside abatch()
call.