aboutsummaryrefslogtreecommitdiff
path: root/bus.go
diff options
context:
space:
mode:
Diffstat (limited to 'bus.go')
-rw-r--r--bus.go20
1 files changed, 13 insertions, 7 deletions
diff --git a/bus.go b/bus.go
index 7ce4860..9ab1980 100644
--- a/bus.go
+++ b/bus.go
@@ -10,7 +10,7 @@ const (
defaultCapacity = 32
)
-type bus struct {
+type Bus struct {
noCopy
listeners node[Listener]
@@ -18,8 +18,8 @@ type bus struct {
wildcard string
}
-func New(opts ...Opt) *bus {
- b := &bus{
+func New(opts ...Opt) *Bus {
+ b := &Bus{
nameSeparator: ".",
wildcard: "*",
}
@@ -44,25 +44,31 @@ type Event interface {
Event() string
}
-func (b *bus) Subscribe(path string) Listener {
+// Subscribe to an event at the specified path. Returns a listener channel for
+// receiving events.
+func (b *Bus) Subscribe(path string) Listener {
splitedPath := strings.Split(path, b.nameSeparator)
ch := make(Listener, 1)
b.listeners.Put(splitedPath, ch)
return ch
}
-func (b *bus) Unsubscribe(l Listener) {
+// Unsubscribe from an event and closes the listener channel.
+func (b *Bus) Unsubscribe(l Listener) {
close(l)
b.listeners.Remove(l)
}
-func (b *bus) Close() {
+// Close the event bus and all listener channels.
+func (b *Bus) Close() {
b.listeners.Clear(func(value Listener) {
close(value)
})
}
-func (b *bus) Fire(ev Event) {
+// Fire an event to all subscribers who are subscribed to the corresponding
+// event path.
+func (b *Bus) Fire(ev Event) {
splitedEventName := strings.Split(ev.Event(), b.nameSeparator)
listeners := b.listeners.Get(splitedEventName, b.wildcard)