Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

first post

  • Loading branch information...
commit b226c3b8bc7814da1d663440c673e4bb85e0ed5b 0 parents
Ted Young tedsuo authored
5 .gitignore
... ... @@ -0,0 +1,5 @@
  1 +/node_modules
  2 +**.sw*
  3 +/config.js
  4 +**.DS_Store
  5 +npm-debug.log
4 Makefile
... ... @@ -0,0 +1,4 @@
  1 +test:
  2 + ./node_modules/.bin/mocha -R spec -r chai test/*.js
  3 +
  4 +.PHONY: test
26 package.json
... ... @@ -0,0 +1,26 @@
  1 +{
  2 + "name": "transactor",
  3 + "author": "tedsuo",
  4 + "license": "BSD",
  5 + "version": "0.0.0",
  6 + "description": "realtime message passing, with transactions",
  7 + "main": "transactor.js",
  8 + "repository": {
  9 + "type": "git",
  10 + "url": "git://github.com/ethersheet-collective/transactor.git"
  11 + },
  12 + "dependencies": {
  13 + "socket.io": ">=0.9.5"
  14 + },
  15 + "devDependencies": {
  16 + "mocha": ">=1.3.0",
  17 + "chai": "*",
  18 + "sinon": "*"
  19 + },
  20 + "scripts": {
  21 + "test": "make test"
  22 + },
  23 + "directories" : {
  24 + "lib" : "./lib"
  25 + }
  26 +}
32 test/transactor.js
... ... @@ -0,0 +1,32 @@
  1 +var Transactor = require('transactor');
  2 +var EventEmitter = require('events').EventEmitter;
  3 +var assert = require('chai').assert;
  4 +
  5 +describe('Transactor', function(){
  6 + var server, server_spy, trans;
  7 +
  8 + beforeEach(function(){
  9 + trans = new Transactor(['channel_1']);
  10 + });
  11 +
  12 + describe('When a transaction occurs on an event', function(){
  13 + var socket, socket_spy;
  14 +
  15 + beforeEach(function(){
  16 + socket = new EventEmitter();
  17 + trans.addSocket(socket);
  18 + });
  19 +
  20 + it('the transaction handler interface conforms to function(channel,data,socket,cb)', function(done){
  21 + var test_data = {foo:'bar'};
  22 + trans.onTransaction(function(channel,data,sock,cb){
  23 + assert.equal(channel,'channel_1');
  24 + assert.equal(data,test_data);
  25 + assert.deepEqual(socket,sock);
  26 + assert.isFunction(cb);
  27 + done();
  28 + });
  29 + socket.emit('channel_1',test_data);
  30 + });
  31 + });
  32 +});
81 transactor.js
... ... @@ -0,0 +1,81 @@
  1 +var inherits = require('util').inherits;
  2 +var EventEmitter = require('events').EventEmitter;
  3 +
  4 +var Transactor = function(channels){
  5 + this.channels = channels || [];
  6 + this.transaction_handler = this.defaultEventHandler;
  7 + this.connection_handler = this.defaultEventHandler;
  8 + this.disconnection_handler = this.defaultEventHandler;
  9 +};
  10 +
  11 +inherits(Transactor,EventEmitter);
  12 +module.exports = Transactor;
  13 +
  14 +// handler interface: function eventHandler(... socket,cb)
  15 +// callback interface: function cb(err,socket)
  16 +Transactor.prototype.defaultEventHandler = function(){
  17 + arguments[arguments.length - 1](null,arguments[arguments.length - 2]);
  18 +};
  19 +
  20 +Transactor.prototype.initializeServer = function(){
  21 + var trans = this;
  22 + this.io.sockets.on('connection', function(socket){
  23 + trans.addSocket(socket);
  24 + });
  25 +};
  26 +
  27 +Transactor.prototype.addSocket = function(socket){
  28 + var trans = this;
  29 +
  30 + // apply the supplied connection handler to validate the connection
  31 + this.connection_handler(socket,function(err,socket){
  32 + if(err) throw err;
  33 +
  34 + // add the supplied transaction handler to each channel on the socket
  35 + trans.channels.forEach(function(channel){
  36 + socket.on(channel,function(data){
  37 + trans.transaction_handler(channel,data,socket,function(err,event){
  38 + if(err) return socket.emit('error',err,event);
  39 + trans.tellOthers(channel,event,socket);
  40 + });
  41 + });
  42 + });
  43 +
  44 + // add the supplied disconnection handler
  45 + socket.on('disconnect', function(){
  46 + trans.disconnection_handler(socket,function(err,socket){
  47 + if(err) throw err;
  48 + trans.emit('disconnect',socket);
  49 + trans.tellOthers('disconnect',{},socket);
  50 + });
  51 + });
  52 +
  53 + });
  54 +};
  55 +
  56 +Transactor.prototype.onTransaction = function(transaction_handler){
  57 + this.transaction_handler = transaction_handler;
  58 +};
  59 +
  60 +Transactor.prototype.onConnection = function(connection_handler){
  61 + this.connection_handler = connection_handler;
  62 +};
  63 +
  64 +Transactor.prototype.onDisconnection = function(disconnection_handler){
  65 + this.disconnection_handler = disconnection_handler;
  66 +};
  67 +
  68 +Transactor.prototype.tellMe = function(channel,params,socket){
  69 + socket.emit(channel,params);
  70 +};
  71 +
  72 +Transactor.prototype.tellOthers = function(channel,params,socket){
  73 + socket.broadcast.emit(channel,params);
  74 +};
  75 +
  76 +Transactor.prototype.tellAll = function(channel,params,socket){
  77 + socket.emit(channel,params);
  78 + socket.broadcast.emit(channel,params);
  79 +};
  80 +
  81 +

0 comments on commit b226c3b

Please sign in to comment.
Something went wrong with that request. Please try again.