不要怂,就是干,撸起袖子干!

Commit ef81d002 by Jan Aagaard Meier

Merge pull request #509 from mweibel/master

CustomEventEmitter: Remove setTimeout in favor of process.nextTick()
2 parents 477289e6 48fe9864
Showing with 21 additions and 19 deletions
var util = require("util") var util = require("util")
, EventEmitter = require("events").EventEmitter , EventEmitter = require("events").EventEmitter;
module.exports = (function() { module.exports = (function() {
var CustomEventEmitter = function(fct) { var CustomEventEmitter = function(fct) {
this.fct = fct this.fct = fct;
var self = this;
} }
util.inherits(CustomEventEmitter, EventEmitter) util.inherits(CustomEventEmitter, EventEmitter);
CustomEventEmitter.prototype.run = function() { CustomEventEmitter.prototype.run = function() {
var self = this var self = this;
// delay the function call and return the emitter process.nextTick(function() {
setTimeout(function(){ if (self.fct) {
self.fct.call(self, self) self.fct.call(self, self)
}, 1) }
}.bind(this));
return this
return this;
} }
CustomEventEmitter.prototype.success = CustomEventEmitter.prototype.success =
CustomEventEmitter.prototype.ok = CustomEventEmitter.prototype.ok =
function(fct) { function(fct) {
this.on('success', fct) this.on('success', fct);
return this return this;
} }
CustomEventEmitter.prototype.failure = CustomEventEmitter.prototype.failure =
CustomEventEmitter.prototype.fail = CustomEventEmitter.prototype.fail =
CustomEventEmitter.prototype.error = CustomEventEmitter.prototype.error =
function(fct) { function(fct) {
this.on('error', fct) this.on('error', fct);
return this return this;
} }
CustomEventEmitter.prototype.done = CustomEventEmitter.prototype.done =
CustomEventEmitter.prototype.complete = CustomEventEmitter.prototype.complete =
function(fct) { function(fct) {
this.on('error', function(err) { fct(err, null) }) this.on('error', function(err) { fct(err, null) })
.on('success', function(result) { fct(null, result) }) .on('success', function(result) { fct(null, result) });
return this return this;
} }
return CustomEventEmitter return CustomEventEmitter;
})() })();
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!