Skip to content

Commit

Permalink
fix: consume iterable as many as call in takeUntil
Browse files Browse the repository at this point in the history
  • Loading branch information
ppeeou committed Dec 2, 2021
1 parent f061567 commit 04a1937
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/Lazy/takeUntil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ function asyncSequential<A, B>(
return this;
},
async next(_concurrent) {
if (end) {
return { done: true, value: undefined };
}

const { done, value } = await iterator.next(_concurrent);
if (done || end) {
return { done: true, value: undefined };
Expand Down
15 changes: 15 additions & 0 deletions test/Lazy/takeUntil.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
delay,
filter,
map,
peek,
pipe,
range,
takeUntil,
Expand Down Expand Up @@ -95,6 +96,20 @@ describe("takeUntil", function () {
expect(res).toEqual([12, 14]);
});

it("should be able to take the element", async function () {
const fn = jest.fn();
const res = await pipe(
toAsync([3, 3, 1, 0, 0]),
peek(fn),
map((a) => delay(100, a)),
takeUntil((a) => a < 3),
toArray,
);

expect(fn).toHaveBeenCalledTimes(3);
expect(res).toEqual([3, 3, 1]);
}, 350);

it("should be able to take the element until the callback result is truthy concurrently", async function () {
const res = await pipe(
toAsync(range(1, 20)),
Expand Down

0 comments on commit 04a1937

Please sign in to comment.