GenerateObservable.d.ts 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. import { IScheduler } from '../Scheduler';
  2. import { Observable } from '../Observable';
  3. import { Subscriber } from '../Subscriber';
  4. import { Subscription } from '../Subscription';
  5. export declare type ConditionFunc<S> = (state: S) => boolean;
  6. export declare type IterateFunc<S> = (state: S) => S;
  7. export declare type ResultFunc<S, T> = (state: S) => T;
  8. export interface GenerateBaseOptions<S> {
  9. /**
  10. * Initial state.
  11. */
  12. initialState: S;
  13. /**
  14. * Condition function that accepts state and returns boolean.
  15. * When it returns false, the generator stops.
  16. * If not specified, a generator never stops.
  17. */
  18. condition?: ConditionFunc<S>;
  19. /**
  20. * Iterate function that accepts state and returns new state.
  21. */
  22. iterate: IterateFunc<S>;
  23. /**
  24. * IScheduler to use for generation process.
  25. * By default, a generator starts immediately.
  26. */
  27. scheduler?: IScheduler;
  28. }
  29. export interface GenerateOptions<T, S> extends GenerateBaseOptions<S> {
  30. /**
  31. * Result selection function that accepts state and returns a value to emit.
  32. */
  33. resultSelector: ResultFunc<S, T>;
  34. }
  35. /**
  36. * We need this JSDoc comment for affecting ESDoc.
  37. * @extends {Ignored}
  38. * @hide true
  39. */
  40. export declare class GenerateObservable<T, S> extends Observable<T> {
  41. private initialState;
  42. private condition;
  43. private iterate;
  44. private resultSelector;
  45. private scheduler;
  46. constructor(initialState: S, condition: ConditionFunc<S>, iterate: IterateFunc<S>, resultSelector: ResultFunc<S, T>, scheduler?: IScheduler);
  47. /**
  48. * Generates an observable sequence by running a state-driven loop
  49. * producing the sequence's elements, using the specified scheduler
  50. * to send out observer messages.
  51. *
  52. * <img src="./img/generate.png" width="100%">
  53. *
  54. * @example <caption>Produces sequence of 0, 1, 2, ... 9, then completes.</caption>
  55. * var res = Rx.Observable.generate(0, x => x < 10, x => x + 1, x => x);
  56. *
  57. * @example <caption>Using asap scheduler, produces sequence of 2, 3, 5, then completes.</caption>
  58. * var res = Rx.Observable.generate(1, x => x < 5, x => x * 2, x => x + 1, Rx.Scheduler.asap);
  59. *
  60. * @see {@link from}
  61. * @see {@link create}
  62. *
  63. * @param {S} initialState Initial state.
  64. * @param {function (state: S): boolean} condition Condition to terminate generation (upon returning false).
  65. * @param {function (state: S): S} iterate Iteration step function.
  66. * @param {function (state: S): T} resultSelector Selector function for results produced in the sequence.
  67. * @param {Scheduler} [scheduler] A {@link IScheduler} on which to run the generator loop. If not provided, defaults to emit immediately.
  68. * @returns {Observable<T>} The generated sequence.
  69. */
  70. static create<T, S>(initialState: S, condition: ConditionFunc<S>, iterate: IterateFunc<S>, resultSelector: ResultFunc<S, T>, scheduler?: IScheduler): Observable<T>;
  71. /**
  72. * Generates an observable sequence by running a state-driven loop
  73. * producing the sequence's elements, using the specified scheduler
  74. * to send out observer messages.
  75. * The overload uses state as an emitted value.
  76. *
  77. * <img src="./img/generate.png" width="100%">
  78. *
  79. * @example <caption>Produces sequence of 0, 1, 2, ... 9, then completes.</caption>
  80. * var res = Rx.Observable.generate(0, x => x < 10, x => x + 1);
  81. *
  82. * @example <caption>Using asap scheduler, produces sequence of 1, 2, 4, then completes.</caption>
  83. * var res = Rx.Observable.generate(1, x => x < 5, x => x * 2, Rx.Scheduler.asap);
  84. *
  85. * @see {@link from}
  86. * @see {@link create}
  87. *
  88. * @param {S} initialState Initial state.
  89. * @param {function (state: S): boolean} condition Condition to terminate generation (upon returning false).
  90. * @param {function (state: S): S} iterate Iteration step function.
  91. * @param {Scheduler} [scheduler] A {@link IScheduler} on which to run the generator loop. If not provided, defaults to emit immediately.
  92. * @returns {Observable<S>} The generated sequence.
  93. */
  94. static create<S>(initialState: S, condition: ConditionFunc<S>, iterate: IterateFunc<S>, scheduler?: IScheduler): Observable<S>;
  95. /**
  96. * Generates an observable sequence by running a state-driven loop
  97. * producing the sequence's elements, using the specified scheduler
  98. * to send out observer messages.
  99. * The overload accepts options object that might contain initial state, iterate,
  100. * condition and scheduler.
  101. *
  102. * <img src="./img/generate.png" width="100%">
  103. *
  104. * @example <caption>Produces sequence of 0, 1, 2, ... 9, then completes.</caption>
  105. * var res = Rx.Observable.generate({
  106. * initialState: 0,
  107. * condition: x => x < 10,
  108. * iterate: x => x + 1
  109. * });
  110. *
  111. * @see {@link from}
  112. * @see {@link create}
  113. *
  114. * @param {GenerateBaseOptions<S>} options Object that must contain initialState, iterate and might contain condition and scheduler.
  115. * @returns {Observable<S>} The generated sequence.
  116. */
  117. static create<S>(options: GenerateBaseOptions<S>): Observable<S>;
  118. /**
  119. * Generates an observable sequence by running a state-driven loop
  120. * producing the sequence's elements, using the specified scheduler
  121. * to send out observer messages.
  122. * The overload accepts options object that might contain initial state, iterate,
  123. * condition, result selector and scheduler.
  124. *
  125. * <img src="./img/generate.png" width="100%">
  126. *
  127. * @example <caption>Produces sequence of 0, 1, 2, ... 9, then completes.</caption>
  128. * var res = Rx.Observable.generate({
  129. * initialState: 0,
  130. * condition: x => x < 10,
  131. * iterate: x => x + 1,
  132. * resultSelector: x => x
  133. * });
  134. *
  135. * @see {@link from}
  136. * @see {@link create}
  137. *
  138. * @param {GenerateOptions<T, S>} options Object that must contain initialState, iterate, resultSelector and might contain condition and scheduler.
  139. * @returns {Observable<T>} The generated sequence.
  140. */
  141. static create<T, S>(options: GenerateOptions<T, S>): Observable<T>;
  142. /** @deprecated internal use only */ _subscribe(subscriber: Subscriber<any>): Subscription | Function | void;
  143. private static dispatch<T, S>(state);
  144. }