()
| 106 | } |
| 107 | |
| 108 | func (b *bus) run() { |
| 109 | defer b.lc.ShutdownCompleted() |
| 110 | |
| 111 | var outch chan<- Event |
| 112 | var curev Event |
| 113 | |
| 114 | loop: |
| 115 | for { |
| 116 | |
| 117 | if b.eventch != nil && len(b.evbuf) > 0 { |
| 118 | // If we're emitting events (Subscriber mode) and there |
| 119 | // are events to emit, set up the output channel and output |
| 120 | // event accordingly. |
| 121 | outch = b.eventch |
| 122 | curev = b.evbuf[0] |
| 123 | } else { |
| 124 | // otherwise block the output (sending to a nil channel always blocks) |
| 125 | outch = nil |
| 126 | } |
| 127 | |
| 128 | select { |
| 129 | case err := <-b.lc.ShutdownRequest(): |
| 130 | b.lc.ShutdownInitiated(err) |
| 131 | break loop |
| 132 | |
| 133 | case outch <- curev: |
| 134 | // Event was emitted. Shrink current event buffer. |
| 135 | b.evbuf = b.evbuf[1:] |
| 136 | |
| 137 | case ev := <-b.pubch: |
| 138 | // publish event |
| 139 | |
| 140 | // Buffer event. |
| 141 | if b.eventch != nil { |
| 142 | b.evbuf = append(b.evbuf, ev) |
| 143 | } |
| 144 | |
| 145 | // Publish to children. |
| 146 | for sub := range b.subscriptions { |
| 147 | if err := sub.Publish(ev); err != nil && !errors.Is(err, ErrNotRunning) { |
| 148 | panic(err) |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | case ch := <-b.subch: |
| 153 | // new subscription |
| 154 | |
| 155 | sub := newSubscriber(b) |
| 156 | b.subscriptions[sub] = true |
| 157 | |
| 158 | ch <- sub |
| 159 | |
| 160 | case sub := <-b.unsubch: |
| 161 | // subscription closed |
| 162 | delete(b.subscriptions, sub) |
| 163 | } |
| 164 | } |
| 165 |
no test coverage detected