[Pkg-javascript-commits] [sockjs-client] 23/434: Dedup iframe code.
Tonnerre Lombard
tonnerre-guest at moszumanska.debian.org
Wed Jan 8 00:46:59 UTC 2014
This is an automated email from the git hooks/post-receive script.
tonnerre-guest pushed a commit to branch master
in repository sockjs-client.
commit 1082f9f31a6e2deffc23b2f1e30d74925d0cc2c0
Author: Marek Majkowski <majek04 at gmail.com>
Date: Tue Jul 26 15:19:00 2011 +0100
Dedup iframe code.
---
lib/trans-iframe-htmlfile.js | 31 +++++++------------------------
lib/trans-iframe.js | 34 ++++++++++------------------------
lib/utils.js | 40 ++++++++++++++++++++++++++++++++++++++++
3 files changed, 57 insertions(+), 48 deletions(-)
diff --git a/lib/trans-iframe-htmlfile.js b/lib/trans-iframe-htmlfile.js
index af61a34..2348e9c 100644
--- a/lib/trans-iframe-htmlfile.js
+++ b/lib/trans-iframe-htmlfile.js
@@ -27,10 +27,7 @@ var HtmlFileTransport = FacadeJS['w-iframe-htmlfile'] = function (ri, trans_url)
switch(t) {
case 'open':
// should get a proper messsage from now on.
- try{
- that.iframe.onload = undefined;
- } catch (x) {}
- that.iframe.onerror = undefined;
+ that.iframeObj.loaded();
that.ri._didOpen();
break;
case 'close':
@@ -48,32 +45,18 @@ var HtmlFileTransport = FacadeJS['w-iframe-htmlfile'] = function (ri, trans_url)
};
_window[WPrefix][that.id] = callback;
- var iframe = that.iframe = _document.createElement('iframe');
- iframe.src = iframe_url;
- iframe.style.display = 'none';
- iframe.style.position = 'absolute';
- iframe.onerror = function() {
- that.cleanup();
- that.ri._didClose(1001, "Can't load iframe");
- };
- iframe.onload = function() {
- // `onload` is triggered before scripts on the iframe are
- // executed. Give it few seconds to actually load stuff.
- setTimeout(function() {
- if (that.iframe && that.iframe.onerror) that.iframe.onerror();
- }, 5000);
- };
- _document.body.appendChild(iframe);
+ that.iframeObj = utils.createIframe(iframe_url, function() {
+ that.cleanup();
+ that.ri._didClose(1001, "Can't load iframe");
+ });
};
// Inheritnace
HtmlFileTransport.prototype = new BufferedSender();
HtmlFileTransport.prototype.cleanup = function() {
var that = this;
- if (that.iframe) {
- that.iframe.onload = that.iframe.onerror = null;
- that.iframe.parentNode.removeChild(that.iframe);
- that.iframe = undefined;
+ if (that.iframeObj) {
+ that.iframeObj.cleanup();
delete _window[WPrefix][that.id];
}
};
diff --git a/lib/trans-iframe.js b/lib/trans-iframe.js
index 85507b4..dfb7ea2 100644
--- a/lib/trans-iframe.js
+++ b/lib/trans-iframe.js
@@ -19,35 +19,21 @@ IframeTransport.prototype.i_constructor = function(ri, trans_url, base_url) {
iframe_url += '?t=' + (+new Date);
}
- var iframe = that.iframe = _document.createElement('iframe');
- iframe.src = iframe_url;
- iframe.style.display = 'none';
- iframe.style.position = 'absolute';
- iframe.onerror = function() {
- that.cleanup();
- that.ri._didClose(1001, "Can't load iframe");
- };
- iframe.onload = function() {
- utils.detachMessage(that.onmessage_cb);
- utils.attachMessage(that.onmessage_cb);
- // `onload` is triggered before scripts on the iframe are
- // executed. Give it few seconds to actually load stuff.
- setTimeout(function() {
- if (that.iframe && that.iframe.onerror) that.iframe.onerror();
- }, 5000);
- };
+ that.iframeObj = utils.createIframe(iframe_url, function() {
+ that.cleanup();
+ that.ri._didClose(1001, "Can't load iframe");
+ });
+
that.onmessage_cb = function(e){that.onmessage(e);};
utils.attachMessage(that.onmessage_cb);
- _document.body.appendChild(iframe);
};
IframeTransport.prototype.cleanup = function() {
var that = this;
- if (that.iframe) {
- that.iframe.onload = that.iframe.onerror = null;
- that.iframe.parentNode.removeChild(that.iframe);
+ if (that.iframeObj) {
+ that.iframeObj.cleanup();
utils.detachMessage(that.onmessage_cb);
- that.onmessage_cb = that.iframe = undefined;
+ that.onmessage_cb = that.iframeObj = undefined;
}
};
@@ -58,7 +44,7 @@ IframeTransport.prototype.onmessage = function(e) {
var msg = JSON.parse(e.data.slice(1));
switch(type) {
case 's':
- that.iframe.onload = that.iframe.onerror = null;
+ that.iframeObj.loaded();
that.postMessage('s', [SockJS.version, that.protocol, that.trans_url]);
break;
case 'o':
@@ -77,7 +63,7 @@ IframeTransport.prototype.onmessage = function(e) {
IframeTransport.prototype.postMessage = function(type, messages) {
var that = this;
var msg = JSON.stringify(messages);
- that.iframe.contentWindow.postMessage(type + msg, that.origin);
+ that.iframeObj.iframe.contentWindow.postMessage(type + msg, that.origin);
};
IframeTransport.prototype.doSend = function (message) {
diff --git a/lib/utils.js b/lib/utils.js
index fff0870..f20b251 100644
--- a/lib/utils.js
+++ b/lib/utils.js
@@ -120,3 +120,43 @@ utils.polluteGlobalNamespace = function() {
_window[WPrefix] = {};
}
};
+
+utils.createIframe = function (iframe_url, error_callback) {
+ var iframe = _document.createElement('iframe');
+ var tref;
+ var unattach = function() {
+ // Explorer had problems with that.
+ try {iframe.onload = null;} catch (x) {}
+ iframe.onerror = null;
+ clearTimeout(tref);
+ };
+ var cleanup = function() {
+ if (iframe) {
+ unattach();
+ iframe.parentNode.removeChild(iframe);
+ iframe = null;
+ }
+ };
+ var onerror = function() {
+ if (iframe) {
+ cleanup();
+ error_callback();
+ }
+ };
+ iframe.src = iframe_url;
+ iframe.style.display = 'none';
+ iframe.style.position = 'absolute';
+ iframe.onerror = onerror;
+ iframe.onload = function() {
+ // `onload` is triggered before scripts on the iframe are
+ // executed. Give it few seconds to actually load stuff.
+ setTimeout(onerror, 2000);
+ };
+ _document.body.appendChild(iframe);
+ tref = setTimeout(cleanup, 5000);
+ return {
+ iframe: iframe,
+ cleanup: cleanup,
+ loaded: unattach
+ };
+};
--
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