Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

generate.d.ts 8.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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> {
  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. }
  27. export 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. * @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);
  42. *
  43. * @example <caption>Using asap scheduler, produces sequence of 2, 3, 5, then completes.</caption>
  44. * const res = generate(1, x => x < 5, x => * 2, x => x + 1, asap);
  45. *
  46. * @see {@link from}
  47. * @see {@link Observable}
  48. *
  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.
  55. */
  56. export declare function generate<T, S>(initialState: S, condition: ConditionFunc<S>, iterate: IterateFunc<S>, resultSelector: ResultFunc<S, T>, scheduler?: SchedulerLike): Observable<T>;
  57. /**
  58. * Generates an Observable by running a state-driven loop
  59. * that emits an element on each iteration.
  60. *
  61. * <span class="informal">Use it instead of nexting values in a for loop.</span>
  62. *
  63. * <img src="./img/generate.png" width="100%">
  64. *
  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).
  74. *
  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.
  80. *
  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.
  83. *
  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.
  90. *
  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.
  99. *
  100. *
  101. * @example <caption>Use with condition and iterate functions.</caption>
  102. * const generated = generate(0, x => x < 3, x => x + 1);
  103. *
  104. * generated.subscribe(
  105. * value => console.log(value),
  106. * err => {},
  107. * () => console.log('Yo!')
  108. * );
  109. *
  110. * // Logs:
  111. * // 0
  112. * // 1
  113. * // 2
  114. * // "Yo!"
  115. *
  116. *
  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);
  119. *
  120. * generated.subscribe(
  121. * value => console.log(value),
  122. * err => {},
  123. * () => console.log('Yo!')
  124. * );
  125. *
  126. * // Logs:
  127. * // 0
  128. * // 1000
  129. * // 2000
  130. * // "Yo!"
  131. *
  132. *
  133. * @example <caption>Use with options object.</caption>
  134. * const generated = generate({
  135. * initialState: 0,
  136. * condition(value) { return value < 3; },
  137. * iterate(value) { return value + 1; },
  138. * resultSelector(value) { return value * 1000; }
  139. * });
  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. * @example <caption>Use options object without condition function.</caption>
  154. * const generated = generate({
  155. * initialState: 0,
  156. * iterate(value) { return value + 1; },
  157. * resultSelector(value) { return value * 1000; }
  158. * });
  159. *
  160. * generated.subscribe(
  161. * value => console.log(value),
  162. * err => {},
  163. * () => console.log('Yo!') // This will never run.
  164. * );
  165. *
  166. * // Logs:
  167. * // 0
  168. * // 1000
  169. * // 2000
  170. * // 3000
  171. * // ...and never stops.
  172. *
  173. *
  174. * @see {@link from}
  175. * @see {@link create}
  176. *
  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.
  183. */
  184. export declare function generate<S>(initialState: S, condition: ConditionFunc<S>, iterate: IterateFunc<S>, scheduler?: SchedulerLike): Observable<S>;
  185. /**
  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.
  191. *
  192. * ![](generate.png)
  193. *
  194. * @example <caption>Produces sequence of 0, 1, 2, ... 9, then completes.</caption>
  195. * const res = generate({
  196. * initialState: 0,
  197. * condition: x => x < 10,
  198. * iterate: x => x + 1,
  199. * });
  200. *
  201. * @see {@link from}
  202. * @see {@link Observable}
  203. *
  204. * @param {GenerateBaseOptions<S>} options Object that must contain initialState, iterate and might contain condition and scheduler.
  205. * @returns {Observable<S>} The generated sequence.
  206. */
  207. export declare function generate<S>(options: GenerateBaseOptions<S>): Observable<S>;
  208. /**
  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.
  214. *
  215. * ![](generate.png)
  216. *
  217. * @example <caption>Produces sequence of 0, 1, 2, ... 9, then completes.</caption>
  218. * const res = generate({
  219. * initialState: 0,
  220. * condition: x => x < 10,
  221. * iterate: x => x + 1,
  222. * resultSelector: x => x,
  223. * });
  224. *
  225. * @see {@link from}
  226. * @see {@link Observable}
  227. *
  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.
  230. */
  231. export declare function generate<T, S>(options: GenerateOptions<T, S>): Observable<T>;