diff --git a/asynciterator.ts b/asynciterator.ts index 38f5872..464b6a2 100644 --- a/asynciterator.ts +++ b/asynciterator.ts @@ -2136,6 +2136,20 @@ export class WrappingIterator extends AsyncIterator { } return null; }; + // Close an unfinished iterator, as `for...of` does on `break`; + // clear it first so its cleanup code cannot re-enter it + source.destroy = () => { + const unfinishedIterator = iterator; + iterator = null; + if (unfinishedIterator !== null && isFunction(unfinishedIterator.return)) { + try { + unfinishedIterator.return(); + } + catch (error) { + this.emit('error', error); + } + } + }; } // Process any other readable source else { @@ -2193,6 +2207,7 @@ export class WrappingIterator extends AsyncIterator { @function @param [source] The source this iterator generates items from @param {object} [options] Settings of the iterator + @param {boolean} [options.destroySource=true] Whether the source should be destroyed when this iterator is closed or destroyed; an unfinished sync iterator is closed with `return()` @returns {module:asynciterator.AsyncIterator} A new iterator with the items from the given iterator */ export function wrap(source?: MaybePromise> | null, diff --git a/test/WrappingIterator-test.js b/test/WrappingIterator-test.js index 3210989..7c4efb4 100644 --- a/test/WrappingIterator-test.js +++ b/test/WrappingIterator-test.js @@ -705,4 +705,71 @@ describe('wrap', () => { }); }); }); + + describe('with a generator source', () => { + let source; + beforeEach(() => { + source = (function * () { + yield 1; + yield 2; + }()); + sinon.spy(source, 'return'); + }); + + it('should return the generator when destroyed early', () => { + const iterator = wrap(source); + iterator.read(); + iterator.destroy(); + source.return.should.have.been.calledOnce; + }); + + it('should return the generator when closed early', done => { + const iterator = wrap(source); + iterator.read(); + iterator.on('end', () => { + source.return.should.have.been.calledOnce; + done(); + }); + iterator.close(); + }); + + it('should return the generator when destroyed before its promise resolves', async () => { + const sourcePromise = Promise.resolve(source); + wrap(sourcePromise).destroy(); + await sourcePromise; + source.return.should.have.been.calledOnce; + }); + + it('should return the generator when wrapped with buffering options and destroyed early', () => { + wrap(source, { maxBufferSize: 1 }).destroy(); + source.return.should.have.been.calledOnce; + }); + + it('should not return the generator when destroySource is false', () => { + const iterator = wrap(source, { destroySource: false }); + iterator.read(); + iterator.destroy(); + source.return.should.not.have.been.called; + }); + + it('should not return the generator when it ends by itself', async () => { + expect(await wrap(source).toArray()).to.deep.equal([1, 2]); + source.return.should.not.have.been.called; + }); + }); + + describe('with an iterator source whose return throws', () => { + it('should emit the error and still end when closed early', done => { + const returnError = new Error('return failed'); + const iterator = wrap({ next: () => ({ value: 1 }), return: () => { throw returnError; } }); + const emittedErrors = []; + iterator.read(); + iterator.on('error', error => emittedErrors.push(error)); + iterator.on('end', () => { + emittedErrors.should.deep.equal([returnError]); + done(); + }); + iterator.close(); + }); + }); });