UNPKG

10.4 kBTypeScriptView Raw
1import { Observable } from '../Observable';
2import { SchedulerLike } from '../types';
3declare type ConditionFunc<S> = (state: S) => boolean;
4declare type IterateFunc<S> = (state: S) => S;
5declare type ResultFunc<S, T> = (state: S) => T;
6export interface GenerateBaseOptions<S> {
7 /**
8 * Initial state.
9 */
10 initialState: S;
11 /**
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.
15 */
16 condition?: ConditionFunc<S>;
17 /**
18 * Iterate function that accepts state and returns new state.
19 */
20 iterate: IterateFunc<S>;
21 /**
22 * SchedulerLike to use for generation process.
23 * By default, a generator starts immediately.
24 */
25 scheduler?: SchedulerLike;
26}
27export interface GenerateOptions<T, S> extends GenerateBaseOptions<S> {
28 /**
29 * Result selection function that accepts state and returns a value to emit.
30 */
31 resultSelector: ResultFunc<S, T>;
32}
33/**
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.
37 *
38 * ![](generate.png)
39 *
40 * ## Examples
41 *
42 * Produces sequence of numbers
43 *
44 * ```ts
45 * import { generate } from 'rxjs';
46 *
47 * const result = generate(0, x => x < 3, x => x + 1, x => x);
48 *
49 * result.subscribe(x => console.log(x));
50 *
51 * // Logs:
52 * // 0
53 * // 1
54 * // 2
55 * ```
56 *
57 * Use `asapScheduler`
58 *
59 * ```ts
60 * import { generate, asapScheduler } from 'rxjs';
61 *
62 * const result = generate(1, x => x < 5, x => x * 2, x => x + 1, asapScheduler);
63 *
64 * result.subscribe(x => console.log(x));
65 *
66 * // Logs:
67 * // 2
68 * // 3
69 * // 5
70 * ```
71 *
72 * @see {@link from}
73 * @see {@link Observable}
74 *
75 * @param {S} initialState Initial state.
76 * @param {function (state: S): boolean} condition Condition to terminate generation (upon returning false).
77 * @param {function (state: S): S} iterate Iteration step function.
78 * @param {function (state: S): T} resultSelector Selector function for results produced in the sequence. (deprecated)
79 * @param {SchedulerLike} [scheduler] A {@link SchedulerLike} on which to run the generator loop. If not provided, defaults to emit immediately.
80 * @returns {Observable<T>} The generated sequence.
81 * @deprecated Instead of passing separate arguments, use the options argument. Signatures taking separate arguments will be removed in v8.
82 */
83export declare function generate<T, S>(initialState: S, condition: ConditionFunc<S>, iterate: IterateFunc<S>, resultSelector: ResultFunc<S, T>, scheduler?: SchedulerLike): Observable<T>;
84/**
85 * Generates an Observable by running a state-driven loop
86 * that emits an element on each iteration.
87 *
88 * <span class="informal">Use it instead of nexting values in a for loop.</span>
89 *
90 * ![](generate.png)
91 *
92 * `generate` allows you to create a stream of values generated with a loop very similar to
93 * a traditional for loop. The first argument of `generate` is a beginning value. The second argument
94 * is a function that accepts this value and tests if some condition still holds. If it does,
95 * then the loop continues, if not, it stops. The third value is a function which takes the
96 * previously defined value and modifies it in some way on each iteration. Note how these three parameters
97 * are direct equivalents of three expressions in a traditional for loop: the first expression
98 * initializes some state (for example, a numeric index), the second tests if the loop can perform the next
99 * iteration (for example, if the index is lower than 10) and the third states how the defined value
100 * will be modified on every step (for example, the index will be incremented by one).
101 *
102 * Return value of a `generate` operator is an Observable that on each loop iteration
103 * emits a value. First of all, the condition function is ran. If it returns true, then the Observable
104 * emits the currently stored value (initial value at the first iteration) and finally updates
105 * that value with iterate function. If at some point the condition returns false, then the Observable
106 * completes at that moment.
107 *
108 * Optionally you can pass a fourth parameter to `generate` - a result selector function which allows you
109 * to immediately map the value that would normally be emitted by an Observable.
110 *
111 * If you find three anonymous functions in `generate` call hard to read, you can provide
112 * a single object to the operator instead where the object has the properties: `initialState`,
113 * `condition`, `iterate` and `resultSelector`, which should have respective values that you
114 * would normally pass to `generate`. `resultSelector` is still optional, but that form
115 * of calling `generate` allows you to omit `condition` as well. If you omit it, that means
116 * condition always holds, or in other words the resulting Observable will never complete.
117 *
118 * Both forms of `generate` can optionally accept a scheduler. In case of a multi-parameter call,
119 * scheduler simply comes as a last argument (no matter if there is a `resultSelector`
120 * function or not). In case of a single-parameter call, you can provide it as a
121 * `scheduler` property on the object passed to the operator. In both cases, a scheduler decides when
122 * the next iteration of the loop will happen and therefore when the next value will be emitted
123 * by the Observable. For example, to ensure that each value is pushed to the Observer
124 * on a separate task in the event loop, you could use the `async` scheduler. Note that
125 * by default (when no scheduler is passed) values are simply emitted synchronously.
126 *
127 *
128 * ## Examples
129 *
130 * Use with condition and iterate functions
131 *
132 * ```ts
133 * import { generate } from 'rxjs';
134 *
135 * const result = generate(0, x => x < 3, x => x + 1);
136 *
137 * result.subscribe({
138 * next: value => console.log(value),
139 * complete: () => console.log('Complete!')
140 * });
141 *
142 * // Logs:
143 * // 0
144 * // 1
145 * // 2
146 * // 'Complete!'
147 * ```
148 *
149 * Use with condition, iterate and resultSelector functions
150 *
151 * ```ts
152 * import { generate } from 'rxjs';
153 *
154 * const result = generate(0, x => x < 3, x => x + 1, x => x * 1000);
155 *
156 * result.subscribe({
157 * next: value => console.log(value),
158 * complete: () => console.log('Complete!')
159 * });
160 *
161 * // Logs:
162 * // 0
163 * // 1000
164 * // 2000
165 * // 'Complete!'
166 * ```
167 *
168 * Use with options object
169 *
170 * ```ts
171 * import { generate } from 'rxjs';
172 *
173 * const result = generate({
174 * initialState: 0,
175 * condition(value) { return value < 3; },
176 * iterate(value) { return value + 1; },
177 * resultSelector(value) { return value * 1000; }
178 * });
179 *
180 * result.subscribe({
181 * next: value => console.log(value),
182 * complete: () => console.log('Complete!')
183 * });
184 *
185 * // Logs:
186 * // 0
187 * // 1000
188 * // 2000
189 * // 'Complete!'
190 * ```
191 *
192 * Use options object without condition function
193 *
194 * ```ts
195 * import { generate } from 'rxjs';
196 *
197 * const result = generate({
198 * initialState: 0,
199 * iterate(value) { return value + 1; },
200 * resultSelector(value) { return value * 1000; }
201 * });
202 *
203 * result.subscribe({
204 * next: value => console.log(value),
205 * complete: () => console.log('Complete!') // This will never run
206 * });
207 *
208 * // Logs:
209 * // 0
210 * // 1000
211 * // 2000
212 * // 3000
213 * // ...and never stops.
214 * ```
215 *
216 * @see {@link from}
217 *
218 * @param {S} initialState Initial state.
219 * @param {function (state: S): boolean} condition Condition to terminate generation (upon returning false).
220 * @param {function (state: S): S} iterate Iteration step function.
221 * @param {function (state: S): T} [resultSelector] Selector function for results produced in the sequence.
222 * @param {Scheduler} [scheduler] A {@link Scheduler} on which to run the generator loop. If not provided, defaults to emitting immediately.
223 * @return {Observable<T>} The generated sequence.
224 * @deprecated Instead of passing separate arguments, use the options argument. Signatures taking separate arguments will be removed in v8.
225 */
226export declare function generate<S>(initialState: S, condition: ConditionFunc<S>, iterate: IterateFunc<S>, scheduler?: SchedulerLike): Observable<S>;
227/**
228 * Generates an observable sequence by running a state-driven loop
229 * producing the sequence's elements, using the specified scheduler
230 * to send out observer messages.
231 * The overload accepts options object that might contain initial state, iterate,
232 * condition and scheduler.
233 *
234 * ![](generate.png)
235 *
236 * ## Examples
237 *
238 * Use options object with condition function
239 *
240 * ```ts
241 * import { generate } from 'rxjs';
242 *
243 * const result = generate({
244 * initialState: 0,
245 * condition: x => x < 3,
246 * iterate: x => x + 1
247 * });
248 *
249 * result.subscribe({
250 * next: value => console.log(value),
251 * complete: () => console.log('Complete!')
252 * });
253 *
254 * // Logs:
255 * // 0
256 * // 1
257 * // 2
258 * // 'Complete!'
259 * ```
260 *
261 * @see {@link from}
262 * @see {@link Observable}
263 *
264 * @param {GenerateBaseOptions<S>} options Object that must contain initialState, iterate and might contain condition and scheduler.
265 * @returns {Observable<S>} The generated sequence.
266 */
267export declare function generate<S>(options: GenerateBaseOptions<S>): Observable<S>;
268/**
269 * Generates an observable sequence by running a state-driven loop
270 * producing the sequence's elements, using the specified scheduler
271 * to send out observer messages.
272 * The overload accepts options object that might contain initial state, iterate,
273 * condition, result selector and scheduler.
274 *
275 * ![](generate.png)
276 *
277 * ## Examples
278 *
279 * Use options object with condition and iterate function
280 *
281 * ```ts
282 * import { generate } from 'rxjs';
283 *
284 * const result = generate({
285 * initialState: 0,
286 * condition: x => x < 3,
287 * iterate: x => x + 1,
288 * resultSelector: x => x
289 * });
290 *
291 * result.subscribe({
292 * next: value => console.log(value),
293 * complete: () => console.log('Complete!')
294 * });
295 *
296 * // Logs:
297 * // 0
298 * // 1
299 * // 2
300 * // 'Complete!'
301 * ```
302 *
303 * @see {@link from}
304 * @see {@link Observable}
305 *
306 * @param {GenerateOptions<T, S>} options Object that must contain initialState, iterate, resultSelector and might contain condition and scheduler.
307 * @returns {Observable<T>} The generated sequence.
308 */
309export declare function generate<T, S>(options: GenerateOptions<T, S>): Observable<T>;
310export {};
311//# sourceMappingURL=generate.d.ts.map
\No newline at end of file