Permalink
Browse files

first post

  • Loading branch information...
0 parents commit b226c3b8bc7814da1d663440c673e4bb85e0ed5b @tedsuo tedsuo committed Nov 18, 2012
Showing with 148 additions and 0 deletions.
  1. +5 −0 .gitignore
  2. +4 −0 Makefile
  3. +26 −0 package.json
  4. +32 −0 test/transactor.js
  5. +81 −0 transactor.js
5 .gitignore
@@ -0,0 +1,5 @@
+/node_modules
+**.sw*
+/config.js
+**.DS_Store
+npm-debug.log
4 Makefile
@@ -0,0 +1,4 @@
+test:
+ ./node_modules/.bin/mocha -R spec -r chai test/*.js
+
+.PHONY: test
26 package.json
@@ -0,0 +1,26 @@
+{
+ "name": "transactor",
+ "author": "tedsuo",
+ "license": "BSD",
+ "version": "0.0.0",
+ "description": "realtime message passing, with transactions",
+ "main": "transactor.js",
+ "repository": {
+ "type": "git",
+ "url": "git://github.com/ethersheet-collective/transactor.git"
+ },
+ "dependencies": {
+ "socket.io": ">=0.9.5"
+ },
+ "devDependencies": {
+ "mocha": ">=1.3.0",
+ "chai": "*",
+ "sinon": "*"
+ },
+ "scripts": {
+ "test": "make test"
+ },
+ "directories" : {
+ "lib" : "./lib"
+ }
+}
32 test/transactor.js
@@ -0,0 +1,32 @@
+var Transactor = require('transactor');
+var EventEmitter = require('events').EventEmitter;
+var assert = require('chai').assert;
+
+describe('Transactor', function(){
+ var server, server_spy, trans;
+
+ beforeEach(function(){
+ trans = new Transactor(['channel_1']);
+ });
+
+ describe('When a transaction occurs on an event', function(){
+ var socket, socket_spy;
+
+ beforeEach(function(){
+ socket = new EventEmitter();
+ trans.addSocket(socket);
+ });
+
+ it('the transaction handler interface conforms to function(channel,data,socket,cb)', function(done){
+ var test_data = {foo:'bar'};
+ trans.onTransaction(function(channel,data,sock,cb){
+ assert.equal(channel,'channel_1');
+ assert.equal(data,test_data);
+ assert.deepEqual(socket,sock);
+ assert.isFunction(cb);
+ done();
+ });
+ socket.emit('channel_1',test_data);
+ });
+ });
+});
81 transactor.js
@@ -0,0 +1,81 @@
+var inherits = require('util').inherits;
+var EventEmitter = require('events').EventEmitter;
+
+var Transactor = function(channels){
+ this.channels = channels || [];
+ this.transaction_handler = this.defaultEventHandler;
+ this.connection_handler = this.defaultEventHandler;
+ this.disconnection_handler = this.defaultEventHandler;
+};
+
+inherits(Transactor,EventEmitter);
+module.exports = Transactor;
+
+// handler interface: function eventHandler(... socket,cb)
+// callback interface: function cb(err,socket)
+Transactor.prototype.defaultEventHandler = function(){
+ arguments[arguments.length - 1](null,arguments[arguments.length - 2]);
+};
+
+Transactor.prototype.initializeServer = function(){
+ var trans = this;
+ this.io.sockets.on('connection', function(socket){
+ trans.addSocket(socket);
+ });
+};
+
+Transactor.prototype.addSocket = function(socket){
+ var trans = this;
+
+ // apply the supplied connection handler to validate the connection
+ this.connection_handler(socket,function(err,socket){
+ if(err) throw err;
+
+ // add the supplied transaction handler to each channel on the socket
+ trans.channels.forEach(function(channel){
+ socket.on(channel,function(data){
+ trans.transaction_handler(channel,data,socket,function(err,event){
+ if(err) return socket.emit('error',err,event);
+ trans.tellOthers(channel,event,socket);
+ });
+ });
+ });
+
+ // add the supplied disconnection handler
+ socket.on('disconnect', function(){
+ trans.disconnection_handler(socket,function(err,socket){
+ if(err) throw err;
+ trans.emit('disconnect',socket);
+ trans.tellOthers('disconnect',{},socket);
+ });
+ });
+
+ });
+};
+
+Transactor.prototype.onTransaction = function(transaction_handler){
+ this.transaction_handler = transaction_handler;
+};
+
+Transactor.prototype.onConnection = function(connection_handler){
+ this.connection_handler = connection_handler;
+};
+
+Transactor.prototype.onDisconnection = function(disconnection_handler){
+ this.disconnection_handler = disconnection_handler;
+};
+
+Transactor.prototype.tellMe = function(channel,params,socket){
+ socket.emit(channel,params);
+};
+
+Transactor.prototype.tellOthers = function(channel,params,socket){
+ socket.broadcast.emit(channel,params);
+};
+
+Transactor.prototype.tellAll = function(channel,params,socket){
+ socket.emit(channel,params);
+ socket.broadcast.emit(channel,params);
+};
+
+

0 comments on commit b226c3b

Please sign in to comment.