MiniSearch / client /modules /pageExcerptWorker.ts
system's picture
system HF Staff
Sync from felladrin/MiniSearch@6c9fb20
1a6d0d2 verified
Raw History Blame Contribute Delete
928 Bytes
/// <reference lib="webworker" />
import { allocatePageExcerpts } from "./pageExcerptAllocation";
import type {
WorkerRequest,
WorkerResponse,
} from "./pageExcerptWorkerProtocol";
/**
* Answers one allocation request with the worker's response message.
*
* Exported as a plain function so the handler can be tested directly under
* jsdom - no bundler, no live worker - and so the worker's answer is
* provably the same computation the synchronous fallback runs.
*/
export function handleExcerptRequest(data: WorkerRequest): WorkerResponse {
try {
return {
type: "excerpts",
excerpts: allocatePageExcerpts(data.contents, data.tokenBudget),
};
} catch (error) {
return {
type: "error",
message: error instanceof Error ? error.message : String(error),
};
}
}
self.onmessage = ({ data }: MessageEvent<WorkerRequest>) => {
self.postMessage(handleExcerptRequest(data));
};