Skip to content

Property current value handling in case of re-activation #770

@raimohanska

Description

@raimohanska

Current behaviour or Properties when created with stream.toProperty() is that is a property is deactivated (all subscribers removed), it will maintain its current value while not connected to the underlying stream. This means that upon re-activation it will emit the possibly stale current value to it's new subscribers.

Here's a running example: https://codesandbox.io/s/property-with-eventstream-4h4sb?file=/src/App.tsx

This behavior is ok for some cases, but there are cases where you may prefer something else.

In some cases the current value could be obtainable with a single synchronous call. Use window scroll position for example. How to do that in Bacon.js? Well you can but it's not obvious. Here's how: https://codesandbox.io/s/topropertywithinitvaluefn-l0n8z?file=/src/App.tsx

Yet sometimes there's no feasible way to fetch a new value synchronously and you might prefer just not emitting a stale value, but instead emitting nothing until a fresh value can be provided. Also this is a simple solution in Bacon.js, which is far from obvious if you're unfamiliar with the library internals. Like this: https://codesandbox.io/s/topropertydropstalevalues-504gv

I'm considering whether these alternatives make sense to you as well and how should the API be shaped? This might be something for the 4.0 release.

In short, here are the implementations:

function toPropertyWithInitValueFn<A>(stream: B.EventStream<A>, initValueFn: () => A): B.Property<A> {
    return new B.Property(
        new B.Desc("Bacon", "toPropertyWithInitValueFn", []),
        sink => {
            sink(new B.Initial(initValueFn()));
            return stream.subscribeInternal(event => sink(event));
        }
    );
}

function toPropertyDropStaleValues<A>(stream: B.EventStream<A>): B.Property<A> {
    return new B.Property<A>(
        new B.Desc("Bacon", "toPropertyDropStaleValues", []),
        sink => stream.subscribeInternal(sink)
    );
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions