mirror of
https://github.com/monkeytypegame/monkeytype.git
synced 2025-11-09 13:44:29 +08:00
fix: presets taking very long to apply when on the settings page
closes #5023
This commit is contained in:
parent
e7f021a37c
commit
67c10a9d41
4 changed files with 31 additions and 28 deletions
|
|
@ -36,9 +36,9 @@ function update(): void {
|
|||
subgroup.list.push({
|
||||
id: "applyPreset" + preset._id,
|
||||
display: dis,
|
||||
exec: (): void => {
|
||||
exec: async (): Promise<void> => {
|
||||
Settings.setEventDisabled(true);
|
||||
PresetController.apply(preset._id);
|
||||
await PresetController.apply(preset._id);
|
||||
Settings.setEventDisabled(false);
|
||||
void Settings.update();
|
||||
void ModesNotice.update();
|
||||
|
|
|
|||
|
|
@ -1839,6 +1839,8 @@ export async function apply(
|
|||
): Promise<void> {
|
||||
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<void> {
|
||||
ConfigEvent.dispatch("fullConfigChange");
|
||||
await apply(DefaultConfig);
|
||||
saveFullConfigToLocalStorage();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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<void> {
|
||||
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();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue