2019-11-16 16:06:34 +01:00
|
|
|
package event
|
|
|
|
|
|
|
|
import (
|
2021-10-06 15:27:17 +02:00
|
|
|
"strings"
|
|
|
|
|
2019-11-16 16:06:34 +01:00
|
|
|
"github.com/leandro-lugaresi/hub"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Hub = hub.Hub
|
|
|
|
type Data = hub.Fields
|
|
|
|
type Message = hub.Message
|
2019-12-02 00:30:58 +01:00
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
const TopicSep = "."
|
|
|
|
|
2020-07-19 16:48:04 +02:00
|
|
|
var channelCap = 100
|
2019-11-16 16:06:34 +01:00
|
|
|
var sharedHub = NewHub()
|
|
|
|
|
2019-12-11 14:10:20 +01:00
|
|
|
func NewHub() *Hub {
|
2019-11-16 16:06:34 +01:00
|
|
|
return hub.New()
|
|
|
|
}
|
|
|
|
|
|
|
|
func SharedHub() *Hub {
|
|
|
|
return sharedHub
|
|
|
|
}
|
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
// Subscribe creates a topic subscription and returns i
|
|
|
|
func Subscribe(topics ...string) hub.Subscription {
|
|
|
|
return SharedHub().NonBlockingSubscribe(channelCap, topics...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unsubscribe deletes the subscription of a topic.
|
|
|
|
func Unsubscribe(s hub.Subscription) {
|
|
|
|
SharedHub().Unsubscribe(s)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Topic splits the topic name into the channel and event names.
|
|
|
|
func Topic(topic string) (ch, ev string) {
|
|
|
|
ch, ev, _ = strings.Cut(topic, TopicSep)
|
|
|
|
return ch, ev
|
|
|
|
}
|