[Pkg-javascript-commits] [sockjs-client] 78/350: Replace custom EventEmitter with standard one

tonnerre at ancient-solutions.com tonnerre at ancient-solutions.com
Fri Aug 5 01:03:44 UTC 2016


This is an automated email from the git hooks/post-receive script.

tonnerre-guest pushed a commit to branch upstream
in repository sockjs-client.

commit 4e6a97b4caa16f8686b52f0eb172e2a94c1a8179
Author: Bryce Kahle <bkahle at gmail.com>
Date:   Thu Aug 21 11:58:33 2014 -0400

    Replace custom EventEmitter with standard one
---
 lib/abstract-xhr.js             | 13 +++++----
 lib/ajax-based.js               |  4 +--
 lib/eventemitter.js             | 63 -----------------------------------------
 lib/info-receiver-fake.js       |  9 ++++--
 lib/info-receiver-iframe.js     | 12 ++++----
 lib/info-receiver.js            | 14 +++++----
 lib/sockjs.js                   |  6 ++--
 lib/trans-receiver-iframe.js    |  4 +--
 lib/trans-receiver-xhr.js       | 11 +++----
 lib/xdr.js                      | 14 +++++----
 tests/html/lib/domtests.js      | 22 +++++++-------
 tests/html/lib/endtoendtests.js | 19 -------------
 12 files changed, 60 insertions(+), 131 deletions(-)

diff --git a/lib/abstract-xhr.js b/lib/abstract-xhr.js
index fd2772a..57b3463 100644
--- a/lib/abstract-xhr.js
+++ b/lib/abstract-xhr.js
@@ -1,12 +1,14 @@
 'use strict';
 
-var EventEmitter = require('./eventemitter');
-var utils = require('./utils');
+var EventEmitter = require('events').EventEmitter
+  , util = require('util')
+  , utils = require('./utils')
+  ;
 
 function AbstractXHRObject() {
 }
 
