MCPcopy Create free account
hub / github.com/anomalyco/opencode / waitGlobalBusEvent

Function waitGlobalBusEvent

packages/opencode/test/server/global-bus.ts:4–31  ·  view source on GitHub ↗
(input: {
  timeout?: number
  message?: string
  predicate: (event: GlobalEvent) => boolean
})

Source from the content-addressed store, hash-verified

2import { Cause, Effect } from "effect"
3
4export function waitGlobalBusEvent(input: {
5 timeout?: number
6 message?: string
7 predicate: (event: GlobalEvent) => boolean
8}) {
9 return Effect.callback<GlobalEvent, unknown>((resume) => {
10 const cleanup = () => GlobalBus.off("event", handler)
11
12 const handler = (event: GlobalEvent) => {
13 try {
14 if (!input.predicate(event)) return
15 cleanup()
16 resume(Effect.succeed(event))
17 } catch (error) {
18 cleanup()
19 resume(Effect.fail(error))
20 }
21 }
22
23 GlobalBus.on("event", handler)
24 return Effect.sync(cleanup)
25 }).pipe(
26 Effect.timeout(input.timeout ?? 10_000),
27 Effect.mapError((error) =>
28 Cause.isTimeoutError(error) ? new Error(input.message ?? "timed out waiting for global bus event") : error,
29 ),
30 )
31}

Callers 3

waitDisposedFunction · 0.90
waitDisposedFunction · 0.90

Calls 3

onMethod · 0.80
syncMethod · 0.80
callbackMethod · 0.45

Tested by 2

waitDisposedFunction · 0.72
waitDisposedFunction · 0.72