Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

transactor passes socket on transaction and close events

  • Loading branch information...
commit 11c76bd1cc80840b12d655ebc92695ac88adf90c 1 parent cd1524f
@tedsuo tedsuo authored
Showing with 14 additions and 14 deletions.
  1. +6 −5 test/transactor.js
  2. +8 −9 transactor.js
View
11 test/transactor.js
@@ -16,9 +16,10 @@ describe('Transactor', function(){
describe('When a socket emits an event', function(){
var test_data = {event:'foo',foo:'bar'};
- it('the transaction handler is invoked with the interface function(channel,data,cb)', function(done){
- trans.onTransaction(function(channel,data,cb){
+ it('the transaction handler is invoked with the interface function(channel,sock,data,cb)', function(done){
+ trans.onTransaction(function(channel,sock,data,cb){
assert.equal(channel,'channel_1');
+ assert.equal(socket.write,sock.write);
assert.equal(data,test_data);
assert.isFunction(cb);
cb(null,data);
@@ -30,7 +31,7 @@ describe('Transactor', function(){
describe('When the transaction is successful', function(){
beforeEach(function(){
- trans.onTransaction(function(channel,data,cb){
+ trans.onTransaction(function(channel,sock,data,cb){
cb(null,data);
});
});
@@ -73,7 +74,7 @@ describe('Transactor', function(){
var test_error = 'transaction error';
beforeEach(function(){
- trans.onTransaction(function(channel,data,cb){
+ trans.onTransaction(function(channel,sock,data,cb){
cb(test_error,data);
});
});
@@ -101,7 +102,7 @@ describe('Transactor', function(){
describe('when a socket closes', function(){
beforeEach(function(){
- trans.onTransaction(function(channel,data,cb){
+ trans.onTransaction(function(channel,sock,data,cb){
cb(null,data);
});
});
View
17 transactor.js
@@ -2,16 +2,19 @@ var uuid = require('node-uuid');
var Transactor = module.exports = function(o){
o = o || {};
- if(typeof o.transaction_handler === 'function'){
- this.onTransaction(o.transaction_handler);
- }
this.sockets = {};
+ this.onTransaction(o.transaction_handler || function(){});
+ this.onClose(o.close_handler || function(){});
};
Transactor.prototype.onTransaction = function(transaction_handler){
this.transaction_handler = transaction_handler;
};
+Transactor.prototype.onClose = function(close_handler){
+ this.close_handler = close_handler;
+};
+
Transactor.prototype.addSocket = function(channel,socket){
var trans = this;
var socket_id = socket.id || uuid.v4();
@@ -24,7 +27,7 @@ Transactor.prototype.addSocket = function(channel,socket){
// add the supplied transaction handler to each channel on the socket
socket.on('data',function(data){
console.log('received',channel,data);
- trans.transaction_handler(channel,data,function(err,data){
+ trans.transaction_handler(channel,socket,data,function(err,data){
if(err) return socket.emit('error',err,data);
trans.broadcast(socket,channel,data);
});
@@ -32,7 +35,7 @@ Transactor.prototype.addSocket = function(channel,socket){
// add the supplied disconnection handler
socket.on('close', function(){
- trans.onClose();
+ trans.close_handler(channel,socket);
delete trans.sockets[channel][socket_id];
});
@@ -46,7 +49,3 @@ Transactor.prototype.broadcast = function(socket,channel,data){
this.sockets[channel][socket_id].write(data);
}
};
-
-Transactor.prototype.onClose = function(){
-
-};

0 comments on commit 11c76bd

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