Permalink
Browse files

try to clean up listeners

  • Loading branch information...
1 parent 6862f6c commit d753f19dbb38c4170528fde321f40b2ac72e7195 @ericz ericz committed Jan 16, 2013
Showing with 26 additions and 6 deletions.
  1. +16 −6 lib/stream.js
  2. +10 −0 lib/util.js
View
@@ -13,21 +13,24 @@ function BinaryStream(socket, id, create, meta) {
this.id = id;
this._socket = socket;
- this._socket.addEventListener('error', function(error){
+ this._errorListener = function(error){
self.readable = false;
self.writable = false;
self.emit('error', error);
- });
- this._socket.addEventListener('close', function(code, message){
+ };
+ this._socket.addEventListener('error', this._errorListener);
+ this._closeListener = function(code, message){
self._onClose();
- });
+ };
+ this._socket.addEventListener('close', this._closeListener);
// if node
- this._socket._sender._socket.on('drain', function(){
+ this._drainListener = function(){
if(!self.paused) {
self.emit('drain');
}
- });
+ }
+ this._socket._sender._socket.on('drain', this._drainListener);
// end node
this.writable = true;
@@ -55,6 +58,13 @@ 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
};
// Write stream
View
@@ -27,6 +27,16 @@ var util = {
// if node
isNode: true,
// end node
+ removeListener: function(obj, type, fn) {
+ if (!obj || !type || !fn) {
+ return;
+ }
+ if (obj.removeListener) {
+ obj.removeListener(type, fn);
+ } else {
+ obj.removeEventListener(type, fn);
+ }
+ },
setZeroTimeout: (function(global) {
// if node

0 comments on commit d753f19

Please sign in to comment.