-AbstractXHRObject.prototype = new EventEmitter(['chunk', 'finish']);
+util.inherits(AbstractXHRObject, EventEmitter);
 
 AbstractXHRObject.prototype._start = function(method, url, payload, opts) {
     var that = this;
@@ -99,9 +101,8 @@ AbstractXHRObject.prototype._cleanup = function(abort) {
 };
 
 AbstractXHRObject.prototype.close = function() {
-    var that = this;
-    that.nuke();
-    that._cleanup(true);
+    this.removeAllListeners();
+    this._cleanup(true);
 };
 
 module.exports = AbstractXHRObject;
\ No newline at end of file
diff --git a/lib/ajax-based.js b/lib/ajax-based.js
index a74dbeb..443a465 100644
--- a/lib/ajax-based.js
+++ b/lib/ajax-based.js
@@ -38,10 +38,10 @@ function createAjaxSender(AjaxObject) {
         var opt = {};
         if (typeof payload === 'string') opt.headers = {'Content-type':'text/plain'};
         var xo = new AjaxObject('POST', url + '/xhr_send', payload, opt);
-        xo.onfinish = function(status, text) {
+        xo.on('finish', function(status, text) {
             callback(status === 200 || status === 204,
                      'http status ' + status);
-        };
+        });
         return function(abort_reason) {
             callback(false, abort_reason);
         };
diff --git a/lib/eventemitter.js b/lib/eventemitter.js
deleted file mode 100644
index 3b0cb9b..0000000
--- a/lib/eventemitter.js
+++ /dev/null
@@ -1,63 +0,0 @@
-'use strict';
-/*
- * ***** BEGIN LICENSE BLOCK *****
- * Copyright (c) 2011-2012 VMware, Inc.
- *
- * For the license see COPYING.
- * ***** END LICENSE BLOCK *****
- */
-
-var utils = require('./utils');
-var JSON3 = require('json3');
-
-var EventEmitter = function(events) {
-    var that = this;
-    that._events = events || [];
-    that._listeners = {};
-};
-EventEmitter.prototype.emit = function(type) {
-    var that = this;
-    that._verifyType(type);
-    if (that._nuked) return;
-
-    var args = Array.prototype.slice.call(arguments, 1);
-    if (that['on'+type]) {
-        that['on'+type].apply(that, args);
-    }
-    if (type in that._listeners) {
-        for(var i = 0; i < that._listeners[type].length; i++) {
-            that._listeners[type][i].apply(that, args);
-        }
-    }
-};
-
-EventEmitter.prototype.on = function(type, callback) {
-    var that = this;
-    that._verifyType(type);
-    if (that._nuked) return;
-
-    if (!(type in that._listeners)) {
-        that._listeners[type] = [];
-    }
-    that._listeners[type].push(callback);
-};
-
-EventEmitter.prototype._verifyType = function(type) {
-    var that = this;
-    if (utils.arrIndexOf(that._events, type) === -1) {
-        utils.log('Event ' + JSON3.stringify(type) +
-                  ' not listed ' + JSON3.stringify(that._events) +
-                  ' in ' + that);
-    }
-};
-
-EventEmitter.prototype.nuke = function() {
-    var that = this;
-    that._nuked = true;
-    for(var i=0; i<that._events.length; i++) {
-        delete that[that._events[i]];
-    }
-    that._listeners = {};
-};
-
-module.exports = EventEmitter;
diff --git a/lib/info-receiver-fake.js b/lib/info-receiver-fake.js
index b85d7d1..84fd6fc 100644
--- a/lib/info-receiver-fake.js
+++ b/lib/info-receiver-fake.js
@@ -7,8 +7,10 @@
  * ***** END LICENSE BLOCK *****
  */
 
-var utils = require('./utils');
-var EventEmitter = require('./eventemitter');
+var EventEmitter = require('events').EventEmitter
+  , util = require('util')
+  , utils = require('./utils')
+  ;
 
 var InfoReceiverFake = function() {
     // It may not be possible to do cross domain AJAX to get the info
@@ -19,6 +21,7 @@ var InfoReceiverFake = function() {
         that.emit('finish', {}, 2000);
     });
 };
-InfoReceiverFake.prototype = new EventEmitter(['finish']);
+
+util.inherits(InfoReceiverFake, EventEmitter);
 
 module.exports = InfoReceiverFake;
\ No newline at end of file
diff --git a/lib/info-receiver-iframe.js b/lib/info-receiver-iframe.js
index 1c64e56..b95b237 100644
--- a/lib/info-receiver-iframe.js
+++ b/lib/info-receiver-iframe.js
@@ -7,10 +7,12 @@
  * ***** END LICENSE BLOCK *****
  */
 
-var utils = require('./utils');
-var EventEmitter = require('./eventemitter');
-var IframeTransport = require('./trans-iframe');
-var JSON3 = require('json3');
+var EventEmitter = require('events').EventEmitter
+  , util = require('util')
+  , JSON3 = require('json3')
+  , utils = require('./utils')
+  , IframeTransport = require('./trans-iframe')
+  ;
 
 function InfoReceiverIframe(base_url) {
     var that = this;
@@ -41,6 +43,6 @@ function InfoReceiverIframe(base_url) {
     }
 }
 
-InfoReceiverIframe.prototype = new EventEmitter(['finish']);
+util.inherits(InfoReceiverIframe, EventEmitter);
 
 module.exports = InfoReceiverIframe;
\ No newline at end of file
diff --git a/lib/info-receiver.js b/lib/info-receiver.js
index e83fc39..cc7f230 100644
--- a/lib/info-receiver.js
+++ b/lib/info-receiver.js
@@ -7,16 +7,18 @@
  * ***** END LICENSE BLOCK *****
  */
 
-var utils = require('./utils');
-var EventEmitter = require('./eventemitter');
-var JSON3 = require('json3');
+var EventEmitter = require('events').EventEmitter
+  , util = require('util')
+  , JSON3 = require('json3')
+  , utils = require('./utils')
+  ;
 
 function InfoReceiver(base_url, AjaxObject) {
     var that = this;
     utils.delay(function(){that.doXhr(base_url, AjaxObject);});
 }
 
-InfoReceiver.prototype = new EventEmitter(['finish']);
+util.inherits(InfoReceiver, EventEmitter);
 
 InfoReceiver.prototype.doXhr = function(base_url, AjaxObject) {
     var that = this;
@@ -26,7 +28,7 @@ InfoReceiver.prototype.doXhr = function(base_url, AjaxObject) {
     var tref = utils.delay(8000,
                            function(){xo.ontimeout();});
 
-    xo.onfinish = function(status, text) {
+    xo.on('finish', function(status, text) {
         clearTimeout(tref);
         tref = null;
         if (status === 200) {
@@ -43,7 +45,7 @@ InfoReceiver.prototype.doXhr = function(base_url, AjaxObject) {
         } else {
             that.emit('finish');
         }
-    };
+    });
     xo.ontimeout = function() {
         xo.close();
         that.emit('finish');
diff --git a/lib/sockjs.js b/lib/sockjs.js
index 88ab1c4..661957e 100644
--- a/lib/sockjs.js
+++ b/lib/sockjs.js
@@ -49,7 +49,7 @@ function SockJS(url, _reserved, options) {
     that.protocol = null;
     that.readyState = SockJS.CONNECTING;
     that._ir = createInfoReceiver(that._base_url);
-    that._ir.onfinish = function(info, rtt) {
+    that._ir.on('finish', function(info, rtt) {
         that._ir = null;
         if (info) {
             that._applyInfo(info, rtt, that._protocols_whitelist);
@@ -57,7 +57,7 @@ function SockJS(url, _reserved, options) {
         } else {
             that._didClose(1002, "Can't connect to server", true);
         }
-    };
+    });
 }
 
 // Inheritance
@@ -112,7 +112,7 @@ SockJS.prototype._didClose = function(code, reason, force) {
         that.readyState !== SockJS.CLOSING)
             throw new Error('INVALID_STATE_ERR');
     if (that._ir) {
-        that._ir.nuke();
+        that._ir.removeAllListeners();
         that._ir = null;
     }
 
diff --git a/lib/trans-receiver-iframe.js b/lib/trans-receiver-iframe.js
index 8ef0fed..8490f8d 100644
--- a/lib/trans-receiver-iframe.js
+++ b/lib/trans-receiver-iframe.js
@@ -6,10 +6,10 @@ var InfoReceiver = require('./info-receiver');
 
 function WInfoReceiverIframe(ri, _trans_url, base_url) {
     var ir = new InfoReceiver(base_url, XHRLocalObject);
-    ir.onfinish = function(info, rtt) {
+    ir.on('finish', function(info, rtt) {
         ri._didMessage('m'+JSON3.stringify([info, rtt]));
         ri._didClose();
-    };
+    });
 }
 
 WInfoReceiverIframe.prototype.doCleanup = function() {};
diff --git a/lib/trans-receiver-xhr.js b/lib/trans-receiver-xhr.js
index 9343ec5..c6a3502 100644
--- a/lib/trans-receiver-xhr.js
+++ b/lib/trans-receiver-xhr.js
@@ -15,7 +15,7 @@ function XhrReceiver(url, AjaxObject) {
     var buf_pos = 0;
 
     that.xo = new AjaxObject('POST', url, null);
-    that.xo.onchunk = function(status, text) {
+    function chunkHandler(status, text) {
         if (status !== 200) return;
         while (1) {
             var buf = text.slice(buf_pos);
@@ -25,13 +25,14 @@ function XhrReceiver(url, AjaxObject) {
             var msg = buf.slice(0, p);
             that.dispatchEvent(new SimpleEvent('message', {data: msg}));
         }
-    };
-    that.xo.onfinish = function(status, text) {
-        that.xo.onchunk(status, text);
+    }
+    that.xo.on('chunk', chunkHandler);
+    that.xo.on('finish', function(status, text) {
+        chunkHandler(status, text);
         that.xo = null;
         var reason = status === 200 ? 'network' : 'permanent';
         that.dispatchEvent(new SimpleEvent('close', {reason: reason}));
-    };
+    });
 }
 
 XhrReceiver.prototype = new REventTarget();
diff --git a/lib/xdr.js b/lib/xdr.js
index a75d250..bf1c527 100644
--- a/lib/xdr.js
+++ b/lib/xdr.js
@@ -1,7 +1,9 @@
 'use strict';
 
-var utils = require('./utils');
-var EventEmitter = require('./eventemitter');
+var EventEmitter = require('events').EventEmitter
+  , util = require('util')
+  , utils = require('./utils')
+  ;
 
 // References:
 //   http://ajaxian.com/archives/100-line-ajax-wrapper
@@ -12,7 +14,8 @@ function XDRObject(method, url, payload) {
     utils.delay(function(){that._start(method, url, payload);});
 }
 
-XDRObject.prototype = new EventEmitter(['chunk', 'finish']);
+util.inherits(XDRObject, EventEmitter);
+
 XDRObject.prototype._start = function(method, url, payload) {
     var that = this;
     var xdr = new XDomainRequest();
@@ -57,9 +60,8 @@ XDRObject.prototype._cleanup = function(abort) {
 };
 
 XDRObject.prototype.close = function() {
-    var that = this;
-    that.nuke();
-    that._cleanup(true);
+    this.removeAllListeners();
+    this._cleanup(true);
 };
 
 module.exports = XDRObject;
\ No newline at end of file
diff --git a/tests/html/lib/domtests.js b/tests/html/lib/domtests.js
index b1e9570..17263f4 100644
--- a/tests/html/lib/domtests.js
+++ b/tests/html/lib/domtests.js
@@ -89,11 +89,11 @@ ajax_simple_factory = function(name, Obj) {
     var x;
     //expect(2);
     x = new Obj('GET', '/simple.txt', null);
-    x.onfinish = function(status, text) {
+    x.on('finish', function(status, text) {
       assert.equal(text.length, 2051);
       assert.equal(text.slice(-2), 'b\n');
       done();
-    };
+    });
   });
 };
 
@@ -103,16 +103,16 @@ ajax_streaming_factory = function(name, Obj) {
     var x;
     //expect(4);
     x = new Obj('GET', '/streaming.txt', null);
-    x.onchunk = function(status, text) {
+    x.on('chunk', function(status, text) {
       assert.equal(status, 200);
       assert.ok(text.length <= 2049, 'Most likely you\'re behind a transparent Proxy that can\'t do streaming. QUnit tests won\'t work properly. Sorry!');
-      delete x.onchunk;
-    };
-    x.onfinish = function(status, text) {
+      x.removeAllListeners('chunk');
+    });
+    x.on('finish', function(status, text) {
       assert.equal(status, 200);
       assert.equal(text.slice(-4), 'a\nb\n');
       done();
-    };
+    });
   });
 };
 
@@ -123,14 +123,14 @@ test_wrong_url = function(name, Obj, url, statuses, done) {
   }
   //expect(2);
   x = new Obj('GET', url, null);
-  x.onchunk = function() {
+  x.on('chunk', function() {
     assert.ok(false, "chunk shall not be received");
-  };
-  x.onfinish = function(status, text) {
+  });
+  x.on('finish', function(status, text) {
     assert.ok(u.arrIndexOf(statuses, status) !== -1);
     assert.equal(text, '');
     done();
-  };
+  });
 };
 
 ajax_wrong_port_factory = function(name, Obj) {
diff --git a/tests/html/lib/endtoendtests.js b/tests/html/lib/endtoendtests.js
index 2383756..1717a98 100644
--- a/tests/html/lib/endtoendtests.js
+++ b/tests/html/lib/endtoendtests.js
@@ -100,22 +100,3 @@ test("close on close", function(done) {
     });
   };
 });
-
-test("EventEmitter exception handling", function(done) {
-  this.runnable().globals(['_sockjs_global', '_jp']);
-  var prev_onerror, r;
-  //expect(1);
-  r = testutils.newSockJS('/echo', 'xhr-streaming');
-  prev_onerror = window.onerror;
-  window.onerror = function(e) {
-    assert.ok(/onopen error/.test('' + e));
-    window.onerror = prev_onerror;
-    r.close();
-  };
-  r.onopen = function(e) {
-    throw "onopen error";
-  };
-  r.onclose = function() {
-    done();
-  };
-});

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/sockjs-client.git



More information about the Pkg-javascript-commits mailing list