popconlaunch: avoid polluting global scope

master
Rob Pilling 2023-05-10 23:26:03 +01:00
parent 1cbda278fe
commit b4d46d5779
2 changed files with 29 additions and 29 deletions

View File

@ -1,29 +1,29 @@
{ (function () {
var oldRead_1 = require("Storage").readJSON; var oldRead = require("Storage").readJSON;
var monthAgo_1 = Date.now() - 1000 * 86400 * 28; var monthAgo = Date.now() - 1000 * 86400 * 28;
var cache_1; var cache;
var ensureCache_1 = function () { var ensureCache = function () {
if (!cache_1) { if (!cache) {
cache_1 = oldRead_1("popcon.cache.json", true); cache = oldRead("popcon.cache.json", true);
if (!cache_1) if (!cache)
cache_1 = {}; cache = {};
} }
return cache_1; return cache;
}; };
var saveCache_1 = function (orderChanged) { var saveCache = function (orderChanged) {
require("Storage").writeJSON("popcon.cache.json", cache_1); require("Storage").writeJSON("popcon.cache.json", cache);
if (orderChanged) { if (orderChanged) {
var info = oldRead_1("popcon.info", true); var info = oldRead("popcon.info", true);
info.cacheBuster = !info.cacheBuster; info.cacheBuster = !info.cacheBuster;
require("Storage").writeJSON("popcon.info", info); require("Storage").writeJSON("popcon.info", info);
} }
}; };
var sortCache_1 = function () { var sortCache = function () {
var ents = Object.values(cache_1); var ents = Object.values(cache);
ents.sort(function (a, b) { ents.sort(function (a, b) {
var n; var n;
var am = (a.last > monthAgo_1); var am = (a.last > monthAgo);
var bm = (b.last > monthAgo_1); var bm = (b.last > monthAgo);
n = bm - am; n = bm - am;
if (n) if (n)
return n; return n;
@ -51,31 +51,31 @@
}; };
require("Storage").readJSON = (function (fname, skipExceptions) { require("Storage").readJSON = (function (fname, skipExceptions) {
var _a; var _a;
var j = oldRead_1(fname, skipExceptions); var j = oldRead(fname, skipExceptions);
if (/\.info$/.test(fname)) { if (/\.info$/.test(fname)) {
var cache_2 = ensureCache_1(); var cache_1 = ensureCache();
var so = void 0; var so = void 0;
if (j.src && (so = (_a = cache_2[j.src]) === null || _a === void 0 ? void 0 : _a.sortorder) != null) if (j.src && (so = (_a = cache_1[j.src]) === null || _a === void 0 ? void 0 : _a.sortorder) != null)
j.sortorder = so; j.sortorder = so;
else else
j.sortorder = 99; j.sortorder = 99;
} }
return j; return j;
}); });
var oldLoad_1 = load; var oldLoad = load;
global.load = function (src) { global.load = function (src) {
if (src) { if (src) {
var cache_3 = ensureCache_1(); var cache_2 = ensureCache();
var ent = cache_3[src] || (cache_3[src] = { var ent = cache_2[src] || (cache_2[src] = {
pop: 0, pop: 0,
last: 0, last: 0,
sortorder: -10, sortorder: -10,
}); });
ent.pop++; ent.pop++;
ent.last = Date.now(); ent.last = Date.now();
var orderChanged = sortCache_1(); var orderChanged = sortCache();
saveCache_1(orderChanged); saveCache(orderChanged);
} }
return oldLoad_1(src); return oldLoad(src);
}; };
} })();

View File

@ -1,4 +1,4 @@
{ (() => {
type Timestamp = number; type Timestamp = number;
const oldRead = require("Storage").readJSON; const oldRead = require("Storage").readJSON;
@ -99,4 +99,4 @@ global.load = (src: string) => {
return oldLoad(src); return oldLoad(src);
}; };
} })()