diff --git a/packages/iter-fest/src/PushAsyncIterableIterator.spec.ts b/packages/iter-fest/src/PushAsyncIterableIterator.spec.ts index 44feced..5d07909 100644 --- a/packages/iter-fest/src/PushAsyncIterableIterator.spec.ts +++ b/packages/iter-fest/src/PushAsyncIterableIterator.spec.ts @@ -41,3 +41,13 @@ describe('comprehensive', () => { test('should completed the for-loop', () => expect(done).toHaveBeenCalledTimes(1)); }); }); + +test('push after close should close the iterable', async () => { + const iterable = new PushAsyncIterableIterator(); + + iterable.close(); + await expect(iterable.next()).resolves.toEqual({ done: true, value: undefined }); + + iterable.push(1); + await expect(iterable.next()).resolves.toEqual({ done: true, value: undefined }); +}); diff --git a/packages/iter-fest/src/PushAsyncIterableIterator.ts b/packages/iter-fest/src/PushAsyncIterableIterator.ts index 974abcd..652c5a7 100644 --- a/packages/iter-fest/src/PushAsyncIterableIterator.ts +++ b/packages/iter-fest/src/PushAsyncIterableIterator.ts @@ -3,6 +3,7 @@ import withResolvers from './private/withResolvers'; const CLOSE = Symbol('close'); export class PushAsyncIterableIterator implements AsyncIterableIterator { + #closed: boolean = false; #pushResolvers: PromiseWithResolvers = withResolvers(); [Symbol.asyncIterator]() { @@ -10,6 +11,7 @@ export class PushAsyncIterableIterator implements AsyncIterableIterator { } close() { + this.#closed = true; this.#pushResolvers.resolve(CLOSE); } @@ -24,7 +26,9 @@ export class PushAsyncIterableIterator implements AsyncIterableIterator { } push(value: T) { - this.#pushResolvers.resolve(value); - this.#pushResolvers = withResolvers(); + if (!this.#closed) { + this.#pushResolvers.resolve(value); + this.#pushResolvers = withResolvers(); + } } }