paldepind / flyd

The minimalistic but powerful, modular, functional reactive programming library in JavaScript.
MIT License
1.56k stars 85 forks source link

switchLatest: stream subscribers called more than once #120

Open AlexGalays opened 8 years ago

AlexGalays commented 8 years ago

Consider the following snippet (directly copy-pastable in requirebin) :


const flyd = require('flyd')
const switchLatest = require('flyd/module/switchlatest')

const stream = flyd.stream(3)
const mapped = switchLatest(
  flyd.map(function(x) { return flyd.stream(x * 2) }, stream)
 )

flyd.on(function(value) {
  console.log('stream ended', value);
}, stream.end)

flyd.on(function(value) {
  console.log('mapped ended', value);
}, mapped.end)

flyd.on(function(value) {
  console.log('stream value', value);
}, stream)

flyd.on(function(value) {
  console.log('mapped value', value);
}, mapped)

stream(4)
stream(5)

It produces the following console ouput:

stream value 3
stream value 4
mapped value 8
mapped value 8
stream value 5
mapped value 10
mapped value 10

Why is the mapped stream called twice for every change to the source stream?

anodynos commented 7 years ago

It's most probably a bug with requirebin it self.

I couldn't reproduce it anywhere else, including locally (node@6.9 with all flyd @ 1.x and above), on runkit and jsfiddle browserified.