39 lines
834 B
Go
39 lines
834 B
Go
package bus
|
|
|
|
import (
|
|
"fmt"
|
|
)
|
|
|
|
// Event is used to transport user data over bus.
|
|
type Event struct {
|
|
ChannelName string
|
|
Payload interface{}
|
|
}
|
|
|
|
// eventChannel stores subscriptions for channels.
|
|
type eventChannel struct {
|
|
subscribers map[string]chan Event
|
|
}
|
|
|
|
// newEventChannel creates new eventChannel.
|
|
func newEventChannel() *eventChannel {
|
|
return &eventChannel{
|
|
subscribers: make(map[string]chan Event),
|
|
}
|
|
}
|
|
|
|
// addSubscriber adds new subscriber.
|
|
func (ch *eventChannel) addSubscriber(subName string, size int) error {
|
|
if _, ok := ch.subscribers[subName]; ok {
|
|
return fmt.Errorf("subscriber %s already exists", subName)
|
|
}
|
|
ch.subscribers[subName] = make(chan Event, size)
|
|
|
|
return nil
|
|
}
|
|
|
|
// delSubscriber removes subscriber.
|
|
func (ch *eventChannel) delSubscriber(subName string) {
|
|
delete(ch.subscribers, subName)
|
|
}
|