Skip to content

Commit 7d8a955

Browse files
authored
Merge pull request #17 from libp2p/fix/with-node
nit: fix with-node
2 parents 0a6e078 + edaaa65 commit 7d8a955

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

p2p/host/eventbus/basic.go

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func NewBus() event.Bus {
5151
}
5252
}
5353

54-
func (b *basicBus) withNode(typ reflect.Type, cb func(*node), async func(*node)) error {
54+
func (b *basicBus) withNode(typ reflect.Type, cb func(*node), async func(*node)) {
5555
b.lk.Lock()
5656

5757
n, ok := b.nodes[typ]
@@ -65,12 +65,14 @@ func (b *basicBus) withNode(typ reflect.Type, cb func(*node), async func(*node))
6565

6666
cb(n)
6767

68-
go func() {
69-
defer n.lk.Unlock()
70-
async(n)
71-
}()
72-
73-
return nil
68+
if async == nil {
69+
n.lk.Unlock()
70+
} else {
71+
go func() {
72+
defer n.lk.Unlock()
73+
async(n)
74+
}()
75+
}
7476
}
7577

7678
func (b *basicBus) tryDropNode(typ reflect.Type) {
@@ -168,7 +170,7 @@ func (b *basicBus) Subscribe(evtTypes interface{}, opts ...event.SubscriptionOpt
168170
for i, etyp := range types {
169171
typ := reflect.TypeOf(etyp)
170172

171-
err = b.withNode(typ.Elem(), func(n *node) {
173+
b.withNode(typ.Elem(), func(n *node) {
172174
n.sinks = append(n.sinks, out.ch)
173175
out.nodes[i] = n
174176
}, func(n *node) {
@@ -210,11 +212,11 @@ func (b *basicBus) Emitter(evtType interface{}, opts ...event.EmitterOpt) (e eve
210212
}
211213
typ = typ.Elem()
212214

213-
err = b.withNode(typ, func(n *node) {
215+
b.withNode(typ, func(n *node) {
214216
atomic.AddInt32(&n.nEmitters, 1)
215217
n.keepLast = n.keepLast || settings.makeStateful
216218
e = &emitter{n: n, typ: typ, dropper: b.tryDropNode}
217-
}, func(_ *node) {})
219+
}, nil)
218220
return
219221
}
220222

0 commit comments

Comments
 (0)