Skip to content

Instantly share code, notes, and snippets.

@isaacs
Created July 25, 2010 00:05
Show Gist options
  • Save isaacs/489119 to your computer and use it in GitHub Desktop.
Save isaacs/489119 to your computer and use it in GitHub Desktop.
/*
© 2010 Isaac Z. Schlueter
Licensed under DWTFPL - No Rights Reserved
Please don't hurt yourself.
*/
var EE = require("events").EventEmitter
, ee = EE.prototype
, SE = SuppressibleEmitter
, se = SE.prototype
, sys = require("sys")
exports.SuppressibleEmitter = SE
exports.makeSuppressible = function (e) {
if (e instanceof SE) return
if (!(e instanceof EE)) throw new Error(
"Can only make EventEmitters suppressible, not other kinds of things")
e.__proto__ = se
SE.call(this)
}
function SuppressibleEmitter () {
EE.apply(this)
this._suppressed = {}
}
sys.inherits(SE, EE)
se.suppress = function (ev) {
this._suppressed[ev] = true
// more fanciness here for buffering, suppressOnce, etc.
}
se.unsupress = function (ev) {
delete this._suppressed[ev]
// add fanciness for buffering, etc.
}
se.emit = function (ev) {
if (this._suppressed && this._suppressed[ev]) {
// add fanciness for buffering, suppressOnce, etc.
return
}
// kludge because Function#apply is slow
// todo: benchmark to see if it's worth it.
return ee.emit.apply(this, arguments)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment