MCPcopy Create free account
hub / github.com/colbymchenry/codegraph / constructor

Method constructor

src/extraction/store-writer.ts:81–127  ·  view source on GitHub ↗
(workerScriptPath: string, dbPath: string, fastInit: boolean)

Source from the content-addressed store, hash-verified

79 private belowWaiters: Array<{ limit: number; resolve: () => void }> = [];
80
81 constructor(workerScriptPath: string, dbPath: string, fastInit: boolean) {
82 this.worker = new Worker(workerScriptPath);
83 this.started = workerStarted(this.worker);
84 let readyResolve!: () => void;
85 let readyReject!: (e: Error) => void;
86 this.readyPromise = new Promise<void>((resolve, reject) => {
87 readyResolve = resolve;
88 readyReject = reject;
89 });
90
91 this.worker.on('message', (msg: { type: string; id?: number; message?: string }) => {
92 if (msg.type === 'ready') {
93 readyResolve();
94 } else if (msg.type === 'ack') {
95 this.settleOne();
96 } else if (msg.type === 'drained' && msg.id !== undefined) {
97 const waiter = this.drainWaiters.get(msg.id);
98 this.drainWaiters.delete(msg.id);
99 if (!waiter) return;
100 if (this.firstError) waiter.reject(this.firstError);
101 else waiter.resolve();
102 } else if (msg.type === 'error') {
103 if (!this.firstError) this.firstError = new Error(`store worker: ${msg.message}`);
104 this.settleOne(); // the error reply is also the failed bundle's ack
105 }
106 });
107 this.worker.on('error', (err) => {
108 this.failAll(err instanceof Error ? err : new Error(String(err)));
109 readyReject(this.firstError!);
110 });
111 this.worker.on('exit', (code) => {
112 this.exited = true;
113 if (code !== 0) {
114 this.failAll(new Error(`store worker exited with code ${code}`));
115 readyReject(this.firstError!);
116 } else if (this.drainWaiters.size > 0 || this.belowWaiters.length > 0) {
117 // A clean exit with waiters pending is a protocol violation (only
118 // close() should end the worker) — settle the waiters instead of
119 // hanging the index forever.
120 this.failAll(new Error('store worker exited before drain completed'));
121 }
122 });
123
124 this.worker.postMessage({ type: 'open', dbPath, fastInit });
125 // The worker holds the event loop open only until close(); don't unref —
126 // bundles must never be dropped because main ran out of work.
127 }
128
129 private failAll(err: Error): void {
130 if (!this.firstError) this.firstError = err;

Callers

nothing calls this directly

Calls 8

settleOneMethod · 0.95
failAllMethod · 0.95
workerStartedFunction · 0.90
onMethod · 0.65
getMethod · 0.65
deleteMethod · 0.65
resolveMethod · 0.65
postMessageMethod · 0.65

Tested by

no test coverage detected