diff --git a/kb.js b/kb.js index 1e54cd8..9a5053f 100644 --- a/kb.js +++ b/kb.js @@ -31,7 +31,7 @@ const crypto = require('crypto'); let netkb = null; try { netkb = require('./netkb'); } catch (e) {} let DATA_DIR = null, SEEDS = [], ready = null, SITE_URL = '', HELP_TEXT = '', MEMORY = false; -let cache = null, cacheAt = 0, index = null; +let cache = null, cacheAt = 0, index = null, netVer = ''; const TTL = 30000; // two app copies share the volume for about 30 s during a deploy: re-read now and then // ---------------------------------------------------------------- text + scoring (pure, unit-tested) @@ -196,9 +196,9 @@ function init(opts) { async function own() { await load(); return cache.slice(); } async function load(force) { if (ready) await ready; - if (!force && cache && Date.now() - cacheAt < TTL) return cache; + if (!force && cache && Date.now() - cacheAt < TTL && (!netkb || netkb.version() === netVer)) return cache; // a new network set rebuilds at once const all = await store().all(); - cache = all.filter(e => !e.deleted); cacheAt = Date.now(); rx.clear(); + cache = all.filter(e => !e.deleted); cacheAt = Date.now(); rx.clear(); netVer = netkb ? netkb.version() : ''; index = buildIndex(cache.concat(netkb ? netkb.remote() : [])); index.sites = netkb ? netkb.sites() : []; return cache; } diff --git a/netkb.js b/netkb.js index 3587d70..0a79f6c 100644 --- a/netkb.js +++ b/netkb.js @@ -102,4 +102,5 @@ function init(opts) { function view() { return Object.assign({ enabled: enabled(), site: siteName(), version: merged.version, sites: Object.keys(merged.sites || {}), entries: (merged.entries || []).length, remote: remote().length, loadedAt }, stats); } function _set(m) { merged = m; } // tests -module.exports = { init, publish, publishSoon, refresh, remote, sites, view, enabled, _set }; +const version = () => merged.version || ''; +module.exports = { init, publish, publishSoon, refresh, remote, sites, view, enabled, version, _set };