bacon.js:holdWhen和onValue的奇怪行为

bacon.js: Strange behaviour with holdWhen and onValue

本文关键字:onValue js holdWhen bacon      更新时间:2023-09-26

在这里查看工作的CodePen:http://codepen.io/djskinner/pen/JdpwyY

// Animation start events push here
var startBus = new Bacon.Bus();
// Animation end events push here
var endBus = new Bacon.Bus();
// Balance updates push here
var balanceBus = new Bacon.Bus();
// A Property that determines if animating or not    
var isAnimating = Bacon.update(false,
    [startBus], function() { return true; },
    [endBus], function() { return false; }
);
// Only update the displayBalance when not animating
var displayBalance = Bacon.update(0,
    [balanceBus.holdWhen(isAnimating)], function(previous, x) {
        return x;
    }
);
setTimeout(function() {
  var streamTemplate = Bacon.combineTemplate({
    balance: displayBalance
  });
  // Uncommenting this block changes the way the system behaves
  // streamTemplate.onValue(function(initialState) {
  //   console.log(initialState);
  //})();
  // Print the displayBalance
  streamTemplate.onValue(function(v) {
    console.log(v.balance);
  });
});

按下平衡按钮会生成一个新的随机数。创建一个使用holdWhen限制余额更新的属性,直到isAnimating属性变为false。

如果我有兴趣获得streamTemplate的初始状态,我可能会获得该值并立即取消订阅:

streamTemplate.onValue(function(initialState) {
    console.log(initialState);
})();

但是,一旦我这样做,displayBalance属性的行为就不同了,我就不再接收更新。

为什么这种看似惰性的变化会使系统发生如此剧烈的变化?当然,系统的行为不应该取决于某人是否在过去的某个时候订阅和取消订阅了streamTemplate?

此行为已被确认为一个已在0.7.67中修复的错误。

请参阅此处了解详细信息。