Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

implemented multi event callback

  • Loading branch information...
commit 897f91780c24bf18d88ceeb71c9729bf6b29e72c 1 parent 56858f0
Adriano Raiano authored
Showing with 41 additions and 10 deletions.
  1. +26 −3 lib/hub.js
  2. +14 −7 lib/observer.js
  3. +1 −0  package.json
29 lib/hub.js
View
@@ -26,12 +26,18 @@
// bus.emit('commands', data);
// });
-var EventEmitter = require('events').EventEmitter
+var EventEmitter2 = require('eventemitter2').EventEmitter2
, uuid = require('./uuid')
, Observer = require('./observer')
, Hub
, hubInstance;
+var eventEmitter = new EventEmitter2({
+ wildcard: true, // should the event emitter use wildcards.
+ delimiter: ':', // the delimiter used to segment namespaces, defaults to `.`.
+ maxListeners: 1000, // the max number of listeners that can be assigned to an event, defaults to 10.
+});
+
// ## Hub
Hub = function() {
@@ -42,7 +48,7 @@ Hub = function() {
// Inherit prototyp and extend it.
(function(S) {
- var P = S.prototype = new EventEmitter();
+ var P = S.prototype = eventEmitter;
// __init:__ initializes the hub.
//
@@ -54,6 +60,19 @@ Hub = function() {
var self = this;
// listen on events...
+ this.on('event:*', function(evt) {
+
+ var eventName = this.event.substr(6);
+
+ var commandCallback = self.observer.getPendingCommand(getCommandIdFunction(evt), eventName);
+
+ if (commandCallback) {
+ commandCallback(evt);
+ }
+
+ });
+
+ // listen on events...
this.on('events', function(evt) {
var commandCallback = self.observer.getPendingCommand(getCommandIdFunction(evt));
@@ -79,8 +98,12 @@ Hub = function() {
cmd.id = uuid().toString();
}
- if (callback) {
+ if (typeof callback === 'function') {
this.observer.observe(cmd.id, callback);
+ } else {
+ for(var m in callback) {
+ this.observer.observe(cmd.id, m, callback[m]);
+ }
}
this.emit('commands', cmd);
21 lib/observer.js
View
@@ -16,22 +16,29 @@ Observer.prototype = {
// __observe:__ observes a command by saving its callback.
//
- // `this.observe(commandId, callback)`
+ // `this.observe(commandId [, eventName], callback)`
//
// - __commandId:__ the command id
+ // - __eventName:__ the event name [optional]
// - __callback:__ `function(event){}`
- observe: function(commandId, callback) {
- this.commands[commandId] = callback;
+ observe: function(commandId, eventName, callback) {
+ if (typeof eventName === 'function') {
+ callback = eventName;
+ eventName = '';
+ }
+ this.commands[commandId + eventName] = callback;
},
// __getPendingCommand:__ returns the callback of the pending command.
//
- // `this.getPendingCommand(commandId)`
+ // `this.getPendingCommand(commandId[, eventName])`
//
// - __commandId:__ the command id
- getPendingCommand: function(commandId) {
- var callback = this.commands[commandId];
- delete this.commands[commandId];
+ // - __eventName:__ the event name [optional]
+ getPendingCommand: function(commandId, eventName) {
+ eventName = eventName || '';
+ var callback = this.commands[commandId + eventName];
+ delete this.commands[commandId + eventName];
return callback;
}
1  package.json
View
@@ -16,6 +16,7 @@
"node": ">= v0.4.0"
}
, "dependencies": {
+ "eventemitter2": ">= v0.4.8"
}
, "devDependencies": {
}
Please sign in to comment.
Something went wrong with that request. Please try again.