import { Observable } from '../Observable'; import { Subscriber } from '../Subscriber'; import { identity } from '../util/identity'; import { SchedulerAction, SchedulerLike } from '../types'; import { isScheduler } from '../util/isScheduler'; export type ConditionFunc = (state: S) => boolean; export type IterateFunc = (state: S) => S; export type ResultFunc = (state: S) => T; interface SchedulerState { needIterate?: boolean; state: S; subscriber: Subscriber; condition?: ConditionFunc; iterate: IterateFunc; resultSelector: ResultFunc; } export interface GenerateBaseOptions { /** * Initial state. */ initialState: S; /** * Condition function that accepts state and returns boolean. * When it returns false, the generator stops. * If not specified, a generator never stops. */ condition?: ConditionFunc; /** * Iterate function that accepts state and returns new state. */ iterate: IterateFunc; /** * SchedulerLike to use for generation process. * By default, a generator starts immediately. */ scheduler?: SchedulerLike; } export interface GenerateOptions extends GenerateBaseOptions { /** * Result selection function that accepts state and returns a value to emit. */ resultSelector: ResultFunc; } /** * Generates an observable sequence by running a state-driven loop * producing the sequence's elements, using the specified scheduler * to send out observer messages. * * ![](generate.png) * * @example Produces sequence of 0, 1, 2, ... 9, then completes. * const res = generate(0, x => x < 10, x => x + 1, x => x); * * @example Using asap scheduler, produces sequence of 2, 3, 5, then completes. * const res = generate(1, x => x < 5, x => * 2, x => x + 1, asap); * * @see {@link from} * @see {@link Observable} * * @param {S} initialState Initial state. * @param {function (state: S): boolean} condition Condition to terminate generation (upon returning false). * @param {function (state: S): S} iterate Iteration step function. * @param {function (state: S): T} resultSelector Selector function for results produced in the sequence. * @param {SchedulerLike} [scheduler] A {@link SchedulerLike} on which to run the generator loop. If not provided, defaults to emit immediately. * @returns {Observable} The generated sequence. */ export function generate(initialState: S, condition: ConditionFunc, iterate: IterateFunc, resultSelector: ResultFunc, scheduler?: SchedulerLike): Observable; /** * Generates an observable sequence by running a state-driven loop * producing the sequence's elements, using the specified scheduler * to send out observer messages. * The overload uses state as an emitted value. * * ![](generate.png) * * @example Produces sequence of 0, 1, 2, ... 9, then completes. * const res = generate(0, x => x < 10, x => x + 1); * * @example Using asap scheduler, produces sequence of 1, 2, 4, then completes. * const res = generate(1, x => x < 5, x => x * 2, Rx.Scheduler.asap); * * @see {@link from} * @see {@link Observable} * * @param {S} initialState Initial state. * @param {function (state: S): boolean} condition Condition to terminate generation (upon returning false). * @param {function (state: S): S} iterate Iteration step function. * @param {SchedulerLike} [scheduler] A {@link SchedulerLike} on which to run the generator loop. If not provided, defaults to emit immediately. * @returns {Observable} The generated sequence. */ export function generate(initialState: S, condition: ConditionFunc, iterate: IterateFunc, scheduler?: SchedulerLike): Observable; /** * Generates an observable sequence by running a state-driven loop * producing the sequence's elements, using the specified scheduler * to send out observer messages. * The overload accepts options object that might contain initial state, iterate, * condition and scheduler. * * ![](generate.png) * * @example Produces sequence of 0, 1, 2, ... 9, then completes. * const res = generate({ * initialState: 0, * condition: x => x < 10, * iterate: x => x + 1, * }); * * @see {@link from} * @see {@link Observable} * * @param {GenerateBaseOptions} options Object that must contain initialState, iterate and might contain condition and scheduler. * @returns {Observable} The generated sequence. */ export function generate(options: GenerateBaseOptions): Observable; /** * Generates an observable sequence by running a state-driven loop * producing the sequence's elements, using the specified scheduler * to send out observer messages. * The overload accepts options object that might contain initial state, iterate, * condition, result selector and scheduler. * * ![](generate.png) * * @example Produces sequence of 0, 1, 2, ... 9, then completes. * const res = generate({ * initialState: 0, * condition: x => x < 10, * iterate: x => x + 1, * resultSelector: x => x, * }); * * @see {@link from} * @see {@link Observable} * * @param {GenerateOptions} options Object that must contain initialState, iterate, resultSelector and might contain condition and scheduler. * @returns {Observable} The generated sequence. */ export function generate(options: GenerateOptions): Observable; export function generate(initialStateOrOptions: S | GenerateOptions, condition?: ConditionFunc, iterate?: IterateFunc, resultSelectorOrObservable?: (ResultFunc) | SchedulerLike, scheduler?: SchedulerLike): Observable { let resultSelector: ResultFunc; let initialState: S; if (arguments.length == 1) { const options = initialStateOrOptions as GenerateOptions; initialState = options.initialState; condition = options.condition; iterate = options.iterate; resultSelector = options.resultSelector || identity as ResultFunc; scheduler = options.scheduler; } else if (resultSelectorOrObservable === undefined || isScheduler(resultSelectorOrObservable)) { initialState = initialStateOrOptions as S; resultSelector = identity as ResultFunc; scheduler = resultSelectorOrObservable as SchedulerLike; } else { initialState = initialStateOrOptions as S; resultSelector = resultSelectorOrObservable as ResultFunc; } return new Observable(subscriber => { let state = initialState; if (scheduler) { return scheduler.schedule>(dispatch, 0, { subscriber, iterate, condition, resultSelector, state }); } do { if (condition) { let conditionResult: boolean; try { conditionResult = condition(state); } catch (err) { subscriber.error(err); return undefined; } if (!conditionResult) { subscriber.complete(); break; } } let value: T; try { value = resultSelector(state); } catch (err) { subscriber.error(err); return undefined; } subscriber.next(value); if (subscriber.closed) { break; } try { state = iterate(state); } catch (err) { subscriber.error(err); return undefined; } } while (true); return undefined; }); } function dispatch(this: SchedulerAction>, state: SchedulerState) { const { subscriber, condition } = state; if (subscriber.closed) { return undefined; } if (state.needIterate) { try { state.state = state.iterate(state.state); } catch (err) { subscriber.error(err); return undefined; } } else { state.needIterate = true; } if (condition) { let conditionResult: boolean; try { conditionResult = condition(state.state); } catch (err) { subscriber.error(err); return undefined; } if (!conditionResult) { subscriber.complete(); return undefined; } if (subscriber.closed) { return undefined; } } let value: T; try { value = state.resultSelector(state.state); } catch (err) { subscriber.error(err); return undefined; } if (subscriber.closed) { return undefined; } subscriber.next(value); if (subscriber.closed) { return undefined; } return this.schedule(state); }