From 5f18424eed9fbd818b3e2b61f08ead77c8b9ce7e Mon Sep 17 00:00:00 2001 From: Miodec Date: Sat, 26 Feb 2022 23:26:18 +0100 Subject: [PATCH] removed some any types, made config event more strict --- frontend/src/scripts/config.ts | 10 ++++-- .../scripts/controllers/sound-controller.ts | 2 +- .../scripts/controllers/theme-controller.ts | 2 +- .../src/scripts/elements/commandline-lists.ts | 6 ++-- .../src/scripts/observables/config-event.ts | 33 ++++++++++++++++--- frontend/src/scripts/test/test-config.ts | 7 ++-- 6 files changed, 46 insertions(+), 14 deletions(-) diff --git a/frontend/src/scripts/config.ts b/frontend/src/scripts/config.ts index 8c2878477..b351b5f86 100644 --- a/frontend/src/scripts/config.ts +++ b/frontend/src/scripts/config.ts @@ -212,7 +212,7 @@ export function setMode(mode: MonkeyTypes.Mode, nosave?: boolean): boolean { } } if (!nosave) saveToLocalStorage(); - ConfigEvent.dispatch("mode", previous, config.mode); + ConfigEvent.dispatch("mode", config.mode, nosave, previous); return true; } @@ -1894,7 +1894,13 @@ export function apply(configObj: MonkeyTypes.Config | null | "null"): void { $("#ad_about2").remove(); } - ConfigEvent.dispatch("configApplied", config); + ConfigEvent.dispatch( + "configApplied", + undefined, + undefined, + undefined, + config + ); } } diff --git a/frontend/src/scripts/controllers/sound-controller.ts b/frontend/src/scripts/controllers/sound-controller.ts index 028319a8c..b11f5d857 100644 --- a/frontend/src/scripts/controllers/sound-controller.ts +++ b/frontend/src/scripts/controllers/sound-controller.ts @@ -263,5 +263,5 @@ export function setVolume(val: string): void { ConfigEvent.subscribe((eventKey, eventValue) => { if (eventKey === "playSoundOnClick" && eventValue !== "off") init(); - if (eventKey === "soundVolume") setVolume(eventValue); + if (eventKey === "soundVolume") setVolume(eventValue as string); }); diff --git a/frontend/src/scripts/controllers/theme-controller.ts b/frontend/src/scripts/controllers/theme-controller.ts index e0898f799..9eb7c451c 100644 --- a/frontend/src/scripts/controllers/theme-controller.ts +++ b/frontend/src/scripts/controllers/theme-controller.ts @@ -240,7 +240,7 @@ ConfigEvent.subscribe((eventKey, eventValue, nosave) => { eventValue ? set("custom") : set(Config.theme); if (eventKey === "theme") { clearPreview(); - set(eventValue); + set(eventValue as string); } if (eventKey === "setThemes") { clearPreview(); diff --git a/frontend/src/scripts/elements/commandline-lists.ts b/frontend/src/scripts/elements/commandline-lists.ts index 860c8e2de..a43ee24c4 100644 --- a/frontend/src/scripts/elements/commandline-lists.ts +++ b/frontend/src/scripts/elements/commandline-lists.ts @@ -3215,16 +3215,16 @@ ConfigEvent.subscribe((eventKey, eventValue) => { if (eventKey === "saveToLocalStorage") { defaultCommands.list.filter( (command) => command.id == "exportSettingsJSON" - )[0].defaultValue = eventValue; + )[0].defaultValue = eventValue as string; } if (eventKey === "customBackground") { defaultCommands.list.filter( (command) => command.id == "changeCustomBackground" - )[0].defaultValue = eventValue; + )[0].defaultValue = eventValue as string; } if (eventKey === "customLayoutFluid") { defaultCommands.list.filter( (command) => command.id == "changeCustomLayoutfluid" - )[0].defaultValue = eventValue?.replace(/#/g, " "); + )[0].defaultValue = (eventValue as string)?.replace(/#/g, " "); } }); diff --git a/frontend/src/scripts/observables/config-event.ts b/frontend/src/scripts/observables/config-event.ts index 424e8d9a8..8b3d0131f 100644 --- a/frontend/src/scripts/observables/config-event.ts +++ b/frontend/src/scripts/observables/config-event.ts @@ -1,15 +1,38 @@ -type SubscribeFunction = (key: string, value?: V, value2?: V2) => void; +type ConfigValues = + | string + | number + | boolean + | string[] + | MonkeyTypes.QuoteLengthArray + | MonkeyTypes.ResultFilters + | MonkeyTypes.CustomBackgroundFilter + | null + | undefined; -const subscribers: SubscribeFunction[] = []; +type SubscribeFunction = ( + key: string, + newValue?: ConfigValues, + nosave?: boolean, + previousValue?: ConfigValues, + fullConfig?: MonkeyTypes.Config +) => void; -export function subscribe(fn: SubscribeFunction): void { +const subscribers: SubscribeFunction[] = []; + +export function subscribe(fn: SubscribeFunction): void { subscribers.push(fn); } -export function dispatch(key: string, value?: V, value2?: V2): void { +export function dispatch( + key: string, + newValue?: ConfigValues, + nosave?: boolean, + previousValue?: ConfigValues, + fullConfig?: MonkeyTypes.Config +): void { subscribers.forEach((fn) => { try { - fn(key, value, value2); + fn(key, newValue, nosave, previousValue, fullConfig); } catch (e) { console.error("Config event subscriber threw an error"); console.error(e); diff --git a/frontend/src/scripts/test/test-config.ts b/frontend/src/scripts/test/test-config.ts index fa23189f7..6aa5a3851 100644 --- a/frontend/src/scripts/test/test-config.ts +++ b/frontend/src/scripts/test/test-config.ts @@ -159,7 +159,10 @@ export function update( ); } -ConfigEvent.subscribe((eventKey, eventValue, eventValue2) => { +ConfigEvent.subscribe((eventKey, eventValue, _nosave, eventPreviousValue) => { if (eventKey === "mode") - update(eventValue as MonkeyTypes.Mode, eventValue2 as MonkeyTypes.Mode); + update( + eventValue as MonkeyTypes.Mode, + eventPreviousValue as MonkeyTypes.Mode + ); });