changeAndNotify is called when a feature is added or removed. It calls change, which should do the work and report whether a change actually occurred. If there was a change, it sets a timer to send a notification. This debounces change notifications: a single notification is sent after multiple chan
(notification string, change func() bool)
| 628 | // This debounces change notifications: a single notification is sent after |
| 629 | // multiple changes occur in close proximity. |
| 630 | func (s *Server) changeAndNotify(notification string, change func() bool) { |
| 631 | s.mu.Lock() |
| 632 | defer s.mu.Unlock() |
| 633 | if change() && s.shouldSendListChangedNotification(notification) { |
| 634 | if len(s.sessions) == 0 { |
| 635 | if t := s.pendingNotifications[notification]; t != nil { |
| 636 | t.Stop() |
| 637 | s.pendingNotifications[notification] = nil |
| 638 | } |
| 639 | return |
| 640 | } |
| 641 | |
| 642 | // Reset the outstanding delayed call, if any. |
| 643 | if t := s.pendingNotifications[notification]; t == nil { |
| 644 | s.pendingNotifications[notification] = time.AfterFunc(notificationDelay, func() { s.notifySessions(notification) }) |
| 645 | } else { |
| 646 | t.Reset(notificationDelay) |
| 647 | } |
| 648 | } |
| 649 | } |
| 650 | |
| 651 | // notifySessions sends the notification n to all existing sessions. |
| 652 | // It is called asynchronously by changeAndNotify. |
no test coverage detected