Module: "urx/src/streams"#

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 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.

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);

Index#

Functions#

Functions#

eventHandler#

eventHandler<T>(emitter: Emitter<T>): Emitter<T>

Defined in urx/src/streams.ts:121

Event handlers are special emitters which can have at most one active subscription. Subscribing to an event handler unsubscribes the previous subscription, if present.

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);

Type parameters:#

Name
T

Parameters:#

NameTypeDescription
emitterEmitter<T>the source emitter.

Returns: Emitter<T>

the single-subscription emitter.


statefulStream#

statefulStream<T>(initial: T): StatefulStream<T>

Defined in urx/src/streams.ts:83

Constructs a new stateful stream.

const foo = statefulStream(42);

Type parameters:#

NameDescription
Tthe type of values to publish in the stream. If omitted, the function infers it from the initial value.

Parameters:#

NameTypeDescription
initialTthe initial value in the stream.

Returns: StatefulStream<T>

a StatefulStream


statefulStreamFromEmitter#

statefulStreamFromEmitter<T>(emitter: Emitter<T>, initial: T): StatefulStream<T>

Defined in urx/src/streams.ts:224

Creates and connects a "junction" stateful stream to the specified emitter. Often used with pipe, to avoid the multiple evaluation of operator sets.

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);

Type parameters:#

Name
T

Parameters:#

NameTypeDescription
emitterEmitter<T>-
initialTthe initial value in the stream.

Returns: StatefulStream<T>

the resulting stateful stream.


stream#

stream<T>(): Stream<T>

Defined in urx/src/streams.ts:47

Constructs a new stateless stream.

const foo = stream<number>();

Type parameters:#

NameDescription
Tthe type of values to publish in the stream.

Returns: Stream<T>

a Stream


streamFromEmitter#

streamFromEmitter<T>(emitter: Emitter<T>): Stream<T>

Defined in urx/src/streams.ts:185

Creates and connects a "junction" stream to the specified emitter. Often used with pipe, to avoid the multiple evaluation of operator sets.

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);

Type parameters:#

Name
T

Parameters:#

NameType
emitterEmitter<T>

Returns: Stream<T>

the resulting stream.