From 84957eb8da2346dd4116c9d2463c557e915e591b Mon Sep 17 00:00:00 2001 From: Atsushi Watanabe Date: Sat, 4 Jul 2020 10:04:39 +0900 Subject: [PATCH] Flush state change/candidate callback After closing the channels. --- agent.go | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/agent.go b/agent.go index 1be5ad7..887036c 100644 --- a/agent.go +++ b/agent.go @@ -319,27 +319,39 @@ func (a *Agent) onSelectedCandidatePairChange(p *candidatePair) { } } +func (a *Agent) onCandidate(c Candidate) { + if onCandidateHdlr, ok := a.onCandidateHdlr.Load().(func(Candidate)); ok { + onCandidateHdlr(c) + } +} + +func (a *Agent) onConnectionStateChange(s ConnectionState) { + if hdlr, ok := a.onConnectionStateChangeHdlr.Load().(func(ConnectionState)); ok { + hdlr(s) + } +} + func (a *Agent) startOnConnectionStateChangeRoutine() { go func() { for { select { case s, isOpen := <-a.chanState: if !isOpen { + for c := range a.chanCandidate { + a.onCandidate(c) + } return } - - if hdlr, ok := a.onConnectionStateChangeHdlr.Load().(func(ConnectionState)); ok { - hdlr(s) - } + a.onConnectionStateChange(s) case c, isOpen := <-a.chanCandidate: if !isOpen { + for s := range a.chanState { + a.onConnectionStateChange(s) + } return } - - if onCandidateHdlr, ok := a.onCandidateHdlr.Load().(func(Candidate)); ok { - onCandidateHdlr(c) - } + a.onCandidate(c) } } }()