From e2422ffc9e0c79f029c217a26b310e11231580eb Mon Sep 17 00:00:00 2001 From: MathMan05 Date: Mon, 20 Jan 2025 18:07:52 -0600 Subject: [PATCH] use proxy to make code more reliable --- src/webpage/channel.ts | 4 ---- src/webpage/hover.ts | 3 +++ src/webpage/localuser.ts | 8 -------- src/webpage/utils/utils.ts | 23 ++++++++++++++++++++++- 4 files changed, 25 insertions(+), 13 deletions(-) diff --git a/src/webpage/channel.ts b/src/webpage/channel.ts index 24a6e7d..23a1f31 100644 --- a/src/webpage/channel.ts +++ b/src/webpage/channel.ts @@ -569,12 +569,10 @@ class Channel extends SnowFlake { if (childrendiv.style.height !== "0px") { decoration.classList.add("hiddencat"); this.perminfo.collapsed = true; - this.localuser.userinfo.updateLocal(); childrendiv.style.height = "0px"; } else { decoration.classList.remove("hiddencat"); this.perminfo.collapsed = false; - this.localuser.userinfo.updateLocal(); childrendiv.style.height = childrendiv.scrollHeight + "px"; } }; @@ -907,7 +905,6 @@ class Channel extends SnowFlake { const buttons = options.addOptions("", {ltr: true}); buttons.addButtonInput("", "Yes", () => { this.perminfo.nsfwOk = true; - this.localuser.userinfo.updateLocal(); this.getHTML(); }); buttons.addButtonInput("", "No", () => { @@ -946,7 +943,6 @@ class Channel extends SnowFlake { } this.guild.prevchannel = this; this.guild.perminfo.prevchannel = this.id; - this.localuser.userinfo.updateLocal(); this.localuser.channelfocus = this; if ( diff --git a/src/webpage/hover.ts b/src/webpage/hover.ts index 52562ee..b8ad4dc 100644 --- a/src/webpage/hover.ts +++ b/src/webpage/hover.ts @@ -12,6 +12,9 @@ class Hover { elm.addEventListener("mouseover", () => { timeOut = setTimeout(async () => { elm2 = await this.makeHover(elm); + elm2.addEventListener("mouseover", () => { + elm2.remove(); + }); }, 300); }); elm.addEventListener("mouseout", () => { diff --git a/src/webpage/localuser.ts b/src/webpage/localuser.ts index 5aa8289..633d9db 100644 --- a/src/webpage/localuser.ts +++ b/src/webpage/localuser.ts @@ -156,7 +156,6 @@ class Localuser { } this.pingEndpoint(); - this.userinfo.updateLocal(); } inrelation = new Set(); outoffocus(): void { @@ -358,7 +357,6 @@ class Localuser { this.info = newurls; this.serverurls = newurls; this.userinfo.json.serverurls = this.info; - this.userinfo.updateLocal(); break; } break; @@ -369,7 +367,6 @@ class Localuser { this.info = newurls; this.serverurls = newurls; this.userinfo.json.serverurls = this.info; - this.userinfo.updateLocal(); break; } break; @@ -382,7 +379,6 @@ class Localuser { this.info = newurls; this.serverurls = newurls; this.userinfo.json.serverurls = this.info; - this.userinfo.updateLocal(); } break; } @@ -1764,7 +1760,6 @@ class Localuser { this.botTokens.set(appId, updateJSON.token); if (this.perminfo.applications[appId]) { this.perminfo.applications[appId] = updateJSON.token; - this.userinfo.updateLocal(); } }); const text = form.addText( @@ -1776,7 +1771,6 @@ class Localuser { const check = form.addOptions("", {noSubmit: true}); if (!this.perminfo.applications) { this.perminfo.applications = {}; - this.userinfo.updateLocal(); } const checkbox = check.addCheckboxInput(I18n.getTranslation("localuser.saveToken"), () => {}, { initState: !!this.perminfo.applications[appId], @@ -1785,14 +1779,12 @@ class Localuser { if (_) { if (this.botTokens.has(appId)) { this.perminfo.applications[appId] = this.botTokens.get(appId); - this.userinfo.updateLocal(); } else { alert(I18n.getTranslation("localuser.noToken")); checkbox.setState(false); } } else { delete this.perminfo.applications[appId]; - this.userinfo.updateLocal(); } }); form.addButtonInput("", I18n.getTranslation("localuser.advancedBot"), () => { diff --git a/src/webpage/utils/utils.ts b/src/webpage/utils/utils.ts index 9563de3..3050661 100644 --- a/src/webpage/utils/utils.ts +++ b/src/webpage/utils/utils.ts @@ -107,8 +107,29 @@ export class Specialuser { this.json.localuserStore = e; this.updateLocal(); } + proxySave(e: Object) { + return new Proxy(e, { + set: (target, p, newValue, receiver) => { + const bool = Reflect.set(target, p, newValue, receiver); + this.updateLocal(); + return bool; + }, + get: (target, p, receiver) => { + const value = Reflect.get(target, p, receiver) as unknown; + if (value instanceof Object) { + return this.proxySave(value); + } + return value; + }, + }); + } get localuserStore() { - return this.json.localuserStore; + type jsonParse = { + [key: string | number]: any; + }; + return this.proxySave(this.json.localuserStore) as { + [key: string | number]: jsonParse; + }; } set id(e) { this.json.id = e;