44 lines
1.1 KiB
Go
Raw Normal View History

2019-06-03 09:32:43 +00:00
package arn
2021-11-20 11:52:08 +00:00
import (
"github.com/aerogo/aero/event"
)
2019-06-03 09:32:43 +00:00
// AddEventStream adds an event stream to the given user.
2021-11-20 11:52:08 +00:00
func (user *User) AddEventStream(stream *event.Stream) {
2019-06-03 09:32:43 +00:00
user.eventStreams.Lock()
defer user.eventStreams.Unlock()
user.eventStreams.value = append(user.eventStreams.value, stream)
}
// RemoveEventStream removes an event stream from the given user
// and returns true if it was removed, otherwise false.
2021-11-20 11:52:08 +00:00
func (user *User) RemoveEventStream(stream *event.Stream) bool {
2019-06-03 09:32:43 +00:00
user.eventStreams.Lock()
defer user.eventStreams.Unlock()
for index, element := range user.eventStreams.value {
if element == stream {
user.eventStreams.value = append(user.eventStreams.value[:index], user.eventStreams.value[index+1:]...)
return true
}
}
return false
}
// BroadcastEvent sends the given event to all event streams for the given user.
2021-11-20 11:52:08 +00:00
func (user *User) BroadcastEvent(evt *event.Event) {
2019-06-03 09:32:43 +00:00
user.eventStreams.Lock()
defer user.eventStreams.Unlock()
for _, stream := range user.eventStreams.value {
// Non-blocking send because we don't know if our listeners are still active.
select {
2021-11-20 11:52:08 +00:00
case stream.Events <- evt:
2019-06-03 09:32:43 +00:00
default:
}
}
}