Skip to content

Commit 0bc0ef8

Browse files
committed
adaptor/macos: handle case when adapter enable sends notification before event delegate is set
Signed-off-by: Ron Evans <[email protected]>
1 parent 266de98 commit 0bc0ef8

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

adapter_darwin.go

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,23 @@ func (a *Adapter) Enable() error {
4343
}
4444

4545
// wait until powered
46-
a.poweredChan = make(chan error)
46+
a.poweredChan = make(chan error, 1)
4747

4848
a.cmd = &centralManagerDelegate{a: a}
4949
a.cm.SetDelegate(a.cmd)
50-
select {
51-
case <-a.poweredChan:
52-
case <-time.NewTimer(10 * time.Second).C:
53-
return errors.New("timeout enabling CentralManager")
50+
51+
if a.cm.State() != cbgo.ManagerStatePoweredOn {
52+
select {
53+
case <-a.poweredChan:
54+
case <-time.NewTimer(10 * time.Second).C:
55+
return errors.New("timeout enabling CentralManager")
56+
}
57+
}
58+
59+
// drain any extra powered-on events from channel
60+
for len(a.poweredChan) > 0 {
61+
<-a.poweredChan
5462
}
55-
a.poweredChan = nil
5663

5764
// wait until powered?
5865
a.pmd = &peripheralManagerDelegate{a: a}
@@ -73,7 +80,7 @@ type centralManagerDelegate struct {
7380
func (cmd *centralManagerDelegate) CentralManagerDidUpdateState(cmgr cbgo.CentralManager) {
7481
// powered on?
7582
if cmgr.State() == cbgo.ManagerStatePoweredOn {
76-
close(cmd.a.poweredChan)
83+
cmd.a.poweredChan <- nil
7784
}
7885

7986
// TODO: handle other state changes.

0 commit comments

Comments
 (0)