MCPcopy Create free account
hub / github.com/Acode-Foundation/Acode / createSearchIndex

Function createSearchIndex

src/sidebarApps/searchInFiles/searchIndex.js:7–113  ·  view source on GitHub ↗
({ readFile, onStatus })

Source from the content-addressed store, hash-verified

5let requestId = 0;
6
7export function createSearchIndex({ readFile, onStatus }) {
8 function getWorker() {
9 if (worker) return worker;
10
11 worker = new Worker("build/searchIndexWorker.js");
12 worker.onmessage = async ({ data }) => {
13 const { action, id, error } = data;
14
15 if (action === "status") {
16 onStatus?.(data.data);
17 return;
18 }
19
20 if (action === "get-file") {
21 handleGetFile(data);
22 return;
23 }
24
25 const pending = requests.get(id);
26 if (!pending) return;
27 requests.delete(id);
28
29 if (error) pending.reject(error);
30 else pending.resolve(data.data);
31 };
32 worker.onerror = (error) => {
33 onStatus?.({ state: "error", message: "Search index unavailable" });
34 for (const pending of requests.values()) {
35 pending.reject(error);
36 }
37 requests.clear();
38 console.error(error);
39 };
40
41 return worker;
42 }
43
44 async function handleGetFile({ id, data }) {
45 const fileRequest = fileRequests.get(id);
46 if (fileRequest) return;
47 fileRequests.set(id, true);
48 const targetWorker = worker;
49
50 try {
51 const content = await readFile(data);
52 if (worker !== targetWorker || !targetWorker) return;
53 targetWorker.postMessage({
54 action: "get-file",
55 id,
56 data: content || "",
57 });
58 } catch (error) {
59 if (worker !== targetWorker || !targetWorker) return;
60 targetWorker.postMessage({
61 action: "get-file",
62 id,
63 error: error?.message || String(error),
64 });

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected