我正在尝试在基于浏览器的客户端中实现调试诊断,我需要检测推送到没有相应订户的培根总线的事件。
有什么想法吗?
答案 0 :(得分:0)
对于调试,您可以在加载bacon.js后添加类似的东西:
(function() {
var old, idCounter, fun;
old = Bacon.Bus;
idCounter = 0;
fun = function() {
var result, id, push;
result = old.apply(this, arguments);
if (result == null) {
result = this;
}
id = "" + (idCounter++);
push = result.push;
result.push = function(val) {
if (!this.hasSubscribers()){
console.log("unsubscribed push to bus " + id, val);
}
return push.apply(this, arguments);
};
return result;
};
fun.prototype = old.prototype;
Bacon.Bus = fun;
}());
然后将记录每个push()
到没有订户的总线:
var bus = new Bacon.Bus();
bus.push(42); // Output: unsubscribed push to bus 0 42
bus.log("foo"); // Add subscriber
bus.push(42); // Output: foo 42