pipe.d.ts 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. import { Emitter } from './actions';
  2. /**
  3. * Operators can transform and control the flow of values.
  4. * [[pipe]] is used to transform one Emitter into another by stacking operators to its values.
  5. * To build your own operator that looks like the built-in ones,
  6. * create a function which returns an operator.
  7. * The following custom operator multiplies the passed value:
  8. *
  9. * ```ts
  10. * function multiplyBy(multiplier: number): Operator<number> {
  11. * return done => value => done(value * multiplier)
  12. * }
  13. *
  14. * const foo = stream<number>()
  15. * const multipliedFoo = pipe(foo, multiplyBy(3))
  16. * subscribe(multipliedFoo, value => console.log(value))
  17. * publish(foo, 42)
  18. * ```
  19. */
  20. export interface Operator<Input, Output = Input> {
  21. (done: (value: Output) => void): (value: Input) => void;
  22. }
  23. /** @internal */
  24. declare type O<I, OP> = Operator<I, OP>;
  25. /**
  26. * Creates a new emitter from the passed one by piping its values through one or more operators.
  27. * Operators can perform various actions like filter values, pull values from other emitters, or compute new values.
  28. *
  29. * ```ts
  30. * const foo = stream<number>()
  31. *
  32. * // create an emitter that first adds 2 to the passed value, then multiplies it by * 2
  33. * const bar = pipe(foo, map(value => value + 2), map(value => value * 2))
  34. * subscribe(bar, value => console.log(value))
  35. * publish(foo, 2) // outputs 8
  36. * ```
  37. * #### Sharing Subscription Calculations
  38. *
  39. * `pipe` acts as a proxy for the source emitter, and re-runs the operators for each subscription to the derived emitter.
  40. * Use [[streamFromEmitter]] or [[statefulStreamFromEmitter]] to avoid that.
  41. */
  42. export declare function pipe<T>(s: Emitter<T>): Emitter<T>;
  43. export declare function pipe<T, O1>(s: Emitter<T>, o1: O<T, O1>): Emitter<O1>;
  44. export declare function pipe<T, O1, O2>(s: Emitter<T>, ...o: [O<T, O1>, O<O1, O2>]): Emitter<O2>;
  45. export declare function pipe<T, O1, O2, O3>(s: Emitter<T>, ...o: [O<T, O1>, O<O1, O2>, O<O2, O3>]): Emitter<O3>;
  46. export declare function pipe<T, O1, O2, O3, O4>(s: Emitter<T>, ...o: [O<T, O1>, O<O1, O2>, O<O2, O3>, O<O3, O4>]): Emitter<O4>;
  47. export declare function pipe<T, O1, O2, O3, O4, O5>(s: Emitter<T>, ...o: [O<T, O1>, O<O1, O2>, O<O2, O3>, O<O3, O4>, O<O4, O5>]): Emitter<O5>;
  48. export declare function pipe<T, O1, O2, O3, O4, O5, O6>(s: Emitter<T>, ...o: [O<T, O1>, O<O1, O2>, O<O2, O3>, O<O3, O4>, O<O4, O5>, O<O5, O6>]): Emitter<O6>;
  49. export declare function pipe<T, O1, O2, O3, O4, O5, O6, O7>(s: Emitter<T>, ...o: [O<T, O1>, O<O1, O2>, O<O2, O3>, O<O3, O4>, O<O4, O5>, O<O5, O6>, O<O6, O7>]): Emitter<O7>;
  50. /**
  51. * A function which determines if two values are equal.
  52. * Implement custom comparators when [[distinctUntilChanged]] needs to work on non-primitive objects.
  53. * @returns true if values should be considered equal.
  54. */
  55. export interface Comparator<T> {
  56. (previous: T, next: T): boolean;
  57. }
  58. /**
  59. * The default [[Comparator]] for [[distinctUntilChanged]] and [[duc]].
  60. */
  61. export declare function defaultComparator<T>(previous: T, next: T): boolean;
  62. /**
  63. * Filters out identical values. Pass an optional [[Comparator]] if you need to filter non-primitive values.
  64. * ```ts
  65. * const foo = stream<number>()
  66. *
  67. * subscribe(
  68. * pipe(foo, distinctUntilChanged()),
  69. * console.log
  70. * ) // will be called only once
  71. *
  72. * publish(foo, 42)
  73. * publish(foo, 42)
  74. * ```
  75. */
  76. export declare function distinctUntilChanged<T>(comparator?: Comparator<T>): Operator<T>;
  77. /**
  78. * Filters out values for which the predicator does not return `true`-ish.
  79. * ```ts
  80. * const foo = stream<number>()
  81. *
  82. * subscribe(
  83. * pipe(foo, filter(value => value % 2 === 0)),
  84. * console.log
  85. * ) // will be called only with even values
  86. *
  87. * publish(foo, 2)
  88. * publish(foo, 3)
  89. * publish(foo, 4)
  90. * publish(foo, 5)
  91. * ```
  92. */
  93. export declare function filter<T>(predicate: (value: T) => boolean): Operator<T>;
  94. /**
  95. * Maps values using the provided project function.
  96. * ```ts
  97. * const foo = stream<number>()
  98. *
  99. * subscribe(
  100. * pipe(foo, map(value => value * 2)),
  101. * console.log
  102. * ) // 4, 6
  103. *
  104. * publish(foo, 2)
  105. * publish(foo, 3)
  106. * ```
  107. */
  108. export declare function map<T, K>(project: (value: T) => K): Operator<T, K>;
  109. /**
  110. * Maps values to the hard-coded value.
  111. * ```ts
  112. * const foo = stream<number>()
  113. *
  114. * subscribe(
  115. * pipe(foo, mapTo(3)),
  116. * console.log
  117. * ) // 3, 3
  118. *
  119. * publish(foo, 1)
  120. * publish(foo, 2)
  121. * ```
  122. */
  123. export declare function mapTo<T>(value: T): Operator<any, T>;
  124. /**
  125. * Works like Array#reduce.
  126. * Applies an accumulator function on the emitter, and outputs intermediate result. Starts with the initial value.
  127. * ```ts
  128. * const foo = stream<number>()
  129. *
  130. * subscribe(
  131. * pipe(foo, scan((acc, value) => acc + value, 2),
  132. * console.log
  133. * ) // 3, 5
  134. *
  135. * publish(foo, 1)
  136. * publish(foo, 2)
  137. * ```
  138. */
  139. export declare function scan<T, K>(scanner: (current: T, value: K) => T, initial: T): Operator<K, T>;
  140. /**
  141. * Skips the specified amount of values from the emitter.
  142. * ```ts
  143. * const foo = stream<number>()
  144. *
  145. * subscribe(
  146. * pipe(foo, skip(2)),
  147. * console.log
  148. * ) // 3, 4
  149. *
  150. * publish(foo, 1) // skipped
  151. * publish(foo, 2) // skipped
  152. * publish(foo, 3)
  153. * publish(foo, 4)
  154. * ```
  155. */
  156. export declare function skip<T>(times: number): Operator<T>;
  157. /**
  158. * Throttles flowing values at the provided interval in milliseconds.
  159. * [Throttle VS Debounce in SO](https://stackoverflow.com/questions/25991367/difference-between-throttling-and-debouncing-a-function).
  160. *
  161. * ```ts
  162. * const foo = stream<number>()
  163. * publish(foo, 1)
  164. *
  165. * setTimeout(() => publish(foo, 2), 20)
  166. * setTimeout(() => publish(foo, 3), 20)
  167. *
  168. * subscribe(pipe(foo, throttleTime(50)), val => {
  169. * console.log(value); // 3
  170. * })
  171. * ```
  172. */
  173. export declare function throttleTime<T>(interval: number): Operator<T>;
  174. /**
  175. * Debounces flowing values at the provided interval in milliseconds.
  176. * [Throttle VS Debounce in SO](https://stackoverflow.com/questions/25991367/difference-between-throttling-and-debouncing-a-function).
  177. *
  178. * ```ts
  179. * const foo = stream<number>()
  180. * publish(foo, 1)
  181. *
  182. * setTimeout(() => publish(foo, 2), 20)
  183. * setTimeout(() => publish(foo, 3), 20)
  184. *
  185. * subscribe(pipe(foo, debounceTime(50)), val => {
  186. * console.log(value); // 3
  187. * })
  188. * ```
  189. */
  190. export declare function debounceTime<T>(interval: number): Operator<T>;
  191. /**
  192. * Combines the source Emitter with the latest values from the specified Emitters into an array. Outputs only when the source Emitter emits.
  193. * See [[combineLatest]] for a transformer that outputs when any of the emitters emit.
  194. *
  195. * ```ts
  196. * const foo = stream<number>()
  197. * const bar = stream<number>()
  198. * subscribe(
  199. * pipe(
  200. * foo,
  201. * withLatestFrom(bar)
  202. * ),
  203. * (([foo, bar]) => console.log({ foo, bar }))
  204. * )
  205. *
  206. * publish(foo, 1) // nothing happens, bar has not emitted yet
  207. * publish(bar, 1) // still nothing
  208. * publish(foo, 2) // logs { foo: 2, bar: 1 }
  209. * publish(bar, 2)
  210. * publish(foo, 3) // logs { foo: 3, bar: 2 }
  211. * ```
  212. */
  213. export declare function withLatestFrom<T, R1>(...s: [Emitter<R1>]): Operator<T, [T, R1]>;
  214. export declare function withLatestFrom<T, R1, R2>(...s: [Emitter<R1>, Emitter<R2>]): Operator<T, [T, R1, R2]>;
  215. export declare function withLatestFrom<T, R1, R2, R3>(...s: [Emitter<R1>, Emitter<R2>, Emitter<R3>]): Operator<T, [T, R1, R2, R3]>;
  216. export declare function withLatestFrom<T, R1, R2, R3, R4>(...s: [Emitter<R1>, Emitter<R2>, Emitter<R3>, Emitter<R4>]): Operator<T, [T, R1, R2, R3, R4]>;
  217. export declare function withLatestFrom<T, R1, R2, R3, R4, R5>(...s: [Emitter<R1>, Emitter<R2>, Emitter<R3>, Emitter<R4>, Emitter<R5>]): Operator<T, [T, R1, R2, R3, R4, R5]>;
  218. export declare function withLatestFrom<T, R1, R2, R3, R4, R5, R6>(...s: [Emitter<R1>, Emitter<R2>, Emitter<R3>, Emitter<R4>, Emitter<R5>, Emitter<R6>]): Operator<T, [T, R1, R2, R3, R4, R5, R6]>;
  219. export declare function withLatestFrom<T, R1, R2, R3, R4, R5, R6, R7>(...s: [Emitter<R1>, Emitter<R2>, Emitter<R3>, Emitter<R4>, Emitter<R5>, Emitter<R6>, Emitter<R7>]): Operator<T, [T, R1, R2, R3, R4, R5, R6, R7]>;
  220. export {};