From 4376989df426e04d065610a8f8d8f690245e26bc Mon Sep 17 00:00:00 2001 From: reality Date: Thu, 24 Jan 2013 18:03:14 +0000 Subject: [PATCH] event majigger as per [#205] --- modules/event/event.js | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 modules/event/event.js diff --git a/modules/event/event.js b/modules/event/event.js new file mode 100644 index 0000000..83fb822 --- /dev/null +++ b/modules/event/event.js @@ -0,0 +1,28 @@ +/** + * Module Name: event + * Description: Allow other modules to emit events and that + */ +var _ = require('underscore')._; + +var event = function(dbot) { + this.dbot = dbot; + this.hooks = {}; + this.api = { + 'addHook': function(eventName, callback) { + if(!_.has(this.hooks, eventName)) this.hooks[eventName] = []; + this.hooks[eventName].push(callback); + }, + + 'emit': function(eventName, args) { + if(_.has(this.hooks, eventName)) { + _.each(this.hooks[eventName], function(callback) { + callback.apply(callback.module, args); + }); + } + } + }; +} + +exports.fetch = function(dbot) { + return new event(dbot); +};