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

Add batch utility #5

Merged
merged 2 commits into from
Aug 13, 2022
Merged

Add batch utility #5

merged 2 commits into from
Aug 13, 2022

Conversation

RobinMalfait
Copy link
Owner

No description provided.

@bjuppa
Copy link

bjuppa commented Sep 22, 2020

I like this direction @RobinMalfait ! The wait function is a great way to make intention clear in a program.

As described in the readme update, this eager() function processes batches of a fixed size, so I suggest actually calling it batch(), to clarify the grouped-processing behaviour. Does that make sense? Or would the batch name intuitively indicate some other collection-operation?

Then if we proceed to add more functions in this style, what I called eager() (in the draft in #4) could perhaps be named parallel() instead. That one would start processing a new next() as soon as any slot is free in the "buffer", not waiting for the full batch to complete.
And then add race() too, if we want a variant that mess with collection order (that will be a challenge to write some solid tests for 😄).

src/eager.ts Outdated
Comment on lines 8 to 25
// TODO: Do we need to deal with this?
if (isAsyncIterable(data) || data instanceof Promise) return data

return {
*[Symbol.iterator]() {
const buffer = []
const iterator = data[Symbol.iterator]()
let state = { more: true }

while (state.more) {
for (let i = 0; i < size; i++) {
const { value, done } = iterator.next()
if (done) state.more = false
if (!done) buffer.push(value)
}

yield* buffer.splice(0)
}
Copy link

Choose a reason for hiding this comment

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

Regarding async iterables here, I think there's a good case to let them through and process them in the same manner!

On line 19 (when pulling out next()), that would return a promise if data is asynciterable, all that's needed to do in that case is await it, before extracting value and done. Everything else could stay the same, I think... await on a non-promise would just resolve right away, am I right?

const program = pipe(
map(id => `/user/${id}`),
map<string, unknown>(path => fetch(path)),
eager(10),
Copy link

Choose a reason for hiding this comment

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

If this test is processing 10 items, what about testing in batches of 5 or 4, just to spice it up a bit? 😄

Copy link
Owner Author

Choose a reason for hiding this comment

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

Oh yes I agree. This allows me to implicitly test that it is doing the batching correctly!

@RobinMalfait RobinMalfait changed the base branch from master to main June 6, 2021 11:57
+ add shortcut `wait` function, which is a bit more ergonomic
@RobinMalfait
Copy link
Owner Author

knock knock is this thing still on?

Welp, it took a while, but let's get this thing merged!

@RobinMalfait RobinMalfait changed the title Add eager utility Add batch utility Aug 13, 2022
@RobinMalfait RobinMalfait merged commit bae1809 into main Aug 13, 2022
@RobinMalfait RobinMalfait deleted the add-eager branch August 13, 2022 17:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants