1 import { Observable } from '../Observable';
2 import { SchedulerLike } from '../types';
3 export declare type ConditionFunc<S> = (state: S) => boolean;
4 export declare type IterateFunc<S> = (state: S) => S;
5 export declare type ResultFunc<S, T> = (state: S) => T;
6 export interface GenerateBaseOptions<S> {
12 * Condition function that accepts state and returns boolean.
13 * When it returns false, the generator stops.
14 * If not specified, a generator never stops.
16 condition?: ConditionFunc<S>;
18 * Iterate function that accepts state and returns new state.
20 iterate: IterateFunc<S>;
22 * SchedulerLike to use for generation process.
23 * By default, a generator starts immediately.
25 scheduler?: SchedulerLike;
27 export interface GenerateOptions<T, S> extends GenerateBaseOptions<S> {
29 * Result selection function that accepts state and returns a value to emit.
31 resultSelector: ResultFunc<S, T>;
34 * Generates an observable sequence by running a state-driven loop
35 * producing the sequence's elements, using the specified scheduler
36 * to send out observer messages.
40 * @example <caption>Produces sequence of 0, 1, 2, ... 9, then completes.</caption>
41 * const res = generate(0, x => x < 10, x => x + 1, x => x);
43 * @example <caption>Using asap scheduler, produces sequence of 2, 3, 5, then completes.</caption>
44 * const res = generate(1, x => x < 5, x => x * 2, x => x + 1, asap);
47 * @see {@link Observable}
49 * @param {S} initialState Initial state.
50 * @param {function (state: S): boolean} condition Condition to terminate generation (upon returning false).
51 * @param {function (state: S): S} iterate Iteration step function.
52 * @param {function (state: S): T} resultSelector Selector function for results produced in the sequence. (deprecated)
53 * @param {SchedulerLike} [scheduler] A {@link SchedulerLike} on which to run the generator loop. If not provided, defaults to emit immediately.
54 * @returns {Observable<T>} The generated sequence.
56 export declare function generate<T, S>(initialState: S, condition: ConditionFunc<S>, iterate: IterateFunc<S>, resultSelector: ResultFunc<S, T>, scheduler?: SchedulerLike): Observable<T>;
58 * Generates an Observable by running a state-driven loop
59 * that emits an element on each iteration.
61 * <span class="informal">Use it instead of nexting values in a for loop.</span>
63 * <img src="./img/generate.png" width="100%">
65 * `generate` allows you to create stream of values generated with a loop very similar to
66 * traditional for loop. First argument of `generate` is a beginning value. Second argument
67 * is a function that accepts this value and tests if some condition still holds. If it does,
68 * loop continues, if not, it stops. Third value is a function which takes previously defined
69 * value and modifies it in some way on each iteration. Note how these three parameters
70 * are direct equivalents of three expressions in regular for loop: first expression
71 * initializes some state (for example numeric index), second tests if loop can make next
72 * iteration (for example if index is lower than 10) and third states how defined value
73 * will be modified on every step (index will be incremented by one).
75 * Return value of a `generate` operator is an Observable that on each loop iteration
76 * emits a value. First, condition function is ran. If it returned true, Observable
77 * emits currently stored value (initial value at the first iteration) and then updates
78 * that value with iterate function. If at some point condition returned false, Observable
79 * completes at that moment.
81 * Optionally you can pass fourth parameter to `generate` - a result selector function which allows you
82 * to immediately map value that would normally be emitted by an Observable.
84 * If you find three anonymous functions in `generate` call hard to read, you can provide
85 * single object to the operator instead. That object has properties: `initialState`,
86 * `condition`, `iterate` and `resultSelector`, which should have respective values that you
87 * would normally pass to `generate`. `resultSelector` is still optional, but that form
88 * of calling `generate` allows you to omit `condition` as well. If you omit it, that means
89 * condition always holds, so output Observable will never complete.
91 * Both forms of `generate` can optionally accept a scheduler. In case of multi-parameter call,
92 * scheduler simply comes as a last argument (no matter if there is resultSelector
93 * function or not). In case of single-parameter call, you can provide it as a
94 * `scheduler` property on object passed to the operator. In both cases scheduler decides when
95 * next iteration of the loop will happen and therefore when next value will be emitted
96 * by the Observable. For example to ensure that each value is pushed to the observer
97 * on separate task in event loop, you could use `async` scheduler. Note that
98 * by default (when no scheduler is passed) values are simply emitted synchronously.
101 * @example <caption>Use with condition and iterate functions.</caption>
102 * const generated = generate(0, x => x < 3, x => x + 1);
104 * generated.subscribe(
105 * value => console.log(value),
107 * () => console.log('Yo!')
117 * @example <caption>Use with condition, iterate and resultSelector functions.</caption>
118 * const generated = generate(0, x => x < 3, x => x + 1, x => x * 1000);
120 * generated.subscribe(
121 * value => console.log(value),
123 * () => console.log('Yo!')
133 * @example <caption>Use with options object.</caption>
134 * const generated = generate({
136 * condition(value) { return value < 3; },
137 * iterate(value) { return value + 1; },
138 * resultSelector(value) { return value * 1000; }
141 * generated.subscribe(
142 * value => console.log(value),
144 * () => console.log('Yo!')
153 * @example <caption>Use options object without condition function.</caption>
154 * const generated = generate({
156 * iterate(value) { return value + 1; },
157 * resultSelector(value) { return value * 1000; }
160 * generated.subscribe(
161 * value => console.log(value),
163 * () => console.log('Yo!') // This will never run.
171 * // ...and never stops.
175 * @see {@link index/Observable.create}
177 * @param {S} initialState Initial state.
178 * @param {function (state: S): boolean} condition Condition to terminate generation (upon returning false).
179 * @param {function (state: S): S} iterate Iteration step function.
180 * @param {function (state: S): T} [resultSelector] Selector function for results produced in the sequence.
181 * @param {Scheduler} [scheduler] A {@link Scheduler} on which to run the generator loop. If not provided, defaults to emitting immediately.
182 * @return {Observable<T>} The generated sequence.
184 export declare function generate<S>(initialState: S, condition: ConditionFunc<S>, iterate: IterateFunc<S>, scheduler?: SchedulerLike): Observable<S>;
186 * Generates an observable sequence by running a state-driven loop
187 * producing the sequence's elements, using the specified scheduler
188 * to send out observer messages.
189 * The overload accepts options object that might contain initial state, iterate,
190 * condition and scheduler.
194 * @example <caption>Produces sequence of 0, 1, 2, ... 9, then completes.</caption>
195 * const res = generate({
197 * condition: x => x < 10,
198 * iterate: x => x + 1,
202 * @see {@link Observable}
204 * @param {GenerateBaseOptions<S>} options Object that must contain initialState, iterate and might contain condition and scheduler.
205 * @returns {Observable<S>} The generated sequence.
207 export declare function generate<S>(options: GenerateBaseOptions<S>): Observable<S>;
209 * Generates an observable sequence by running a state-driven loop
210 * producing the sequence's elements, using the specified scheduler
211 * to send out observer messages.
212 * The overload accepts options object that might contain initial state, iterate,
213 * condition, result selector and scheduler.
217 * @example <caption>Produces sequence of 0, 1, 2, ... 9, then completes.</caption>
218 * const res = generate({
220 * condition: x => x < 10,
221 * iterate: x => x + 1,
222 * resultSelector: x => x,
226 * @see {@link Observable}
228 * @param {GenerateOptions<T, S>} options Object that must contain initialState, iterate, resultSelector and might contain condition and scheduler.
229 * @returns {Observable<T>} The generated sequence.
231 export declare function generate<T, S>(options: GenerateOptions<T, S>): Observable<T>;