Documentation
¶
Index ¶
- type Bus
- func (b *Bus[T]) On(topic string, callback func(event T)) func()
- func (b *Bus[T]) Once(topic string, callback func(event T)) func()
- func (b *Bus[T]) Publish(topic string, event T)
- func (b *Bus[T]) Subscribe(topic string) (<-chan T, func())
- func (b *Bus[T]) SubscribeOnce(topic string) (<-chan T, func())
- type Serializer
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Bus ¶
type Bus[T any] struct { // contains filtered or unexported fields }
Bus can be used to implement event-driven architectures in Golang. Each bus can have multiple subscribers to different topics.
func (*Bus[T]) On ¶
On executes the given callback whenever an event is sent to the given topic, Also an unsubscribe functions is returned that can be used to cancel the subscription.
func (*Bus[T]) Once ¶
Once executes the given callback as soon as receiving the first event on the given topic. After one callback execution, Callback will not be called again. Also, an unsubscribe functions is returned that can be used to cancel the subscription before receiving any events.
func (*Bus[T]) Publish ¶
Publish sends the given event to all the subscribers of the given topic. Publish is non-blocking and doesn't wait for subscribers. Although this is non-blocking, event ordering is guaranteed in a FIFO manner. Each subscriber gets events in the same order they were published. A slow subscriber doesn't block other subscribers, Ordering is handled for each subscriber separately and different subscribers on the same topic can be reading different events at a given time.
func (*Bus[T]) Subscribe ¶
Subscribe returns a channel for listening on a given topic events, Also an unsubscribe functions is returned that can be used to cancel the subscription.
func (*Bus[T]) SubscribeOnce ¶
SubscribeOnce returns a channel for listening on a given topic events, After the first event, subscription is automatically cancelled. Also, an unsubscribe functions is returned that can be used to cancel the subscription before receiving any events.
type Serializer ¶ added in v0.1.1
type Serializer struct {
// contains filtered or unexported fields
}
Serializer can be used to serialize execution of some goroutines according to their sequence number. If goroutine A with a higher sequence number gets scheduled before goroutine B with a lower sequence number, A will be queued and will be executed after execution of B.
func NewSerializer ¶ added in v0.1.1
func NewSerializer(lastExecutedSequence uint64) *Serializer
NewSerializer creates a new serializer.
func (*Serializer) Execute ¶ added in v0.1.1
func (s *Serializer) Execute(cb callback, sequence uint64)
Execute runs the given callbacks serially according to each callback sequence number.