Skip to content

Commit

Permalink
Fix for ESNext
Browse files Browse the repository at this point in the history
  • Loading branch information
compulim committed Oct 13, 2024
1 parent 8492ed7 commit 35cb4d4
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 0 deletions.
14 changes: 14 additions & 0 deletions packages/iter-fest/src/asyncGeneratorWithLastValue.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,3 +191,17 @@ test('return in try-finally', async () => {
await expect(generator.next()).resolves.toEqual({ done: true, value: undefined });
expect(generator.lastValue()).toEqual(undefined);
});

test('passthrough asyncDispose', async () => {
const dispose = jest.fn();
const symbolAsyncDispose: typeof Symbol.asyncDispose = Symbol.asyncDispose || Symbol.for('Symbol.asyncDispose');

const generator = (async function* () {})();

// Currently, in Node.js 22.9.0, `Generator` does not have `asyncDispose` yet. We are ponyfilling it in.
generator[symbolAsyncDispose] || (generator[symbolAsyncDispose] = dispose);

await asyncGeneratorWithLastValue<number, void, void>(generator)[symbolAsyncDispose]?.();

expect(dispose).toHaveBeenCalledTimes(1);
});
1 change: 1 addition & 0 deletions packages/iter-fest/src/asyncGeneratorWithLastValue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export function asyncGeneratorWithLastValue<T = unknown, TReturn = any, TNext =
let lastValue: typeof STILL_ITERATING | TReturn = STILL_ITERATING;

const asyncGeneratorWithLastValue = {
...generator,
[Symbol.asyncIterator]() {
return asyncGeneratorWithLastValue;
},
Expand Down
10 changes: 10 additions & 0 deletions packages/iter-fest/src/generatorWithLastValue.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,3 +162,13 @@ test('return in try-finally', () => {
expect(generator.next()).toEqual({ done: true, value: undefined });
expect(generator.lastValue()).toEqual(undefined);
});

test('passthrough map', () => {
const generator = (function* () {
yield 1;
yield 2;
yield 3;
})();

expect(generator.reduce((sum, value) => sum + value, 0)).toBe(6);
});
1 change: 1 addition & 0 deletions packages/iter-fest/src/generatorWithLastValue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export function generatorWithLastValue<T = unknown, TReturn = any, TNext = unkno
let lastValue: typeof STILL_ITERATING | TReturn = STILL_ITERATING;

const generatorWithLastValue = {
...generator,
[Symbol.iterator]() {
return generatorWithLastValue;
},
Expand Down

0 comments on commit 35cb4d4

Please sign in to comment.