diff --git a/apps/popconlaunch/ChangeLog b/apps/popconlaunch/ChangeLog index 5560f00bc..8a2124e33 100644 --- a/apps/popconlaunch/ChangeLog +++ b/apps/popconlaunch/ChangeLog @@ -1 +1,2 @@ 0.01: New App! +0.02: Trim old entries from the popcon app cache diff --git a/apps/popconlaunch/README.md b/apps/popconlaunch/README.md index 2814082a7..18a5354a4 100644 --- a/apps/popconlaunch/README.md +++ b/apps/popconlaunch/README.md @@ -3,6 +3,6 @@ Popcon Display apps sorted by regular use. No config - install the app and all your launchers will sort apps by most popular, based off launch counts within the last month, and then sort them by the most recently launched app. -:warning: Warning: this app overrides [`Storage.readJSON`], so may slow down your watch when using apps that perform I/O. +⚠️ Warning: this app overrides [`Storage.readJSON`], so may slow down your watch when using apps that perform I/O. [`Storage.readJSON`]: https://www.espruino.com/ReferenceBANGLEJS2#l_Storage_readJSON diff --git a/apps/popconlaunch/boot.js b/apps/popconlaunch/boot.js index eb3f18e1b..9124344b2 100644 --- a/apps/popconlaunch/boot.js +++ b/apps/popconlaunch/boot.js @@ -1,6 +1,7 @@ { var oldRead_1 = require("Storage").readJSON; - var monthAgo_1 = Date.now() - 1000 * 86400 * 28; + var oneMonth_1 = 1000 * 86400 * 28; + var monthAgo_1 = Date.now() - oneMonth_1; var cache_1; var ensureCache_1 = function () { if (!cache_1) { @@ -10,8 +11,20 @@ } return cache_1; }; - var saveCache_1 = function (orderChanged) { - require("Storage").writeJSON("popcon.cache.json", cache_1); + var trimCache_1 = function (cache) { + var threeMonthsBack = Date.now() - oneMonth_1 * 3; + var del = []; + for (var k in cache) + if (cache[k].last < threeMonthsBack) + del.push(k); + for (var _i = 0, del_1 = del; _i < del_1.length; _i++) { + var k = del_1[_i]; + delete cache[k]; + } + }; + var saveCache_1 = function (cache, orderChanged) { + trimCache_1(cache); + require("Storage").writeJSON("popcon.cache.json", cache); if (orderChanged) { var info = oldRead_1("popcon.info", true); info.cacheBuster = !info.cacheBuster; @@ -74,7 +87,7 @@ ent.pop++; ent.last = Date.now(); var orderChanged = sortCache_1(); - saveCache_1(orderChanged); + saveCache_1(cache_3, orderChanged); } return oldLoad_1(src); }; diff --git a/apps/popconlaunch/boot.ts b/apps/popconlaunch/boot.ts index a7ac73518..306d4844a 100644 --- a/apps/popconlaunch/boot.ts +++ b/apps/popconlaunch/boot.ts @@ -1,9 +1,6 @@ { type Timestamp = number; - -const oldRead = require("Storage").readJSON; -const monthAgo = Date.now() - 1000 * 86400 * 28; -let cache: undefined | { +type Cache = { [key: string]: { sortorder: number, pop: number, // amount of launches @@ -11,6 +8,11 @@ let cache: undefined | { } }; +const oldRead = require("Storage").readJSON; +const oneMonth = 1000 * 86400 * 28; +const monthAgo = Date.now() - oneMonth; +let cache: undefined | Cache; + const ensureCache = (): NonNull => { if(!cache){ cache = oldRead("popcon.cache.json", true); @@ -20,7 +22,19 @@ const ensureCache = (): NonNull => { return cache; }; -const saveCache = (orderChanged: boolean) => { +const trimCache = (cache: Cache) => { + const threeMonthsBack = Date.now() - oneMonth * 3; + const del = []; + for(const k in cache) + if(cache[k]!.last < threeMonthsBack) + del.push(k); + + for(const k of del) + delete cache[k]; +}; + +const saveCache = (cache: Cache, orderChanged: boolean) => { + trimCache(cache); require("Storage").writeJSON("popcon.cache.json", cache); if(orderChanged){ // ensure launchers reload their caches: @@ -94,7 +108,7 @@ global.load = (src: string) => { ent.pop++; ent.last = Date.now(); const orderChanged = sortCache(); - saveCache(orderChanged); + saveCache(cache, orderChanged); } return oldLoad(src); diff --git a/apps/popconlaunch/metadata.json b/apps/popconlaunch/metadata.json index 0ccc54d9e..1acab2b6c 100644 --- a/apps/popconlaunch/metadata.json +++ b/apps/popconlaunch/metadata.json @@ -2,7 +2,7 @@ "id": "popconlaunch", "name": "Popcon Launcher", "shortName": "Popcon", - "version": "0.01", + "version": "0.02", "description": "Launcher modification - your launchers will display your favourite (popular) apps first. Overrides `readJSON`, may slow down your watch", "readme": "README.md", "icon": "app.png",