Publish publishes the provided message to the PubSub, and invokes callbacks registered by subscribers asynchronously.
(msg any)
| 97 | // Publish publishes the provided message to the PubSub, and invokes |
| 98 | // callbacks registered by subscribers asynchronously. |
| 99 | func (ps *PubSub) Publish(msg any) { |
| 100 | ps.mu.Lock() |
| 101 | defer ps.mu.Unlock() |
| 102 | |
| 103 | ps.msg = msg |
| 104 | for sub := range ps.subscribers { |
| 105 | s := sub |
| 106 | ps.cs.TrySchedule(func(context.Context) { |
| 107 | ps.mu.Lock() |
| 108 | defer ps.mu.Unlock() |
| 109 | if !ps.subscribers[s] { |
| 110 | return |
| 111 | } |
| 112 | s.OnMessage(msg) |
| 113 | }) |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | // Done returns a channel that is closed after the context passed to NewPubSub |
| 118 | // is canceled and all updates have been sent to subscribers. |