Anatomy
Type Rx.Reactive is the base type of reactives, with this signature:
type Reactive<T> = { // Listen for values or messages on(handler: ((value: Passed<T>) => void)): Unsubscriber // Listen for values onValue(handler: ((value: T) => void)): Unsubscriber // Some reactives allow setting values set?(value: T): void // Close the reactive, rendering it unusable dispose(reason: string): void // Check if the reactive is disposed isDisposed(): boolean}
Consuming
For the most part, you’ll use onValue
to listen for data emitted by the reactive. If you use on
, you’ll get the data and also signalling messages, for example when the reactive completes or there’s an error.
rx.onValue(value => { // value of reactive has changed})
Unsubscribing
Both on
and onValue
return type Unsubscriber
, a function to call to remove the subscription. Use this when you want to keep the source alive, but no longer want to receive values.
const rxOff = rx.onValue(handler);
// Later, call the provided unsubscribe function:rxOff();
Setting values
Only Type Rx.ReactiveWritable reactives support the set
function. This allows you to set a value to the stream, which is then emitted at the on
and onValue
events.
Some types of reactives provide bespoke functions for changing values. For example when using Rx.From.object
, you get an updateField
function for changing the value of a named field.