Documentation
¶
Overview ¶
Package resourceeventbus is a tiny, type-safe wrapper around an in-process publish/subscribe bus. It exposes:
- Bus: a minimal pub/sub interface (Publish, Subscribe[Async|Once], Unsubscribe, HasCallback, WaitAsync).
- A process-wide singleton accessible via GetBus / SetBus.
- TypedEvent[T]: a generic wrapper that pins a topic to a payload type so callers don't have to deal with stringly-typed topics or any.
The default implementation wraps github.com/asaskevich/EventBus. The underlying library is interchangeable via SetBus(), which is useful in tests.
Index ¶
- func SetBus(b Bus)
- type Bus
- type TypedEvent
- func (e *TypedEvent[T]) HasCallback() bool
- func (e *TypedEvent[T]) Publish(data T)
- func (e *TypedEvent[T]) PublishAsync(data T)
- func (e *TypedEvent[T]) Subscribe(handler func(T)) error
- func (e *TypedEvent[T]) SubscribeAsync(handler func(T), transactional bool) error
- func (e *TypedEvent[T]) SubscribeOnce(handler func(T)) error
- func (e *TypedEvent[T]) SubscribeOnceAsync(handler func(T)) error
- func (e *TypedEvent[T]) Unsubscribe(handler func(T)) error
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Bus ¶
type Bus interface {
Publish(topic string, args ...any)
Subscribe(topic string, fn any) error
SubscribeAsync(topic string, fn any, transactional bool) error
SubscribeOnce(topic string, fn any) error
SubscribeOnceAsync(topic string, fn any) error
Unsubscribe(topic string, handler any) error
HasCallback(topic string) bool
WaitAsync()
}
Bus is the minimal pub/sub surface this library promises. It matches the shape of asaskevich/EventBus on purpose so consumers can swap in any compatible implementation.
type TypedEvent ¶
TypedEvent ties a topic string to a single payload type, so publishers and subscribers don't have to remember the wire shape themselves.
Typical usage:
type UserCreated struct{ ID uint }
var UserCreatedEvent = &resourceeventbus.TypedEvent[UserCreated]{Topic: "user.created"}
// publisher
UserCreatedEvent.PublishAsync(UserCreated{ID: 42})
// subscriber
UserCreatedEvent.SubscribeAsync(func(p UserCreated) { ... }, false)
func (*TypedEvent[T]) HasCallback ¶
func (e *TypedEvent[T]) HasCallback() bool
HasCallback reports whether at least one subscriber is attached.
func (*TypedEvent[T]) Publish ¶
func (e *TypedEvent[T]) Publish(data T)
Publish fires the event synchronously on the configured topic.
func (*TypedEvent[T]) PublishAsync ¶
func (e *TypedEvent[T]) PublishAsync(data T)
PublishAsync fires the event from a fresh goroutine. Useful when the publishing call site shouldn't block on subscriber work.
func (*TypedEvent[T]) Subscribe ¶
func (e *TypedEvent[T]) Subscribe(handler func(T)) error
Subscribe attaches a sync handler. Errors come from the underlying bus (typically: handler is not a function with the right signature).
func (*TypedEvent[T]) SubscribeAsync ¶
func (e *TypedEvent[T]) SubscribeAsync(handler func(T), transactional bool) error
SubscribeAsync attaches an async handler. transactional=true serializes async handlers; false runs them concurrently.
func (*TypedEvent[T]) SubscribeOnce ¶
func (e *TypedEvent[T]) SubscribeOnce(handler func(T)) error
SubscribeOnce attaches a handler that fires exactly once.
func (*TypedEvent[T]) SubscribeOnceAsync ¶
func (e *TypedEvent[T]) SubscribeOnceAsync(handler func(T)) error
SubscribeOnceAsync attaches an async handler that fires exactly once.
func (*TypedEvent[T]) Unsubscribe ¶
func (e *TypedEvent[T]) Unsubscribe(handler func(T)) error
Unsubscribe removes a previously registered handler. The handler reference must match what was passed to Subscribe* (functions compare by identity).
Directories
¶
| Path | Synopsis |
|---|---|
|
示例 DSL 文件.
|
示例 DSL 文件. |
|
cmd
|
|
|
eventbus-gen
command
eventbus-gen 把 DSL 文件 (一个声明 EventsDSL() []generator.ResourceDef 的 .go) 转成两份强类型源:
|
eventbus-gen 把 DSL 文件 (一个声明 EventsDSL() []generator.ResourceDef 的 .go) 转成两份强类型源: |
|
Package generator 提供 eventbus 的 DSL 类型与代码生成器.
|
Package generator 提供 eventbus 的 DSL 类型与代码生成器. |