diff --git a/frontend/src/ts/commandline/lists/presets.ts b/frontend/src/ts/commandline/lists/presets.ts index 2baf7b719..8205e6e6c 100644 --- a/frontend/src/ts/commandline/lists/presets.ts +++ b/frontend/src/ts/commandline/lists/presets.ts @@ -36,9 +36,9 @@ function update(): void { subgroup.list.push({ id: "applyPreset" + preset._id, display: dis, - exec: (): void => { + exec: async (): Promise => { Settings.setEventDisabled(true); - PresetController.apply(preset._id); + await PresetController.apply(preset._id); Settings.setEventDisabled(false); void Settings.update(); void ModesNotice.update(); diff --git a/frontend/src/ts/config.ts b/frontend/src/ts/config.ts index 0b5df4e93..a9eb760aa 100644 --- a/frontend/src/ts/config.ts +++ b/frontend/src/ts/config.ts @@ -1839,6 +1839,8 @@ export async function apply( ): Promise { if (configToApply === undefined) return; + ConfigEvent.dispatch("fullConfigChange"); + configToApply = replaceLegacyValues(configToApply); const configObj = configToApply as MonkeyTypes.Config; @@ -1948,10 +1950,10 @@ export async function apply( config ); } + ConfigEvent.dispatch("fullConfigChangeFinished"); } export async function reset(): Promise { - ConfigEvent.dispatch("fullConfigChange"); await apply(DefaultConfig); saveFullConfigToLocalStorage(); } diff --git a/frontend/src/ts/controllers/preset-controller.ts b/frontend/src/ts/controllers/preset-controller.ts index c4b1af50f..bdbacffbe 100644 --- a/frontend/src/ts/controllers/preset-controller.ts +++ b/frontend/src/ts/controllers/preset-controller.ts @@ -4,25 +4,28 @@ import * as Notifications from "../elements/notifications"; import * as TestLogic from "../test/test-logic"; import * as TagController from "./tag-controller"; -export function apply(_id: string): void { - // console.log(DB.getSnapshot().presets); +export async function apply(_id: string): Promise { const snapshot = DB.getSnapshot(); if (!snapshot) return; - snapshot.presets?.forEach(async (preset) => { - if (preset._id === _id) { - await UpdateConfig.apply(preset.config); - TagController.clear(true); - if (preset.config.tags) { - preset.config.tags.forEach((tagid) => { - TagController.set(tagid, true, false); - }); - TagController.saveActiveToLocalStorage(); - } - TestLogic.restart(); - Notifications.add("Preset applied", 1, { - duration: 2, - }); - UpdateConfig.saveFullConfigToLocalStorage(); + + const presetToApply = snapshot.presets?.find((preset) => preset._id === _id); + + if (presetToApply === undefined) { + Notifications.add("Preset not found", 0); + return; + } + + await UpdateConfig.apply(presetToApply.config); + TagController.clear(true); + if (presetToApply.config.tags) { + for (const tagId of presetToApply.config.tags) { + TagController.set(tagId, true, false); } + TagController.saveActiveToLocalStorage(); + } + TestLogic.restart(); + Notifications.add("Preset applied", 1, { + duration: 2, }); + UpdateConfig.saveFullConfigToLocalStorage(); } diff --git a/frontend/src/ts/pages/settings.ts b/frontend/src/ts/pages/settings.ts index a23a1a54b..3cfa1ccfe 100644 --- a/frontend/src/ts/pages/settings.ts +++ b/frontend/src/ts/pages/settings.ts @@ -1043,13 +1043,10 @@ $(".pageSettings .section.tags").on( $(".pageSettings .section.presets").on( "click", ".presetsList .preset .presetButton", - (e) => { + async (e) => { const target = e.currentTarget; const presetid = $(target).parent(".preset").attr("data-id") as string; - console.log("Applying Preset"); - configEventDisabled = true; - PresetController.apply(presetid); - configEventDisabled = false; + await PresetController.apply(presetid); void update(); } ); @@ -1217,11 +1214,12 @@ let configEventDisabled = false; export function setEventDisabled(value: boolean): void { configEventDisabled = value; } + ConfigEvent.subscribe((eventKey) => { if (eventKey === "fullConfigChange") setEventDisabled(true); - if (eventKey === "fullConfigChangeFinished") { - setEventDisabled(false); - } + if (eventKey === "fullConfigChangeFinished") setEventDisabled(false); + + //make sure the page doesnt update a billion times when applying a preset/config at once if (configEventDisabled || eventKey === "saveToLocalStorage") return; if (ActivePage.get() === "settings" && eventKey !== "theme") { void update();