Skip to content

Commit

Permalink
fix: race condition on async list await
Browse files Browse the repository at this point in the history
  • Loading branch information
marcobrotas-prowarehouse committed Oct 16, 2024
1 parent 9d03890 commit 749af25
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions async.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package command

import "sync"

// Async is the struct returned from async commands.
type Async struct {
sync.Mutex
hdl Handler
cmd Command
data any
Expand Down Expand Up @@ -55,14 +58,18 @@ func (as *Async) success(data any) {
}

func (as *Async) setListener(listener func(as *Async)) {
as.Lock()
as.listener = listener
as.Unlock()
if as.done.enabled() {
as.notifyListener()
}
}

func (as *Async) notifyListener() {
as.Lock()
if as.listener != nil {
as.listener(as)
}
as.Unlock()
}

0 comments on commit 749af25

Please sign in to comment.