Anki/ts/reviewer/answering.ts
RumovZ 31b7464c67
Add card meta for persisting custom scheduling state (#2040)
* Add card meta for persisting custom scheduling state

* Rename meta -> custom_data

* Enforce limits on size of custom data

Large values will slow down table scans of the cards table, and it's
easier to be strict now and possibly relax things in the future than
the opposite.

* Pack card states and customData into a single message

+ default customData to empty if it can't be parsed

Co-authored-by: Damien Elmes <gpg@ankiweb.net>
2022-09-02 11:22:49 +10:00

41 lines
1.2 KiB
TypeScript

// Copyright: Ankitects Pty Ltd and contributors
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
import { postRequest } from "../lib/postrequest";
import { Scheduler } from "../lib/proto";
async function getCustomScheduling(): Promise<Scheduler.CustomScheduling> {
return Scheduler.CustomScheduling.decode(
await postRequest("/_anki/getCustomScheduling", ""),
);
}
async function setCustomScheduling(
key: string,
scheduling: Scheduler.CustomScheduling,
): Promise<void> {
const bytes = Scheduler.CustomScheduling.encode(scheduling).finish();
await postRequest("/_anki/setCustomScheduling", bytes, { key });
}
export async function mutateNextCardStates(
key: string,
mutator: (
states: Scheduler.NextCardStates,
customData: Record<string, unknown>,
) => void,
): Promise<void> {
const scheduling = await getCustomScheduling();
let customData = {};
try {
customData = JSON.parse(scheduling.customData);
} catch {
// can't be parsed
}
mutator(scheduling.states!, customData);
scheduling.customData = JSON.stringify(customData);
await setCustomScheduling(key, scheduling);
}