filechanges.go 956 B

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