mirror of
https://github.com/libp2p/go-eventbus.git
synced 2025-03-25 12:10:06 +08:00
Use reflect.Type in node map
This commit is contained in:
parent
8b50ba1149
commit
71ffb0ebf1
20
basic.go
20
basic.go
@ -13,24 +13,22 @@ import (
|
||||
|
||||
type bus struct {
|
||||
lk sync.Mutex
|
||||
nodes map[string]*node
|
||||
nodes map[reflect.Type]*node
|
||||
}
|
||||
|
||||
func NewBus() Bus {
|
||||
return &bus{
|
||||
nodes: map[string]*node{},
|
||||
nodes: map[reflect.Type]*node{},
|
||||
}
|
||||
}
|
||||
|
||||
func (b *bus) withNode(typ reflect.Type, cb func(*node), async func(*node)) error {
|
||||
path := typePath(typ)
|
||||
|
||||
b.lk.Lock()
|
||||
|
||||
n, ok := b.nodes[path]
|
||||
n, ok := b.nodes[typ]
|
||||
if !ok {
|
||||
n = newNode(typ)
|
||||
b.nodes[path] = n
|
||||
b.nodes[typ] = n
|
||||
}
|
||||
|
||||
n.lk.Lock()
|
||||
@ -47,10 +45,8 @@ func (b *bus) withNode(typ reflect.Type, cb func(*node), async func(*node)) erro
|
||||
}
|
||||
|
||||
func (b *bus) tryDropNode(typ reflect.Type) {
|
||||
path := typePath(typ)
|
||||
|
||||
b.lk.Lock()
|
||||
n, ok := b.nodes[path]
|
||||
n, ok := b.nodes[typ]
|
||||
if !ok { // already dropped
|
||||
b.lk.Unlock()
|
||||
return
|
||||
@ -64,7 +60,7 @@ func (b *bus) tryDropNode(typ reflect.Type) {
|
||||
}
|
||||
n.lk.Unlock()
|
||||
|
||||
delete(b.nodes, path)
|
||||
delete(b.nodes, typ)
|
||||
b.lk.Unlock()
|
||||
}
|
||||
|
||||
@ -201,8 +197,4 @@ func (n *node) emit(event interface{}) {
|
||||
///////////////////////
|
||||
// UTILS
|
||||
|
||||
func typePath(t reflect.Type) string {
|
||||
return t.PkgPath() + "/" + t.String()
|
||||
}
|
||||
|
||||
var _ Bus = &bus{}
|
||||
|
Loading…
Reference in New Issue
Block a user