Add resolver, server, PDF rendering and web app
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
1 parent
6a7fcdc4b8
commit
bdfdf581f3
59 files changed
+4088
No files matched your search
@@ -0,0 +1,39 @@
|
||||
import { normalizeIsbn } from '../shared/isbn.ts';
|
||||
|
||||
export const REPEAT_MS = 5000;
|
||||
|
||||
export interface ReadState {
|
||||
streak: Record<string, number>;
|
||||
emittedAt: Record<string, number>;
|
||||
rejectedAt: Record<string, number>;
|
||||
}
|
||||
|
||||
export function newReadState(): ReadState {
|
||||
return { streak: {}, emittedAt: {}, rejectedAt: {} };
|
||||
}
|
||||
|
||||
/** Call once per detected code in a detection round. */
|
||||
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);
|
||||
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 };
|
||||
}
|
||||
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 };
|
||||
}
|
||||
|
||||
/** 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 };
|
||||
}
|
||||
Reference in new issue
Block a user