Subscribe returns a channel that receives a signal whenever paths change for chatID, along with an unsubscribe function that removes the channel.
(chatID uuid.UUID)
| 114 | // paths change for chatID, along with an unsubscribe function |
| 115 | // that removes the channel. |
| 116 | func (ps *PathStore) Subscribe(chatID uuid.UUID) (<-chan struct{}, func()) { |
| 117 | ch := make(chan struct{}, 1) |
| 118 | |
| 119 | ps.mu.Lock() |
| 120 | ps.subscribers[chatID] = append(ps.subscribers[chatID], ch) |
| 121 | ps.mu.Unlock() |
| 122 | |
| 123 | unsub := func() { |
| 124 | ps.mu.Lock() |
| 125 | defer ps.mu.Unlock() |
| 126 | subs := ps.subscribers[chatID] |
| 127 | for i, s := range subs { |
| 128 | if s == ch { |
| 129 | ps.subscribers[chatID] = append(subs[:i], subs[i+1:]...) |
| 130 | break |
| 131 | } |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | return ch, unsub |
| 136 | } |