mirror of
https://github.com/NanmiCoder/cc-haha
synced 2026-07-17 13:13:35 +08:00
25 lines
1.0 KiB
TypeScript
25 lines
1.0 KiB
TypeScript
import { parseHostMessage, serializeAgentMessage, type AgentMessage, type HostMessage } from './protocol'
|
|
|
|
type Deps = { postToHost: (raw: string) => void; location: Location; title: string }
|
|
|
|
export function createBridge(deps: Deps) {
|
|
const handlers = new Map<HostMessage['type'], Array<(m: HostMessage) => void>>()
|
|
const send = (m: AgentMessage) => deps.postToHost(serializeAgentMessage(m))
|
|
return {
|
|
reportReady: () => send({ type: 'ready' }),
|
|
reportNavigated: () => send({ type: 'navigated', url: deps.location.href, title: deps.title }),
|
|
reportError: (message: string) => send({ type: 'error', message }),
|
|
send,
|
|
on<T extends HostMessage['type']>(type: T, fn: (m: Extract<HostMessage, { type: T }>) => void) {
|
|
const arr = handlers.get(type) ?? []
|
|
arr.push(fn as (m: HostMessage) => void)
|
|
handlers.set(type, arr)
|
|
},
|
|
handleHostRaw(raw: string) {
|
|
const msg = parseHostMessage(raw)
|
|
if (!msg) return
|
|
for (const fn of handlers.get(msg.type) ?? []) fn(msg)
|
|
},
|
|
}
|
|
}
|