MCPcopy Create free account
hub / github.com/UsefulSoftwareCo/executor / waitForOAuthResult

Function waitForOAuthResult

apps/local/src/oauth-result-store.ts:106–134  ·  view source on GitHub ↗
(
  sessionId: string,
  opts: { readonly timeoutMs: number; readonly signal?: AbortSignal },
)

Source from the content-addressed store, hash-verified

104 * this answers `null` immediately instead of holding.
105 */
106export const waitForOAuthResult = (
107 sessionId: string,
108 opts: { readonly timeoutMs: number; readonly signal?: AbortSignal },
109): Promise<AnyResult | null> => {
110 const immediate = consumeOAuthResult(sessionId);
111 if (immediate !== null) return Promise.resolve(immediate);
112 if (opts.timeoutMs <= 0 || opts.signal?.aborted === true) return Promise.resolve(null);
113 if (waiters.has(sessionId) || waiters.size >= MAX_HELD_WAITERS_TOTAL) {
114 return Promise.resolve(null);
115 }
116
117 return new Promise((resolve) => {
118 let done = false;
119 const finish = (result: AnyResult | null) => {
120 if (done) return;
121 done = true;
122 clearTimeout(timer);
123 opts.signal?.removeEventListener("abort", onAbort);
124 removeWaiter(sessionId, wake);
125 resolve(result);
126 };
127 const wake = () => finish(consumeOAuthResult(sessionId));
128 const onAbort = () => finish(null);
129 const timer = setTimeout(() => finish(null), opts.timeoutMs);
130
131 waiters.set(sessionId, wake);
132 opts.signal?.addEventListener("abort", onAbort, { once: true });
133 });
134};
135
136/** Test-only — clears the store and resolves any held waiters as pending. */
137export const __resetOAuthResultStoreForTests = (): void => {

Callers 2

fetchFunction · 0.90

Calls 4

consumeOAuthResultFunction · 0.85
resolveMethod · 0.80
finishFunction · 0.70
setMethod · 0.65

Tested by

no test coverage detected