Skip to content

Commit

Permalink
experiments with removing jsonp script
Browse files Browse the repository at this point in the history
  • Loading branch information
robertpalmer committed Feb 24, 2011
1 parent 65593e3 commit 0b5003e
Showing 1 changed file with 35 additions and 17 deletions.
52 changes: 35 additions & 17 deletions lib/linccer.js
Expand Up @@ -6,6 +6,8 @@ var Linccer = function(_options) {
var isReady;
var timerId;

var connections;

var options = { 'server' : '' };
HC.extend(options, _options);

Expand Down Expand Up @@ -34,30 +36,46 @@ var Linccer = function(_options) {
var jsonpName = 'jsonp' + Math.floor(Math.random() * 10000000001);
var scriptURL = "https://linccer" + options["server"] + ".hoccer.com/v3" + scriptName + "&jsonp=" + jsonpName;
// var scriptURL = "http://127.0.0.1:9292/v3" + scriptName + "&jsonp=" + jsonpName;

var script = document.createElement("script");
var head = document.getElementsByTagName("head")[0] || document.documentElement,
script = document.createElement("script");

script.setAttribute("type", "text/javascript");
script.setAttribute("src", scriptURL);

document.body.appendChild(script);

if (script.src.match(/receive.js/)) {
console.log('receive with', jsonpName);
}

head.appendChild(script);

window[jsonpName] = function(data) {
if (callbackFunctionName !== undefined
&& that[callbackFunctionName] !== undefined) {
window[jsonpName] = function(jsonpName, script) {
return function(data) {
if (head) {
if (script.src.match(/receive.js/)) {
console.log('release script', script.src);
}
head.removeChild(script);
}

console.log('undefining', jsonpName);
window[jsonpName] = undefined;

if (callbackFunctionName !== undefined
&& that[callbackFunctionName] !== undefined) {
that[callbackFunctionName](data);
}
}

window[jsonpName] = undefined;
document.body.removeChild(script);
delete script;
}
}(jsonpName, script);

if (timeout !== undefined) {
setTimeout(function() {
if (window[jsonpName] !== undefined) {
window[jsonpName]( { message: 'request_timeout' } );
setTimeout(function(name) {
return function() {
if (window[name] !== undefined) {
console.log('sending request timeout', name);
window[name]( { message: 'request_timeout' } );
}
}
}, timeout);
}(jsonpName), timeout);
}
}

Expand Down Expand Up @@ -133,7 +151,7 @@ var Linccer = function(_options) {
var timeout = options['timeout'];
delete options['timeout'];
}

params = [];
params.push('mode=' + mode);

Expand Down

0 comments on commit 0b5003e

Please sign in to comment.