Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions asynciterator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2136,6 +2136,20 @@ export class WrappingIterator<T> extends AsyncIterator<T> {
}
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 = () => {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suspect we'll also want this behaviour on calling asynciterator's close().
So instead of implementing source.destroy, source._end() could also be implemented, which should cover both cases.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

close() is already covered:

I'd keep destroy over _end: WrappingIterator only ever calls destroy() on its source, in _end() and when a promised source resolves after the iterator is done. Nothing calls _end() on a source, so an adapter _end() would need new calls in both places, duplicating the existing destroy() handling, destroySource check included.

~ Claude

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I didn't see there also was a test for closing early. Should be fine then indeed.

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 {
Expand Down Expand Up @@ -2193,6 +2207,7 @@ export class WrappingIterator<T> extends AsyncIterator<T> {
@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<T>(source?: MaybePromise<IterableSource<T>> | null,
Expand Down
67 changes: 67 additions & 0 deletions test/WrappingIterator-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});
});