Skip to content
Permalink
Browse files

replacing caching engine to something more robust

  • Loading branch information...
bevacqua committed Nov 15, 2014
1 parent 4107061 commit b9b059143580905e5de707817270006c0f2cbdb0
Showing with 41 additions and 43 deletions.
  1. +7 −6 browser/activator.js
  2. +2 −2 browser/cache.js
  3. +6 −4 browser/caching.js
  4. +1 −1 browser/links.js
  5. +15 −9 browser/mount.js
  6. +3 −3 browser/stores/idb.js
  7. +5 −2 lib/render.js
  8. +1 −1 package.json
  9. +1 −15 versioning.js
@@ -45,23 +45,24 @@ function go (url, options) {
fetcher.abortPending();
fetcher(route, { element: context, source: 'intent' }, resolved);

function resolved (err, model) {
function resolved (err, data) {
if (err) {
return;
}
if (versioning.ensure(model.__tv, state.version)) {
if (data.__tv !== state.version) {
location.href = url; // version changes demands fallback to strict navigation
}
navigation(route, model, direction);
partial(state.container, null, model, route);
navigation(route, data.model, direction);
partial(state.container, null, data.model, route);
scrollInto(id(route.parts.hash), o.scroll);
}
}

function start (model) {
if (versioning.ensure(model.__tv, state.version)) {
function start (data) {
if (data.__tv !== state.version) {
location.reload(); // version may change between Taunus being loaded and a model being available
}
var model = data.model;
var route = replaceWith(model);
emitter.emit('start', state.container, model);
partial(state.container, null, model, route, { render: false });
@@ -35,7 +35,7 @@ function get (url, done) {
if (!item) {
return false; // cache must have item
}
var mismatch = typeof item.version !== 'string' || !versioning.match(item.version, state.version);
var mismatch = typeof item.version !== 'string' || item.version !== state.version;
if (mismatch) {
return false; // cache must match current version
}
@@ -59,7 +59,7 @@ function set (url, data, duration) {
function store (s) {
s.set(url, {
data: cloned,
version: cloned.__tv,
version: state.version,
expires: Date.now() + duration
});
}
@@ -12,8 +12,8 @@ function e (value) {
return value || '';
}

function getKey (route) {
return route.parts.pathname + e(route.parts.query);
function getViewKey (route) {
return 'view:/' + route.parts.pathname + e(route.parts.query);
}

function setup (duration, route) {
@@ -28,7 +28,7 @@ function setup (duration, route) {
}

function intercept (e) {
cache.get(getKey(e.route), result);
cache.get(getViewKey(e.route), result);

function result (err, data) {
if (!err && data) {
@@ -58,7 +58,9 @@ function persist (route, context, data) {
if (typeof route.cache === 'number') {
d = route.cache;
}
cache.set(getKey(route), data, parseDuration(d) * 1000);
if (data.model) {
cache.set(getViewKey(route), data.model, parseDuration(d) * 1000);
}
}

function ready (fn) {
@@ -92,7 +92,7 @@ function prefetch (e, anchor) {
prefetching.push(anchor);
fetcher(route, { element: anchor, source: 'prefetch' }, resolved);

function resolved (err, data) {
function resolved () {
prefetching.splice(prefetching.indexOf(anchor), 1);
if (clicksOnHold.indexOf(anchor) !== -1) {
clicksOnHold.splice(clicksOnHold.indexOf(anchor), 1);
@@ -61,38 +61,44 @@ function mount (container, wiring, options) {

function fetched (err, data) {
if (err) {
throw new Error('Fetching JSON data model for first view failed.');
throw new Error('Fetching JSON data model failed at mountpoint.');
}
boot(data);
}

function inlineboot () {
var id = container.getAttribute('data-taunus');
var script = document.getElementById(id);
var model = JSON.parse(unescape(script.innerText || script.textContent));
boot(model);
var data = JSON.parse(unescape(script.innerText || script.textContent));
boot(data);
}

function manualboot () {
if (typeof g.taunusReady === 'function') {
g.taunusReady = boot; // not yet an object? turn it into the boot method
} else if (g.taunusReady && typeof g.taunusReady === 'object') {
boot(g.taunusReady); // already an object? boot with that as the model
boot(g.taunusReady); // already an object? boot with that as the data object
} else {
throw new Error('Did you forget to add the taunusReady global?');
}
}

function boot (model) {
function boot (data) {
if (booted) { // sanity
return;
}
if (!model || typeof model !== 'object') {
throw new Error('Taunus model must be an object!');
if (!data) {
throw new Error('Taunus data is required! Boot failed');
}
if (!data.__tv) {
throw new Error('Version data is missing! Boot failed');
}
if (!data.model || typeof data.model !== 'object') {
throw new Error('Taunus model must be an object! Boot failed');
}
booted = true;
caching.persist(route, state.container, model);
activator.start(model);
caching.persist(route, state.container, data);
activator.start(data);
}
}

@@ -5,9 +5,9 @@ var g = global;
var idb = g.indexedDB || g.mozIndexedDB || g.webkitIndexedDB || g.msIndexedDB;
var supports;
var db;
var dbName = 'taunus-cache';
var store = 'view-models';
var keyPath = 'url';
var dbName = 'taunus';
var store = 'wildstore';
var keyPath = 'key';
var setQueue = [];
var testedQueue = [];

@@ -25,7 +25,7 @@ function render (defaultAction, vm, req, res, next) {

data.requestUrl = req.url + '?json';
data.model = data.model || {};
data.model.__tv = state.version;
data.__tv = state.version;

var action = data.model.action || defaultAction;
var strategy;
@@ -47,7 +47,10 @@ function render (defaultAction, vm, req, res, next) {

if (accept === 'json' || 'json' in req.query) {
strategy = 'callback' in req.query ? 'jsonp' : 'json';
res[strategy](data.model);
res[strategy]({
__tv: data.__tv,
model: data.model
});
} else if (accept === 'html') {
html(res.send.bind(res));
} else if (accept === 'text') {
@@ -1,7 +1,7 @@
{
"name": "taunus",
"description": "Micro Isomorphic MVC Engine for Node.js",
"version": "2.10.0",
"version": "2.11.0",
"homepage": "https://github.com/bevacqua/taunus",
"authors": [
"Nicolas Bevacqua <nicolasbevacqua@gmail.com>"
@@ -6,20 +6,6 @@ function get (v) {
return 't' + t + ';v' + v;
}

function match (v, expected) {
return v === expected;
}

function ensure (v, expected) {
var mismatch = !match(v, expected);
if (mismatch) {
global.location.reload();
}
return mismatch;
}

module.exports = {
get: get,
match: match,
ensure: ensure
get: get
};

0 comments on commit b9b0591

Please sign in to comment.
You can’t perform that action at this time.