Skip to content

Commit

Permalink
fix: support When custom context
Browse files Browse the repository at this point in the history
  • Loading branch information
pancsta committed Jan 15, 2024
1 parent 8b81dee commit 2960dc1
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions pkg/machine/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,20 @@ func (m *Machine) When(states []string, ctx context.Context) chan struct{} {
m.indexWhen[s] = append(m.indexWhen[s], binding)
}
}
go func() {
// dispose the binding on ctx.Done() and m.Ctx.Done()
select {
case <-ctx.Done():
case <-m.Ctx.Done():
m.activeStatesLock.Lock()
for _, s := range states {
if _, ok := m.indexWhen[s]; ok {
m.indexWhen[s] = lo.Without(m.indexWhen[s], binding)
}
}
m.activeStatesLock.Unlock()
}
}()
m.activeStatesLock.Unlock()

return ch
Expand Down Expand Up @@ -310,6 +324,20 @@ func (m *Machine) WhenNot(states []string, ctx context.Context) chan struct{} {
m.indexWhen[s] = append(m.indexWhen[s], binding)
}
}
go func() {
// dispose the binding on ctx.Done() and m.Ctx.Done()
select {
case <-ctx.Done():
case <-m.Ctx.Done():
m.activeStatesLock.Lock()
for _, s := range states {
if _, ok := m.indexWhen[s]; ok {
m.indexWhen[s] = lo.Without(m.indexWhen[s], binding)
}
}
m.activeStatesLock.Unlock()
}
}()
m.activeStatesLock.Unlock()

return ch
Expand Down

0 comments on commit 2960dc1

Please sign in to comment.