bacon.js:holdWhen 和 onValue 的奇怪行为
bacon.js: Strange behaviour with holdWhen and onValue
在这里查看工作中的 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
属性 的行为就不同了,我不再收到更新。
为什么这个看似惰性的变化会使系统发生如此巨大的变化?当然,系统的行为不应该依赖于某人是否在过去的某个时刻订阅和取消订阅了流模板?
此行为已被确认为已在 0.7.67 中修复的错误。
详情见here。
在这里查看工作中的 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
属性 的行为就不同了,我不再收到更新。
为什么这个看似惰性的变化会使系统发生如此巨大的变化?当然,系统的行为不应该依赖于某人是否在过去的某个时刻订阅和取消订阅了流模板?
此行为已被确认为已在 0.7.67 中修复的错误。
详情见here。