Skip to content

flatMapLatest spawns next substream before the previous is disposed #783

@semmel

Description

@semmel

Sometimes flatMapLatest(fn) creates the substream fn(x) before the past substream got disposed.

I did not expect this behaviour. When I used tasks.flatMapLatest(sendTaskToWebWorkerAndWaitForCancellationOrResult) to feed tasks into a single WebWorker thread, the webworker did sometimes receive a new task before the previous got cancelled.

Also easy to reproduce using fromArray.

The reason is that in the implementation, which is roughly

flatMapLatest(obs, fn) = obs.flatMap(x => fn(x).takeUntil(obs));

It is unclear if a new x value first cancels the previous substream fn(x).takeUntil(obs) or first calls fn(x) to create the next substream.

An implementation

flatMapLatest(obs, fn) = obs.flatMap(x => B.later(0, x).map(fn).takeUntil(obs));

might fix it.

However, I don't know if I am aware of all use cases. Otherwise I might attempt a PR.

Do you think it's worth fixing?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions