Repository navigation
Close wrapped sync iterators on early end - #111
jitsedesmet wants to merge 3 commits into
Conversation
WrappingIterator now calls return() on an unfinished sync iterator when it is closed or destroyed early, as for...of does on break, so generator finally blocks run. Respects destroySource: false.
Use isFunction like the other optional calls, and assert via return() spies in a single test suite, also covering the buffering options path.
A throwing return() no longer escapes _end, which left the wrapper unended and crashed the process on close().
| }; | ||
| // Close an unfinished iterator, as `for...of` does on `break`; | ||
| // clear it first so its cleanup code cannot re-enter it | ||
| source.destroy = () => { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
close() is already covered:
close()schedulesthis._end(), anddestroy()calls the samethis._end(true).WrappingIterator._end()callsthis._source.destroy()unlessdestroySourceis false, which runsreturn().- Tested in
should return the generator when closed early.
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
There was a problem hiding this comment.
Ah, I didn't see there also was a test for closing early. Should be fine then indeed.
Needed for: jitsedesmet/rdf-stores.js#1
wrap()never calledreturn()on a wrapped sync iterator, so closing or destroying the resulting AsyncIterator early skipped generatorfinallyblocks.Changes
WrappingIteratornow has adestroy()that callsreturn()on an unfinished iterator, asfor...ofdoes onbreak. It goes through the existingdestroySourcepath, sodestroySource: falsestill skips it.return(), so cleanup code cannot re-enter it.return()throws, the error is emitted as'error'and the wrapper still ends.wrap()JSDoc now documentsdestroySource.Tests
New
wrapsuites cover: destroy, close, destroy before a promised source resolves, the buffering-options path,destroySource: false, natural end, and a throwingreturn(). Coverage stays at 100%.Generated by Claude Code