actions.d.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /**
  2. * urx Actions operate on streams - `publish` publishes data in a stream, and `subscribe` attaches a subscription to a stream.
  3. * @packageDocumentation
  4. */
  5. import { PUBLISH, RESET, SUBSCRIBE, VALUE } from './constants';
  6. /**
  7. * A Publisher is the **input end** of a Stream. The [[publish]] action publishes values in publishers.
  8. * @typeParam T the type of values to be published.
  9. */
  10. export interface Publisher<T> {
  11. /** @internal */
  12. (action: PUBLISH, value: T): void;
  13. }
  14. /**
  15. * An Emitter is the **output end** of a Stream. The [[subscribe]] action binds {@link Subscription | subscriptions} to emitters.
  16. * @typeParam T the type of values that will be emitted.
  17. */
  18. export interface Emitter<T> {
  19. /** @internal */
  20. (action: SUBSCRIBE, subscription: Subscription<T>): Unsubscribe;
  21. /** @internal */
  22. (action: RESET): void;
  23. }
  24. /**
  25. * Subscriptions are bound to Emitters with the [[subscribe]] action, and get called with the new values.
  26. * @typeParam T the Emitter value type.
  27. */
  28. export interface Subscription<T> {
  29. (value: T): any;
  30. }
  31. /**
  32. * Subscribe-like actions return unsubscribe handles of the Unsubscribe type, which, when called, unbind the subscription.
  33. */
  34. export interface Unsubscribe {
  35. (): void;
  36. }
  37. /**
  38. * Streams present both the input and the output ends of a stream.
  39. * A single stream instance can be both subscribed to and published in.
  40. */
  41. export interface Stream<T> extends Publisher<T>, Emitter<T> {
  42. /** @internal */
  43. (action: SUBSCRIBE | PUBLISH | RESET): any;
  44. }
  45. /**
  46. * Just like {@link Stream | streams}, stateful streams present both input and output ends of a stream.
  47. * A single stream instance can be both subscribed to and published in.
  48. * Stateful Streams can also act like depots, preserving the last passed value and immediately publishing it to new subscriptions.
  49. * [[getValue]] can be used to extract value from stateful streams.
  50. */
  51. export interface StatefulStream<T> extends Publisher<T>, Emitter<T> {
  52. /** @internal */
  53. (action: SUBSCRIBE | PUBLISH | RESET | VALUE): any;
  54. }
  55. /**
  56. * Subscribes the specified [[Subscription]] to the updates from the Emitter.
  57. * The emitter calls the subscription with the new data each time new data is published into it.
  58. *
  59. * ```ts
  60. * const foo = stream<number>();
  61. * subscribe(foo, (value) => console.log(value));
  62. * ```
  63. *
  64. * @returns an [[Unsubscribe]] handle - calling it will unbind the subscription from the emitter.
  65. *```ts
  66. * const foo = stream<number>();
  67. * const unsub = subscribe(foo, (value) => console.log(value));
  68. * unsub();
  69. *```
  70. */
  71. export declare function subscribe<T>(emitter: Emitter<T>, subscription: Subscription<T>): Unsubscribe;
  72. /**
  73. * Publishes the value into the passed [[Publisher]].
  74. *
  75. * ```ts
  76. * const foo = stream<number>();
  77. * publish(foo, 42);
  78. * ```
  79. */
  80. export declare function publish<T>(publisher: Publisher<T>, value: T): void;
  81. /**
  82. * Clears all subscriptions from the [[Emitter]].
  83. * ```ts
  84. * const foo = stream<number>();
  85. * subscribe(foo, (value) => console.log(value));
  86. * reset(foo);
  87. * publish(foo, 42);
  88. * ```
  89. */
  90. export declare function reset(emitter: Emitter<any>): void;
  91. /**
  92. * Extracts the current value from a stateful stream. Use it only as an escape hatch, as it violates the concept of reactive programming.
  93. * ```ts
  94. * const foo = statefulStream(42);
  95. * console.log(getValue(foo));
  96. * ```
  97. */
  98. export declare function getValue<T>(depot: StatefulStream<T>): T;
  99. /**
  100. * Connects two streams - any value emitted from the emitter will be published in the publisher.
  101. * ```ts
  102. * const foo = stream<number>();
  103. * const bar = stream<number>();
  104. * subscribe(bar, (value) => console.log(`Bar emitted ${value}`));
  105. *
  106. * connect(foo, bar);
  107. * publish(foo);
  108. * ```
  109. * @returns an [[Unsubscribe]] handle which will disconnect the two streams.
  110. */
  111. export declare function connect<T>(emitter: Emitter<T>, publisher: Publisher<T>): Unsubscribe;
  112. /**
  113. * Executes the passed subscription at most once, for the next emit from the emitter.
  114. * ```ts
  115. * const foo = stream<number>()
  116. * handleNext(foo, value => console.log(value)) // called once, with 42
  117. * publish(foo, 42)
  118. * publish(foo, 43)
  119. * ```
  120. * @returns an [[Unsubscribe]] handle to unbind the subscription if necessary.
  121. */
  122. export declare function handleNext<T>(emitter: Emitter<T>, subscription: Subscription<T>): Unsubscribe;