[Pkg-javascript-commits] [sockjs-client] 131/350: EventSource and Htmlfile cleanup

tonnerre at ancient-solutions.com tonnerre at ancient-solutions.com
Fri Aug 5 01:03:54 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 d4b791395dddfc6fbaef5fe8ef0b29f27e9ac91b
Author: Bryce Kahle <bkahle at gmail.com>
Date:   Mon Oct 13 13:58:01 2014 -0400

    EventSource and Htmlfile cleanup
---
 lib/transport/receiver/eventsource.js | 40 ++++++++++++++++++++---------------
 lib/transport/receiver/htmlfile.js    | 34 +++++++++++++++++------------
 lib/utils/iframe.js                   |  2 +-
 3 files changed, 44 insertions(+), 32 deletions(-)

diff --git a/lib/transport/receiver/eventsource.js b/lib/transport/receiver/eventsource.js
index 5de91ae..1073074 100644
--- a/lib/transport/receiver/eventsource.js
+++ b/lib/transport/receiver/eventsource.js
@@ -8,35 +8,41 @@ function EventSourceReceiver(url) {
   EventEmitter.call(this);
 
   var self = this;
-  var es = new global.EventSource(url);
+  var es = this.es = new global.EventSource(url);
   es.onmessage = function(e) {
     self.emit('message', decodeURI(e.data));
   };
-  this.esClose = es.onerror = function(e, abortReason) {
+  es.onerror = function () {
     // ES on reconnection has readyState = 0 or 1.
     // on network error it's CLOSED = 2
-    var reason = abortReason ? 'user' :
-        (es.readyState !== 2 ? 'network' : 'permanent');
-    self.esClose = es.onmessage = es.onerror = null;
-    // EventSource reconnects automatically.
-    es.close();
-    es = null;
-    // Safari and chrome < 15 crash if we close window before
-    // waiting for ES cleanup. See:
-    //   https://code.google.com/p/chromium/issues/detail?id=89155
-    setTimeout(function() {
-      self.emit('close', null, reason);
-      self.removeAllListeners();
-    }, 200);
+    var reason = (es.readyState !== 2 ? 'network' : 'permanent');
+    self._cleanup();
+    self.close(reason);
   };
 }
 
 util.inherits(EventSourceReceiver, EventEmitter);
 
 EventSourceReceiver.prototype.abort = function() {
-  if (this.esClose) {
-    this.esClose({}, true);
+  this._cleanup();
+  this._close('user');
+};
+
+EventSourceReceiver.prototype._cleanup = function () {
+  var es = this.es;
+  if (es) {
+    es.onmessage = es.onerror = null;
+    es.close();
+    this.es = null;
   }
 };
 
+EventSourceReceiver.prototype._close = function (reason) {
+  var self = this;
+  setTimeout(function() {
+    self.emit('close', null, reason);
+    self.removeAllListeners();
+  }, 200);
+};
+
 module.exports = EventSourceReceiver;
diff --git a/lib/transport/receiver/htmlfile.js b/lib/transport/receiver/htmlfile.js
index b920eb4..e291912 100644
--- a/lib/transport/receiver/htmlfile.js
+++ b/lib/transport/receiver/htmlfile.js
@@ -33,36 +33,42 @@ function HtmlfileReceiver(url) {
   var constructor = isIeHtmlfileCapable() ?
       iframeUtils.createHtmlfile : iframeUtils.createIframe;
 
-  var iframeObj;
   global[iframeUtils.WPrefix][this.id] = {
     start: function () {
-      iframeObj.loaded();
+      self.iframeObj.loaded();
     },
     message: function (data) {
       self.emit('message', data);
     },
     stop: function () {
-      self.iframeClose({}, 'network');
+      self._cleanup();
+      self._close('network');
     }
   };
-  this.iframeClose = function(e, abortReason) {
-    iframeObj.cleanup();
-    self.iframeClose = iframeObj = null;
-    delete global[iframeUtils.WPrefix][self.id];
-    self.emit('close', null, abortReason);
-    self.removeAllListeners();
-  };
-  iframeObj = constructor(url, function() {
-    self.iframeClose({}, 'permanent');
+  this.iframeObj = constructor(url, function() {
+    self._cleanup();
+    self._close('permanent');
   });
 }
 
 util.inherits(HtmlfileReceiver, EventEmitter);
 
 HtmlfileReceiver.prototype.abort = function() {
-  if (this.iframeClose) {
-    this.iframeClose({}, 'user');
+  this._cleanup();
+  this._close('user');
+};
+
+HtmlfileReceiver.prototype._cleanup = function () {
+  if (this.iframeObj) {
+    this.iframeObj.cleanup();
+    this.iframeObj = null;
   }
+  delete global[iframeUtils.WPrefix][this.id];
+};
+
+HtmlfileReceiver.prototype._close = function (reason) {
+  this.emit('close', null, reason);
+  this.removeAllListeners();
 };
 
 module.exports = HtmlfileReceiver;
diff --git a/lib/utils/iframe.js b/lib/utils/iframe.js
index 39efb94..3c20a3e 100644
--- a/lib/utils/iframe.js
+++ b/lib/utils/iframe.js
@@ -91,7 +91,7 @@ module.exports = {
 /* jshint undef: false, newcap: false */
 /* eslint no-undef: 0, new-cap: 0 */
 , createHtmlfile: function (iframeUrl, errorCallback) {
-    var doc = new ActiveXObject('htmlfile');
+    var doc = new global.ActiveXObject('htmlfile');
     var tref, unloadRef;
     var iframe;
     var unattach = function() {

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