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
- Variables
- func AfterDispatch(fn func(event string, payload any))
- func Dispatch(event string, payload any) error
- func DispatchAsync(event string, payload any)
- func Listen(event string, fn Listener)
- type Dispatcher
- func (d *Dispatcher) Clear(events ...string)
- func (d *Dispatcher) Dispatch(event string, payload any) error
- func (d *Dispatcher) DispatchAsync(event string, payload any)
- func (d *Dispatcher) Fake(record func(event string, payload any), except ...string) (restore func())
- func (d *Dispatcher) Has(event string) bool
- func (d *Dispatcher) Listen(event string, fn Listener)
- func (d *Dispatcher) ListenerCount(event string) int
- type Event
- type Listener
Constants ¶
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 ¶
var Default = New()
Default is the application-wide event dispatcher.
Functions ¶
func AfterDispatch ¶
AfterDispatch registers a hook called after every successful Dispatch (after all listeners). Used by Telescope; safe to register multiple times.
func DispatchAsync ¶
DispatchAsync is a shortcut for Default.DispatchAsync.
Types ¶
type Dispatcher ¶
type Dispatcher struct {
// contains filtered or unexported fields
}
Dispatcher is the event bus.
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.