Unsubscribe 取消订阅
(ch <-chan types.AgentEventEnvelope)
| 329 | |
| 330 | // Unsubscribe 取消订阅 |
| 331 | func (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事件处理器 |
| 375 | func (eb *EventBus) OnControl(eventType string, handler EventHandler) func() { |