Subscribe 订阅指定通道的事件(返回channel)
(channels []types.AgentChannel, opts *types.SubscribeOptions)
| 294 | |
| 295 | // Subscribe 订阅指定通道的事件(返回channel) |
| 296 | func (eb *EventBus) Subscribe(channels []types.AgentChannel, opts *types.SubscribeOptions) <-chan types.AgentEventEnvelope { |
| 297 | eb.mu.Lock() |
| 298 | defer eb.mu.Unlock() |
| 299 | |
| 300 | // 创建缓冲channel(避免阻塞) |
| 301 | ch := make(chan types.AgentEventEnvelope, 100) |
| 302 | |
| 303 | // 生成唯一订阅ID |
| 304 | subID := generateSubID() |
| 305 | |
| 306 | // 注册到对应通道 |
| 307 | if len(channels) == 0 { |
| 308 | channels = []types.AgentChannel{types.ChannelProgress, types.ChannelControl, types.ChannelMonitor} |
| 309 | } |
| 310 | |
| 311 | for _, channel := range channels { |
| 312 | switch channel { |
| 313 | case types.ChannelProgress: |
| 314 | eb.progressSubs[subID] = ch |
| 315 | case types.ChannelControl: |
| 316 | eb.controlSubs[subID] = ch |
| 317 | case types.ChannelMonitor: |
| 318 | eb.monitorSubs[subID] = ch |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | // 如果指定了since,回放历史事件 |
| 323 | if opts != nil && opts.Since != nil { |
| 324 | go eb.replay(ch, opts.Since, opts.Kinds, channels) |
| 325 | } |
| 326 | |
| 327 | return ch |
| 328 | } |
| 329 | |
| 330 | // Unsubscribe 取消订阅 |
| 331 | func (eb *EventBus) Unsubscribe(ch <-chan types.AgentEventEnvelope) { |