update config automatically, and update parent limits at same time

This commit is contained in:
Damien Elmes 2021-04-17 21:44:33 +10:00
parent 633e93904a
commit c24cfc041e
2 changed files with 17 additions and 16 deletions

View file

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

View file

@ -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) {