Comparing version 1.0.2 to 1.1.0
@@ -14,4 +14,5 @@ (function( $ ) { | ||
$this.socket = io.connect(url, {secure: true}); | ||
$this.socket = io.connect(url); | ||
$this._trigger = $this.trigger; | ||
$this._bind = $this.bind; | ||
$this.events = {}; | ||
@@ -21,5 +22,5 @@ | ||
_this.socket.$emit = function() { | ||
$this.triggerHandler.call($this, arguments[0], Array.prototype.splice.call(arguments, 1)); | ||
_this.triggerHandler.call(_this, arguments[0], Array.prototype.splice.call(arguments, 1)); | ||
}; | ||
$this._trigger('eventHubReady'); | ||
_this._trigger('eventHubReady'); | ||
}); | ||
@@ -31,2 +32,10 @@ | ||
/* Tell event switch we're listening for a unicast event... */ | ||
$this.bind = function(eventName, func, args) { | ||
this._bind.call(this, eventName, func); | ||
if (typeof(args) !== 'undefined') { | ||
this.trigger('eventHub:on', eventName, args); | ||
} | ||
}; | ||
/* | ||
@@ -41,3 +50,3 @@ * An optional helper function to set up compiler-checkable event names | ||
this.events[eventName] = { | ||
bind: function(callback) { _this.bind.call(_this, eventName, callback); } | ||
bind: function(callback, args) { _this.bind.call(_this, eventName, callback, args); } | ||
, trigger: function() { | ||
@@ -44,0 +53,0 @@ Array.prototype.unshift.call(arguments, eventName); |
@@ -6,2 +6,3 @@ YUI().add('EventHub', function(Y) { | ||
this._fire = this.fire; | ||
this._on = this.on; | ||
this.socket = io.connect(url); | ||
@@ -11,2 +12,3 @@ this.socket.on('connect', function () { | ||
_this._fire('eventHubReady'); | ||
Y.Global.Hub = _this; | ||
}); | ||
@@ -17,2 +19,10 @@ this.fire = function() { | ||
/* Tell event switch we're listening for a unicast event... */ | ||
this.on = function(eventName, func, args) { | ||
this._on.apply(this, arguments); | ||
if (typeof(args) !== 'undefined') { | ||
this.fire('eventHub:on', eventName, args); | ||
} | ||
}; | ||
/* | ||
@@ -27,3 +37,3 @@ * An optional helper function to set up compiler-checkable event names | ||
this.events[eventName] = { | ||
on: function(callback) { _this.on.call(_this, eventName, callback); } | ||
on: function(callback, args) { _this.on.call(_this, eventName, callback, args); } | ||
, fire: function() { | ||
@@ -30,0 +40,0 @@ Array.prototype.unshift.call(arguments, eventName); |
@@ -9,4 +9,6 @@ module.exports = { | ||
this._emit = this.emit; // keep original emit method | ||
this.emit = function() { | ||
this._emit.apply(this, arguments); | ||
this._on = this.on; // keep original emit method | ||
this.events = {}; // keep original emit method | ||
this.emit = function() { | ||
this._emit.apply(this, arguments); // fire locally | ||
if (this.socket) { | ||
@@ -16,2 +18,10 @@ this.socket.emit.apply(this.socket, arguments); | ||
}; | ||
/* Tell event switch we're listening for a unicast event... */ | ||
this.on = function(eventName, func, args) { | ||
this._on.apply(this, arguments); | ||
if (typeof(args) !== 'undefined') { | ||
this.emit('eventHub:on', eventName, args); | ||
} | ||
}; | ||
}; | ||
@@ -29,3 +39,3 @@ | ||
//socket.$emit = function() { events.EventEmitter.prototype.emit.apply(_this, arguments); }; | ||
socket.$emit = function() {console.log('local emit');console.log(arguments); _this._emit.apply(_this, arguments); }; | ||
socket.$emit = function() { _this._emit.apply(_this, arguments); }; | ||
@@ -35,2 +45,21 @@ this._emit('eventHubReady'); | ||
/* | ||
* An optional helper function to set up compiler-checkable event names | ||
* var clickEvent = hub.addEvent('click'); | ||
* clickEvent.on(function(...) { ... } ); | ||
* clickEvent.fire({ foo: 'goo' }, ... ); | ||
*/ | ||
eventHubF.addEvent = function(eventName) { | ||
var _this = this; | ||
this.events[eventName] = { | ||
on: function(callback, args) { _this.on.call(_this, eventName, callback, args); } | ||
, emit: function() { | ||
Array.prototype.unshift.call(arguments, eventName); | ||
_this.emit.apply(_this, arguments); | ||
} | ||
}; | ||
return this.events[eventName]; | ||
}; | ||
return new eventHubF(); | ||
@@ -37,0 +66,0 @@ }, |
var eventHub = require('../../clients/server/eventClient.js').getClientHub('http://localhost:5883'); | ||
eventHub.on('eventHubReady', function() { console.log("EventHub ready!"); }); | ||
eventHub.on('click', function(data, callback) { | ||
console.log('GOT A CLICK Event'); | ||
console.log(data); | ||
callback('howdy from server', { mark: 'trostler' }); | ||
eventHub.on('eventHubReady', function() { | ||
eventHub.on('click', function(data, callback) { | ||
console.log('GOT A CLICK Event'); | ||
console.log(data); | ||
callback('howdy from server', { mark: 'trostler', pid: process.pid }); | ||
eventHub.emit('zot', process.pid); | ||
}, { type: 'unicast' }); | ||
// this goes to everyone BUT me... | ||
// eventHub.emit('eventClient:done', 'click'); | ||
eventHub.on('eventClient:done', function(event) { | ||
console.log('DONE LISTENING FOR ' + event); | ||
// wait for us to finish processing any current event and then.... | ||
process.exit(0); | ||
}); | ||
}); |
var port = process.env['npm_package_config_' + port] || 5883 | ||
, io = require('socket.io').listen(port) | ||
, sockets = {} | ||
, events = {} | ||
, toArray = function (enu) { | ||
@@ -47,11 +48,44 @@ var arr = [], i; | ||
socket.on('disconnect', function() { console.log('DISCONNECT'); }); | ||
// All sockets connected to hub just broadcast their events to everyone else.... | ||
socket.$emit = function() { | ||
if (arguments[0] !== 'newListener') { | ||
for (sock in sockets) { | ||
// we'll use the emit from this socket BUT set 'this' to be the actual socket we wanna use | ||
// we send oursevles because 'broadcast' does not handle acknowledgements | ||
socket.emit.apply(sockets[sock], arguments); | ||
var ss, event, i; | ||
if (arguments[0] === 'disconnect') { | ||
delete sockets[socket.id]; | ||
for (event in events) { | ||
if (events[event] === socket.id) { | ||
events[event] = null; | ||
} | ||
} | ||
} | ||
if (arguments[0] === 'eventHub:on') { | ||
eventName = arguments[1]; | ||
args = arguments[2]; | ||
if (args.type === 'unicast') { | ||
console.log('set unicast for ' + eventName + ' to ' + socket.id); | ||
if (events[eventName]) { | ||
// tell previous dude he's been replaced | ||
console.log('tell ' + events[eventName] + ' they are done with ' + eventName); | ||
socket.emit.call(sockets[events[eventName]], 'eventClient:done', eventName); | ||
} | ||
events[eventName] = socket.id; | ||
} | ||
} else { | ||
if (arguments[0] !== 'newListener') { | ||
if (events[arguments[0]]) { // UNICAST | ||
ss = sockets[events[arguments[0]]]; | ||
socket.emit.apply(ss, arguments); | ||
} else { // BROADCAST | ||
for (sock in sockets) { | ||
// we'll use the emit from this socket BUT set 'this' to be the actual socket we wanna use | ||
// we send oursevles because 'broadcast' does not handle acknowledgements | ||
if (arguments[0] !== 'eventClient:done' || sock !== socket.id) { | ||
socket.emit.apply(sockets[sock], arguments); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}; | ||
@@ -58,0 +92,0 @@ |
@@ -13,3 +13,3 @@ { | ||
], | ||
"version": "1.0.2", | ||
"version": "1.1.0", | ||
"author": "Mark Ethan Trostler <mark@zzo.com>", | ||
@@ -16,0 +16,0 @@ "preferGlobal": true, |
@@ -143,2 +143,11 @@ EventHub | ||
var eventHub = require('EventHub/clients/server/eventClient.js').getClientHub('http://localhost:5883'); | ||
eventHub.on('eventHubReady', function() { console.log("EventHub ready!"); }); | ||
eventHub.on('click', function(data, callback) { | ||
console.log('GOT A CLICK Event'); | ||
console.log(data); | ||
callback('howdy from server', { mark: 'trostler' }); | ||
}); | ||
Putting It All Together | ||
@@ -151,1 +160,34 @@ ----------------------- | ||
## Event Metadata | ||
You can attach metadata to the 'on' or 'bind' eventHub method which will signal the hub to act like a switch for these messages. | ||
hub.on('eventName', callback, { type: 'unicast' }); | ||
### Unicast | ||
If { type: 'unicast' } is specified as the metadata (the only thing currently supported) each time a new listener comes online with this metadata the event hub will ONLY pass the named event to ONLY this listener. Any current listener will receive a special event 'eventHub:done' to notify the older listener it will no longer receive events of that type. | ||
hub.on('eventClient:done', function(eventName) { | ||
console.log('I will no longer receive ' + eventName + ' events!'); | ||
}); | ||
Note the event hub itself emits that event. | ||
That would be a good time to finish up any event handling the listener is currently doing & then perhaps exit. Makes deploying a breeze! | ||
This aids deployment by allowing safe shutdown of older modules/handlers and bring up of new without dropping any events | ||
### Broadcast | ||
YOU can signal 'eventClient:done' yourself as a hint/shove to listeners of braodcasted (the default) events to shut themselves down. | ||
hub.fire('eventClient:done', 'someBroadcastedEvent'); | ||
The event hub will broadcast this event to every connected client (except the sending one). The event hub will not take any other action! It's up to your listeners to do the right thing - which is probably 'removeListener' followed by exiting after servicing any current events. | ||
hub.on('eventClient:done', function(eventName) { | ||
hub.removeAllListeners(eventName); | ||
}); | ||
This aids deployment by allowing safe shutdown of older modules/handlers and bring up of new without dropping any events |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
23277
14
299
192