MCPcopy
hub / github.com/anomalyco/opencode / waitEvent

Function waitEvent

packages/opencode/src/control-plane/util.ts:4–39  ·  view source on GitHub ↗
(input: { timeout: number; signal?: AbortSignal; fn: (event: GlobalEvent) => boolean })

Source from the content-addressed store, hash-verified

2import { Effect } from "effect"
3
4export function waitEvent(input: { timeout: number; signal?: AbortSignal; fn: (event: GlobalEvent) => boolean }) {
5 if (input.signal?.aborted) return Effect.fail(input.signal.reason ?? new Error("Request aborted"))
6
7 return Effect.callback<void, unknown>((resume) => {
8 const abort = () => {
9 cleanup()
10 resume(Effect.fail(input.signal?.reason ?? new Error("Request aborted")))
11 }
12
13 const handler = (event: GlobalEvent) => {
14 try {
15 if (!input.fn(event)) return
16 cleanup()
17 resume(Effect.void)
18 } catch (error) {
19 cleanup()
20 resume(Effect.fail(error))
21 }
22 }
23
24 const cleanup = () => {
25 clearTimeout(timeout)
26 GlobalBus.off("event", handler)
27 input.signal?.removeEventListener("abort", abort)
28 }
29
30 const timeout = setTimeout(() => {
31 cleanup()
32 resume(Effect.fail(new Error("Timed out waiting for global event")))
33 }, input.timeout)
34
35 GlobalBus.on("event", handler)
36 input.signal?.addEventListener("abort", abort, { once: true })
37 return Effect.sync(cleanup)
38 })
39}

Callers 2

workspace.tsFile · 0.90
waitUntilSyncedFunction · 0.90

Calls 6

resumeFunction · 0.85
onMethod · 0.80
addEventListenerMethod · 0.80
syncMethod · 0.80
cleanupFunction · 0.70
callbackMethod · 0.45

Tested by

no test coverage detected