Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

flatMapLatest(Bacon.once) is unlike flatMap(Bacon.once) #758

Open
semmel opened this issue Dec 16, 2019 · 2 comments
Open

flatMapLatest(Bacon.once) is unlike flatMap(Bacon.once) #758

semmel opened this issue Dec 16, 2019 · 2 comments

Comments

@semmel
Copy link
Member

semmel commented Dec 16, 2019

It swallows startWith and mapEnd values:

expected behaviour

Calling .startWith or .mapEnd multiple times on a stream injects those values just fine in the right order:

Bacon.fromArray([1, 2, 3])
.startWith(-1)
.startWith(-2)
.mapEnd(10)
.mapEnd(11)
.flatMap(x => Bacon.once(x))
// -> -2, -1, 1, 2, 3, 10, 11

observed behaviour

Bacon.fromArray([1, 2, 3])
.startWith(-1) // <- missing after flatMapLatest(Bacon.once)
.startWith(-2)
.mapEnd(10) // <- missing after flatMapLatest(Bacon.once)
.mapEnd(11)
.flatMapLatest(x => Bacon.once(x))
// -> -2, 1, 2, 3, 11

I'd expect .flatMapLatest(Bacon.once) to behave like .flatMap(Bacon.once).

Somehow the following startWith and mapEnd events are delivered too quickly to allow Bacon.once to emit just it's only value.

@semmel
Copy link
Member Author

semmel commented Sep 15, 2020

Found a workaround: delay(0)!

Bacon.concatAll([
  Bacon.once(-2), 

  Bacon.fromArray([1, 2, 3])
  .startWith(-1)
  .mapEnd(10)
  .delay(0),    // <---

  Bacon.once(11)
])
.flatMapLatest(x => Bacon.once(x))
// -> -2, -1, 1, 2, 3, 10, 11

@semmel
Copy link
Member Author

semmel commented Sep 11, 2023

I guess the eagerness of Bacon streams is a problem;

const
  // source streams
  t1 = Bacon.once("foo"),
  t2 = Bacon.once("foo"),
  t3 = Bacon.once("foo"),
  // derived streams
  d1 = t1
    .map(s => s.toUpperCase())
    .startWith("zero"),
  d2 = t2
    .map(s => s.toUpperCase()),
  d3 = t3
    .map(s => s.toUpperCase())
    .startWith("zero");

d1.onValue(val => { console.log("d1:", val); });
// -> "zero", "FOO", <end>    (Great!)

t2.onValue(() => {});
d2.onValue(val => { console.log("d2:", val); });
// -> "FOO", <end>   (Great!)

t3.onValue(() => {});
d3.onValue(val => { console.log("d3:", val); });
// -> "zero", <end>    (WTF??!)

It shows that e.g. Bacon.once breaks referential transparency. I think with lazy reactive streams you can have referential transparency.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant