Documentation
¶
Overview ¶
Package pubsub provides a simple publisher/subscriber system that is type-safe and generic. Events are published and subscribed to according to type. Due to the nature of golangs type system, some types (slices, maps, and funcs) do not work with this system.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Publish ¶
Publish will send the value val into the global event scope. If the context is canceled, the value may not be sent to all subscribers.
func PublishToScope ¶
func PublishToScope[T any](ctx context.Context, e *EventScope, val T)
PublishToScope will send the value val on the specified event scope. If the context is canceled, the value may not be sent to all subscribers.
Types ¶
type EventScope ¶
type EventScope struct {
// contains filtered or unexported fields
}
EventScope describes a scope where publishers and subscribers communicate with each other. Data published to an event scope will not be seen by subscribers listening on a differnt event scope. Multiple event scopes should only be used when you need to publish data with the same type but different handlers.
var ( // Global is the default event scope. Publish and SubscribeTo use this event scope. Global *EventScope )
func NewEventScope ¶
func NewEventScope() *EventScope
type UnsubFn ¶
type UnsubFn func()
UnSubFn is a function which unsubscribes from the data type. Calling this will close the channel returned by SubscribeTo/SubscribeToScope.
func SubscribeTo ¶
SubscribeTo creates a channel to listen for events of type T. When listeners are finished processing these events, the UnsubFn should be called.
func SubscribeToScope ¶
func SubscribeToScope[T any](ctx context.Context, e *EventScope) (chan T, UnsubFn)
SubscribeTo creates a channel to listen for events of type T published on the provided event scope. When listeners are finished processing these events, the UnsubFn should be called.