Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

transactor passes socket on transaction and close events

  • Loading branch information...
commit 11c76bd1cc80840b12d655ebc92695ac88adf90c 1 parent cd1524f
Ted Young tedsuo authored

Showing 2 changed files with 14 additions and 14 deletions. Show diff stats Hide diff stats

  1. +6 5 test/transactor.js
  2. +8 9 transactor.js
11 test/transactor.js
@@ -16,9 +16,10 @@ describe('Transactor', function(){
16 16 describe('When a socket emits an event', function(){
17 17 var test_data = {event:'foo',foo:'bar'};
18 18
19   - it('the transaction handler is invoked with the interface function(channel,data,cb)', function(done){
20   - trans.onTransaction(function(channel,data,cb){
  19 + it('the transaction handler is invoked with the interface function(channel,sock,data,cb)', function(done){
  20 + trans.onTransaction(function(channel,sock,data,cb){
21 21 assert.equal(channel,'channel_1');
  22 + assert.equal(socket.write,sock.write);
22 23 assert.equal(data,test_data);
23 24 assert.isFunction(cb);
24 25 cb(null,data);
@@ -30,7 +31,7 @@ describe('Transactor', function(){
30 31 describe('When the transaction is successful', function(){
31 32
32 33 beforeEach(function(){
33   - trans.onTransaction(function(channel,data,cb){
  34 + trans.onTransaction(function(channel,sock,data,cb){
34 35 cb(null,data);
35 36 });
36 37 });
@@ -73,7 +74,7 @@ describe('Transactor', function(){
73 74 var test_error = 'transaction error';
74 75
75 76 beforeEach(function(){
76   - trans.onTransaction(function(channel,data,cb){
  77 + trans.onTransaction(function(channel,sock,data,cb){
77 78 cb(test_error,data);
78 79 });
79 80 });
@@ -101,7 +102,7 @@ describe('Transactor', function(){
101 102
102 103 describe('when a socket closes', function(){
103 104 beforeEach(function(){
104   - trans.onTransaction(function(channel,data,cb){
  105 + trans.onTransaction(function(channel,sock,data,cb){
105 106 cb(null,data);
106 107 });
107 108 });
17 transactor.js
@@ -2,16 +2,19 @@ var uuid = require('node-uuid');
2 2
3 3 var Transactor = module.exports = function(o){
4 4 o = o || {};
5   - if(typeof o.transaction_handler === 'function'){
6   - this.onTransaction(o.transaction_handler);
7   - }
8 5 this.sockets = {};
  6 + this.onTransaction(o.transaction_handler || function(){});
  7 + this.onClose(o.close_handler || function(){});
9 8 };
10 9
11 10 Transactor.prototype.onTransaction = function(transaction_handler){
12 11 this.transaction_handler = transaction_handler;
13 12 };
14 13
  14 +Transactor.prototype.onClose = function(close_handler){
  15 + this.close_handler = close_handler;
  16 +};
  17 +
15 18 Transactor.prototype.addSocket = function(channel,socket){
16 19 var trans = this;
17 20 var socket_id = socket.id || uuid.v4();
@@ -24,7 +27,7 @@ Transactor.prototype.addSocket = function(channel,socket){
24 27 // add the supplied transaction handler to each channel on the socket
25 28 socket.on('data',function(data){
26 29 console.log('received',channel,data);
27   - trans.transaction_handler(channel,data,function(err,data){
  30 + trans.transaction_handler(channel,socket,data,function(err,data){
28 31 if(err) return socket.emit('error',err,data);
29 32 trans.broadcast(socket,channel,data);
30 33 });
@@ -32,7 +35,7 @@ Transactor.prototype.addSocket = function(channel,socket){
32 35
33 36 // add the supplied disconnection handler
34 37 socket.on('close', function(){
35   - trans.onClose();
  38 + trans.close_handler(channel,socket);
36 39 delete trans.sockets[channel][socket_id];
37 40 });
38 41
@@ -46,7 +49,3 @@ Transactor.prototype.broadcast = function(socket,channel,data){
46 49 this.sockets[channel][socket_id].write(data);
47 50 }
48 51 };
49   -
50   -Transactor.prototype.onClose = function(){
51   -
52   -};

0 comments on commit 11c76bd

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