1package notification
2
3// Sink is a function that accepts notification requests.
4// This allows agents to publish notifications without knowing about the UI.
5type Sink func(Notification)
6
7// NewChannelSink creates a Sink that sends notifications to a channel. The
8// channel should have a buffer of 1.
9//
10// Any pending notification is discarded before sending the new one. This
11// ensures the consumer always sees the most recent notification rather
12// than a potential barrage when only one is needed.
13func NewChannelSink(ch chan Notification) Sink {
14 return func(n Notification) {
15 // Drain any existing notification.
16 select {
17 case <-ch:
18 default:
19 }
20 // Send the new notification. The channel should be empty, but it uses a
21 // non-blocking send for safety in case of a race with the consumer.
22 select {
23 case ch <- n:
24 default:
25 }
26 }
27}