removed some any types, made config event more strict

This commit is contained in:
Miodec 2022-02-26 23:26:18 +01:00
parent 8ef55423d7
commit 5f18424eed
6 changed files with 46 additions and 14 deletions

View file

@ -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
);
}
}

View file

@ -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);
});

View file

@ -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();

View file

@ -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, " ");
}
});

View file

@ -1,15 +1,38 @@
type SubscribeFunction<V, V2> = (key: string, value?: V, value2?: V2) => void;
type ConfigValues =
| string
| number
| boolean
| string[]
| MonkeyTypes.QuoteLengthArray
| MonkeyTypes.ResultFilters
| MonkeyTypes.CustomBackgroundFilter
| null
| undefined;
const subscribers: SubscribeFunction<any, any>[] = [];
type SubscribeFunction = (
key: string,
newValue?: ConfigValues,
nosave?: boolean,
previousValue?: ConfigValues,
fullConfig?: MonkeyTypes.Config
) => void;
export function subscribe(fn: SubscribeFunction<any, any>): void {
const subscribers: SubscribeFunction[] = [];
export function subscribe(fn: SubscribeFunction): void {
subscribers.push(fn);
}
export function dispatch<V, V2>(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);

View file

@ -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
);
});