shelving
Version:
Toolkit for using data in JavaScript.
28 lines (27 loc) • 969 B
JavaScript
import { awaitDispose, isAsyncDisposable } from "../util/dispose.js";
import { Sequence } from "./Sequence.js";
/**
* Async iterable that pulls values from a source async iterable.
* - Can be used to turn an `AsyncIterator` into an `AsyncIterableIterator`
* - Can be used to ensure `throw()` and `return()` are always set on an `AsyncIterator`
*/
export class ThroughSequence extends Sequence {
source;
constructor(source) {
super();
this.source = source;
}
next(value) {
return this.source.next(value);
}
async return(value) {
return this.source.return ? this.source.return(value) : super.return(value);
}
throw(reason) {
return this.source.throw ? this.source.throw(reason) : super.throw(reason);
}
async [Symbol.asyncDispose]() {
await awaitDispose(isAsyncDisposable(this.source) ? this.source : undefined, // Stop the source.
super[Symbol.asyncDispose]());
}
}