diff options
| author | 2026-01-24 17:56:58 +0300 | |
|---|---|---|
| committer | 2026-01-24 17:56:58 +0300 | |
| commit | 9fb220096a8a50bd89f0d7578d572629531f8141 (patch) | |
| tree | d454c0efc1268cc0c15384d2a4585312562a1c0c /bus.go | |
| parent | v1.0.0 (diff) | |
| download | eventbus-9fb220096a8a50bd89f0d7578d572629531f8141.tar.gz eventbus-9fb220096a8a50bd89f0d7578d572629531f8141.tar.bz2 eventbus-9fb220096a8a50bd89f0d7578d572629531f8141.tar.xz eventbus-9fb220096a8a50bd89f0d7578d572629531f8141.zip | |
v1.0.1v1.0.1
Diffstat (limited to 'bus.go')
| -rw-r--r-- | bus.go | 20 |
1 files changed, 13 insertions, 7 deletions
@@ -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) |
