Documentation
¶
Overview ¶
Package delegate is an in-process, synchronous event bus for decoupling domain packages that cannot import one another (avoiding import cycles).
A producer domain dispatches an event by (domain, action); consumer domains register handlers for the pairs they care about. The producer never imports the consumers — it only knows the event's name and payload — so dependencies flow one way. Handlers run synchronously on the dispatching goroutine.
Choose delegate for internal, in-process reactions that should happen as part of the same call (and whose failure should surface to the caller). It is NOT a message broker: it offers no durability, async delivery, retries, or cross-service transport — for those, use servicekit's outbox + broker. The two are complementary: a domain change can both fire a delegate event for in-process side effects and write to the outbox for reliable external delivery.
Dispatch semantics:
- Call — stop at the first handler error and return it.
- Publish — run every handler and join their errors (errors.Join).
delegate dispatches outside any transaction: handlers run after the producer's write. When you need a domain change and its reactions to commit atomically — or to survive a crash — use servicekit's outbox instead, which writes events in the same transaction as the domain change and delivers them reliably.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Data ¶
Data is a single event passed between domains. Params are opaque bytes (JSON by convention) so the bus stays decoupled from any concrete type: the producer encodes, the consumer decodes, and neither needs the other's struct beyond the shared contract.
func MustData ¶
MustData is NewData that panics on a marshal error. Use it for static param types that cannot fail to encode (the common case), where an error would indicate a programming mistake rather than a runtime condition.
type Delegate ¶
type Delegate struct {
// contains filtered or unexported fields
}
Delegate routes events to the handlers registered for each (domain, action). It is safe for concurrent use: registration and dispatch are guarded by a mutex, and handlers are snapshotted before they run so a handler may itself register without deadlocking.
func (*Delegate) Call ¶
Call dispatches data to its handlers synchronously, stopping at and returning the first error. A pair with no handlers is a no-op. This is the right choice when a side effect's failure must abort the producer's operation.