1 | import { AsyncIterableX } from './asynciterablex';
|
2 | import { throwIfAborted } from '../aborterror';
|
3 | class GenerateAsyncIterable extends AsyncIterableX {
|
4 | constructor(initialState, condition, iterate, resultSelector) {
|
5 | super();
|
6 | this._initialState = initialState;
|
7 | this._condition = condition;
|
8 | this._iterate = iterate;
|
9 | this._resultSelector = resultSelector;
|
10 | }
|
11 | async *[Symbol.asyncIterator](signal) {
|
12 | throwIfAborted(signal);
|
13 | for (let i = this._initialState; await this._condition(i, signal); i = await this._iterate(i, signal)) {
|
14 | yield await this._resultSelector(i, signal);
|
15 | }
|
16 | }
|
17 | }
|
18 | /**
|
19 | * Generates an async-iterable sequence by running a state-driven loop producing the sequence's elements.
|
20 | *
|
21 | * @export
|
22 | * @template TState The type of the state used in the generator loop.
|
23 | * @template TResult The type of the elements in the produced sequence.
|
24 | * @param {TState} initialState The initial state.
|
25 | * @param {((value: TState, signal?: AbortSignal) => boolean | Promise<boolean>)} condition Condition to terminate generation (upon returning false).
|
26 | * @param {((value: TState, signal?: AbortSignal) => TState | Promise<TState>)} iterate Iteration step function.
|
27 | * @param {((value: TState, signal?: AbortSignal) => TResult | Promise<TResult>)} resultSelector Selector function for results produced in
|
28 | * the sequence.
|
29 | * @returns {AsyncIterableX<TResult>} The generated async-iterable sequence.
|
30 | */
|
31 | export function generate(initialState, condition, iterate, resultSelector) {
|
32 | return new GenerateAsyncIterable(initialState, condition, iterate, resultSelector);
|
33 | }
|
34 |
|
35 | //# sourceMappingURL=generate.mjs.map
|