[Pkg-javascript-commits] [sockjs-client] 210/350: Reuse EventTarget instead of importing entire EventEmitter.

tonnerre at ancient-solutions.com tonnerre at ancient-solutions.com
Fri Aug 5 01:04:22 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 45a80c13d599280bbffe4d7b18bea3bdebcc7e45
Author: Bryce Kahle <bkahle at gmail.com>
Date:   Mon Oct 20 20:36:05 2014 -0400

    Reuse EventTarget instead of importing entire EventEmitter.
---
 gulpfile.js                           |  1 -
 lib/event/emitter.js                  | 51 +++++++++++++++++++++++++++++++++++
 lib/event/eventtarget.js              |  6 ++---
 lib/transport/browser/abstract-xhr.js | 16 ++++++++---
 lib/transport/lib/ajax-based.js       |  4 ++-
 lib/transport/sender/xhr-cors.js      |  5 +---
 lib/transport/sender/xhr-local.js     |  9 +++----
 package.json                          |  3 ++-
 8 files changed, 74 insertions(+), 21 deletions(-)

diff --git a/gulpfile.js b/gulpfile.js
index e695157..569c85f 100644
--- a/gulpfile.js
+++ b/gulpfile.js
@@ -18,7 +18,6 @@ var libName = 'sockjs-' + pkg.version
   , browserifyOptions = {
       entries: './lib/entry.js'
     , standalone: 'SockJS'
-    , fullPaths: true
     , insertGlobalVars: {
         process: function () {
           return '{ env: {} }';
diff --git a/lib/event/emitter.js b/lib/event/emitter.js
new file mode 100644
index 0000000..15d6c28
--- /dev/null
+++ b/lib/event/emitter.js
@@ -0,0 +1,51 @@
+'use strict';
+
+var inherits = require('inherits')
+  , EventTarget = require('./eventtarget')
+  ;
+
+function EventEmitter() {
+  EventTarget.call(this);
+}
+
+inherits(EventEmitter, EventTarget);
+
+EventEmitter.prototype.removeAllListeners = function(type) {
+  if (type) {
+    delete this._listeners[type];
+  } else {
+    this._listeners = {};
+  }
+};
+
+EventEmitter.prototype.once = function(type, listener) {
+  var self = this
+    , fired = false;
+
+  function g() {
+    self.removeListener(type, g);
+
+    if (!fired) {
+      fired = true;
+      listener.apply(this, arguments);
+    }
+  }
+
+  this.on(type, g);
+};
+
+EventEmitter.prototype.emit = function(type) {
+  var listeners = this._listeners[type];
+  if (!listeners) {
+    return;
+  }
+  var args = Array.prototype.slice.call(arguments, 1);
+  for (var i = 0; i < listeners.length; i++) {
+    listeners[i].apply(this, args);
+  }
+};
+
+EventEmitter.prototype.on = EventEmitter.prototype.addListener = EventTarget.prototype.addEventListener;
+EventEmitter.prototype.removeListener = EventTarget.prototype.removeEventListener;
+
+module.exports.EventEmitter = EventEmitter;
diff --git a/lib/event/eventtarget.js b/lib/event/eventtarget.js
index aa4adff..3edec76 100644
--- a/lib/event/eventtarget.js
+++ b/lib/event/eventtarget.js
@@ -19,14 +19,13 @@ EventTarget.prototype.addEventListener = function(eventType, listener) {
     arr = arr.concat([listener]);
   }
   this._listeners[eventType] = arr;
-  return;
 };
 
 EventTarget.prototype.removeEventListener = function(eventType, listener) {
-  if (!(eventType in this._listeners)) {
+  var arr = this._listeners[eventType];
+  if (!arr) {
     return;
   }
-  var arr = this._listeners[eventType];
   var idx = arr.indexOf(listener);
   if (idx !== -1) {
     if (arr.length > 1) {
@@ -37,7 +36,6 @@ EventTarget.prototype.removeEventListener = function(eventType, listener) {
     }
     return;
   }
-  return;
 };
 
 EventTarget.prototype.dispatchEvent = function(event) {
diff --git a/lib/transport/browser/abstract-xhr.js b/lib/transport/browser/abstract-xhr.js
index 90fd256..d8b349c 100644
--- a/lib/transport/browser/abstract-xhr.js
+++ b/lib/transport/browser/abstract-xhr.js
@@ -17,6 +17,16 @@ function AbstractXHRObject(method, url, payload, opts) {
   var self = this;
   EventEmitter.call(this);
 
+  setTimeout(function () {
+    self._start(method, url, payload, opts);
+  }, 0);
+}
+
+inherits(AbstractXHRObject, EventEmitter);
+
+AbstractXHRObject.prototype._start = function(method, url, payload, opts) {
+  var self = this;
+
   try {
     this.xhr = new XHR();
   } catch (x) {}
@@ -112,9 +122,7 @@ function AbstractXHRObject(method, url, payload, opts) {
     self.emit('finish', 0, '');
     self._cleanup(false);
   }
-}
-
-inherits(AbstractXHRObject, EventEmitter);
+};
 
 AbstractXHRObject.prototype._cleanup = function(abort) {
   debug('cleanup');
@@ -157,7 +165,7 @@ if (!AbstractXHRObject.enabled && (axo in global)) {
 }
 
 var cors = false;
-try { 
+try {
   cors = 'withCredentials' in new XHR();
 } catch (ignored) {}
 
diff --git a/lib/transport/lib/ajax-based.js b/lib/transport/lib/ajax-based.js
index cdd057a..1b02787 100644
--- a/lib/transport/lib/ajax-based.js
+++ b/lib/transport/lib/ajax-based.js
@@ -28,7 +28,9 @@ function createAjaxSender(AjaxObject) {
     });
     return function() {
       debug('abort');
-      callback(new Error('Aborted'));
+      var err = new Error('Aborted');
+      err.code = 1000;
+      callback(err);
     };
   };
 }
diff --git a/lib/transport/sender/xhr-cors.js b/lib/transport/sender/xhr-cors.js
index 9c3b95d..9373662 100644
--- a/lib/transport/sender/xhr-cors.js
+++ b/lib/transport/sender/xhr-cors.js
@@ -5,10 +5,7 @@ var inherits = require('inherits')
   ;
 
 function XHRCorsObject(method, url, payload, opts) {
-  var self = this;
-  setTimeout(function() {
-    XhrDriver.call(self, method, url, payload, opts);
-  }, 0);
+  XhrDriver.call(this, method, url, payload, opts);
 }
 
 inherits(XHRCorsObject, XhrDriver);
diff --git a/lib/transport/sender/xhr-local.js b/lib/transport/sender/xhr-local.js
index 372869b..defe4d2 100644
--- a/lib/transport/sender/xhr-local.js
+++ b/lib/transport/sender/xhr-local.js
@@ -5,12 +5,9 @@ var inherits = require('inherits')
   ;
 
 function XHRLocalObject(method, url, payload /*, opts */) {
-  var self = this;
-  setTimeout(function() {
-    XhrDriver.call(self, method, url, payload, {
-      noCredentials: true
-    });
-  }, 0);
+  XhrDriver.call(this, method, url, payload, {
+    noCredentials: true
+  });
 }
 
 inherits(XHRLocalObject, XhrDriver);
diff --git a/package.json b/package.json
index 1beedc1..8127c8f 100644
--- a/package.json
+++ b/package.json
@@ -7,7 +7,8 @@
     "./lib/transport/driver/websocket.js": "./lib/transport/browser/websocket.js",
     "eventsource": "./lib/transport/browser/eventsource.js",
     "./lib/transport/driver/xhr.js": "./lib/transport/browser/abstract-xhr.js",
-    "crypto": "./lib/utils/browser-crypto.js"
+    "crypto": "./lib/utils/browser-crypto.js",
+    "events": "./lib/event/emitter.js"
   },
   "bugs": {
     "url": "https://github.com/sockjs/sockjs-client/issues"

-- 
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