summaryrefslogtreecommitdiffhomepage
path: root/packages/app/src/context/event.tsx
blob: a2aa54181a725f9a2eb9d95c58f258bc1ff8bde1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import { createContext, useContext, type ParentProps } from "solid-js"
import { createEventBus } from "@solid-primitives/event-bus"
import type { Event as SDKEvent } from "@opencode-ai/sdk"
import { useSDK } from "@/context"

export type Event = SDKEvent // can extend with custom events later

function init() {
  const sdk = useSDK()
  const bus = createEventBus<Event>()
  sdk.event.subscribe().then(async (events) => {
    for await (const event of events.stream) {
      bus.emit(event)
    }
  })
  return bus
}

type EventContext = ReturnType<typeof init>

const ctx = createContext<EventContext>()

export function EventProvider(props: ParentProps) {
  const value = init()
  return <ctx.Provider value={value}>{props.children}</ctx.Provider>
}

export function useEvent() {
  const value = useContext(ctx)
  if (!value) {
    throw new Error("useEvent must be used within a EventProvider")
  }
  return value
}