You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

37 lines
1.0 KiB

  1. // Copyright (c) 2019 FOSS contributors of https://github.com/nxadm/tail
  2. package watch
  3. type FileChanges struct {
  4. Modified chan bool // Channel to get notified of modifications
  5. Truncated chan bool // Channel to get notified of truncations
  6. Deleted chan bool // Channel to get notified of deletions/renames
  7. }
  8. func NewFileChanges() *FileChanges {
  9. return &FileChanges{
  10. make(chan bool, 1), make(chan bool, 1), make(chan bool, 1)}
  11. }
  12. func (fc *FileChanges) NotifyModified() {
  13. sendOnlyIfEmpty(fc.Modified)
  14. }
  15. func (fc *FileChanges) NotifyTruncated() {
  16. sendOnlyIfEmpty(fc.Truncated)
  17. }
  18. func (fc *FileChanges) NotifyDeleted() {
  19. sendOnlyIfEmpty(fc.Deleted)
  20. }
  21. // sendOnlyIfEmpty sends on a bool channel only if the channel has no
  22. // backlog to be read by other goroutines. This concurrency pattern
  23. // can be used to notify other goroutines if and only if they are
  24. // looking for it (i.e., subsequent notifications can be compressed
  25. // into one).
  26. func sendOnlyIfEmpty(ch chan bool) {
  27. select {
  28. case ch <- true:
  29. default:
  30. }
  31. }