Permalink
Browse files

BREAKS COMPAT with `streamws` < 0.1.0 ; remove callbacks from .write …

…; fixes callback leaks for socket error/close/drain events
  • Loading branch information...
1 parent 0db32e9 commit eee753a89b019e961d10d88d4784ee8a26bc3da2 @ericz ericz committed Jan 16, 2013
Showing with 17 additions and 31 deletions.
  1. +17 −31 lib/stream.js
View
@@ -13,26 +13,7 @@ function BinaryStream(socket, id, create, meta) {
this.id = id;
this._socket = socket;
- this._errorListener = function(error){
- self.readable = false;
- self.writable = false;
- self.emit('error', error);
- };
- this._socket.addEventListener('error', this._errorListener);
- this._closeListener = function(code, message){
- self._onClose();
- };
- this._socket.addEventListener('close', this._closeListener);
-
- // if node
- this._drainListener = function(){
- if(!self.paused) {
- self.emit('drain');
- }
- }
- this._socket._sender._socket.on('drain', this._drainListener);
- // end node
-
+
this.writable = true;
this.readable = true;
this.paused = false;
@@ -49,6 +30,12 @@ function BinaryStream(socket, id, create, meta) {
util.inherits(BinaryStream, Stream);
+BinaryStream.prototype._onDrain = function() {
+ if(!this.paused) {
+ this.emit('drain');
+ }
+};
+
BinaryStream.prototype._onClose = function() {
// Emit close event
if (this._closed) {
@@ -58,13 +45,12 @@ BinaryStream.prototype._onClose = function() {
this.writable = false;
this._closed = true;
this.emit('close');
- util.removeListener(this._socket, 'error', this._errorListener);
- util.removeListener(this._socket, 'close', this._closeListener);
- // if node
- if (this._socket._sender) {
- util.removeListener(this._socket._sender._socket, 'drain', this._drainListener);
- }
- // end node
+};
+
+BinaryStream.prototype._onError = function(error){
+ this.readable = false;
+ this.writable = false;
+ this.emit('error', error);
};
// Write stream
@@ -82,14 +68,14 @@ BinaryStream.prototype._onResume = function() {
this.emit('drain');
};
-BinaryStream.prototype._write = function(code, data, bonus, cb) {
+BinaryStream.prototype._write = function(code, data, bonus) {
var message = util.pack([code, data, bonus]);
- return this._socket.send(message, {binary: true}, cb) !== false;
+ return this._socket.send(message) !== false;
};
-BinaryStream.prototype.write = function(data, cb) {
+BinaryStream.prototype.write = function(data) {
if(this.writable) {
- var out = this._write(2, data, this.id, cb);
+ var out = this._write(2, data, this.id);
return !this.paused && out;
} else {
throw new Error('Stream is not writable');

0 comments on commit eee753a

Please sign in to comment.