Skip to content

Last values

Some reactives allow reading the last value that passed through it with .last(). In some scenarios is can be more convenient to poll this function instead of using a callback to receive values. If the reactive you’re using doesn’t support this, there are some options:

Initial values

When creating a reactive, you can sometimes specify an initial value. This lets you call last() on the reactive and get a non-undefined value. For example in the below case we have an empty point as the initial value.

const pointermove = Rx.From.event(document, `pointermove`, { x:0, y:0 });

With value

Rx.withValue allows any reactive to be wrapped with an initial value, so it’s immediately readable. When - or if - the reactive emits a value, that takes the place of the initial value. Thus it provides a caching of the last received value.

const pointermove = Rx.withValue(Rx.From.event(document, `pointermove`), { initial: {x:0, y:0 }});

A big benefit of using an initial value where possible is that there is better type hinting. Your editor will know that the reactive emits values of the same type as the initial value.