From df50a4dd7bf99d11a8e6013fc7af3d1465833d7f Mon Sep 17 00:00:00 2001 From: Rob Pilling Date: Sat, 29 Apr 2023 10:29:51 +0100 Subject: [PATCH 01/13] Add AppInfo type --- typescript/types/info.d.ts | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 typescript/types/info.d.ts diff --git a/typescript/types/info.d.ts b/typescript/types/info.d.ts new file mode 100644 index 000000000..41dec8578 --- /dev/null +++ b/typescript/types/info.d.ts @@ -0,0 +1,12 @@ +type AppInfo = { + src: string, + img: string, + icon: string, + name: string, + type: AppType, + sortorder?: number, +}; + +type AppType = "app" | "clock" | "widget" | "module" | "bootloader" | + "settings" | "clkinfo" | "RAM" | "launch" | "textinput" | "scheduler" | + "notify" | "locale"; From d4878be7fa95f3b1782961bacd36895e8e552b38 Mon Sep 17 00:00:00 2001 From: Rob Pilling Date: Mon, 24 Apr 2023 22:29:17 +0100 Subject: [PATCH 02/13] popcon: initial launch-sortorder modification app --- apps/popconlaunch/ChangeLog | 1 + apps/popconlaunch/README.md | 9 +++++++ apps/popconlaunch/app.png | Bin 0 -> 899 bytes apps/popconlaunch/boot.ts | 43 ++++++++++++++++++++++++++++++++ apps/popconlaunch/metadata.json | 19 ++++++++++++++ 5 files changed, 72 insertions(+) create mode 100644 apps/popconlaunch/ChangeLog create mode 100644 apps/popconlaunch/README.md create mode 100644 apps/popconlaunch/app.png create mode 100644 apps/popconlaunch/boot.ts create mode 100644 apps/popconlaunch/metadata.json diff --git a/apps/popconlaunch/ChangeLog b/apps/popconlaunch/ChangeLog new file mode 100644 index 000000000..5560f00bc --- /dev/null +++ b/apps/popconlaunch/ChangeLog @@ -0,0 +1 @@ +0.01: New App! diff --git a/apps/popconlaunch/README.md b/apps/popconlaunch/README.md new file mode 100644 index 000000000..7804ebd73 --- /dev/null +++ b/apps/popconlaunch/README.md @@ -0,0 +1,9 @@ +Popcon +====== + +Display apps sorted by regular use. + +Settings +-------- + +- `Show Clocks` - Whether clocks are shown in the app list. Default `Yes`. diff --git a/apps/popconlaunch/app.png b/apps/popconlaunch/app.png new file mode 100644 index 0000000000000000000000000000000000000000..8b4e6caa2fe4720a32f492bd00c6e68751fabfbc GIT binary patch literal 899 zcmV-}1AP36P)EZi8a;XNfX0KK)EG5t2&jmXSR!CR3JOvSF^w(VT@Oq6O4&}^ElAua zX|l63`{wtXcjoO3*x1-CYovub^cOBY?cfcO1>;+VoWa;>Pk;!SG_WW*Es5gZ1-`R@ z4t>oKYJ)f#xYia)IV)#&xZ*BHYck+F1K9eED0&f|F;Lm{VL;rb?(c)W{8d& zzrH4`vJJ?>K!fln4K4LjpAx19%+|UjgL{LF?3gt^suTXG8HN@KQv>tY{cLjA%Q)j? zIX0ma_G_?6n>drF(WulAaitj}A^+b$v5k$*zr})O^uogfX;+bphn_9#OZ}pdk^!&c zqrUVgUd3m%o}@|o!cmyJiIaP-*BlK-1F7-kNoN^X5h5L~tnN`_& zI3(jUhvcNZU>jbg3|-gg8hTDln@m+>N(dP^vh_T*8x8>Qb*ytvU&Y#;lz3_Z*tBLl zEghFFmS~QUfhzCr>E}|Fxr&yKOBoXz4vqjF zlzKwtw{idZM95W(Tbb#rO0Wkqaj6$F@MWZp>h=2o=nr;LTzCQiH!$$4i=w<5W87%F zs6NXOGI0OH6?#VB0k9%#HO2Wg(|z6FTj}`r1kmXWJk5wmGlUFGsuA7}JOW^&y9!O$ zkR=&S*XaHEp1^o_Mn#&D^ig6k6`9rJMEHEc@fMjg8GR Z=Px>?AD_-bQjY)t002ovPDHLkV1j2qp7Q_z literal 0 HcmV?d00001 diff --git a/apps/popconlaunch/boot.ts b/apps/popconlaunch/boot.ts new file mode 100644 index 000000000..cb62ca643 --- /dev/null +++ b/apps/popconlaunch/boot.ts @@ -0,0 +1,43 @@ +// TODO: fastload +const oldRead = require("Storage").readJSON; +let cache; + +const ensureCache = () => { + if(!cache){ + cache = oldRead("popcon.cache.json", 1); + if(!cache) + cache = {}; + } +}; + +const saveCache = () => { + require("Storage").writeJSON("popcon.cache.json", cache); +}; + +const sortCache = () => { + // TODO +}; + +require("Storage").readJSON = (fname, skipExceptions) => { + const j = oldRead(fname, skipExceptions); + + if(fname.test(/\.info$/)){ + ensureCache(); + + if(j.src && cache[j.src]?.sortorder) + j.sortorder = cache[j.src].sortorder; + } + + return j; +} satisfies typeof oldRead; + +const oldLoad = load; +load = (src) => { + ensureCache(); + cache[src].pop++; + cache[src].last = Date.now(); + sortCache(); + saveCache(); + + return oldLoad(src); +}; diff --git a/apps/popconlaunch/metadata.json b/apps/popconlaunch/metadata.json new file mode 100644 index 000000000..906ad473a --- /dev/null +++ b/apps/popconlaunch/metadata.json @@ -0,0 +1,19 @@ +{ + "id": "popconlaunch", + "name": "Popcon Launcher", + "shortName": "Popcon", + "version": "0.01", + "description": "Launcher displaying your favourite (popular) apps first", + "readme": "README.md", + "icon": "TODO_app.png", + "type": "launch", + "tags": "tool,system,launcher", + "supports": ["BANGLEJS2"], + "storage": [ + {"name":"popcon.boot.js","url":"boot.js"} + ], + "data": [ + {"name":"popcon.cache.json"} + ], + "sortorder": -10 +} From 3b16c8557867f60cac62cb49e79cfccf8690410e Mon Sep 17 00:00:00 2001 From: Rob Pilling Date: Tue, 2 May 2023 22:31:47 +0100 Subject: [PATCH 03/13] popcon: change type --- apps/popconlaunch/metadata.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/popconlaunch/metadata.json b/apps/popconlaunch/metadata.json index 906ad473a..c20202c6e 100644 --- a/apps/popconlaunch/metadata.json +++ b/apps/popconlaunch/metadata.json @@ -6,7 +6,7 @@ "description": "Launcher displaying your favourite (popular) apps first", "readme": "README.md", "icon": "TODO_app.png", - "type": "launch", + "type": "bootloader", "tags": "tool,system,launcher", "supports": ["BANGLEJS2"], "storage": [ From fcfc33399dd56b919053f958c5467b004df6dc23 Mon Sep 17 00:00:00 2001 From: Rob Pilling Date: Wed, 3 May 2023 07:50:43 +0100 Subject: [PATCH 04/13] popcon: app sorting (and debug logs) --- apps/popconlaunch/boot.ts | 97 +++++++++++++++++++++++++++++++-------- 1 file changed, 79 insertions(+), 18 deletions(-) diff --git a/apps/popconlaunch/boot.ts b/apps/popconlaunch/boot.ts index cb62ca643..323d9ca27 100644 --- a/apps/popconlaunch/boot.ts +++ b/apps/popconlaunch/boot.ts @@ -1,43 +1,104 @@ // TODO: fastload -const oldRead = require("Storage").readJSON; -let cache; +type Timestamp = number; -const ensureCache = () => { +const oldRead = require("Storage").readJSON; +const monthAgo = Date.now() - 1000 * 86400 * 28; +let cache: undefined | { + [key: string]: { + sortorder: number, + pop: number, // amount of launches + last: Timestamp, + } +}; +let orderchanged = false; + +const ensureCache = (): NonNull => { if(!cache){ - cache = oldRead("popcon.cache.json", 1); + cache = oldRead("popcon.cache.json", true); if(!cache) cache = {}; } + return cache; }; const saveCache = () => { require("Storage").writeJSON("popcon.cache.json", cache); + console.log("saved cache:" + JSON.stringify(cache, 0, 2)); + if(orderchanged){ + // wipe launcher cache + require("Storage") + .list(/launch.*cache/) + .forEach(f => require("Storage").erase(f)) + } }; const sortCache = () => { - // TODO + const ents = Object.values(cache); + + ents.sort((a, b) => { + // group the most recently launched apps in the last month, + // then sort by launch count + // then by name + let n; + + const am = (a.last > monthAgo) as unknown as number; + const bm = (b.last > monthAgo) as unknown as number; + n = bm - am; + if(n) return n; + + n = b.pop - a.pop; + if(n) return n; + + // pops are the same, sort by most recent + n = b.last - a.last; + if(n) return n; + + if(a.nameb.name) return 1; + return 0; + }); + + let i = 0; + for(const ent of ents){ + if(ent.sortorder !== i) orderchanged = true; + ent.sortorder = i++; + } }; -require("Storage").readJSON = (fname, skipExceptions) => { - const j = oldRead(fname, skipExceptions); +require("Storage").readJSON = ((fname, skipExceptions) => { + const j: AppInfo = oldRead(fname, skipExceptions); + // ^ technically only AppInfo if we're "*.info" - if(fname.test(/\.info$/)){ - ensureCache(); + if(/\.info$/.test(fname)){ + const cache = ensureCache(); + let so; - if(j.src && cache[j.src]?.sortorder) - j.sortorder = cache[j.src].sortorder; + if(j.src && (so = cache[j.src]?.sortorder) != null) + j.sortorder = so, + console.log("readJSON(), modifying " + fname + "'s sort order to " + j.sortorder); + else + j.sortorder = 99, + console.log("readJSON(), leaving " + fname + "'s sort order as 99"); } return j; -} satisfies typeof oldRead; +}) satisfies typeof oldRead; const oldLoad = load; -load = (src) => { - ensureCache(); - cache[src].pop++; - cache[src].last = Date.now(); - sortCache(); - saveCache(); +global.load = (src: string) => { + if(src){ + const cache = ensureCache(); + console.log("load(\"" + src + "\")"); + const ent = cache[src] ||= (console.log("new cache entry"), { + pop: 0, + last: 0, + sortorder: -10, + }); + ent.pop++; + ent.last = Date.now(); + sortCache(); + saveCache(); + } return oldLoad(src); }; From 7f528585e7f87ca1c85dbe7d1c488dc28bb6e350 Mon Sep 17 00:00:00 2001 From: Rob Pilling Date: Tue, 2 May 2023 23:08:04 +0100 Subject: [PATCH 05/13] popcon: remove debug logs --- apps/popconlaunch/boot.ts | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/apps/popconlaunch/boot.ts b/apps/popconlaunch/boot.ts index 323d9ca27..8b2e9fe18 100644 --- a/apps/popconlaunch/boot.ts +++ b/apps/popconlaunch/boot.ts @@ -23,7 +23,6 @@ const ensureCache = (): NonNull => { const saveCache = () => { require("Storage").writeJSON("popcon.cache.json", cache); - console.log("saved cache:" + JSON.stringify(cache, 0, 2)); if(orderchanged){ // wipe launcher cache require("Storage") @@ -74,11 +73,9 @@ require("Storage").readJSON = ((fname, skipExceptions) => { let so; if(j.src && (so = cache[j.src]?.sortorder) != null) - j.sortorder = so, - console.log("readJSON(), modifying " + fname + "'s sort order to " + j.sortorder); + j.sortorder = so; else - j.sortorder = 99, - console.log("readJSON(), leaving " + fname + "'s sort order as 99"); + j.sortorder = 99; } return j; @@ -88,12 +85,11 @@ const oldLoad = load; global.load = (src: string) => { if(src){ const cache = ensureCache(); - console.log("load(\"" + src + "\")"); - const ent = cache[src] ||= (console.log("new cache entry"), { + const ent = cache[src] ||= { pop: 0, last: 0, sortorder: -10, - }); + }; ent.pop++; ent.last = Date.now(); sortCache(); From e6078df861b32512f8b2fac8e4af8c7c0d2fd6c9 Mon Sep 17 00:00:00 2001 From: Rob Pilling Date: Tue, 2 May 2023 23:08:58 +0100 Subject: [PATCH 06/13] popcon: generate JS --- apps/popconlaunch/boot.js | 78 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 apps/popconlaunch/boot.js diff --git a/apps/popconlaunch/boot.js b/apps/popconlaunch/boot.js new file mode 100644 index 000000000..613cb3474 --- /dev/null +++ b/apps/popconlaunch/boot.js @@ -0,0 +1,78 @@ +var oldRead = require("Storage").readJSON; +var monthAgo = Date.now() - 1000 * 86400 * 28; +var cache; +var orderchanged = false; +var ensureCache = function () { + if (!cache) { + cache = oldRead("popcon.cache.json", true); + if (!cache) + cache = {}; + } + return cache; +}; +var saveCache = function () { + require("Storage").writeJSON("popcon.cache.json", cache); + if (orderchanged) { + require("Storage") + .list(/launch.*cache/) + .forEach(function (f) { return require("Storage").erase(f); }); + } +}; +var sortCache = function () { + var ents = Object.values(cache); + ents.sort(function (a, b) { + var n; + var am = (a.last > monthAgo); + var bm = (b.last > monthAgo); + n = bm - am; + if (n) + return n; + n = b.pop - a.pop; + if (n) + return n; + n = b.last - a.last; + if (n) + return n; + if (a.name < b.name) + return -1; + if (a.name > b.name) + return 1; + return 0; + }); + var i = 0; + for (var _i = 0, ents_1 = ents; _i < ents_1.length; _i++) { + var ent = ents_1[_i]; + if (ent.sortorder !== i) + orderchanged = true; + ent.sortorder = i++; + } +}; +require("Storage").readJSON = (function (fname, skipExceptions) { + var _a; + var j = oldRead(fname, skipExceptions); + if (/\.info$/.test(fname)) { + var cache_1 = ensureCache(); + var so = void 0; + if (j.src && (so = (_a = cache_1[j.src]) === null || _a === void 0 ? void 0 : _a.sortorder) != null) + j.sortorder = so; + else + j.sortorder = 99; + } + return j; +}); +var oldLoad = load; +global.load = function (src) { + if (src) { + var cache_2 = ensureCache(); + var ent = cache_2[src] || (cache_2[src] = { + pop: 0, + last: 0, + sortorder: -10, + }); + ent.pop++; + ent.last = Date.now(); + sortCache(); + saveCache(); + } + return oldLoad(src); +}; From a628124ac38a6d3c926af26585cb7c0fdb51f705 Mon Sep 17 00:00:00 2001 From: Rob Pilling Date: Wed, 3 May 2023 08:03:49 +0100 Subject: [PATCH 07/13] popcon: icons --- apps/popconlaunch/app.png | Bin 899 -> 1455 bytes apps/popconlaunch/icon.js | 1 + apps/popconlaunch/metadata.json | 5 +++-- 3 files changed, 4 insertions(+), 2 deletions(-) create mode 100644 apps/popconlaunch/icon.js diff --git a/apps/popconlaunch/app.png b/apps/popconlaunch/app.png index 8b4e6caa2fe4720a32f492bd00c6e68751fabfbc..d7cd295e417d88e4ec8b229f65acec47fe418b2b 100644 GIT binary patch delta 1437 zcmV;O1!DSx2d@i|B!3BTNLh0L01FcU01FcV0GgZ_000GHNklsPZhvwTGIQ?S@8_Iz?>!qV zmW{D7*4KcE;zv}OZBQk4Se2M)LSpZzGIIoOS|75k_W`3%KAe!*zJ$yI!TP{&eLNwt z?W)YqCM3q2klEIC1*Xbuqlx%Mn;<-Ez3)QjWcxN%V!uOCRAn}ikXVl@Gv6j;MqUK~ zH;M7yH{k%q4}Yo>)47_cxxZPMzh;B)+*txrm_AEJM z*D|3!AIOE&AGwgcVTZ&RV+uh%6I#uaB7QRu<_Byr-+ym|biZvuWWNpf8)5LQT*`su za2^pNE-|%K0W!y)ZV?noQmG&)lBm?5c9{P?7gB?E4H}StPY%reg=a#QnQK%5RGC$E z8+9xb(H<+LZrHWjN0CSMkKV221WIKBpzRI3jKWj_urpjj5zU3vukOMD_Utmxn6t!CLVw)X z)BzNcYsQa7ayxOa`eh8@UiB;FV|SL&Mu^W+MNKpwjDLU^J*Wpy9@Ur_hzBnkJ$dke#)F>J7&U4LsECqS zB49uY3Q`L(jV;|>4@>w;*-qOnNZcoBva>V$=J%U-=IspF*nik8Yovub^cOBY?cfcO z1>;+VoWa;>Pk;!SG_WW*Es5gZ1-`R@4t>oKYJ)f#xYia)IV)#&xZ*BHYck+F1K9eED0&f|F;Lm{VL;rb?(c)W{8d&zrH4`v=Yc(ki11mCgAi%s^z;OwY%W1?mqSG(dy!Ck-w2QlAp02F%vFB!hc{ zjO>^-KB^P`s2PS92~z{}*ZpjA*ULELWH~mW6ZUJcNPn9+lc&+B)DUr{86zS8-#@X9 zjm^Ksf@So=!vkqok&B0(E$d7DqL7jSui~S=^eA4%XSJTBN&3Q3n7fISd#>Yfkq`Y7oUZo6O*XSC0PE4CjRbxsB z891`_Jb$$t4gm0VtaDCZ#oC>ecxnOIv}QXk9hWVZXpWG9D(~*;=TgsBg26ZB$NCv_ z6jS1v>8o5s3^(92ez~L?D*iyLPXgC^Ss0rh= zETODzL{X|K%Bv>Fxr&yKOBoXz4vqjFlzKwtw{idZM95W(Tbb#rO0Wkqaj6$F z@MWZp>h=2o=nr;LTzCQiH!$$4i=w<5W87%Fs6NXOGI0OH6?#VB0k9%#HO2Wg(|z6F zTV?6^_5{%B3_Q((i7K)Fmx`$;#+_0- Date: Wed, 3 May 2023 08:08:58 +0100 Subject: [PATCH 08/13] popcon: update README & metadata --- apps/popconlaunch/README.md | 7 +------ apps/popconlaunch/metadata.json | 2 +- 2 files changed, 2 insertions(+), 7 deletions(-) diff --git a/apps/popconlaunch/README.md b/apps/popconlaunch/README.md index 7804ebd73..416151751 100644 --- a/apps/popconlaunch/README.md +++ b/apps/popconlaunch/README.md @@ -1,9 +1,4 @@ Popcon ====== -Display apps sorted by regular use. - -Settings --------- - -- `Show Clocks` - Whether clocks are shown in the app list. Default `Yes`. +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. diff --git a/apps/popconlaunch/metadata.json b/apps/popconlaunch/metadata.json index 5963cc44c..4088f8dc0 100644 --- a/apps/popconlaunch/metadata.json +++ b/apps/popconlaunch/metadata.json @@ -3,7 +3,7 @@ "name": "Popcon Launcher", "shortName": "Popcon", "version": "0.01", - "description": "Launcher displaying your favourite (popular) apps first", + "description": "Launcher modification - your launchers will display your favourite (popular) apps first", "readme": "README.md", "icon": "app.png", "type": "bootloader", From 09c3445ac3d5608db4d2d8b39e0c5cf2aac5f370 Mon Sep 17 00:00:00 2001 From: Rob Pilling Date: Wed, 3 May 2023 22:14:41 +0100 Subject: [PATCH 09/13] popcon: make orderChanged local --- apps/popconlaunch/boot.ts | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/apps/popconlaunch/boot.ts b/apps/popconlaunch/boot.ts index 8b2e9fe18..ef522b9f1 100644 --- a/apps/popconlaunch/boot.ts +++ b/apps/popconlaunch/boot.ts @@ -10,7 +10,6 @@ let cache: undefined | { last: Timestamp, } }; -let orderchanged = false; const ensureCache = (): NonNull => { if(!cache){ @@ -21,7 +20,7 @@ const ensureCache = (): NonNull => { return cache; }; -const saveCache = () => { +const saveCache = (orderChanged: boolean) => { require("Storage").writeJSON("popcon.cache.json", cache); if(orderchanged){ // wipe launcher cache @@ -58,10 +57,12 @@ const sortCache = () => { }); let i = 0; + let orderChanged = false; for(const ent of ents){ - if(ent.sortorder !== i) orderchanged = true; + if(ent.sortorder !== i) orderChanged = true; ent.sortorder = i++; } + return orderChanged; }; require("Storage").readJSON = ((fname, skipExceptions) => { @@ -92,8 +93,8 @@ global.load = (src: string) => { }; ent.pop++; ent.last = Date.now(); - sortCache(); - saveCache(); + const orderChanged = sortCache(); + saveCache(orderChanged); } return oldLoad(src); From eb0af982c5f7006eba1f190f40d311f520c3aef1 Mon Sep 17 00:00:00 2001 From: Rob Pilling Date: Wed, 3 May 2023 22:14:54 +0100 Subject: [PATCH 10/13] popcon: use own .info file to bust launcher cache --- apps/popconlaunch/boot.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/apps/popconlaunch/boot.ts b/apps/popconlaunch/boot.ts index ef522b9f1..10ea38b61 100644 --- a/apps/popconlaunch/boot.ts +++ b/apps/popconlaunch/boot.ts @@ -22,11 +22,11 @@ const ensureCache = (): NonNull => { const saveCache = (orderChanged: boolean) => { require("Storage").writeJSON("popcon.cache.json", cache); - if(orderchanged){ - // wipe launcher cache - require("Storage") - .list(/launch.*cache/) - .forEach(f => require("Storage").erase(f)) + if(orderChanged){ + // ensure launchers reload their caches: + const info: AppInfo & { cacheBuster?: boolean } = oldRead("popcon.info", true); + info.cacheBuster = !info.cacheBuster; + require("Storage").writeJSON("popcon.info", info); } }; From 988945f0dcd6845807875606c8f1565987e9d3b1 Mon Sep 17 00:00:00 2001 From: Rob Pilling Date: Wed, 3 May 2023 22:16:29 +0100 Subject: [PATCH 11/13] popcon: regenerate JS --- apps/popconlaunch/boot.js | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/apps/popconlaunch/boot.js b/apps/popconlaunch/boot.js index 613cb3474..d7ba2e525 100644 --- a/apps/popconlaunch/boot.js +++ b/apps/popconlaunch/boot.js @@ -1,7 +1,6 @@ var oldRead = require("Storage").readJSON; var monthAgo = Date.now() - 1000 * 86400 * 28; var cache; -var orderchanged = false; var ensureCache = function () { if (!cache) { cache = oldRead("popcon.cache.json", true); @@ -10,12 +9,12 @@ var ensureCache = function () { } return cache; }; -var saveCache = function () { +var saveCache = function (orderChanged) { require("Storage").writeJSON("popcon.cache.json", cache); - if (orderchanged) { - require("Storage") - .list(/launch.*cache/) - .forEach(function (f) { return require("Storage").erase(f); }); + if (orderChanged) { + var info = oldRead("popcon.info", true); + info.cacheBuster = !info.cacheBuster; + require("Storage").writeJSON("popcon.info", info); } }; var sortCache = function () { @@ -40,12 +39,14 @@ var sortCache = function () { return 0; }); var i = 0; + var orderChanged = false; for (var _i = 0, ents_1 = ents; _i < ents_1.length; _i++) { var ent = ents_1[_i]; if (ent.sortorder !== i) - orderchanged = true; + orderChanged = true; ent.sortorder = i++; } + return orderChanged; }; require("Storage").readJSON = (function (fname, skipExceptions) { var _a; @@ -71,8 +72,8 @@ global.load = function (src) { }); ent.pop++; ent.last = Date.now(); - sortCache(); - saveCache(); + var orderChanged = sortCache(); + saveCache(orderChanged); } return oldLoad(src); }; From 945c7ca70a3f70c151d182ddd6a0c83e675746ea Mon Sep 17 00:00:00 2001 From: Rob Pilling Date: Thu, 4 May 2023 22:53:44 +0100 Subject: [PATCH 12/13] popcon: note about potential app slowdown --- apps/popconlaunch/README.md | 4 ++++ apps/popconlaunch/metadata.json | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/apps/popconlaunch/README.md b/apps/popconlaunch/README.md index 416151751..2814082a7 100644 --- a/apps/popconlaunch/README.md +++ b/apps/popconlaunch/README.md @@ -2,3 +2,7 @@ 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. + +[`Storage.readJSON`]: https://www.espruino.com/ReferenceBANGLEJS2#l_Storage_readJSON diff --git a/apps/popconlaunch/metadata.json b/apps/popconlaunch/metadata.json index 4088f8dc0..0ccc54d9e 100644 --- a/apps/popconlaunch/metadata.json +++ b/apps/popconlaunch/metadata.json @@ -3,7 +3,7 @@ "name": "Popcon Launcher", "shortName": "Popcon", "version": "0.01", - "description": "Launcher modification - your launchers will display your favourite (popular) apps first", + "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", "type": "bootloader", From 623b11b5187393e06d9477ddb5e2a387c931cc6f Mon Sep 17 00:00:00 2001 From: Rob Pilling Date: Thu, 4 May 2023 22:58:00 +0100 Subject: [PATCH 13/13] popcon: narrow variable scope, don't pollute global namespace --- apps/popconlaunch/boot.js | 158 +++++++++++++++++++------------------- apps/popconlaunch/boot.ts | 3 +- 2 files changed, 82 insertions(+), 79 deletions(-) diff --git a/apps/popconlaunch/boot.js b/apps/popconlaunch/boot.js index d7ba2e525..eb3f18e1b 100644 --- a/apps/popconlaunch/boot.js +++ b/apps/popconlaunch/boot.js @@ -1,79 +1,81 @@ -var oldRead = require("Storage").readJSON; -var monthAgo = Date.now() - 1000 * 86400 * 28; -var cache; -var ensureCache = function () { - if (!cache) { - cache = oldRead("popcon.cache.json", true); - if (!cache) - cache = {}; - } - return cache; -}; -var saveCache = function (orderChanged) { - require("Storage").writeJSON("popcon.cache.json", cache); - if (orderChanged) { - var info = oldRead("popcon.info", true); - info.cacheBuster = !info.cacheBuster; - require("Storage").writeJSON("popcon.info", info); - } -}; -var sortCache = function () { - var ents = Object.values(cache); - ents.sort(function (a, b) { - var n; - var am = (a.last > monthAgo); - var bm = (b.last > monthAgo); - n = bm - am; - if (n) - return n; - n = b.pop - a.pop; - if (n) - return n; - n = b.last - a.last; - if (n) - return n; - if (a.name < b.name) - return -1; - if (a.name > b.name) - return 1; - return 0; - }); - var i = 0; - var orderChanged = false; - for (var _i = 0, ents_1 = ents; _i < ents_1.length; _i++) { - var ent = ents_1[_i]; - if (ent.sortorder !== i) - orderChanged = true; - ent.sortorder = i++; - } - return orderChanged; -}; -require("Storage").readJSON = (function (fname, skipExceptions) { - var _a; - var j = oldRead(fname, skipExceptions); - if (/\.info$/.test(fname)) { - var cache_1 = ensureCache(); - var so = void 0; - if (j.src && (so = (_a = cache_1[j.src]) === null || _a === void 0 ? void 0 : _a.sortorder) != null) - j.sortorder = so; - else - j.sortorder = 99; - } - return j; -}); -var oldLoad = load; -global.load = function (src) { - if (src) { - var cache_2 = ensureCache(); - var ent = cache_2[src] || (cache_2[src] = { - pop: 0, - last: 0, - sortorder: -10, +{ + var oldRead_1 = require("Storage").readJSON; + var monthAgo_1 = Date.now() - 1000 * 86400 * 28; + var cache_1; + var ensureCache_1 = function () { + if (!cache_1) { + cache_1 = oldRead_1("popcon.cache.json", true); + if (!cache_1) + cache_1 = {}; + } + return cache_1; + }; + var saveCache_1 = function (orderChanged) { + require("Storage").writeJSON("popcon.cache.json", cache_1); + if (orderChanged) { + var info = oldRead_1("popcon.info", true); + info.cacheBuster = !info.cacheBuster; + require("Storage").writeJSON("popcon.info", info); + } + }; + var sortCache_1 = function () { + var ents = Object.values(cache_1); + ents.sort(function (a, b) { + var n; + var am = (a.last > monthAgo_1); + var bm = (b.last > monthAgo_1); + n = bm - am; + if (n) + return n; + n = b.pop - a.pop; + if (n) + return n; + n = b.last - a.last; + if (n) + return n; + if (a.name < b.name) + return -1; + if (a.name > b.name) + return 1; + return 0; }); - ent.pop++; - ent.last = Date.now(); - var orderChanged = sortCache(); - saveCache(orderChanged); - } - return oldLoad(src); -}; + var i = 0; + var orderChanged = false; + for (var _i = 0, ents_1 = ents; _i < ents_1.length; _i++) { + var ent = ents_1[_i]; + if (ent.sortorder !== i) + orderChanged = true; + ent.sortorder = i++; + } + return orderChanged; + }; + require("Storage").readJSON = (function (fname, skipExceptions) { + var _a; + var j = oldRead_1(fname, skipExceptions); + if (/\.info$/.test(fname)) { + var cache_2 = ensureCache_1(); + var so = void 0; + if (j.src && (so = (_a = cache_2[j.src]) === null || _a === void 0 ? void 0 : _a.sortorder) != null) + j.sortorder = so; + else + j.sortorder = 99; + } + return j; + }); + var oldLoad_1 = load; + global.load = function (src) { + if (src) { + var cache_3 = ensureCache_1(); + var ent = cache_3[src] || (cache_3[src] = { + pop: 0, + last: 0, + sortorder: -10, + }); + ent.pop++; + ent.last = Date.now(); + var orderChanged = sortCache_1(); + saveCache_1(orderChanged); + } + return oldLoad_1(src); + }; +} diff --git a/apps/popconlaunch/boot.ts b/apps/popconlaunch/boot.ts index 10ea38b61..a7ac73518 100644 --- a/apps/popconlaunch/boot.ts +++ b/apps/popconlaunch/boot.ts @@ -1,4 +1,4 @@ -// TODO: fastload +{ type Timestamp = number; const oldRead = require("Storage").readJSON; @@ -99,3 +99,4 @@ global.load = (src: string) => { return oldLoad(src); }; +}