From 35cb4d4efd4bdf4d4905c70f33b366e0562d6fe4 Mon Sep 17 00:00:00 2001 From: William Wong Date: Sun, 13 Oct 2024 03:36:20 -0700 Subject: [PATCH] Fix for ESNext --- .../src/asyncGeneratorWithLastValue.spec.ts | 14 ++++++++++++++ .../iter-fest/src/asyncGeneratorWithLastValue.ts | 1 + .../iter-fest/src/generatorWithLastValue.spec.ts | 10 ++++++++++ packages/iter-fest/src/generatorWithLastValue.ts | 1 + 4 files changed, 26 insertions(+) diff --git a/packages/iter-fest/src/asyncGeneratorWithLastValue.spec.ts b/packages/iter-fest/src/asyncGeneratorWithLastValue.spec.ts index cbb1c8c..a477b33 100644 --- a/packages/iter-fest/src/asyncGeneratorWithLastValue.spec.ts +++ b/packages/iter-fest/src/asyncGeneratorWithLastValue.spec.ts @@ -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(generator)[symbolAsyncDispose]?.(); + + expect(dispose).toHaveBeenCalledTimes(1); +}); diff --git a/packages/iter-fest/src/asyncGeneratorWithLastValue.ts b/packages/iter-fest/src/asyncGeneratorWithLastValue.ts index 2475e0c..64b963b 100644 --- a/packages/iter-fest/src/asyncGeneratorWithLastValue.ts +++ b/packages/iter-fest/src/asyncGeneratorWithLastValue.ts @@ -16,6 +16,7 @@ export function asyncGeneratorWithLastValue { 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); +}); diff --git a/packages/iter-fest/src/generatorWithLastValue.ts b/packages/iter-fest/src/generatorWithLastValue.ts index ff9b53c..85226a3 100644 --- a/packages/iter-fest/src/generatorWithLastValue.ts +++ b/packages/iter-fest/src/generatorWithLastValue.ts @@ -12,6 +12,7 @@ export function generatorWithLastValue