-
-
Notifications
You must be signed in to change notification settings - Fork 105
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 batch
function for batching writes
#298
Conversation
@ai not sure if I'm misunderstanding your question, but the PR description has a bunch of details on why this is an improvement over the existing |
Sorry, I was too tired on initial review. Thanks for explaining it shorter in next comment. Seems good for me. We can deprecate Let’s wait for @dkzlv point of view. |
We need to rebase this PR to |
@ai @russelldavis Yeah, we were talking about implementing transactions for a while now. Tbh, the implementation is very good, tests are as well, I don't see a reason not to merge this. Good job, thanks!
I'm not sure if I agree or disagree. const $post = createFetcherStore([$categoryId, $postId]); Initially I turned this array of dependencies into a single Now, 2 things to consider here:
So it's exactly what you point out that |
We can move
|
@dkzlv @ai thanks for the review! Totally agreed, Given that, and the reasons below, I'm actually having second thoughts about moving that to a separate package:
Let me know what you guys think.
I have a branch where I've done this. I'm working on some perf optimizations and fixing an issue that this original PR didn't address: making sure multiple writes to the same atom inside a batch only calls the listeners once. I'll submit a new PR when it's ready. |
Superseded by #306 |
The existing
batched
function has a few problems:batched
instead ofcomputed
).batched
, it still allows invalid/stale intermediate values to be observed viasubscribe
. For example, if I want to log both$foo
and$bar
, but only when$foo
changes (not when$bar
changes), my subscription to$foo
could see the invalid/stale intermediate value of$bar
when they change at the same time. To fix this, I'd have to do a setTimeout hack in my callback likebatched
does.For the reasons above, all state management libraries I know of that do manual batching do it on event production rather than event consumption. Examples:
This PR brings that same ability to nanostores with a new
batch
function. For all writes that happen inside the batch callback, notifications won't be sent until the callback completes. Example:If you batch your writes using
batch
,batched
is no longer necessary, so it might make sense to deprecatebatched
and encouragebatch
instead. Let me know what you think, I can update the PR with docs.The implementation is simple and minimal because there was already a
listenerQueue
that was doing implicit batching for writes happening inside listener callbacks. This only adds 22 bytes to the min bundle size and 18 bytes to the max size. Ifbatched
eventually gets removed, it will probably be a net reduction in bytes.