Skip to content

Commit 735740d

Browse files
Miodecfehmer
andauthored
refactor: change config event params to an options object (@Miodec) (monkeytypegame#7227)
Also makes the types stronger on the event. Also cleans up unused events. --------- Co-authored-by: Christian Fehmer <[email protected]>
1 parent 9a9b6d7 commit 735740d

31 files changed

+195
-229
lines changed

frontend/__tests__/root/config.spec.ts

Lines changed: 24 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -145,26 +145,26 @@ describe("Config", () => {
145145
Config.setConfig("confidenceMode", "max");
146146

147147
//THEN
148-
expect(dispatchConfigEventMock).not.toHaveBeenCalledWith(
149-
"freedomMode",
150-
false,
151-
true,
152-
true,
153-
);
148+
expect(dispatchConfigEventMock).not.toHaveBeenCalledWith({
149+
key: "freedomMode",
150+
newValue: false,
151+
nosave: true,
152+
previousValue: true,
153+
});
154154

155-
expect(dispatchConfigEventMock).toHaveBeenCalledWith(
156-
"stopOnError",
157-
"off",
158-
false,
159-
"letter",
160-
);
155+
expect(dispatchConfigEventMock).toHaveBeenCalledWith({
156+
key: "stopOnError",
157+
newValue: "off",
158+
nosave: false,
159+
previousValue: "letter",
160+
});
161161

162-
expect(dispatchConfigEventMock).toHaveBeenCalledWith(
163-
"confidenceMode",
164-
"max",
165-
false,
166-
"off",
167-
);
162+
expect(dispatchConfigEventMock).toHaveBeenCalledWith({
163+
key: "confidenceMode",
164+
newValue: "max",
165+
nosave: false,
166+
previousValue: "off",
167+
});
168168
});
169169

170170
it("saves to localstorage if nosave=false", async () => {
@@ -186,12 +186,6 @@ describe("Config", () => {
186186

187187
//hide loading
188188
expect(accountButtonLoadingMock).toHaveBeenNthCalledWith(2, false);
189-
190-
//send event
191-
expect(dispatchConfigEventMock).toHaveBeenCalledWith(
192-
"saveToLocalStorage",
193-
expect.stringContaining("numbers"),
194-
);
195189
});
196190

197191
it("saves configOverride values to localstorage if nosave=false", async () => {
@@ -210,16 +204,6 @@ describe("Config", () => {
210204
minWpmCustomSpeed: 120,
211205
minWpm: "custom",
212206
});
213-
214-
//send event
215-
expect(dispatchConfigEventMock).toHaveBeenCalledWith(
216-
"saveToLocalStorage",
217-
expect.stringContaining("minWpmCustomSpeed"),
218-
);
219-
expect(dispatchConfigEventMock).toHaveBeenCalledWith(
220-
"saveToLocalStorage",
221-
expect.stringContaining("minWpm"),
222-
);
223207
});
224208

225209
it("does not save to localstorage if nosave=true", async () => {
@@ -236,11 +220,6 @@ describe("Config", () => {
236220

237221
expect(accountButtonLoadingMock).not.toHaveBeenCalled();
238222
expect(dbSaveConfigMock).not.toHaveBeenCalled();
239-
240-
expect(dispatchConfigEventMock).not.toHaveBeenCalledWith(
241-
"saveToLocalStorage",
242-
expect.any(String),
243-
);
244223
});
245224

246225
it("dispatches event on set", () => {
@@ -252,12 +231,12 @@ describe("Config", () => {
252231

253232
//THEN
254233

255-
expect(dispatchConfigEventMock).toHaveBeenCalledWith(
256-
"numbers",
257-
true,
258-
true,
259-
false,
260-
);
234+
expect(dispatchConfigEventMock).toHaveBeenCalledWith({
235+
key: "numbers",
236+
newValue: true,
237+
nosave: true,
238+
previousValue: false,
239+
});
261240
});
262241

263242
it("triggers resize if property is set", () => {

frontend/src/ts/commandline/lists/themes.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ export function update(themes: Theme[]): void {
7676
}
7777

7878
// subscribe to theme-related config events to update the theme command list
79-
ConfigEvent.subscribe((eventKey, _eventValue) => {
80-
if (eventKey === "favThemes") {
79+
ConfigEvent.subscribe(({ key }) => {
80+
if (key === "favThemes") {
8181
// update themes list when favorites change
8282
try {
8383
update(ThemesList);

frontend/src/ts/config.ts

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,6 @@ function saveToLocalStorage(
6666
configToSend[key] = config[key];
6767
saveToDatabase();
6868
}
69-
const localToSaveStringified = JSON.stringify(config);
70-
ConfigEvent.dispatch("saveToLocalStorage", localToSaveStringified);
7169
}
7270

7371
export function saveFullConfigToLocalStorage(noDbCheck = false): void {
@@ -78,8 +76,6 @@ export function saveFullConfigToLocalStorage(noDbCheck = false): void {
7876
void DB.saveConfig(config);
7977
AccountButton.loading(false);
8078
}
81-
const stringified = JSON.stringify(config);
82-
ConfigEvent.dispatch("saveToLocalStorage", stringified);
8379
}
8480

8581
function isConfigChangeBlocked(): boolean {
@@ -92,9 +88,9 @@ function isConfigChangeBlocked(): boolean {
9288
return false;
9389
}
9490

95-
export function setConfig<T extends keyof ConfigSchemas.Config>(
91+
export function setConfig<T extends keyof Config>(
9692
key: T,
97-
value: ConfigSchemas.Config[T],
93+
value: Config[T],
9894
nosave: boolean = false,
9995
): boolean {
10096
const metadata = configMetadata[key] as ConfigMetadataObject[T];
@@ -183,7 +179,14 @@ export function setConfig<T extends keyof ConfigSchemas.Config>(
183179

184180
config[key] = value;
185181
if (!nosave) saveToLocalStorage(key, nosave);
186-
ConfigEvent.dispatch(key, value, nosave, previousValue);
182+
183+
// @ts-expect-error i can't figure this out
184+
ConfigEvent.dispatch({
185+
key: key,
186+
newValue: value,
187+
nosave,
188+
previousValue: previousValue as Config[T],
189+
});
187190

188191
if (metadata.triggerResize && !nosave) {
189192
triggerResize();
@@ -201,6 +204,8 @@ export function toggleFunbox(funbox: FunboxName, nosave?: boolean): boolean {
201204
return false;
202205
}
203206

207+
const previousValue = config.funbox;
208+
204209
let newConfig: FunboxName[] = config.funbox;
205210

206211
if (newConfig.includes(funbox)) {
@@ -216,7 +221,12 @@ export function toggleFunbox(funbox: FunboxName, nosave?: boolean): boolean {
216221

217222
config.funbox = newConfig;
218223
saveToLocalStorage("funbox", nosave);
219-
ConfigEvent.dispatch("funbox", config.funbox);
224+
ConfigEvent.dispatch({
225+
key: "funbox",
226+
newValue: config.funbox,
227+
nosave,
228+
previousValue,
229+
});
220230

221231
return true;
222232
}
@@ -248,7 +258,7 @@ export async function applyConfig(
248258
//migrate old values if needed, remove additional keys and merge with default config
249259
const fullConfig: Config = migrateConfig(partialConfig);
250260

251-
ConfigEvent.dispatch("fullConfigChange");
261+
ConfigEvent.dispatch({ key: "fullConfigChange" });
252262

253263
const defaultConfig = getDefaultConfig();
254264
for (const key of typedKeys(fullConfig)) {
@@ -276,14 +286,7 @@ export async function applyConfig(
276286
saveToLocalStorage(key);
277287
}
278288

279-
ConfigEvent.dispatch(
280-
"configApplied",
281-
undefined,
282-
undefined,
283-
undefined,
284-
config,
285-
);
286-
ConfigEvent.dispatch("fullConfigChangeFinished");
289+
ConfigEvent.dispatch({ key: "fullConfigChangeFinished" });
287290
}
288291

289292
export async function resetConfig(): Promise<void> {

frontend/src/ts/controllers/ad-controller.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -300,14 +300,14 @@ window.addEventListener("resize", () => {
300300
debouncedBreakpoint2Update();
301301
});
302302

303-
ConfigEvent.subscribe((event, value) => {
304-
if (event === "ads") {
305-
if (value === "off") {
303+
ConfigEvent.subscribe(({ key, newValue }) => {
304+
if (key === "ads") {
305+
if (newValue === "off") {
306306
removeAll();
307-
} else if (value === "result") {
307+
} else if (newValue === "result") {
308308
removeSellout();
309309
removeOn();
310-
} else if (value === "on") {
310+
} else if (newValue === "on") {
311311
removeSellout();
312312
}
313313
}

frontend/src/ts/controllers/challenge-controller.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ export async function setup(challengeName: string): Promise<boolean> {
337337
}
338338
}
339339

340-
ConfigEvent.subscribe((eventKey) => {
340+
ConfigEvent.subscribe(({ key }) => {
341341
if (
342342
[
343343
"difficulty",
@@ -354,7 +354,7 @@ ConfigEvent.subscribe((eventKey) => {
354354
"keymapMode",
355355
"keymapLayout",
356356
"layout",
357-
].includes(eventKey)
357+
].includes(key)
358358
) {
359359
clearActive();
360360
}

frontend/src/ts/controllers/chart-controller.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1432,12 +1432,12 @@ export function updateAllChartColors(): void {
14321432
void miniResult.updateColors();
14331433
}
14341434

1435-
ConfigEvent.subscribe((eventKey, eventValue) => {
1436-
if (eventKey === "accountChart" && ActivePage.get() === "account") {
1435+
ConfigEvent.subscribe(({ key, newValue }) => {
1436+
if (key === "accountChart" && ActivePage.get() === "account") {
14371437
updateResults();
14381438
updateAccuracy();
14391439
updateAverage10();
14401440
updateAverage100();
14411441
}
1442-
if (eventKey === "fontFamily") setDefaultFontFamily(eventValue as string);
1442+
if (key === "fontFamily") setDefaultFontFamily(newValue);
14431443
});

frontend/src/ts/controllers/quotes-controller.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ class QuotesController {
251251

252252
const quoteController = new QuotesController();
253253

254-
subscribe((key, newValue) => {
254+
subscribe(({ key, newValue }) => {
255255
if (key === "quoteLength") {
256256
quoteController.updateQuoteQueue(newValue as number[]);
257257
}

frontend/src/ts/controllers/sound-controller.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -736,9 +736,9 @@ function setVolume(val: number): void {
736736
}
737737
}
738738

739-
ConfigEvent.subscribe((eventKey, eventValue) => {
740-
if (eventKey === "playSoundOnClick" && eventValue !== "off") void init();
741-
if (eventKey === "soundVolume") {
742-
setVolume(parseFloat(eventValue as string));
739+
ConfigEvent.subscribe(({ key, newValue }) => {
740+
if (key === "playSoundOnClick" && newValue !== "off") void init();
741+
if (key === "soundVolume") {
742+
setVolume(newValue);
743743
}
744744
});

frontend/src/ts/controllers/theme-controller.ts

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -477,11 +477,11 @@ window
477477

478478
let ignoreConfigEvent = false;
479479

480-
ConfigEvent.subscribe(async (eventKey, eventValue, nosave) => {
481-
if (eventKey === "fullConfigChange") {
480+
ConfigEvent.subscribe(async ({ key, newValue, nosave }) => {
481+
if (key === "fullConfigChange") {
482482
ignoreConfigEvent = true;
483483
}
484-
if (eventKey === "fullConfigChangeFinished") {
484+
if (key === "fullConfigChangeFinished") {
485485
ignoreConfigEvent = false;
486486

487487
await clearRandom();
@@ -506,26 +506,26 @@ ConfigEvent.subscribe(async (eventKey, eventValue, nosave) => {
506506
// once the full config is loaded, we can apply everything once
507507
if (ignoreConfigEvent) return;
508508

509-
if (eventKey === "randomTheme") {
509+
if (key === "randomTheme") {
510510
void changeThemeList();
511511
}
512-
if (eventKey === "customTheme") {
513-
(eventValue as boolean) ? await set("custom") : await set(Config.theme);
512+
if (key === "customTheme") {
513+
newValue ? await set("custom") : await set(Config.theme);
514514
}
515-
if (eventKey === "customThemeColors") {
515+
if (key === "customThemeColors") {
516516
nosave ? preview("custom") : await set("custom");
517517
}
518-
if (eventKey === "theme") {
518+
if (key === "theme") {
519519
await clearRandom();
520520
await clearPreview(false);
521-
await set(eventValue as string);
521+
await set(newValue as string);
522522
}
523-
if (eventKey === "randomTheme" && eventValue === "off") await clearRandom();
524-
if (eventKey === "customBackground") await applyCustomBackground();
523+
if (key === "randomTheme" && newValue === "off") await clearRandom();
524+
if (key === "customBackground") await applyCustomBackground();
525525

526-
if (eventKey === "customBackgroundSize") applyCustomBackgroundSize();
527-
if (eventKey === "autoSwitchTheme") {
528-
if (eventValue as boolean) {
526+
if (key === "customBackgroundSize") applyCustomBackgroundSize();
527+
if (key === "autoSwitchTheme") {
528+
if (newValue) {
529529
if (prefersColorSchemeDark()) {
530530
await set(Config.themeDark, true);
531531
} else {
@@ -536,15 +536,15 @@ ConfigEvent.subscribe(async (eventKey, eventValue, nosave) => {
536536
}
537537
}
538538
if (
539-
eventKey === "themeLight" &&
539+
key === "themeLight" &&
540540
Config.autoSwitchTheme &&
541541
!prefersColorSchemeDark() &&
542542
!nosave
543543
) {
544544
await set(Config.themeLight, true);
545545
}
546546
if (
547-
eventKey === "themeDark" &&
547+
key === "themeDark" &&
548548
Config.autoSwitchTheme &&
549549
window.matchMedia !== undefined &&
550550
window.matchMedia("(prefers-color-scheme: dark)").matches &&
@@ -559,7 +559,7 @@ ConfigEvent.subscribe(async (eventKey, eventValue, nosave) => {
559559
"customThemeColors",
560560
"randomTheme",
561561
"favThemes",
562-
].includes(eventKey)
562+
].includes(key)
563563
) {
564564
updateFooterIndicator();
565565
}

frontend/src/ts/elements/custom-background-filter.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,9 @@ const debouncedSave = debounce(2000, async () => {
133133
setConfig("customBackgroundFilter", arr, false);
134134
});
135135

136-
ConfigEvent.subscribe((eventKey, eventValue) => {
137-
if (eventKey === "customBackgroundFilter" && (eventValue as boolean)) {
138-
loadConfig(eventValue as CustomBackgroundFilter);
136+
ConfigEvent.subscribe(({ key, newValue }) => {
137+
if (key === "customBackgroundFilter") {
138+
loadConfig(newValue);
139139
apply();
140140
}
141141
});

0 commit comments

Comments
 (0)