sendStatusEvent sends subscription status event to all channels. If there is no listener, sendStatusEvent will not block. Lock should be held entering.
(status SubStatus)
| 5137 | // If there is no listener, sendStatusEvent |
| 5138 | // will not block. Lock should be held entering. |
| 5139 | func (s *Subscription) sendStatusEvent(status SubStatus) { |
| 5140 | for ch, statuses := range s.statListeners { |
| 5141 | if !slices.Contains(statuses, status) { |
| 5142 | continue |
| 5143 | } |
| 5144 | // only send event if someone's listening |
| 5145 | select { |
| 5146 | case ch <- status: |
| 5147 | default: |
| 5148 | } |
| 5149 | } |
| 5150 | // After sending SubscriptionClosed status to all listeners, |
| 5151 | // close all channels and clear the map to prevent future |
| 5152 | // sends to closed channels that could cause panics |
| 5153 | if status == SubscriptionClosed { |
| 5154 | for ch := range s.statListeners { |
| 5155 | close(ch) |
| 5156 | } |
| 5157 | s.statListeners = nil |
| 5158 | } |
| 5159 | } |
| 5160 | |
| 5161 | // changeSubStatus changes subscription status and sends events |
| 5162 | // to all listeners. Lock should be held entering. |