LazySequenceclass

new LazySequence<T, R, N>(source: AsyncIterator<T, R, N>, start: StartCallback)
ParamType
sourceAsyncIterator<T, R, N>
Async iterator to pull values from. required
startStartCallback
Callback run when the first iterator starts; its returned stop callback runs when the last iterator finishes. required
Return
LazySequence<T, R, N>
Sequence of values that calls a StartCallback when it has iterators that are iterating, and calls the corresponding StopCallback when all iterators have finished.
PropertyType
.iteratorsnumber
Number of iterators currently registered as iterating. required readonly

Sequence of values that calls a StartCallback when it has iterators that are iterating, and calls the corresponding StopCallback when all iterators have finished.

  • The start callback runs lazily on the first active iterator and the returned stop callback runs when the last iterator finishes.

A ThroughSequence paired with a start/stop callback. The StartCallback runs when the first iterator begins iterating; the StopCallback it returns runs when the last iterator finishes. This is the sequence equivalent of an observable subscription — work happens only while something is listening.

Usage

Pass a source iterator and a start callback. The callback sets up the work and returns a teardown function:

ts
import { DeferredSequence, LazySequence } from "shelving/sequence";

const deferred = new DeferredSequence<number>();

const seq = new LazySequence(deferred, () => {
  const id = setInterval(() => deferred.resolve(Date.now()), 500);
  return () => clearInterval(id); // StopCallback — runs when the last iterator finishes.
});

for await (const ts of seq) {
  console.log(ts); // the interval runs only for the duration of this loop
}

Examples

const seq = new LazySequence(source, () => {
		const timer = setInterval(tick, 1000);
		return () => clearInterval(timer); // Stop callback.
	});

Methods

Go

LazySequence.start()method

An iterator started iterating.

start(iterator: LazyIterator<T, R, N>): void
Go

LazySequence.stop()method

An iterator stopped iterating.

stop(iterator: LazyIterator<T, R, N>): void