| // Copyright 2020 The Go Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style |
| // license that can be found in the LICENSE file. |
| type debounceEvent struct { |
| events map[string]*debounceEvent |
| func newDebouncer() *debouncer { |
| events: make(map[string]*debounceEvent), |
| // debounce returns a channel that receives a boolean reporting whether, |
| // by the time the delay channel receives a value, this call is (or will be) |
| // the most recent call with the highest order number for its key. |
| func (d *debouncer) debounce(key string, order uint64, delay <-chan time.Time) <-chan bool { |
| okc := make(chan bool, 1) |
| if prev, ok := d.events[key]; ok { |
| // If we have a logical ordering of events (as is the case for snapshots), |
| // don't overwrite a later event with an earlier event. |
| done := make(chan struct{}) |
| if d.events[key] == next { |
| // The event was superseded before we acquired d.mu. |