Documentation
¶
Overview ¶
Package events provides a type-based event bus API for Go applications.
Index ¶
- Variables
- type API
- type Bus
- func (b *Bus) Driver() eventscore.Driver
- func (b *Bus) Publish(event any) error
- func (b *Bus) PublishContext(ctx context.Context, event any) error
- func (b *Bus) Ready() error
- func (b *Bus) ReadyContext(ctx context.Context) error
- func (b *Bus) Subscribe(handler any) (Subscription, error)
- func (b *Bus) SubscribeContext(ctx context.Context, handler any) (Subscription, error)
- type Codec
- type Config
- type Fake
- type Option
- type Record
- type Subscription
- type TopicEvent
Constants ¶
This section is empty.
Variables ¶
var ( // ErrInvalidHandler indicates a subscribe handler has an unsupported shape. ErrInvalidHandler = errors.New("events: invalid handler") // ErrNilEvent indicates a publish call received a nil event. ErrNilEvent = errors.New("events: nil event") // ErrEmptyTopic indicates topic resolution produced an empty topic. ErrEmptyTopic = errors.New("events: empty topic") )
Functions ¶
This section is empty.
Types ¶
type API ¶
type API interface {
// Driver reports the active bus backend.
// @group Core
//
// Example: inspect the active backend through the interface
//
// api, _ := events.NewSync()
// var bus events.API = api
// fmt.Println(bus.Driver())
// // Output: sync
Driver() eventscore.Driver
// Ready performs a background-context readiness check.
// @group Core
//
// Example: check readiness through the interface
//
// api, _ := events.NewSync()
// var bus events.API = api
// fmt.Println(bus.Ready() == nil)
// // Output: true
Ready() error
// ReadyContext performs a readiness check with the provided context.
// @group Core
//
// Example: check readiness with a caller context
//
// api, _ := events.NewSync()
// var bus events.API = api
// fmt.Println(bus.ReadyContext(context.Background()) == nil)
// // Output: true
ReadyContext(ctx context.Context) error
// Publish dispatches an event with the background context.
// @group Core
//
// Example: publish a typed event through the interface
//
// type UserCreated struct {
// ID string `json:"id"`
// }
//
// api, _ := events.NewSync()
// var bus events.API = api
// _, _ = bus.Subscribe(func(event UserCreated) {
// fmt.Println(event.ID)
// })
// _ = bus.Publish(UserCreated{ID: "123"})
// // Output: 123
Publish(event any) error
// PublishContext dispatches an event with the provided context.
// @group Core
//
// Example: publish with a caller context
//
// type UserCreated struct {
// ID string `json:"id"`
// }
//
// api, _ := events.NewSync()
// var bus events.API = api
// _, _ = bus.Subscribe(func(ctx context.Context, event UserCreated) error {
// fmt.Println(event.ID, ctx != nil)
// return nil
// })
// _ = bus.PublishContext(context.Background(), UserCreated{ID: "123"})
// // Output: 123 true
PublishContext(ctx context.Context, event any) error
// Subscribe registers a typed handler using the background context.
// @group Core
//
// Example: subscribe through the interface
//
// type UserCreated struct {
// ID string `json:"id"`
// }
//
// api, _ := events.NewSync()
// var bus events.API = api
// sub, _ := bus.Subscribe(func(ctx context.Context, event UserCreated) error {
// _ = ctx
// _ = event
// return nil
// })
// defer sub.Close()
Subscribe(handler any) (Subscription, error)
// SubscribeContext registers a typed handler with the provided context.
// @group Core
//
// Example: subscribe with a caller context through the interface
//
// type UserCreated struct {
// ID string `json:"id"`
// }
//
// api, _ := events.NewSync()
// var bus events.API = api
// sub, _ := bus.SubscribeContext(context.Background(), func(ctx context.Context, event UserCreated) error {
// _ = ctx
// _ = event
// return nil
// })
// defer sub.Close()
SubscribeContext(ctx context.Context, handler any) (Subscription, error)
}
API is the root application-facing bus contract. @group Core
Example: keep an API-typed bus reference
api, _ := events.NewSync() var bus events.API = api fmt.Println(bus.Driver()) // Output: sync
type Bus ¶
type Bus struct {
// contains filtered or unexported fields
}
Bus is the root event bus implementation. @group Bus
func New ¶
New constructs a root bus for the requested driver. @group Construction
Example: construct a bus from config
bus, _ := events.New(events.Config{Driver: "sync"})
fmt.Println(bus.Driver())
// Output: sync
func NewNull ¶
NewNull constructs the root null bus. @group Construction
Example: construct a null bus
bus, _ := events.NewNull() fmt.Println(bus.Driver()) // Output: null
func NewSync ¶
NewSync constructs the root sync bus. @group Construction
Example: construct a sync bus
bus, _ := events.NewSync() fmt.Println(bus.Driver()) // Output: sync
func (*Bus) Driver ¶
func (b *Bus) Driver() eventscore.Driver
Driver reports the active backend. @group Bus
Example: inspect the active backend
bus, _ := events.NewSync() fmt.Println(bus.Driver()) // Output: sync
func (*Bus) Publish ¶
Publish publishes an event using the background context. @group Publish
Example: publish a typed event
type UserCreated struct {
ID string `json:"id"`
}
bus, _ := events.NewSync()
_, _ = bus.Subscribe(func(event UserCreated) {
fmt.Println(event.ID)
})
_ = bus.Publish(UserCreated{ID: "123"})
// Output: 123
func (*Bus) PublishContext ¶
PublishContext publishes an event using the configured codec and dispatch flow. @group Publish
Example: publish with a caller context
type UserCreated struct {
ID string `json:"id"`
}
bus, _ := events.NewSync()
_, _ = bus.Subscribe(func(ctx context.Context, event UserCreated) error {
fmt.Println(event.ID, ctx != nil)
return nil
})
_ = bus.PublishContext(context.Background(), UserCreated{ID: "123"})
// Output: 123 true
func (*Bus) Ready ¶
Ready reports whether the bus is ready. @group Bus
Example: check readiness
bus, _ := events.NewSync() fmt.Println(bus.Ready() == nil) // Output: true
func (*Bus) ReadyContext ¶
ReadyContext reports whether the bus is ready. @group Bus
Example: check readiness with a caller context
bus, _ := events.NewSync() fmt.Println(bus.ReadyContext(context.Background()) == nil) // Output: true
func (*Bus) Subscribe ¶
func (b *Bus) Subscribe(handler any) (Subscription, error)
Subscribe registers a handler using the background context. @group Subscribe
Example: subscribe to a typed event
type UserCreated struct {
ID string `json:"id"`
}
bus, _ := events.NewSync()
sub, _ := bus.Subscribe(func(ctx context.Context, event UserCreated) error {
fmt.Println(event.ID)
return nil
})
defer sub.Close()
_ = bus.Publish(UserCreated{ID: "123"})
// Output: 123
func (*Bus) SubscribeContext ¶
SubscribeContext registers a typed handler. @group Subscribe
Example: subscribe with a caller context
type UserCreated struct {
ID string `json:"id"`
}
bus, _ := events.NewSync()
sub, _ := bus.SubscribeContext(context.Background(), func(ctx context.Context, event UserCreated) error {
fmt.Println(event.ID, ctx != nil)
return nil
})
defer sub.Close()
_ = bus.PublishContext(context.Background(), UserCreated{ID: "123"})
// Output: 123 true
type Codec ¶
Codec marshals and unmarshals event payloads. @group Options
Example: define a custom codec
var codec events.Codec fmt.Println(codec == nil) // Output: true
type Config ¶
type Config struct {
// Driver selects the root bus backend.
Driver eventscore.Driver
// Codec overrides the default JSON codec.
Codec Codec
// Transport installs a driver-backed transport for distributed delivery.
Transport eventscore.DriverAPI
}
Config configures root bus construction. @group Config
Example: define bus construction config
cfg := events.Config{Driver: eventscore.DriverSync}
_ = cfg
Example: define bus construction config with all fields
cfg := events.Config{
Driver: eventscore.DriverSync, // default: "sync" when empty and no Transport is provided
Codec: nil, // default: nil uses the built-in JSON codec
Transport: nil, // default: nil keeps dispatch in-process
}
_ = cfg
type Fake ¶
type Fake struct {
// contains filtered or unexported fields
}
Fake provides a root-package testing helper that records published events. @group Testing
Example: keep a fake for assertions in tests
fake := events.NewFake() fmt.Println(fake.Count()) // Output: 0
func NewFake ¶
func NewFake() *Fake
NewFake creates a new fake event harness backed by the root sync bus. @group Testing
Example: construct a recording fake
fake := events.NewFake() fmt.Println(fake.Count()) // Output: 0
func (*Fake) Bus ¶
Bus returns the wrapped API to inject into code under test. @group Testing
Example: inject the fake bus into application code
fake := events.NewFake() bus := fake.Bus() fmt.Println(bus.Ready() == nil) // Output: true
func (*Fake) Count ¶
Count returns the total number of recorded publishes. @group Testing
Example: count recorded publishes
type UserCreated struct {
ID string `json:"id"`
}
fake := events.NewFake()
_ = fake.Bus().Publish(UserCreated{ID: "123"})
fmt.Println(fake.Count())
// Output: 1
type Option ¶
type Option func(*options)
Option configures root bus behavior. @group Options
Example: keep an option for later bus construction
opt := events.WithCodec(nil) fmt.Println(opt != nil) // Output: true
type Record ¶
type Record struct {
Event any
}
Record captures one published event observed by a Fake bus. @group Testing
Example: inspect a recorded event
type UserCreated struct {
ID string `json:"id"`
}
record := events.Record{Event: UserCreated{ID: "123"}}
fmt.Printf("%T\n", record.Event)
// Output: main.UserCreated
type Subscription ¶
type Subscription = eventscore.Subscription
Subscription releases a subscription when closed. @group Subscribe
Example: unsubscribe from a typed event
type UserCreated struct {
ID string `json:"id"`
}
bus, _ := events.NewSync()
sub, _ := bus.Subscribe(func(event UserCreated) {
fmt.Println("received", event.ID)
})
_ = bus.Publish(UserCreated{ID: "123"})
_ = sub.Close()
_ = bus.Publish(UserCreated{ID: "456"})
// Output: received 123
type TopicEvent ¶
type TopicEvent interface {
Topic() string
}
TopicEvent overrides the derived topic for an event. @group Publish
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
driver
|
|
|
gcppubsubevents
module
|
|
|
kafkaevents
module
|
|
|
natsevents
module
|
|
|
redisevents
module
|
|
|
eventscore
module
|
|
|
eventstest
module
|