event

package
v0.0.0-...-f8853a5 Latest Latest
Warning

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

Go to latest
Published: Oct 1, 2026 License: BSD-3-Clause Imports: 4 Imported by: 0

Documentation

Overview

Package event provides a small pub/sub store for intercepting events and waiting for a specific one to arrive.

Index

Constants

This section is empty.

Variables

View Source
var ErrAwaitTimeout = errors.New("await timeout")

Functions

This section is empty.

Types

type Awaiter

type Awaiter[T any] interface {
	// Match reports whether the event satisfies the awaiter, in which case the
	// store drops it.
	Match(event T) bool
}

Awaiter waits for a specific event.

type EventAwaiter

type EventAwaiter[T, Z any] struct {
	// contains filtered or unexported fields
}

EventAwaiter waits for an event of type T that matches a condition.

func NewEventAwaiter

func NewEventAwaiter[T, Z any](s *Store[Z], match func(T) bool) *EventAwaiter[T, Z]

NewEventAwaiter creates and registers a new EventAwaiter.

func (*EventAwaiter[T, Z]) Await

func (e *EventAwaiter[T, Z]) Await(deadline time.Duration) (T, error)

Await waits for a matching event or for the deadline to expire.

func (*EventAwaiter[T, Z]) Close

func (e *EventAwaiter[T, Z]) Close()

func (*EventAwaiter[T, Z]) Match

func (e *EventAwaiter[T, Z]) Match(event Z) bool

Match delivers the event to the waiting caller if it matches.

type RemoveHandler

type RemoveHandler func()

RemoveHandler undoes a handler registration.

type Store

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

Store manages a collection of interceptors and awaiters for events of type T.

func NewStore

func NewStore[T any](m func(T) any) *Store[T]

NewStore creates a new Store with the provided mapping function.

func (*Store[T]) AddAwaiter

func (s *Store[T]) AddAwaiter(a Awaiter[T])

AddAwaiter adds an awaiter to the store.

func (*Store[T]) AddInterceptor

func (s *Store[T]) AddInterceptor(f func(T)) RemoveHandler

AddInterceptor adds an interceptor to the store and returns the function that removes it again.

Removal is by the returned handle rather than by the callback value, because func values cannot be compared: every closure produced by one func literal shares that literal's code pointer, so the handlers HandleEvent registers are indistinguishable from each other.

func (*Store[T]) Intercept

func (s *Store[T]) Intercept(event T)

Intercept passes an event to every interceptor, then to every awaiter, dropping the awaiters that matched.

Jump to

Keyboard shortcuts

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