MediaWiki:Common.js: Difference between revisions

No edit summary
No edit summary
Line 3,161: Line 3,161:
     // -------------------------------------------------------------------
     // -------------------------------------------------------------------
     var API = API_BASE.replace(/\/+$/, '');
     var API = API_BASE.replace(/\/+$/, '');
    var TTL = 6 * 60 * 60 * 1000;                // names change rarely
     var mem = {};
     var mem = {};
     var warned = false;
     var warned = false;
Line 3,169: Line 3,168:
     var libraryDown = !LIBRARY_ENABLED;
     var libraryDown = !LIBRARY_ENABLED;


     function cacheGet(id) {
     // In memory only, for the life of the page. Names used to be kept in
        if (mem[id]) return mem[id];
    // sessionStorage for six hours, which quietly outlived any correction
        try {
    // made in the library or on the map page. They now come from the map
            var raw = sessionStorage.getItem('gr-lib:' + id);
    // page, which is read once per load, so there is nothing to gain from
            if (!raw) return null;
    // a second cache that can disagree with it.
            var o = JSON.parse(raw);
    function cacheGet(id) { return mem[id] || null; }
            if (!o || (Date.now() - o.t) > TTL) return null;
     function cacheSet(id, v) { mem[id] = v; }
            mem[id] = o.v;
            return o.v;
        } catch (e) { return null; }
    }
     function cacheSet(id, v) {
        mem[id] = v;
        try { sessionStorage.setItem('gr-lib:' + id, JSON.stringify({ t: Date.now(), v: v })); }
        catch (e) { }
    }


     // The library returns camelCase fields; normalise to what the citation
     // The library returns camelCase fields; normalise to what the citation
Line 3,280: Line 3,270:
     function granthaMap() {
     function granthaMap() {
         if (mapPromise) return mapPromise;
         if (mapPromise) return mapPromise;
         var cached = null;
         // Deliberately NOT cached in sessionStorage. It was, for six hours,
         try {
        // which meant that editing the map page changed nothing for anyone
            var raw = sessionStorage.getItem('gr-lib:map');
        // whose tab had already read the old one - the exact failure you hit
            var o = raw && JSON.parse(raw);
        // while setting this up. mapPromise already collapses it to one
            if (o && (Date.now() - o.t) < TTL) cached = o.v;
        // request per page load, and the browser's own HTTP cache handles
        } catch (e) { }
        // the rest; a stale map is far more expensive than a small fetch.
        if (cached) { mapPromise = Promise.resolve(cached); return mapPromise; }
         try { sessionStorage.removeItem('gr-lib:map'); } catch (e) { }


         var url = (window.mw && mw.util && mw.util.getUrl)
         var url = (window.mw && mw.util && mw.util.getUrl)
Line 3,294: Line 3,284:
             .then(function (r) { return r.ok ? r.json() : {}; })
             .then(function (r) { return r.ok ? r.json() : {}; })
             .then(function (m) {
             .then(function (m) {
                 // Either a flat {page: id} object, or {pages:{...},
                 // Either a flat {page: id} object, or {pages, works,
                 // locators:{...}} when division names are recorded too.
                 // locators} when names and division names are recorded too.
                 m = m || {};
                 m = m || {};
                 if (!m.pages) m = { pages: m, locators: {} };
                 if (!m.pages) m = { pages: m };
                m.works = m.works || {};
                 m.locators = m.locators || {};
                 m.locators = m.locators || {};
                 try { sessionStorage.setItem('gr-lib:map', JSON.stringify({ t: Date.now(), v: m })); }
                 try {
                 catch (e) { }
                    if (!Object.keys(m.works).length) {
                        console.info('[grantha] the map page has no "works" section, ' +
                            'so names must come from the library API. Re-run ' +
                            'sync_grantha_map.py to embed them.');
                    }
                 } catch (e) { }
                 return m;
                 return m;
             })
             })