Permalink
Browse files

added possibility to create own instance of hub and command

  • Loading branch information...
1 parent 89c65b1 commit 01a18008db418f68bbbd5da0fa1fde095721a60b @adrai committed Dec 18, 2011
Showing with 87 additions and 16 deletions.
  1. +17 −2 README.markdown
  2. +38 −0 example/exampleInstance.js
  3. +23 −7 lib/command.js
  4. +9 −7 lib/hub.js
View
@@ -8,11 +8,26 @@ Project goal is to provide a simple command/event handling for evented systems l
$ npm install nodeEventedCommand
-# Usage
+# Simple usage
// get the hub
var hub = require('nodeEventedCommand').hub;
+ // initialize the hub by passing the function that gets the command id from the event
+ hub.init(
+ function(evt) {
+ return evt.commandId;
+ }
+ );
+
+ // and the command
+ var Command = require('nodeEventedCommand').Command;
+
+# Advanced usage with own instance
+
+ // get the hub
+ var hub = require('nodeEventedCommand').hub.create();
+
// initialize the hub by passing the function that gets the command id from the event
hub.init(
function(evt) {
@@ -23,7 +38,7 @@ Project goal is to provide a simple command/event handling for evented systems l
);
// and the command
- var Command = require('nodeEventedCommand').Command;
+ var Command = require('nodeEventedCommand').Command.create(hub);
## Wire up commands and events
View
@@ -0,0 +1,38 @@
+// get the hub
+var hub = require('../lib/hub').create();
+
+// initialize the hub by passing the function that gets the command id from the event
+hub.init(
+ function(evt) {
+ return evt.id;
+ }
+);
+
+hub.on('commands', function(data) {
+ if (data.command === 'multi') {
+ hub.emit('event:event1', {event: 'event1', id: data.id});
+ hub.emit('event:event2', {event: 'event2', id: data.id});
+ }
+
+ hub.emit('events', data);
+});
+
+// and the command
+var Command = require('../lib/command').create(hub);
+
+(new Command({
+ command: 'bla'
+})).emit(function(evt) {
+ console.log(evt);
+});
+
+(new Command({
+ command: 'multi'
+})).emit({
+ event1: function(evt) {
+ console.log(evt);
+ },
+ event2: function(evt) {
+ console.log(evt);
+ }
+});
View
@@ -22,13 +22,10 @@
//
// });
-var hub = require('./hub')
- , Command;
-
// ## Command
-Command = function(cmd) {
+var Command = function(data) {
- this.cmd = cmd;
+ this.data = data;
};
@@ -41,10 +38,29 @@ Command.prototype = {
// - __callback:__ `function(event){}` [optional]
emit: function(callback) {
- hub.sendCommand(this.cmd, callback);
+ this.hub.sendCommand(this.data, callback);
}
};
-module.exports = Command;
+Command.create = function(hub) {
+
+ var NewCommand = function(data) {
+ this.data = data;
+ };
+
+ for (var member in Command) {
+ NewCommand[member] = Command[member];
+ }
+
+ for (var pmember in Command.prototype) {
+ NewCommand.prototype[pmember] = Command.prototype[pmember];
+ }
+
+ NewCommand.prototype.hub = hub;
+ return NewCommand;
+
+}
+
+module.exports = Command.create(require('./hub'));
View
@@ -32,12 +32,6 @@ var EventEmitter2 = require('eventemitter2').EventEmitter2
, 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() {
@@ -48,7 +42,11 @@ Hub = function() {
// Inherit prototyp and extend it.
(function(S) {
- var P = S.prototype = eventEmitter;
+ var P = S.prototype = 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.
+ });
// __init:__ initializes the hub.
//
@@ -116,4 +114,8 @@ if (!hubInstance) { // just to be shure...
hubInstance = new Hub();
}
+hubInstance.create = function() {
+ return new Hub();
+};
+
module.exports = hubInstance;

0 comments on commit 01a1800

Please sign in to comment.