[6a3a178] | 1 | import { Observable } from '../Observable';
|
---|
| 2 | import { Subscriber } from '../Subscriber';
|
---|
| 3 | import { identity } from '../util/identity';
|
---|
| 4 | import { SchedulerAction, SchedulerLike } from '../types';
|
---|
| 5 | import { isScheduler } from '../util/isScheduler';
|
---|
| 6 |
|
---|
| 7 | export type ConditionFunc<S> = (state: S) => boolean;
|
---|
| 8 | export type IterateFunc<S> = (state: S) => S;
|
---|
| 9 | export type ResultFunc<S, T> = (state: S) => T;
|
---|
| 10 |
|
---|
| 11 | interface SchedulerState<T, S> {
|
---|
| 12 | needIterate?: boolean;
|
---|
| 13 | state: S;
|
---|
| 14 | subscriber: Subscriber<T>;
|
---|
| 15 | condition?: ConditionFunc<S>;
|
---|
| 16 | iterate: IterateFunc<S>;
|
---|
| 17 | resultSelector: ResultFunc<S, T>;
|
---|
| 18 | }
|
---|
| 19 |
|
---|
| 20 | export interface GenerateBaseOptions<S> {
|
---|
| 21 | /**
|
---|
| 22 | * Initial state.
|
---|
| 23 | */
|
---|
| 24 | initialState: S;
|
---|
| 25 | /**
|
---|
| 26 | * Condition function that accepts state and returns boolean.
|
---|
| 27 | * When it returns false, the generator stops.
|
---|
| 28 | * If not specified, a generator never stops.
|
---|
| 29 | */
|
---|
| 30 | condition?: ConditionFunc<S>;
|
---|
| 31 | /**
|
---|
| 32 | * Iterate function that accepts state and returns new state.
|
---|
| 33 | */
|
---|
| 34 | iterate: IterateFunc<S>;
|
---|
| 35 | /**
|
---|
| 36 | * SchedulerLike to use for generation process.
|
---|
| 37 | * By default, a generator starts immediately.
|
---|
| 38 | */
|
---|
| 39 | scheduler?: SchedulerLike;
|
---|
| 40 | }
|
---|
| 41 |
|
---|
| 42 | export interface GenerateOptions<T, S> extends GenerateBaseOptions<S> {
|
---|
| 43 | /**
|
---|
| 44 | * Result selection function that accepts state and returns a value to emit.
|
---|
| 45 | */
|
---|
| 46 | resultSelector: ResultFunc<S, T>;
|
---|
| 47 | }
|
---|
| 48 |
|
---|
| 49 | /**
|
---|
| 50 | * Generates an observable sequence by running a state-driven loop
|
---|
| 51 | * producing the sequence's elements, using the specified scheduler
|
---|
| 52 | * to send out observer messages.
|
---|
| 53 | *
|
---|
| 54 | * ![](generate.png)
|
---|
| 55 | *
|
---|
| 56 | * @example <caption>Produces sequence of 0, 1, 2, ... 9, then completes.</caption>
|
---|
| 57 | * const res = generate(0, x => x < 10, x => x + 1, x => x);
|
---|
| 58 | *
|
---|
| 59 | * @example <caption>Using asap scheduler, produces sequence of 2, 3, 5, then completes.</caption>
|
---|
| 60 | * const res = generate(1, x => x < 5, x => x * 2, x => x + 1, asap);
|
---|
| 61 | *
|
---|
| 62 | * @see {@link from}
|
---|
| 63 | * @see {@link Observable}
|
---|
| 64 | *
|
---|
| 65 | * @param {S} initialState Initial state.
|
---|
| 66 | * @param {function (state: S): boolean} condition Condition to terminate generation (upon returning false).
|
---|
| 67 | * @param {function (state: S): S} iterate Iteration step function.
|
---|
| 68 | * @param {function (state: S): T} resultSelector Selector function for results produced in the sequence. (deprecated)
|
---|
| 69 | * @param {SchedulerLike} [scheduler] A {@link SchedulerLike} on which to run the generator loop. If not provided, defaults to emit immediately.
|
---|
| 70 | * @returns {Observable<T>} The generated sequence.
|
---|
| 71 | */
|
---|
| 72 | export function generate<T, S>(initialState: S,
|
---|
| 73 | condition: ConditionFunc<S>,
|
---|
| 74 | iterate: IterateFunc<S>,
|
---|
| 75 | resultSelector: ResultFunc<S, T>,
|
---|
| 76 | scheduler?: SchedulerLike): Observable<T>;
|
---|
| 77 |
|
---|
| 78 | /**
|
---|
| 79 | * Generates an Observable by running a state-driven loop
|
---|
| 80 | * that emits an element on each iteration.
|
---|
| 81 | *
|
---|
| 82 | * <span class="informal">Use it instead of nexting values in a for loop.</span>
|
---|
| 83 | *
|
---|
| 84 | * <img src="./img/generate.png" width="100%">
|
---|
| 85 | *
|
---|
| 86 | * `generate` allows you to create stream of values generated with a loop very similar to
|
---|
| 87 | * traditional for loop. First argument of `generate` is a beginning value. Second argument
|
---|
| 88 | * is a function that accepts this value and tests if some condition still holds. If it does,
|
---|
| 89 | * loop continues, if not, it stops. Third value is a function which takes previously defined
|
---|
| 90 | * value and modifies it in some way on each iteration. Note how these three parameters
|
---|
| 91 | * are direct equivalents of three expressions in regular for loop: first expression
|
---|
| 92 | * initializes some state (for example numeric index), second tests if loop can make next
|
---|
| 93 | * iteration (for example if index is lower than 10) and third states how defined value
|
---|
| 94 | * will be modified on every step (index will be incremented by one).
|
---|
| 95 | *
|
---|
| 96 | * Return value of a `generate` operator is an Observable that on each loop iteration
|
---|
| 97 | * emits a value. First, condition function is ran. If it returned true, Observable
|
---|
| 98 | * emits currently stored value (initial value at the first iteration) and then updates
|
---|
| 99 | * that value with iterate function. If at some point condition returned false, Observable
|
---|
| 100 | * completes at that moment.
|
---|
| 101 | *
|
---|
| 102 | * Optionally you can pass fourth parameter to `generate` - a result selector function which allows you
|
---|
| 103 | * to immediately map value that would normally be emitted by an Observable.
|
---|
| 104 | *
|
---|
| 105 | * If you find three anonymous functions in `generate` call hard to read, you can provide
|
---|
| 106 | * single object to the operator instead. That object has properties: `initialState`,
|
---|
| 107 | * `condition`, `iterate` and `resultSelector`, which should have respective values that you
|
---|
| 108 | * would normally pass to `generate`. `resultSelector` is still optional, but that form
|
---|
| 109 | * of calling `generate` allows you to omit `condition` as well. If you omit it, that means
|
---|
| 110 | * condition always holds, so output Observable will never complete.
|
---|
| 111 | *
|
---|
| 112 | * Both forms of `generate` can optionally accept a scheduler. In case of multi-parameter call,
|
---|
| 113 | * scheduler simply comes as a last argument (no matter if there is resultSelector
|
---|
| 114 | * function or not). In case of single-parameter call, you can provide it as a
|
---|
| 115 | * `scheduler` property on object passed to the operator. In both cases scheduler decides when
|
---|
| 116 | * next iteration of the loop will happen and therefore when next value will be emitted
|
---|
| 117 | * by the Observable. For example to ensure that each value is pushed to the observer
|
---|
| 118 | * on separate task in event loop, you could use `async` scheduler. Note that
|
---|
| 119 | * by default (when no scheduler is passed) values are simply emitted synchronously.
|
---|
| 120 | *
|
---|
| 121 | *
|
---|
| 122 | * @example <caption>Use with condition and iterate functions.</caption>
|
---|
| 123 | * const generated = generate(0, x => x < 3, x => x + 1);
|
---|
| 124 | *
|
---|
| 125 | * generated.subscribe(
|
---|
| 126 | * value => console.log(value),
|
---|
| 127 | * err => {},
|
---|
| 128 | * () => console.log('Yo!')
|
---|
| 129 | * );
|
---|
| 130 | *
|
---|
| 131 | * // Logs:
|
---|
| 132 | * // 0
|
---|
| 133 | * // 1
|
---|
| 134 | * // 2
|
---|
| 135 | * // "Yo!"
|
---|
| 136 | *
|
---|
| 137 | *
|
---|
| 138 | * @example <caption>Use with condition, iterate and resultSelector functions.</caption>
|
---|
| 139 | * const generated = generate(0, x => x < 3, x => x + 1, x => x * 1000);
|
---|
| 140 | *
|
---|
| 141 | * generated.subscribe(
|
---|
| 142 | * value => console.log(value),
|
---|
| 143 | * err => {},
|
---|
| 144 | * () => console.log('Yo!')
|
---|
| 145 | * );
|
---|
| 146 | *
|
---|
| 147 | * // Logs:
|
---|
| 148 | * // 0
|
---|
| 149 | * // 1000
|
---|
| 150 | * // 2000
|
---|
| 151 | * // "Yo!"
|
---|
| 152 | *
|
---|
| 153 | *
|
---|
| 154 | * @example <caption>Use with options object.</caption>
|
---|
| 155 | * const generated = generate({
|
---|
| 156 | * initialState: 0,
|
---|
| 157 | * condition(value) { return value < 3; },
|
---|
| 158 | * iterate(value) { return value + 1; },
|
---|
| 159 | * resultSelector(value) { return value * 1000; }
|
---|
| 160 | * });
|
---|
| 161 | *
|
---|
| 162 | * generated.subscribe(
|
---|
| 163 | * value => console.log(value),
|
---|
| 164 | * err => {},
|
---|
| 165 | * () => console.log('Yo!')
|
---|
| 166 | * );
|
---|
| 167 | *
|
---|
| 168 | * // Logs:
|
---|
| 169 | * // 0
|
---|
| 170 | * // 1000
|
---|
| 171 | * // 2000
|
---|
| 172 | * // "Yo!"
|
---|
| 173 | *
|
---|
| 174 | * @example <caption>Use options object without condition function.</caption>
|
---|
| 175 | * const generated = generate({
|
---|
| 176 | * initialState: 0,
|
---|
| 177 | * iterate(value) { return value + 1; },
|
---|
| 178 | * resultSelector(value) { return value * 1000; }
|
---|
| 179 | * });
|
---|
| 180 | *
|
---|
| 181 | * generated.subscribe(
|
---|
| 182 | * value => console.log(value),
|
---|
| 183 | * err => {},
|
---|
| 184 | * () => console.log('Yo!') // This will never run.
|
---|
| 185 | * );
|
---|
| 186 | *
|
---|
| 187 | * // Logs:
|
---|
| 188 | * // 0
|
---|
| 189 | * // 1000
|
---|
| 190 | * // 2000
|
---|
| 191 | * // 3000
|
---|
| 192 | * // ...and never stops.
|
---|
| 193 | *
|
---|
| 194 | *
|
---|
| 195 | * @see {@link from}
|
---|
| 196 | * @see {@link index/Observable.create}
|
---|
| 197 | *
|
---|
| 198 | * @param {S} initialState Initial state.
|
---|
| 199 | * @param {function (state: S): boolean} condition Condition to terminate generation (upon returning false).
|
---|
| 200 | * @param {function (state: S): S} iterate Iteration step function.
|
---|
| 201 | * @param {function (state: S): T} [resultSelector] Selector function for results produced in the sequence.
|
---|
| 202 | * @param {Scheduler} [scheduler] A {@link Scheduler} on which to run the generator loop. If not provided, defaults to emitting immediately.
|
---|
| 203 | * @return {Observable<T>} The generated sequence.
|
---|
| 204 | */
|
---|
| 205 | export function generate<S>(initialState: S,
|
---|
| 206 | condition: ConditionFunc<S>,
|
---|
| 207 | iterate: IterateFunc<S>,
|
---|
| 208 | scheduler?: SchedulerLike): Observable<S>;
|
---|
| 209 |
|
---|
| 210 | /**
|
---|
| 211 | * Generates an observable sequence by running a state-driven loop
|
---|
| 212 | * producing the sequence's elements, using the specified scheduler
|
---|
| 213 | * to send out observer messages.
|
---|
| 214 | * The overload accepts options object that might contain initial state, iterate,
|
---|
| 215 | * condition and scheduler.
|
---|
| 216 | *
|
---|
| 217 | * ![](generate.png)
|
---|
| 218 | *
|
---|
| 219 | * @example <caption>Produces sequence of 0, 1, 2, ... 9, then completes.</caption>
|
---|
| 220 | * const res = generate({
|
---|
| 221 | * initialState: 0,
|
---|
| 222 | * condition: x => x < 10,
|
---|
| 223 | * iterate: x => x + 1,
|
---|
| 224 | * });
|
---|
| 225 | *
|
---|
| 226 | * @see {@link from}
|
---|
| 227 | * @see {@link Observable}
|
---|
| 228 | *
|
---|
| 229 | * @param {GenerateBaseOptions<S>} options Object that must contain initialState, iterate and might contain condition and scheduler.
|
---|
| 230 | * @returns {Observable<S>} The generated sequence.
|
---|
| 231 | */
|
---|
| 232 | export function generate<S>(options: GenerateBaseOptions<S>): Observable<S>;
|
---|
| 233 |
|
---|
| 234 | /**
|
---|
| 235 | * Generates an observable sequence by running a state-driven loop
|
---|
| 236 | * producing the sequence's elements, using the specified scheduler
|
---|
| 237 | * to send out observer messages.
|
---|
| 238 | * The overload accepts options object that might contain initial state, iterate,
|
---|
| 239 | * condition, result selector and scheduler.
|
---|
| 240 | *
|
---|
| 241 | * ![](generate.png)
|
---|
| 242 | *
|
---|
| 243 | * @example <caption>Produces sequence of 0, 1, 2, ... 9, then completes.</caption>
|
---|
| 244 | * const res = generate({
|
---|
| 245 | * initialState: 0,
|
---|
| 246 | * condition: x => x < 10,
|
---|
| 247 | * iterate: x => x + 1,
|
---|
| 248 | * resultSelector: x => x,
|
---|
| 249 | * });
|
---|
| 250 | *
|
---|
| 251 | * @see {@link from}
|
---|
| 252 | * @see {@link Observable}
|
---|
| 253 | *
|
---|
| 254 | * @param {GenerateOptions<T, S>} options Object that must contain initialState, iterate, resultSelector and might contain condition and scheduler.
|
---|
| 255 | * @returns {Observable<T>} The generated sequence.
|
---|
| 256 | */
|
---|
| 257 | export function generate<T, S>(options: GenerateOptions<T, S>): Observable<T>;
|
---|
| 258 |
|
---|
| 259 | export function generate<T, S>(initialStateOrOptions: S | GenerateOptions<T, S>,
|
---|
| 260 | condition?: ConditionFunc<S>,
|
---|
| 261 | iterate?: IterateFunc<S>,
|
---|
| 262 | resultSelectorOrObservable?: (ResultFunc<S, T>) | SchedulerLike,
|
---|
| 263 | scheduler?: SchedulerLike): Observable<T> {
|
---|
| 264 |
|
---|
| 265 | let resultSelector: ResultFunc<S, T>;
|
---|
| 266 | let initialState: S;
|
---|
| 267 |
|
---|
| 268 | if (arguments.length == 1) {
|
---|
| 269 | const options = initialStateOrOptions as GenerateOptions<T, S>;
|
---|
| 270 | initialState = options.initialState;
|
---|
| 271 | condition = options.condition;
|
---|
| 272 | iterate = options.iterate;
|
---|
| 273 | resultSelector = options.resultSelector || identity as ResultFunc<S, T>;
|
---|
| 274 | scheduler = options.scheduler;
|
---|
| 275 | } else if (resultSelectorOrObservable === undefined || isScheduler(resultSelectorOrObservable)) {
|
---|
| 276 | initialState = initialStateOrOptions as S;
|
---|
| 277 | resultSelector = identity as ResultFunc<S, T>;
|
---|
| 278 | scheduler = resultSelectorOrObservable as SchedulerLike;
|
---|
| 279 | } else {
|
---|
| 280 | initialState = initialStateOrOptions as S;
|
---|
| 281 | resultSelector = resultSelectorOrObservable as ResultFunc<S, T>;
|
---|
| 282 | }
|
---|
| 283 |
|
---|
| 284 | return new Observable<T>(subscriber => {
|
---|
| 285 | let state = initialState;
|
---|
| 286 | if (scheduler) {
|
---|
| 287 | return scheduler.schedule<SchedulerState<T, S>>(dispatch, 0, {
|
---|
| 288 | subscriber,
|
---|
| 289 | iterate,
|
---|
| 290 | condition,
|
---|
| 291 | resultSelector,
|
---|
| 292 | state
|
---|
| 293 | });
|
---|
| 294 | }
|
---|
| 295 |
|
---|
| 296 | do {
|
---|
| 297 | if (condition) {
|
---|
| 298 | let conditionResult: boolean;
|
---|
| 299 | try {
|
---|
| 300 | conditionResult = condition(state);
|
---|
| 301 | } catch (err) {
|
---|
| 302 | subscriber.error(err);
|
---|
| 303 | return undefined;
|
---|
| 304 | }
|
---|
| 305 | if (!conditionResult) {
|
---|
| 306 | subscriber.complete();
|
---|
| 307 | break;
|
---|
| 308 | }
|
---|
| 309 | }
|
---|
| 310 | let value: T;
|
---|
| 311 | try {
|
---|
| 312 | value = resultSelector(state);
|
---|
| 313 | } catch (err) {
|
---|
| 314 | subscriber.error(err);
|
---|
| 315 | return undefined;
|
---|
| 316 | }
|
---|
| 317 | subscriber.next(value);
|
---|
| 318 | if (subscriber.closed) {
|
---|
| 319 | break;
|
---|
| 320 | }
|
---|
| 321 | try {
|
---|
| 322 | state = iterate(state);
|
---|
| 323 | } catch (err) {
|
---|
| 324 | subscriber.error(err);
|
---|
| 325 | return undefined;
|
---|
| 326 | }
|
---|
| 327 | } while (true);
|
---|
| 328 |
|
---|
| 329 | return undefined;
|
---|
| 330 | });
|
---|
| 331 | }
|
---|
| 332 |
|
---|
| 333 | function dispatch<T, S>(this: SchedulerAction<SchedulerState<T, S>>, state: SchedulerState<T, S>) {
|
---|
| 334 | const { subscriber, condition } = state;
|
---|
| 335 | if (subscriber.closed) {
|
---|
| 336 | return undefined;
|
---|
| 337 | }
|
---|
| 338 | if (state.needIterate) {
|
---|
| 339 | try {
|
---|
| 340 | state.state = state.iterate(state.state);
|
---|
| 341 | } catch (err) {
|
---|
| 342 | subscriber.error(err);
|
---|
| 343 | return undefined;
|
---|
| 344 | }
|
---|
| 345 | } else {
|
---|
| 346 | state.needIterate = true;
|
---|
| 347 | }
|
---|
| 348 | if (condition) {
|
---|
| 349 | let conditionResult: boolean;
|
---|
| 350 | try {
|
---|
| 351 | conditionResult = condition(state.state);
|
---|
| 352 | } catch (err) {
|
---|
| 353 | subscriber.error(err);
|
---|
| 354 | return undefined;
|
---|
| 355 | }
|
---|
| 356 | if (!conditionResult) {
|
---|
| 357 | subscriber.complete();
|
---|
| 358 | return undefined;
|
---|
| 359 | }
|
---|
| 360 | if (subscriber.closed) {
|
---|
| 361 | return undefined;
|
---|
| 362 | }
|
---|
| 363 | }
|
---|
| 364 | let value: T;
|
---|
| 365 | try {
|
---|
| 366 | value = state.resultSelector(state.state);
|
---|
| 367 | } catch (err) {
|
---|
| 368 | subscriber.error(err);
|
---|
| 369 | return undefined;
|
---|
| 370 | }
|
---|
| 371 | if (subscriber.closed) {
|
---|
| 372 | return undefined;
|
---|
| 373 | }
|
---|
| 374 | subscriber.next(value);
|
---|
| 375 | if (subscriber.closed) {
|
---|
| 376 | return undefined;
|
---|
| 377 | }
|
---|
| 378 | return this.schedule(state);
|
---|
| 379 | }
|
---|