eventbus

package
v0.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 9, 2026 License: MIT Imports: 1 Imported by: 0

Documentation

Overview

Package eventbus provides a typed, in-process event bus modeled on Cordis. Emit broadcasts sequentially, Bail stops at the first matching result, and Serial collects results in registration order. Waterfall dispatch lives in pkg/waterfall. Retain and call the disposer returned by On during cleanup.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Bus

type Bus[T any, R any] struct {
	// contains filtered or unexported fields
}

Bus is a concurrency-safe event channel with ordered listeners. The zero value is ready to use. Listener updates copy the registration list so dispatch can read a stable snapshot without allocating. This favors workloads that dispatch more often than they register or remove listeners.

func New

func New[T any, R any]() *Bus[T, R]

New creates an empty event bus.

func (*Bus[T, R]) Bail

func (b *Bus[T, R]) Bail(payload T, shouldShort func(R) bool) (R, bool)

Bail returns the first result for which shouldShort returns true. If no result matches, it returns the zero value and false.

func (*Bus[T, R]) Emit

func (b *Bus[T, R]) Emit(payload T)

Emit invokes all listeners in registration order and ignores their results.

func (*Bus[T, R]) Len

func (b *Bus[T, R]) Len() int

Len returns the number of registered listeners.

func (*Bus[T, R]) On

func (b *Bus[T, R]) On(fn Listener[T, R]) func()

On registers a listener and returns an idempotent disposer. The listener remains registered until the disposer is called.

func (*Bus[T, R]) Serial

func (b *Bus[T, R]) Serial(payload T) []R

Serial invokes listeners sequentially and returns results in registration order.

type Listener

type Listener[T any, R any] func(payload T) R

Listener receives a payload and returns a result. Emit ignores the result; Bail and Serial use it for short-circuiting and aggregation, respectively.

type Signal

type Signal[T any] struct {
	// contains filtered or unexported fields
}

Signal is a notification bus whose listeners do not return results.

func NewSignal

func NewSignal[T any]() *Signal[T]

NewSignal creates a notification bus.

func (*Signal[T]) Emit

func (s *Signal[T]) Emit(payload T)

Emit broadcasts a notification.

func (*Signal[T]) Len

func (s *Signal[T]) Len() int

Len returns the number of notification listeners.

func (*Signal[T]) On

func (s *Signal[T]) On(fn func(T)) func()

On registers a notification listener and returns its disposer.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL