github.com/BGrewell/tail@v1.0.1-0.20210309152823-689d25348e0e/watch/filechanges.go (about)

     1  package watch
     2  
     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  
     9  func NewFileChanges() *FileChanges {
    10  	return &FileChanges{
    11  		make(chan bool, 1), make(chan bool, 1), make(chan bool, 1)}
    12  }
    13  
    14  func (fc *FileChanges) NotifyModified() {
    15  	sendOnlyIfEmpty(fc.Modified)
    16  }
    17  
    18  func (fc *FileChanges) NotifyTruncated() {
    19  	sendOnlyIfEmpty(fc.Truncated)
    20  }
    21  
    22  func (fc *FileChanges) NotifyDeleted() {
    23  	sendOnlyIfEmpty(fc.Deleted)
    24  }
    25  
    26  // sendOnlyIfEmpty sends on a bool channel only if the channel has no
    27  // backlog to be read by other goroutines. This concurrency pattern
    28  // can be used to notify other goroutines if and only if they are
    29  // looking for it (i.e., subsequent notifications can be compressed
    30  // into one).
    31  func sendOnlyIfEmpty(ch chan bool) {
    32  	select {
    33  	case ch <- true:
    34  	default:
    35  	}
    36  }