Skip to content
This repository
Browse code

try to clean up listeners

  • Loading branch information...
commit d753f19dbb38c4170528fde321f40b2ac72e7195 1 parent 6862f6c
Eric Zhang authored January 15, 2013
22  lib/stream.js
@@ -13,21 +13,24 @@ function BinaryStream(socket, id, create, meta) {
13 13
   
14 14
   this.id = id;
15 15
   this._socket = socket;
16  
-  this._socket.addEventListener('error', function(error){
  16
+  this._errorListener = function(error){
17 17
     self.readable = false;
18 18
     self.writable = false;
19 19
     self.emit('error', error);
20  
-  });
21  
-  this._socket.addEventListener('close', function(code, message){
  20
+  };
  21
+  this._socket.addEventListener('error', this._errorListener);
  22
+  this._closeListener = function(code, message){
22 23
     self._onClose();
23  
-  });
  24
+  };
  25
+  this._socket.addEventListener('close', this._closeListener);
24 26
   
25 27
   // if node
26  
-  this._socket._sender._socket.on('drain', function(){
  28
+  this._drainListener = function(){
27 29
     if(!self.paused) {
28 30
       self.emit('drain');
29 31
     }
30  
-  });
  32
+  }
  33
+  this._socket._sender._socket.on('drain', this._drainListener);
31 34
   // end node
32 35
   
33 36
   this.writable = true;
@@ -55,6 +58,13 @@ BinaryStream.prototype._onClose = function() {
55 58
   this.writable = false;
56 59
   this._closed = true;
57 60
   this.emit('close');
  61
+  util.removeListener(this._socket, 'error', this._errorListener);
  62
+  util.removeListener(this._socket, 'close', this._closeListener);
  63
+  // if node
  64
+  if (this._socket._sender) {
  65
+    util.removeListener(this._socket._sender._socket, 'drain', this._drainListener);
  66
+  }
  67
+  // end node
58 68
 };
59 69
 
60 70
 // Write stream
10  lib/util.js
@@ -27,6 +27,16 @@ var util = {
27 27
   // if node
28 28
   isNode: true,
29 29
   // end node
  30
+  removeListener: function(obj, type, fn) {
  31
+    if (!obj || !type || !fn) {
  32
+      return;
  33
+    }
  34
+    if (obj.removeListener) {
  35
+      obj.removeListener(type, fn);
  36
+    } else {
  37
+      obj.removeEventListener(type, fn);
  38
+    }
  39
+  },
30 40
   setZeroTimeout: (function(global) {
31 41
     // if node
32 42
     

0 notes on commit d753f19

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