From c24cfc041e03c745353264dbca1785fc5c9972fe Mon Sep 17 00:00:00 2001 From: Damien Elmes Date: Sat, 17 Apr 2021 21:44:33 +1000 Subject: [PATCH] update config automatically, and update parent limits at same time --- ts/deckconfig/lib.test.ts | 7 ++++--- ts/deckconfig/lib.ts | 26 +++++++++++++------------- 2 files changed, 17 insertions(+), 16 deletions(-) diff --git a/ts/deckconfig/lib.test.ts b/ts/deckconfig/lib.test.ts index e47673229..7dd4c747a 100644 --- a/ts/deckconfig/lib.test.ts +++ b/ts/deckconfig/lib.test.ts @@ -1,6 +1,10 @@ // Copyright: Ankitects Pty Ltd and contributors // License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html +/* eslint +@typescript-eslint/no-explicit-any: "off", + */ + import * as pb from "anki/backend_proto"; import { DeckConfigState } from "./lib"; import { get } from "svelte/store"; @@ -227,8 +231,5 @@ test("parent counts", () => { c.newPerDay = 123; return c; }); - // fixme: we should do this automatically when currentConfig is updated, or the - // parent count will become stale - state.saveCurrentConfig(); expect(get(state.parentLimits)).toStrictEqual({ newCards: 123, reviews: 200 }); }); diff --git a/ts/deckconfig/lib.ts b/ts/deckconfig/lib.ts index d6717e20a..93b49132d 100644 --- a/ts/deckconfig/lib.ts +++ b/ts/deckconfig/lib.ts @@ -80,30 +80,22 @@ export class DeckConfigState { return; }); - // create a temporary subscription to force our setters to be set, so unit - // tests don't get stale results + // create a temporary subscription to force our setters to be set immediately, + // so unit tests don't get stale results get(this.configList); get(this.parentLimits); + + // update our state when the current config is changed + this.currentConfig.subscribe((val) => this.onCurrentConfigChanged(val)); } setCurrentIndex(index: number): void { - this.saveCurrentConfig(); this.selectedIdx = index; this.updateCurrentConfig(); // use counts have changed this.updateConfigList(); } - /// Persist any changes made to the current config into the list of configs. - saveCurrentConfig(): void { - const config = get(this.currentConfig); - if (!isEqual(config, this.configs[this.selectedIdx].config.config)) { - this.configs[this.selectedIdx].config.config = config; - this.configs[this.selectedIdx].config.mtimeSecs = 0; - } - this.parentLimitsSetter?.(this.getParentLimits()); - } - getCurrentName(): string { return this.configs[this.selectedIdx].config.name; } @@ -150,6 +142,14 @@ export class DeckConfigState { this.updateConfigList(); } + private onCurrentConfigChanged(config: ConfigInner): void { + if (!isEqual(config, this.configs[this.selectedIdx].config.config)) { + this.configs[this.selectedIdx].config.config = config; + this.configs[this.selectedIdx].config.mtimeSecs = 0; + } + this.parentLimitsSetter?.(this.getParentLimits()); + } + private ensureNewNameUnique(name: string): string { const idx = this.configs.findIndex((e) => e.config.name === name); if (idx !== -1) {