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:
1 parent
468cafc6db
commit
b5defb4e4f
46 files changed
+1957
-406
No files matched your search
@@ -6,13 +6,20 @@ import { createOpenLibrary } from '../sources/openlibrary.ts';
|
||||
import type { OlInfo } from '../sources/openlibrary.ts';
|
||||
import type { MarcFields } from '../sources/marc.ts';
|
||||
import { TemporaryError } from '../sources/http.ts';
|
||||
import { recordMatches } from './match.ts';
|
||||
import type { SourceCache } from '../sources/http.ts';
|
||||
|
||||
export { TemporaryError };
|
||||
export type { SourceCache };
|
||||
|
||||
export interface ResolveOpts {
|
||||
onMeta?: (meta: BookMeta) => void;
|
||||
/** Last-attempt mode: a Library of Congress failure is not fatal; use Open Library's numbers if it has any. */
|
||||
tolerateLocFailure?: boolean;
|
||||
}
|
||||
|
||||
export interface Resolver {
|
||||
resolve(input: ResolveInput, opts?: { onMeta?: (meta: BookMeta) => void }): Promise<Resolution>;
|
||||
resolve(input: ResolveInput, opts?: ResolveOpts): Promise<Resolution>;
|
||||
search(title: string, author: string): Promise<SearchResult[]>;
|
||||
}
|
||||
|
||||
@@ -50,7 +57,16 @@ export function createResolver(deps: {
|
||||
sleep: deps.sleep ?? ((ms) => new Promise((r) => setTimeout(r, ms))),
|
||||
});
|
||||
|
||||
async function resolve(input: ResolveInput, opts?: { onMeta?: (meta: BookMeta) => void }): Promise<Resolution> {
|
||||
async function resolve(input: ResolveInput, opts?: ResolveOpts): Promise<Resolution> {
|
||||
// In tolerant mode the first LoC failure switches the LoC tiers off for the rest of this lookup.
|
||||
let locError: unknown = null;
|
||||
const locCall = async (fn: () => Promise<MarcFields[]>): Promise<MarcFields[]> => {
|
||||
if (locError) return [];
|
||||
try { return await fn(); } catch (e) {
|
||||
if (opts?.tolerateLocFailure && e instanceof TemporaryError) { locError = e; return []; }
|
||||
throw e;
|
||||
}
|
||||
};
|
||||
const isbn = input.isbn13 ? (normalizeIsbn(input.isbn13) ?? input.isbn13) : undefined;
|
||||
let notified = false;
|
||||
const notify = (m: BookMeta | null) => {
|
||||
@@ -82,11 +98,11 @@ export function createResolver(deps: {
|
||||
let locRecs: MarcFields[] = [];
|
||||
let exactSource: 'loc-isbn' | 'loc-lccn' = 'loc-isbn';
|
||||
if (lccn) {
|
||||
locRecs = await loc.byLccn(lccn);
|
||||
locRecs = await locCall(() => loc.byLccn(lccn));
|
||||
if (locRecs.length > 0) exactSource = 'loc-lccn';
|
||||
}
|
||||
if (isbn && lccOf(locRecs).length === 0) {
|
||||
const recs = await loc.byIsbn(isbn);
|
||||
const recs = await locCall(() => loc.byIsbn(isbn));
|
||||
if (recs.length > 0) { locRecs = recs; exactSource = 'loc-isbn'; }
|
||||
}
|
||||
|
||||
@@ -112,8 +128,11 @@ export function createResolver(deps: {
|
||||
work ??= await getWork(false);
|
||||
const m = metaOf([work?.meta]);
|
||||
const title = m?.title ?? input.title;
|
||||
if (title) {
|
||||
locTitleRecs = await loc.byTitle(title, m?.authors[0] ?? input.author ?? '');
|
||||
const author = m?.authors[0] ?? input.author ?? '';
|
||||
// Without an author a title search cannot be verified, so the tier is skipped.
|
||||
if (title && author) {
|
||||
const found = await locCall(() => loc.byTitle(title, author));
|
||||
locTitleRecs = found.filter((r) => recordMatches(r, title, author));
|
||||
chosen = chooseCallNumber(
|
||||
{ locExact: [], olEdition: [], locTitle: lccOf(locTitleRecs), olWork: [] },
|
||||
forChoose(m), exactSource,
|
||||
@@ -141,6 +160,8 @@ export function createResolver(deps: {
|
||||
confidence: chosen.confidence, source: chosen.source, dewey, reason: null,
|
||||
};
|
||||
}
|
||||
// Open Library had nothing and the LoC could not be asked: report that, not "no number found".
|
||||
if (locError) throw locError;
|
||||
return {
|
||||
meta, callNumber: null, sortKey: null, confidence: null, source: null, dewey,
|
||||
reason: meta ? 'No Library of Congress number found' : 'Not found in Open Library or the Library of Congress',
|
||||
|
||||
Reference in new issue
Block a user