Rework scan screen, make lookups non-blocking, fix review findings

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
swaitsandClaude Fable 5.2 committed 2026-11-18 10:19:32 -07:00
1 parent 468cafc6db
commit b5defb4e4f
46 files changed
+1957 -406

No files matched your search

+15 -17
View File
@@ -2,38 +2,36 @@ import { normalizeIsbn } from '../shared/isbn.ts';
export const REPEAT_MS = 5000;
/** Remembers when each code was last reported, so a barcode held in view is announced once per 5 seconds. */
export interface ReadState {
streak: Record<string, number>;
emittedAt: Record<string, number>;
rejectedAt: Record<string, number>;
}
export function newReadState(): ReadState {
return { streak: {}, emittedAt: {}, rejectedAt: {} };
return { emittedAt: {}, rejectedAt: {} };
}
/** Call once per detected code in a detection round. */
/**
* Judge one decoded barcode. A valid ISBN-13 (check digit, 978 or 979 prefix) is accepted at once, with no
* second confirming read: the EAN checksum already makes a false read very unlikely. Anything else
* (a UPC, a price code, a bad check digit) is rejected. Neither is repeated within REPEAT_MS.
*/
export function acceptRead(
state: ReadState, code: string, now: number,
): { state: ReadState; emit: string | null; reject: boolean } {
const streak = { ...state.streak, [code]: (state.streak[code] ?? 0) + 1 };
const next: ReadState = { ...state, streak };
if (streak[code] < 2) return { state: next, emit: null, reject: false };
const isbn = normalizeIsbn(code);
const isbn = /^\d{13}$/.test(code) ? normalizeIsbn(code) : null;
if (isbn === null) {
const last = state.rejectedAt[code];
if (last !== undefined && now - last < REPEAT_MS) return { state: next, emit: null, reject: false };
return { state: { ...next, rejectedAt: { ...state.rejectedAt, [code]: now } }, emit: null, reject: true };
if (last !== undefined && now - last < REPEAT_MS) return { state, emit: null, reject: false };
return { state: { ...state, rejectedAt: { ...state.rejectedAt, [code]: now } }, emit: null, reject: true };
}
const last = state.emittedAt[isbn];
if (last !== undefined && now - last < REPEAT_MS) return { state: next, emit: null, reject: false };
return { state: { ...next, emittedAt: { ...state.emittedAt, [isbn]: now } }, emit: isbn, reject: false };
if (last !== undefined && now - last < REPEAT_MS) return { state, emit: null, reject: false };
return { state: { ...state, emittedAt: { ...state.emittedAt, [isbn]: now } }, emit: isbn, reject: false };
}
/** Call at the end of every detection round with the codes seen in it; codes not seen lose their streak. */
export function endRound(state: ReadState, seen: string[]): ReadState {
const streak: Record<string, number> = {};
for (const c of seen) if (state.streak[c] !== undefined) streak[c] = state.streak[c];
return { ...state, streak };
/** Record that an ISBN was just reported by some other path (the Capture button), so the loop stays quiet. */
export function markEmitted(state: ReadState, isbn: string, now: number): ReadState {
return { ...state, emittedAt: { ...state.emittedAt, [isbn]: now } };
}