/
eventer.js
71 lines (63 loc) · 2.13 KB
/
eventer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
var util = require('util');
var EventEmitter = require('events').EventEmitter;
var defaultError = function () {};
var Eventer = module.exports = function(hosting){
var self = this;
self.hosting = hosting;
self.on('error', defaultError);
};
util.inherits(Eventer, EventEmitter);
Eventer.description = 'A wrapper around EventEmitter to provide some high level functionality and eased useage pattern within MongoMQ.';
Eventer.prototype.call = function(eventName, err, response){
var self = this;
self.emit(eventName, err, response||self.hosting);
};
Eventer.prototype.call.description = 'Calls an event.';
Eventer.prototype.registerTo = function(what){
var self = this, keys = Object.keys(self), i, l=keys.length, key, value;
for(i=0; i<l; i++){
key = keys[i];
value = self[key];
if(typeof(value)==='function'){
what[key]=(function(f){
return function(){
f.apply(self, arguments);
}
})(value);
}
}
};
Eventer.prototype.call.description = 'Adds the Eventer methods to the provided object through closures so they can be used directly.';
Eventer.prototype.checkNoError = function(err, callback, noThrow){
var self = this, handlers = self.listeners('error'), handlerCount = handlers.length, hasCallback = typeof(callback)==='function';
if(!err){
return true;
}
if(!(err instanceof Error)){
err = new Error(err);
}
if(handlerCount>0){
self.emit('error', err, self.hosting);
}
if(hasCallback){
callback(err);
}
if((handlerCount===0)&&(!noThrow)&&(!hasCallback)){
throw err.toString();
}
return false;
};
Eventer.prototype.checkNoError.description = 'Checks to see if there is an error, and if their is handles it appropriatly.';
Eventer.prototype.surface = function(what){
what = what instanceof Array?what:[what];
var self = this, i, l = what.length, event;
for(i=0; i<l; i++){
event = what[i];
self.hosting[event] = (function(eventName){
return function(callback){
self.on(eventName, callback);
};
})(event);
}
};
Eventer.prototype.surface.description = 'Used to surface a specific set of event wrappers to the hosted object.';