2

I am trying to implement a debug diagnostic in a browser based client and I need to detect events pushed to a Bacon bus that have no corresponding subscriber(s).

Any ideas ?

Huangism
  • 16,278
  • 7
  • 48
  • 74
Nicholas
  • 15,916
  • 4
  • 42
  • 66

1 Answers1

0

For debugging you could add something like this after loading 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;

}());

Then every push() to a bus with no subscriber(s) will be logged:

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
hhelwich
  • 200
  • 6