Skip to content

Commit

Permalink
Comment cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
chdxD1 committed Jul 4, 2023
1 parent 60ab675 commit 6f13d58
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions pkg/debounce/debounce.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,11 @@ func (d *Debouncer) debounceRoutine(ctx context.Context) {

err := d.function(ctx)
if err == nil {
// If debounce was called during execution run debounceRoutine again otherwise reset
// scheduled to false
if d.calledDuringExecution.CompareAndSwap(true, false) {
d.debounceRoutine(ctx)
} else {
// Reset scheduled to 0
d.scheduled.Store(false)
}
break
Expand All @@ -49,9 +50,12 @@ func (d *Debouncer) debounceRoutine(ctx context.Context) {

// Run function. First run will be in debounceTime, runs will be seperated by debounceTime
func (d *Debouncer) Debounce(ctx context.Context) {
// If we haven't scheduled a goroutine yet, set scheduled=0 and run goroutine
// We use atomic compare-and-swap to first check if scheduled equals 0 (not yet scheduled)
// and then swap the value with 1
// If we haven't scheduled a goroutine yet, set scheduled=false and run goroutine
// We use atomic compare-and-swap to first check if scheduled equals false (not yet scheduled)
// and then swap the value with true
// Always set calledDuringExection to true but reset it to false if we schedule it the first time.
// This way a debounce during running execution (scheduled is still true, calledDuringExecution will
// be true) will run the debounced routine once again
d.calledDuringExecution.Store(true)
if d.scheduled.CompareAndSwap(false, true) {
d.calledDuringExecution.Store(false)
Expand Down

0 comments on commit 6f13d58

Please sign in to comment.