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

Function waitFor

__tests__/daemon-pid-reuse.test.ts:121–143  ·  view source on GitHub ↗
(
  predicate: () => T | undefined | null | false,
  timeoutMs: number,
  pollMs = 25,
  label = '',
)

Source from the content-addressed store, hash-verified

119}
120
121function waitFor<T>(
122 predicate: () => T | undefined | null | false,
123 timeoutMs: number,
124 pollMs = 25,
125 label = '',
126): Promise<T> {
127 return new Promise((resolve, reject) => {
128 const started = Date.now();
129 const tick = () => {
130 let v: T | undefined | null | false;
131 try { v = predicate(); } catch (e) { return reject(e); }
132 if (v) return resolve(v as T);
133 if (Date.now() - started > timeoutMs) {
134 // Name the wait: an async stack loses the await site, so an unlabeled
135 // timeout can't tell WHICH step flaked (the #662 test's recurring
136 // timeout was undiagnosable for exactly this reason).
137 return reject(new Error(`Timed out after ${timeoutMs}ms${label ? ` waiting for: ${label}` : ''}`));
138 }
139 setTimeout(tick, pollMs);
140 };
141 tick();
142 });
143}
144
145function isAlive(pid: number): boolean {
146 try { process.kill(pid, 0); return true; } catch { return false; }

Callers 2

waitProcessExitFunction · 0.70

Calls 1

tickFunction · 0.70

Tested by

no test coverage detected