Permalink
Browse files

document a bit

  • Loading branch information...
1 parent 1ba1717 commit 1688892a024f19fddd071519acf090cc5e01a2a2 @adrai committed Feb 1, 2012
Showing with 117 additions and 20 deletions.
  1. +10 −1 README.markdown
  2. +30 −0 lib/command.js
  3. +60 −19 lib/hub.js
  4. +17 −0 lib/observer.js
View
@@ -12,6 +12,15 @@ Project goal is to provide a simple command/event handling for evented systems l
// 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) {
+ var idEndIndex = evt.id.indexOf('_event');
+ var id = evt.id.substring(0, idEndIndex);
+ return id;
+ }
+ );
// and the command
var Command = require('nodeEventedCommand').Command;
@@ -31,7 +40,7 @@ Project goal is to provide a simple command/event handling for evented systems l
## Send commands
var cmd = new Command({
- name: 'changePerson',
+ command: 'changePerson',
payload: {
id: 8,
name: 'my name'
View
@@ -1,6 +1,31 @@
+// lib/command.js v0.0.1
+// (c) 2012 Adriano Raiano (adrai); under MIT License
+
+// This is the wrapper object for the commands.
+//
+// __Example:__
+//
+// var cmd = new Command({
+// command: 'changePerson',
+// payload: {
+// id: 8,
+// name: 'my name'
+// }
+// });
+//
+// // emit it
+// cmd.emit();
+//
+//
+// // if you want to observe the command pass a callback
+// cmd.emit(function(evt) {
+//
+// });
+
var hub = require('./hub')
, Command;
+// ## Command
Command = function(cmd) {
this.cmd = cmd;
@@ -9,6 +34,11 @@ Command = function(cmd) {
Command.prototype = {
+ // __emit:__ sends this command to the hub.
+ //
+ // `this.emit(callback)`
+ //
+ // - __callback:__ `function(event){}` [optional]
emit: function(callback) {
hub.sendCommand(this.cmd, callback);
View
@@ -1,42 +1,83 @@
+// lib/hub.js v0.0.1
+// (c) 2012 Adriano Raiano (adrai); under MIT License
+
+// The hub is the module that have to wire up the commands and events.
+//
+// __Example:__
+//
+// var hub = require('nodeEventedCommand').hub;
+//
+// // initialize the hub by passing the function that gets the command id from the event
+// hub.init(
+// function(evt) {
+// var idEndIndex = evt.id.indexOf('_event');
+// var id = evt.id.substring(0, idEndIndex);
+// return id;
+// }
+// );
+//
+// // pass in events from your bus
+// bus.on('events', function(data){
+// hub.emit('events', data);
+// });
+//
+// // pass commands to bus
+// hub.on('commands', function(data) {
+// bus.emit('commands', data);
+// });
+
var EventEmitter = require('events').EventEmitter
, uuid = require('./uuid')
, Observer = require('./observer')
, Hub
, hubInstance;
+// ## Hub
Hub = function() {
this.observer = new Observer();
- var self = this;
+};
- this.on('events', function(evt) {
-
- var getCommandId = function(e) {
- var idEndIndex = e.id.indexOf('_event');
- var id = e.id.substring(0, idEndIndex);
- return id;
- };
+// Inherit prototyp and extend it.
+(function(S) {
- var commandCallback = self.observer.getPendingCommand(getCommandId(evt));
+ var P = S.prototype = new EventEmitter();
- if (commandCallback) {
- commandCallback(evt);
- }
+ // __init:__ initializes the hub.
+ //
+ // `this.init(getCommandIdFunction)`
+ //
+ // - __getCommandIdFunction:__ `function(event){}`
+ P.init = function(getCommandIdFunction) {
+
+ var self = this;
- });
+ // listen on events...
+ this.on('events', function(evt) {
+
+ var commandCallback = self.observer.getPendingCommand(getCommandIdFunction(evt));
-};
+ if (commandCallback) {
+ commandCallback(evt);
+ }
-// Inherit prototyp and extend it.
-(function(S) {
+ });
- var P = S.prototype = new EventEmitter();
+ };
+ // __sendCommand:__ sends a command.
+ //
+ // `this.sendCommand(cmd, callback)`
+ //
+ // - __cmd:__ the command
+ // - __callback:__ `function(event){}` [optional]
P.sendCommand = function(cmd, callback) {
- // get id...
- cmd.id = uuid().toString();
+ if (!cmd.id) {
+ // get id...
+ cmd.id = uuid().toString();
+ }
if (callback) {
this.observer.observe(cmd.id, callback);
View
@@ -1,5 +1,11 @@
+// lib/observer.js v0.0.1
+// (c) 2012 Adriano Raiano (adrai); under MIT License
+
+// The command observer... observes a command until an event is coming...
+
var Observer;
+// ## Observer
Observer = function() {
this.commands = {};
@@ -8,10 +14,21 @@ Observer = function() {
Observer.prototype = {
+ // __observe:__ observes a command by saving its callback.
+ //
+ // `this.observe(commandId, callback)`
+ //
+ // - __commandId:__ the command id
+ // - __callback:__ `function(event){}`
observe: function(commandId, callback) {
this.commands[commandId] = callback;
},
+ // __getPendingCommand:__ returns the callback of the pending command.
+ //
+ // `this.getPendingCommand(commandId)`
+ //
+ // - __commandId:__ the command id
getPendingCommand: function(commandId) {
return this.commands[commandId];
}

0 comments on commit 1688892

Please sign in to comment.