events

package
v1.16.0 Latest Latest
Warning

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

Go to latest
Published: Oct 29, 2026 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package events provides a lightweight pub/sub event dispatcher for Nimbus.

Register listeners and fire events:

events.Listen("user.created", func(payload any) error {
    u := payload.(*models.User)
    // send welcome email…
    return nil
})
events.Dispatch("user.created", user)

Index

Constants

View Source
const (
	// Boot sequence
	ProviderRegister = "provider:register" // payload: nil — all providers registered
	PluginRegister   = "plugin:register"   // payload: nil — all plugins registered + bindings
	ProviderBoot     = "provider:boot"     // payload: nil — all providers booted
	PluginBoot       = "plugin:boot"       // payload: nil — all plugins booted

	// App lifecycle
	AppBooted   = "app:booted"   // payload: nil — boot complete, capabilities applied
	AppWarmed   = "app:warmed"   // payload: *App — warmup complete, assembled for run or inspection
	AppStarted  = "app:started"  // payload: string (port) — server listening
	AppReady    = "app:ready"    // payload: string (port) — HTTP server actively accepting traffic
	AppShutdown = "app:shutdown" // payload: os.Signal — graceful shutdown started

	// Route registration
	RouteRegistered      = "route:registered"      // payload: nil — plugin routes mounted
	MiddlewareRegistered = "middleware:registered" // payload: nil — plugin middleware merged
	// Database events
	DatabaseQuery  = "db:query"
	DatabaseInsert = "db:insert"
	DatabaseUpdate = "db:update"
	DatabaseDelete = "db:delete"
)

── Framework lifecycle events ────────────────────────────────── These are dispatched automatically by the app at each stage. Listen for them in plugins or userland code:

app.Events.Listen(events.AppBooted, func(payload any) error { … })

Variables

View Source
var Default = New()

Default is the application-wide event dispatcher.

Functions

func AfterDispatch

func AfterDispatch(fn func(event string, payload any))

AfterDispatch registers a hook called after every successful Dispatch (after all listeners). Used by Telescope; safe to register multiple times.

func Dispatch

func Dispatch(event string, payload any) error

Dispatch is a shortcut for Default.Dispatch.

func DispatchAsync

func DispatchAsync(event string, payload any)

DispatchAsync is a shortcut for Default.DispatchAsync.

func Listen

func Listen(event string, fn Listener)

Listen is a shortcut for Default.Listen.

Types

type Dispatcher

type Dispatcher struct {
	// contains filtered or unexported fields
}

Dispatcher is the event bus.

func New

func New() *Dispatcher

New returns a new Dispatcher.

func (*Dispatcher) Clear

func (d *Dispatcher) Clear(events ...string)

Clear removes all listeners for the given events, or all if none specified.

func (*Dispatcher) Dispatch

func (d *Dispatcher) Dispatch(event string, payload any) error

Dispatch fires the event synchronously. All listeners run in order. Returns the first error encountered; remaining listeners still run.

func (*Dispatcher) DispatchAsync

func (d *Dispatcher) DispatchAsync(event string, payload any)

DispatchAsync fires the event asynchronously. Each listener runs in its own goroutine; errors are logged.

func (*Dispatcher) Fake added in v1.15.0

func (d *Dispatcher) Fake(record func(event string, payload any), except ...string) (restore func())

Fake makes the dispatcher hand every event to record instead of running its listeners, until restore is called. Listeners for the events named in except still run. Used by nimbustesting.FakeEvents.

func (*Dispatcher) Has

func (d *Dispatcher) Has(event string) bool

Has returns true if the event has at least one listener.

func (*Dispatcher) Listen

func (d *Dispatcher) Listen(event string, fn Listener)

Listen registers a listener for the event.

func (*Dispatcher) ListenerCount

func (d *Dispatcher) ListenerCount(event string) int

ListenerCount returns the number of listeners for the given event.

type Event

type Event = string

Event is a type for event names (e.g. "user.created").

type Listener

type Listener func(payload any) error

Listener handles an event. Return an error to signal failure.

Jump to

Keyboard shortcuts

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