a06f331eb8
CI / benchmark (push) Has been skipped
install-script / posix-syntax (push) Successful in 6m1s
CI / build-onnx (push) Failing after 6m43s
init-smoke / dry-run (push) Failing after 15m57s
security / govulncheck (push) Has been cancelled
security / trivy-fs (push) Has been cancelled
CI / test (1.26, ubuntu-latest) (push) Has been cancelled
Scorecard supply-chain security / Scorecard analysis (push) Has been cancelled
CI / test (1.26, macos-latest) (push) Has been cancelled
CI / build-windows (push) Has been cancelled
CI / lint (push) Has been cancelled
install-script / powershell-syntax (push) Has been cancelled
install-script / install (macos-14) (push) Has been cancelled
install-script / install (ubuntu-latest) (push) Has been cancelled
50 lines
1022 B
Go
50 lines
1022 B
Go
package ledger
|
|
|
|
// EventKind enumerates ledger lifecycle events.
|
|
type EventKind int
|
|
|
|
const (
|
|
EventAccountOpened EventKind = iota
|
|
EventPosted
|
|
EventPostFailed
|
|
)
|
|
|
|
// Event is a ledger lifecycle notification.
|
|
type Event struct {
|
|
Kind EventKind
|
|
Subject string
|
|
}
|
|
|
|
// Listener receives ledger events.
|
|
type Listener interface {
|
|
Notify(ev Event)
|
|
}
|
|
|
|
// ListenerFunc adapts a plain function to the Listener interface.
|
|
type ListenerFunc func(ev Event)
|
|
|
|
// Notify invokes the underlying function.
|
|
func (f ListenerFunc) Notify(ev Event) { f(ev) }
|
|
|
|
// Dispatcher fans events out to registered listeners.
|
|
type Dispatcher struct {
|
|
listeners []Listener
|
|
}
|
|
|
|
// NewDispatcher returns an empty dispatcher.
|
|
func NewDispatcher() *Dispatcher {
|
|
return &Dispatcher{}
|
|
}
|
|
|
|
// Subscribe adds a listener.
|
|
func (d *Dispatcher) Subscribe(l Listener) {
|
|
d.listeners = append(d.listeners, l)
|
|
}
|
|
|
|
// Emit delivers ev to every listener in registration order.
|
|
func (d *Dispatcher) Emit(ev Event) {
|
|
for _, l := range d.listeners {
|
|
l.Notify(ev)
|
|
}
|
|
}
|