@@ -1998,9 +1998,11 @@ class HistoryReader<T> {
19981998 */
19991999export class WrappingIterator < T > extends AsyncIterator < T > {
20002000 protected _source : InternalSource < T > | null = null ;
2001+ protected _destroySource : boolean ;
20012002
2002- constructor ( source ?: MaybePromise < IterableSource < T > > ) {
2003+ constructor ( source ?: MaybePromise < IterableSource < T > > , opts ?: SourcedIteratorOptions ) {
20032004 super ( ) ;
2005+ this . _destroySource = opts ?. destroySource !== false ;
20042006
20052007 // If promise, set up a temporary source and replace when ready
20062008 if ( isPromise ( source ) ) {
@@ -2018,9 +2020,6 @@ export class WrappingIterator<T> extends AsyncIterator<T> {
20182020
20192021 set source ( value : IterableSource < T > ) {
20202022 let source : InternalSource < T > = value as any ;
2021- // Do not change sources if the iterator is already done
2022- if ( this . done )
2023- return ;
20242023 if ( this . _source !== null )
20252024 throw new Error ( 'The source cannot be changed after it has been set' ) ;
20262025
@@ -2051,6 +2050,13 @@ export class WrappingIterator<T> extends AsyncIterator<T> {
20512050 source = ensureSourceAvailable ( source ) ;
20522051 }
20532052
2053+ // Do not change sources if the iterator is already done
2054+ if ( this . done ) {
2055+ if ( this . _destroySource && isFunction ( source . destroy ) )
2056+ source . destroy ( ) ;
2057+ return ;
2058+ }
2059+
20542060 // Set up event handling
20552061 source [ DESTINATION ] = this ;
20562062 source . on ( 'end' , destinationClose ) ;
@@ -2073,15 +2079,17 @@ export class WrappingIterator<T> extends AsyncIterator<T> {
20732079 }
20742080
20752081 protected _end ( destroy : boolean = false ) {
2076- super . _end ( destroy ) ;
2077- // Clean up event handlers
20782082 if ( this . _source !== null ) {
20792083 this . _source . removeListener ( 'end' , destinationClose ) ;
20802084 this . _source . removeListener ( 'error' , destinationEmitError ) ;
20812085 this . _source . removeListener ( 'readable' , destinationSetReadable ) ;
20822086 delete this . _source [ DESTINATION ] ;
2087+
2088+ if ( this . _destroySource && isFunction ( this . _source . destroy ) )
2089+ this . _source . destroy ( ) ;
20832090 this . _source = null ;
20842091 }
2092+ super . _end ( destroy ) ;
20852093 }
20862094}
20872095
@@ -2098,7 +2106,7 @@ export class WrappingIterator<T> extends AsyncIterator<T> {
20982106export function wrap < T > ( source ?: MaybePromise < IterableSource < T > > | null ,
20992107 options ?: TransformIteratorOptions < T > ) : AsyncIterator < T > {
21002108 // TransformIterator if TransformIteratorOptions were specified
2101- if ( options )
2109+ if ( options && ( 'autoStart' in options || 'optional' in options || 'source' in options || 'maxBufferSize' in options ) )
21022110 return new TransformIterator < T > ( source as MaybePromise < AsyncIterator < T > > , options ) ;
21032111
21042112 // Empty iterator if no source specified
@@ -2107,7 +2115,7 @@ export function wrap<T>(source?: MaybePromise<IterableSource<T>> | null,
21072115
21082116 // Unwrap promised sources
21092117 if ( isPromise < T > ( source ) )
2110- return new WrappingIterator ( source ) ;
2118+ return new WrappingIterator ( source , options ) ;
21112119
21122120 // Directly return any AsyncIterator
21132121 if ( source instanceof AsyncIterator )
@@ -2117,7 +2125,7 @@ export function wrap<T>(source?: MaybePromise<IterableSource<T>> | null,
21172125 if ( Array . isArray ( source ) )
21182126 return fromArray < T > ( source ) ;
21192127 if ( isIterable ( source ) || isIterator ( source ) || isEventEmitter ( source ) )
2120- return new WrappingIterator < T > ( source ) ;
2128+ return new WrappingIterator < T > ( source , options ) ;
21212129
21222130 // Other types are unsupported
21232131 throw new TypeError ( `Invalid source: ${ source } ` ) ;
0 commit comments