mirror of
https://github.com/ksyasuda/SubMiner.git
synced 2026-11-26 04:46:07 -08:00
fix(anki): stop the new-card poll from dropping mined cards (#339)
This commit is contained in:
1 parent
e05fb34f7b
commit
713cdb3ef8
4 files changed
+114
-4
No files matched your search
@@ -173,3 +173,90 @@ test('a failed source lookup skips plugin events without stopping counting or en
|
||||
assert.deepEqual(h.ready, []);
|
||||
assert.equal(h.warnings.length, 2);
|
||||
});
|
||||
|
||||
/** A runner whose `findNotes` read stays pending until `finishRead`, sharing one update lock. */
|
||||
function makeSlowReadRunner() {
|
||||
const state = { updating: false, processed: [] as number[], tracked: new Set<number>() };
|
||||
let finishRead: (noteIds: number[]) => void = () => undefined;
|
||||
const runner = new PollingRunner({
|
||||
getDeck: () => 'Mining',
|
||||
getPollingRate: () => 3000,
|
||||
findNotes: (query) =>
|
||||
query.includes('tag:')
|
||||
? Promise.resolve([])
|
||||
: new Promise<number[]>((resolve) => {
|
||||
finishRead = resolve;
|
||||
}),
|
||||
shouldAutoUpdateNewCards: () => true,
|
||||
processNewCard: async (noteId) => {
|
||||
state.processed.push(noteId);
|
||||
},
|
||||
isUpdateInProgress: () => state.updating,
|
||||
setUpdateInProgress: (value) => {
|
||||
state.updating = value;
|
||||
},
|
||||
getTrackedNoteIds: () => state.tracked,
|
||||
setTrackedNoteIds: (noteIds) => {
|
||||
state.tracked = noteIds;
|
||||
},
|
||||
showStatusNotification: () => undefined,
|
||||
logDebug: () => undefined,
|
||||
logInfo: () => undefined,
|
||||
logWarn: () => undefined,
|
||||
});
|
||||
return {
|
||||
runner,
|
||||
state,
|
||||
finishRead: (noteIds: number[]) => finishRead(noteIds),
|
||||
/** The first poll only records the notes already in the deck. */
|
||||
initialize: async () => {
|
||||
const poll = runner.pollOnce();
|
||||
finishRead([10]);
|
||||
await poll;
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
test('a poll waiting on Anki leaves the update lock free for mining', async () => {
|
||||
const h = makeSlowReadRunner();
|
||||
await h.initialize();
|
||||
|
||||
const poll = h.runner.pollOnce();
|
||||
assert.equal(h.state.updating, false, 'mining can start while the read is pending');
|
||||
h.finishRead([10, 11]);
|
||||
await poll;
|
||||
|
||||
assert.deepEqual(h.state.processed, [11]);
|
||||
assert.equal(h.state.updating, false);
|
||||
});
|
||||
|
||||
test('a poll leaves new notes alone when mining starts during its read', async () => {
|
||||
const h = makeSlowReadRunner();
|
||||
await h.initialize();
|
||||
|
||||
const poll = h.runner.pollOnce();
|
||||
// Mining takes the lock and adds note 11 while the poll's read is in flight.
|
||||
h.state.updating = true;
|
||||
h.finishRead([10, 11]);
|
||||
await poll;
|
||||
|
||||
assert.deepEqual(h.state.processed, []);
|
||||
assert.deepEqual([...h.state.tracked], [10]);
|
||||
assert.equal(h.state.updating, true, 'the poll must not release a lock it never took');
|
||||
});
|
||||
|
||||
test('the first poll keeps notes mined while its read was pending', async () => {
|
||||
const h = makeSlowReadRunner();
|
||||
|
||||
const init = h.runner.pollOnce();
|
||||
// Mining finishes during the initial read and tracks note 11, which the read missed.
|
||||
h.state.tracked.add(11);
|
||||
h.finishRead([10]);
|
||||
await init;
|
||||
|
||||
const poll = h.runner.pollOnce();
|
||||
h.finishRead([10, 11]);
|
||||
await poll;
|
||||
|
||||
assert.deepEqual(h.state.processed, []);
|
||||
});
|
||||
@@ -26,6 +26,8 @@ export interface PollingRunnerDeps {
|
||||
|
||||
export class PollingRunner {
|
||||
private pollingInterval: ReturnType<typeof setInterval> | null = null;
|
||||
// Keeps interval ticks from stacking up behind a slow read, now that reads skip the update lock.
|
||||
private pollInFlight = false;
|
||||
private initialized = false;
|
||||
private backoffMs = 200;
|
||||
private maxBackoffMs = 5000;
|
||||
@@ -56,19 +58,29 @@ export class PollingRunner {
|
||||
}
|
||||
|
||||
async pollOnce(): Promise<void> {
|
||||
if (this.deps.isUpdateInProgress()) return;
|
||||
if (this.pollInFlight || this.deps.isUpdateInProgress()) return;
|
||||
if (Date.now() < this.nextPollTime) return;
|
||||
|
||||
this.deps.setUpdateInProgress(true);
|
||||
this.pollInFlight = true;
|
||||
let holdsUpdateLock = false;
|
||||
try {
|
||||
const query = this.deps.getDeck() ? `"deck:${this.deps.getDeck()}" added:1` : 'added:1';
|
||||
// The read runs without the update lock: mining checks that lock and gives up when it is
|
||||
// held, so holding it for a routine poll would drop a card mined at the wrong moment.
|
||||
const noteIds = await this.deps.findNotes(query, {
|
||||
maxRetries: 0,
|
||||
});
|
||||
// An update started during the read (its creator tracks any note it adds), so leave the
|
||||
// new notes to the next poll.
|
||||
if (this.deps.isUpdateInProgress()) return;
|
||||
this.deps.setUpdateInProgress(true);
|
||||
holdsUpdateLock = true;
|
||||
const currentNoteIds = new Set(noteIds);
|
||||
|
||||
const previousNoteIds = this.deps.getTrackedNoteIds();
|
||||
if (!this.initialized) {
|
||||
// Keep notes mined while the read was pending; the read may have started before they existed.
|
||||
for (const noteId of previousNoteIds) currentNoteIds.add(noteId);
|
||||
this.deps.setTrackedNoteIds(currentNoteIds);
|
||||
this.initialized = true;
|
||||
this.deps.logInfo(`AnkiConnect initialized with ${currentNoteIds.size} existing cards`);
|
||||
@@ -140,7 +152,8 @@ export class PollingRunner {
|
||||
}
|
||||
this.deps.logWarn((error as Error).message);
|
||||
} finally {
|
||||
this.deps.setUpdateInProgress(false);
|
||||
this.pollInFlight = false;
|
||||
if (holdsUpdateLock) this.deps.setUpdateInProgress(false);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user