This commit is contained in:
Kevin Ngo 2014-05-06 16:44:13 -07:00
Родитель 8845b4eea6
Коммит 9fedbd91ab
2 изменённых файлов: 63 добавлений и 21 удалений

Просмотреть файл

@ -1,6 +1,6 @@
define('requests',
['cache', 'defer', 'log', 'utils'],
function(cache, defer, log, utils) {
['cache', 'defer', 'log', 'settings', 'utils'],
function(cache, defer, log, settings, utils) {
var console = log('req');
@ -96,32 +96,58 @@ define('requests',
return def;
}
function get(url, nocache, persistent) {
// During a single session, we never want to fetch the same URL more than
// once. Because our persistent offline cache does XHRs in the background
// to keep the cache fresh, we want to do that only once per session. In
// order to do all this magic, we have to keep an array of all of the URLs
// we hit per session.
var urls_fetched = {};
function get(url, nocache) {
var cache_offline = settings.offline_cache_enabled();
var cached;
if (cache.has(url) && !nocache) {
console.log('GETing from cache', url);
cached = cache.get(url);
} else if (cache.persist.has(url) && persistent && !nocache) {
console.log('GETing from persistent cache', url);
cached = cache.persist.get(url);
}
if (cached) {
return defer.Deferred()
.resolve(cached)
.promise({__cached: true});
}
return _get.apply(this, arguments, persistent);
}
function _get(url, nocache, persistent) {
var def_cached;
if (cached) {
def_cached = defer.Deferred()
.resolve(cached)
.promise({__cached: true});
if (!cache_offline || url in urls_fetched) {
// If we don't need to make an XHR in the background to update
// the cache, then let's bail now.
return def_cached;
}
}
console.log('GETing', url);
return ajax('GET', url).done(function(data, xhr) {
urls_fetched[url] = null;
var def_ajax = ajax('GET', url).done(function(data) {
console.log('GOT', url);
if (!nocache) {
data.__time = +(new Date());
cache.set(url, data);
if (persistent) cache.persist.set(url, data);
}
if (cached && cache_offline) {
console.log('Updating request cache', url);
}
});
if (cached && cache_offline) {
// If the response was cached, we still want to fire off the
// AJAX request so the cache can get updated in the background,
// but let's resolve this deferred with the cached response
// so the request pool can get closed and the builder can render
// the template for the `defer` block.
return def_cached;
}
return def_ajax;
}
function handle_errors(xhr, type, status) {
@ -175,6 +201,7 @@ define('requests',
var initiated = 0;
var marked_to_finish = false;
var closed = false;
var failed = false;
function finish() {
if (closed) {
@ -185,11 +212,7 @@ define('requests',
closed = true;
// Resolve the deferred whenevs.
if (window.setImmediate) {
setImmediate(def.resolve);
} else {
setTimeout(def.resolve, 0);
}
setTimeout(failed ? def.reject : def.resolve, 0);
}
}
@ -202,6 +225,9 @@ define('requests',
var req = func.apply(this, args);
initiated++;
requests.push(req);
req.fail(function() {
failed = true;
});
req.always(function() {
initiated--;
finish();

Просмотреть файл

@ -1,6 +1,14 @@
define('settings', ['l10n', 'settings_local', 'underscore'], function(l10n, settings_local, _) {
var gettext = l10n.gettext;
function offline_cache_enabled() {
var storage = require('storage');
if (storage.getItem('offline_cache_disabled') || require('capabilities').phantom) {
return false;
}
return window.location.search.indexOf('cache=false') === -1;
}
return _.defaults(settings_local, {
app_name: 'commonplace app',
init_module: 'main',
@ -13,6 +21,14 @@ define('settings', ['l10n', 'settings_local', 'underscore'], function(l10n, sett
api_param_blacklist: null,
api_cdn_whitelist: {},
// These are the only URLs that should be cached
// (key: URL; value: TTL [time to live] in seconds).
// Keep in mind that the cache is always refreshed asynchronously;
// these TTLs apply to only when the app is first launched.
offline_cache_whitelist: {},
offline_cache_enabled: offline_cache_enabled,
offline_cache_limit: 1024 * 1024 * 4, // 4 MB
model_prototypes: {},
fragment_error_template: 'errors/fragment.html',