Comparing version 0.0.4 to 0.0.5
@@ -7,3 +7,3 @@ { | ||
], | ||
"version": "0.0.4", | ||
"version": "0.0.5", | ||
"main": "d6.js", | ||
@@ -10,0 +10,0 @@ "homepage": "http://lighter.io/d6", |
@@ -8,8 +8,2 @@ /** | ||
var d6Load = function (url) { | ||
if (window.d6) { | ||
d6._LOAD(url); | ||
} | ||
}; | ||
(function () { | ||
@@ -47,5 +41,5 @@ | ||
if (isSameDomain(url) && (!which || which == 1)) { | ||
//+env:dev | ||
//+env:debug | ||
log('Loading URL: "' + url + '"'); | ||
//-env:dev | ||
//-env:debug | ||
preventDefault(event); | ||
@@ -83,23 +77,18 @@ pushHistory(url); | ||
var prefetchUrl = function (url) { | ||
// Only proceed if it's not already prefetched. | ||
if (!cache[url]) { | ||
// Create a callback queue. | ||
var callbacks = cache[url] = []; | ||
getD6Json(url, function (response) { | ||
// If callbacks were registered, use it and remove it. | ||
if (getLength(callbacks)) { | ||
forEach(callbacks, function (callback) { | ||
callback(response); | ||
}); | ||
delete cache[url]; | ||
} | ||
// Otherwise, wait up to its TTL for it to be loaded. | ||
else { | ||
cache[url] = response; | ||
// By default, give the user 10 seconds to click. | ||
var ttl = response.ttl || 1e4; | ||
setTimeout(function () { | ||
// Create a callback queue to execute when data arrives. | ||
cache[url] = [function (response) { | ||
// Cache the response so data can be used without a queue. | ||
cache[url] = response; | ||
// Remove the data after 10 seconds, or the given TTL. | ||
var ttl = response.ttl || 1e4; | ||
setTimeout(function () { | ||
// Only delete if it's not a new callback queue. | ||
if (!isArray(cache[url])) { | ||
delete cache[url]; | ||
}, ttl); | ||
} | ||
}); | ||
} | ||
}, ttl); | ||
}]; | ||
getD6Json(url); | ||
} | ||
@@ -123,3 +112,4 @@ }; | ||
if (!resource) { | ||
getD6Json(url, callback); | ||
cache[url] = [callback]; | ||
getD6Json(url); | ||
} | ||
@@ -137,6 +127,21 @@ // If the "resource" is a callback queue, then pushing means listening. | ||
// Request JSON, indicating with a URL param that D6 is requesting it. | ||
var getD6Json = function (url, callback) { | ||
url += (contains(url, '?') ? '&' : '?') + 'd6=on'; | ||
getJson(url, callback); | ||
/** | ||
* Request JSON, then execute any callbacks that have been waiting for it. | ||
*/ | ||
var getD6Json = function (url, data) { | ||
// Indicate with a URL param that D6 is requesting data, so we'll get JSON. | ||
var d6Url = url + (contains(url, '?') ? '&' : '?') + 'd6=on'; | ||
// When data is received, execute all callbacks that have been waiting. | ||
var onComplete = function (response) { | ||
forEach(cache[url], function (callback) { | ||
callback(response); | ||
}); | ||
// Once everything's been executed, remove the queue. | ||
delete cache[url]; | ||
}; | ||
// Fire the JSON request. | ||
getResponse(d6Url, data, onComplete, onComplete, 1); | ||
}; | ||
@@ -143,0 +148,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
11372
259