alpha version
This commit is contained in:
38
bus/events.go
Normal file
38
bus/events.go
Normal file
@ -0,0 +1,38 @@
|
||||
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)
|
||||
}
|
Reference in New Issue
Block a user