MCPcopy Create free account
hub / github.com/astercloud/aster / Subscribe

Method Subscribe

pkg/events/bus.go:296–328  ·  view source on GitHub ↗

Subscribe 订阅指定通道的事件(返回channel)

(channels []types.AgentChannel, opts *types.SubscribeOptions)

Source from the content-addressed store, hash-verified

294
295// Subscribe 订阅指定通道的事件(返回channel)
296func (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 取消订阅
331func (eb *EventBus) Unsubscribe(ch <-chan types.AgentEventEnvelope) {

Callers 5

TestEmitAndSubscribeFunction · 0.95
TestUnsubscribeFunction · 0.95
TestCloseFunction · 0.95
mainFunction · 0.95

Calls 2

replayMethod · 0.95
generateSubIDFunction · 0.85

Tested by 4

TestEmitAndSubscribeFunction · 0.76
TestUnsubscribeFunction · 0.76
TestCloseFunction · 0.76