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

Method Unsubscribe

pkg/events/bus.go:331–372  ·  view source on GitHub ↗

Unsubscribe 取消订阅

(ch <-chan types.AgentEventEnvelope)

Source from the content-addressed store, hash-verified

329
330// Unsubscribe 取消订阅
331func (eb *EventBus) Unsubscribe(ch <-chan types.AgentEventEnvelope) {
332 eb.mu.Lock()
333 defer eb.mu.Unlock()
334
335 var writeCh chan types.AgentEventEnvelope
336 found := false
337
338 // 从所有订阅 map 中查找并移除(需要检查所有 map,因为同一个 channel 可能订阅了多个通道)
339 // 在第一次找到时保存双向 channel 用于关闭
340 for id, subCh := range eb.progressSubs {
341 if subCh == ch {
342 delete(eb.progressSubs, id)
343 if !found {
344 writeCh = subCh
345 found = true
346 }
347 }
348 }
349 for id, subCh := range eb.controlSubs {
350 if subCh == ch {
351 delete(eb.controlSubs, id)
352 if !found {
353 writeCh = subCh
354 found = true
355 }
356 }
357 }
358 for id, subCh := range eb.monitorSubs {
359 if subCh == ch {
360 delete(eb.monitorSubs, id)
361 if !found {
362 writeCh = subCh
363 found = true
364 }
365 }
366 }
367
368 // 只关闭一次 channel
369 if found && writeCh != nil {
370 close(writeCh)
371 }
372}
373
374// OnControl 注册Control事件处理器
375func (eb *EventBus) OnControl(eventType string, handler EventHandler) func() {

Callers 2

TestUnsubscribeFunction · 0.95

Calls 1

deleteFunction · 0.85

Tested by 2

TestUnsubscribeFunction · 0.76