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

Initial Events of a subscribed Property are replayed in a later Subscription which is debounced #753

Closed
semmel opened this issue Dec 3, 2019 · 2 comments

Comments

@semmel
Copy link
Member

semmel commented Dec 3, 2019

This error took me hours to hunt down to this simplified test case 😣

The initial value of the source property appears twice in the derived stream:

var source = Bacon.constant("Y"); 
var unlog = source.log("source"); //necessary that original is already subscribed
var timer = setTimeout(function() {
   var derived = 
      source.doAction(x => console.log(`doAction:${x}`))
      .debounce(100);  //necessary
   derived.log("derived");
}, 20); //any timeout will do
// Output:
// source – "Y" 
// source – "<end>"
// doAction:Y
// doAction:Y  <--- DUPLICATE VALUE!
// derived – "Y" 
// derived – "<end>" 

Tested in Node.js and the Browser with the new ES6 build.

On closer inspection it is indeed the initial event (Perhaps getting "bounced" too often by PropertyDispatcher.subscribe, as commented in propertydispatcher.ts?)

var source = Bacon.constant("Y"); 
var unlog = source.log("source");
var timer = setTimeout(function() {
   var derived = source
     .withStateMachine([], (acc, event) => {
	   if (!event.hasValue) { return [acc, [event]]; }
	   console.log(`isInitial=${Bacon.isInitial(event)}, value=${event.value}`);
	   return [event.value, [event]];
      })
      .debounce(100);
   derived.log("derived");
}, 20);
// ...
// isInitial=true, value=Y
// isInitial=true, value=Y
// ...

Unfortunately the quick workaround filtering with .skipDuplicates() is not possible, since it's implemented to let Initial events pass. However with .withStateMachine it's possible to write your own .skipDuplicates.

It's not obvious to me how to fix it: PropertyDispatcher.subscribe, UpdateBarrier and stuff are pretty core Baconjs.
😕

raimohanska added a commit that referenced this issue Dec 7, 2019
@raimohanska
Copy link
Contributor

Is fixed in 3.0.12. The problem was in addPropertyInitValueToStream which had two separate subscriptions to the source Property. It's now called transformPropertyChanges and avoids the double subscription.

@semmel
Copy link
Member Author

semmel commented Dec 15, 2019

@raimohanska Thanks for the quick fix!
I am very glad! 😄
Still have to run my tests though.. 🤞

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

2 participants