40b74838ba17fa23a507676e83bdb30367f991b93c19253a20098382fc518d8917fa107bbd327bf025844575fffdaa02fbdca7fd25152e65bc24e1966b37a5 9.8 KB

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