123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- /**
- * Streams are the basic building blocks of a reactive system. Think of them as the system permanent "data tubes".
- *
- * A stream acts as both an [[Emitter]] and [[Publisher]]. Each stream can have multiple {@link Subscription | Subscriptions}.
- *
- * urx streams are either **stateless** or **stateful**.
- * Stateless streams emit data to existing subscriptions when published, without keeping track of it.
- * Stateful streams remember the last published value and immediately publish it to new subscriptions.
- *
- * ```ts
- * import { stream, statefulStream, publish, subscribe } from "@virtuoso.dev/urx";
- *
- * // foo is a stateless stream
- * const foo = stream<number>();
- *
- * publish(foo, 42);
- * // this subsription will not be called...
- * subscribe(foo, (value) => console.log(value));
- * // it will only catch published values after it
- * publish(foo, 43);
- *
- * // stateful streams always start with an initial value
- * const bar = statefulStream(42);
- *
- * // subscribing to a stateful stream
- * // immediately calls the subscription with the current value
- * subscribe(bar, (value) => console.log(value));
- *
- * // subsequent publishing works just like stateless streams
- * publish(bar, 43);
- * ```
- * @packageDocumentation
- */
- import { Emitter, StatefulStream, Stream } from './actions';
- /**
- * Constructs a new stateless stream.
- * ```ts
- * const foo = stream<number>();
- * ```
- * @typeParam T the type of values to publish in the stream.
- * @returns a [[Stream]]
- */
- export declare function stream<T>(): Stream<T>;
- /**
- * Constructs a new stateful stream.
- * ```ts
- * const foo = statefulStream(42);
- * ```
- * @param initial the initial value in the stream.
- * @typeParam T the type of values to publish in the stream. If omitted, the function infers it from the initial value.
- * @returns a [[StatefulStream]]
- */
- export declare function statefulStream<T>(initial: T): StatefulStream<T>;
- /**
- * Event handlers are special emitters which can have **at most one active subscription**.
- * Subscribing to an event handler unsubscribes the previous subscription, if present.
- * ```ts
- * const foo = stream<number>();
- * const fooEvent = eventHandler(foo);
- *
- * // will be called once with 42
- * subscribe(fooEvent, (value) => console.log(`Sub 1 ${value}`));
- * publish(foo, 42);
- *
- * // unsubscribes sub 1
- * subscribe(fooEvent, (value) => console.log(`Sub 2 ${value}`));
- * publish(foo, 43);
- * ```
- * @param emitter the source emitter.
- * @returns the single-subscription emitter.
- */
- export declare function eventHandler<T>(emitter: Emitter<T>): Emitter<T>;
- /**
- * Creates and connects a "junction" stream to the specified emitter. Often used with [[pipe]], to avoid the multiple evaluation of operator sets.
- *
- * ```ts
- * const foo = stream<number>();
- *
- * const fooX2 = pipe(
- * foo,
- * map((value) => {
- * console.log(`multiplying ${value}`);
- * return value * 2;
- * })
- * );
- *
- * subscribe(fooX2, (value) => console.log(value));
- * subscribe(fooX2, (value) => console.log(value));
- *
- * publish(foo, 42); // executes the map operator twice for each subscription.
- *
- * const sharedFooX2 = streamFromEmitter(pipe(
- * foo,
- * map((value) => {
- * console.log(`shared multiplying ${value}`);
- * return value * 2;
- * })
- * ));
- *
- * subscribe(sharedFooX2, (value) => console.log(value));
- * subscribe(sharedFooX2, (value) => console.log(value));
- *
- * publish(foo, 42);
- *```
- * @returns the resulting stream.
- */
- export declare function streamFromEmitter<T>(emitter: Emitter<T>): Stream<T>;
- /**
- * Creates and connects a "junction" stateful stream to the specified emitter. Often used with [[pipe]], to avoid the multiple evaluation of operator sets.
- *
- * ```ts
- * const foo = stream<number>();
- *
- * const fooX2 = pipe(
- * foo,
- * map((value) => {
- * console.log(`multiplying ${value}`);
- * return value * 2;
- * })
- * );
- *
- * subscribe(fooX2, (value) => console.log(value));
- * subscribe(fooX2, (value) => console.log(value));
- *
- * publish(foo, 42); // executes the map operator twice for each subscription.
- *
- * const sharedFooX2 = statefulStreamFromEmitter(pipe(
- * foo,
- * map((value) => {
- * console.log(`shared multiplying ${value}`);
- * return value * 2;
- * })
- * ), 42);
- *
- * subscribe(sharedFooX2, (value) => console.log(value));
- * subscribe(sharedFooX2, (value) => console.log(value));
- *
- * publish(foo, 42);
- *```
- * @param initial the initial value in the stream.
- * @returns the resulting stateful stream.
- */
- export declare function statefulStreamFromEmitter<T>(emitter: Emitter<T>, initial: T): StatefulStream<T>;
|