import { sse } from '@cloudbase/functions-typings' import { TcbContext } from './types' /** * SSESender - 用于发送 Server-Sent Events */ export class SSESender { private _status: 'initial' | 'started' | 'ended' = 'initial' private _sse: sse.IServerSentEvent | null = null get status() { return this._status } constructor(readonly context: TcbContext) {} // lazy,用的时候再转成 sse get sse() { if (!this._sse) { const sse = this.context?.sse?.() if (!sse) { throw new Error('Invalid sse') } this._sse = sse this._status = 'started' } return this._sse } send(data: sse.Event) { this.sse.send(data) } end() { this.sse.end({ data: '[DONE]' }) this._status = 'ended' } }