streams.d.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /**
  2. * Streams are the basic building blocks of a reactive system. Think of them as the system permanent "data tubes".
  3. *
  4. * A stream acts as both an [[Emitter]] and [[Publisher]]. Each stream can have multiple {@link Subscription | Subscriptions}.
  5. *
  6. * urx streams are either **stateless** or **stateful**.
  7. * Stateless streams emit data to existing subscriptions when published, without keeping track of it.
  8. * Stateful streams remember the last published value and immediately publish it to new subscriptions.
  9. *
  10. * ```ts
  11. * import { stream, statefulStream, publish, subscribe } from "@virtuoso.dev/urx";
  12. *
  13. * // foo is a stateless stream
  14. * const foo = stream<number>();
  15. *
  16. * publish(foo, 42);
  17. * // this subsription will not be called...
  18. * subscribe(foo, (value) => console.log(value));
  19. * // it will only catch published values after it
  20. * publish(foo, 43);
  21. *
  22. * // stateful streams always start with an initial value
  23. * const bar = statefulStream(42);
  24. *
  25. * // subscribing to a stateful stream
  26. * // immediately calls the subscription with the current value
  27. * subscribe(bar, (value) => console.log(value));
  28. *
  29. * // subsequent publishing works just like stateless streams
  30. * publish(bar, 43);
  31. * ```
  32. * @packageDocumentation
  33. */
  34. import { Emitter, StatefulStream, Stream } from './actions';
  35. /**
  36. * Constructs a new stateless stream.
  37. * ```ts
  38. * const foo = stream<number>();
  39. * ```
  40. * @typeParam T the type of values to publish in the stream.
  41. * @returns a [[Stream]]
  42. */
  43. export declare function stream<T>(): Stream<T>;
  44. /**
  45. * Constructs a new stateful stream.
  46. * ```ts
  47. * const foo = statefulStream(42);
  48. * ```
  49. * @param initial the initial value in the stream.
  50. * @typeParam T the type of values to publish in the stream. If omitted, the function infers it from the initial value.
  51. * @returns a [[StatefulStream]]
  52. */
  53. export declare function statefulStream<T>(initial: T): StatefulStream<T>;
  54. /**
  55. * Event handlers are special emitters which can have **at most one active subscription**.
  56. * Subscribing to an event handler unsubscribes the previous subscription, if present.
  57. * ```ts
  58. * const foo = stream<number>();
  59. * const fooEvent = eventHandler(foo);
  60. *
  61. * // will be called once with 42
  62. * subscribe(fooEvent, (value) => console.log(`Sub 1 ${value}`));
  63. * publish(foo, 42);
  64. *
  65. * // unsubscribes sub 1
  66. * subscribe(fooEvent, (value) => console.log(`Sub 2 ${value}`));
  67. * publish(foo, 43);
  68. * ```
  69. * @param emitter the source emitter.
  70. * @returns the single-subscription emitter.
  71. */
  72. export declare function eventHandler<T>(emitter: Emitter<T>): Emitter<T>;
  73. /**
  74. * Creates and connects a "junction" stream to the specified emitter. Often used with [[pipe]], to avoid the multiple evaluation of operator sets.
  75. *
  76. * ```ts
  77. * const foo = stream<number>();
  78. *
  79. * const fooX2 = pipe(
  80. * foo,
  81. * map((value) => {
  82. * console.log(`multiplying ${value}`);
  83. * return value * 2;
  84. * })
  85. * );
  86. *
  87. * subscribe(fooX2, (value) => console.log(value));
  88. * subscribe(fooX2, (value) => console.log(value));
  89. *
  90. * publish(foo, 42); // executes the map operator twice for each subscription.
  91. *
  92. * const sharedFooX2 = streamFromEmitter(pipe(
  93. * foo,
  94. * map((value) => {
  95. * console.log(`shared multiplying ${value}`);
  96. * return value * 2;
  97. * })
  98. * ));
  99. *
  100. * subscribe(sharedFooX2, (value) => console.log(value));
  101. * subscribe(sharedFooX2, (value) => console.log(value));
  102. *
  103. * publish(foo, 42);
  104. *```
  105. * @returns the resulting stream.
  106. */
  107. export declare function streamFromEmitter<T>(emitter: Emitter<T>): Stream<T>;
  108. /**
  109. * Creates and connects a "junction" stateful stream to the specified emitter. Often used with [[pipe]], to avoid the multiple evaluation of operator sets.
  110. *
  111. * ```ts
  112. * const foo = stream<number>();
  113. *
  114. * const fooX2 = pipe(
  115. * foo,
  116. * map((value) => {
  117. * console.log(`multiplying ${value}`);
  118. * return value * 2;
  119. * })
  120. * );
  121. *
  122. * subscribe(fooX2, (value) => console.log(value));
  123. * subscribe(fooX2, (value) => console.log(value));
  124. *
  125. * publish(foo, 42); // executes the map operator twice for each subscription.
  126. *
  127. * const sharedFooX2 = statefulStreamFromEmitter(pipe(
  128. * foo,
  129. * map((value) => {
  130. * console.log(`shared multiplying ${value}`);
  131. * return value * 2;
  132. * })
  133. * ), 42);
  134. *
  135. * subscribe(sharedFooX2, (value) => console.log(value));
  136. * subscribe(sharedFooX2, (value) => console.log(value));
  137. *
  138. * publish(foo, 42);
  139. *```
  140. * @param initial the initial value in the stream.
  141. * @returns the resulting stateful stream.
  142. */
  143. export declare function statefulStreamFromEmitter<T>(emitter: Emitter<T>, initial: T): StatefulStream<T>;