Documentation
¶
Index ¶
- Variables
- func Invoke[Tx Transaction, I any, O any](ctx context.Context, hub *Hub[Tx], op Operation[Tx, I, O], input *I) (*O, error)
- func InvokeTx[Tx Transaction, I any, O any](ctx context.Context, hub *Hub[Tx], op TxOperation[Tx, I, O], input *I) (*O, error)
- type AfterFunc
- type Event
- type Hub
- type OpContext
- type Operation
- type Transaction
- type TransactionProvider
- type TxOperation
Constants ¶
This section is empty.
Variables ¶
var ( ErrInvalidState = errors.New("invalid state") ErrEventHandlerCoercionFailed = errors.New("failed to create event handler") )
var ErrRecovered = errors.New("operation recovered from panic")
Functions ¶
func Invoke ¶
func Invoke[Tx Transaction, I any, O any](ctx context.Context, hub *Hub[Tx], op Operation[Tx, I, O], input *I) (*O, error)
InvokeTx() executes the supplied operation with the given input parameters.
The supplied *Hub is used as a transaction provider and event dispatcher.
Returns the operation's output on success, or error on failure.
func InvokeTx ¶
func InvokeTx[Tx Transaction, I any, O any](ctx context.Context, hub *Hub[Tx], op TxOperation[Tx, I, O], input *I) (*O, error)
InvokeTx() begins a transaction then executes the supplied operation with the given input parameters. Prefer InvokeTx() over Invoke() to reduce boilerplate if the operation is guaranteed to use a transaction.
The supplied *Hub is used as a transaction provider and event dispatcher.
Returns the operation's output on success, or error on failure.
Types ¶
type AfterFunc ¶
type AfterFunc[Tx Transaction] func(*OpContext[Tx])
AfterFunc is a function that runs after an operation has successfully completed.
type Hub ¶
type Hub[Tx Transaction] struct { // contains filtered or unexported fields }
A Hub is the central object through which operations are invoked, comprising a transaction provider, and a registry of event handlers.
Once a Hub is configured, use the package-level Invoke() function to invoke operations.
func NewHub ¶
func NewHub[Tx Transaction](transactionProvider TransactionProvider[Tx]) *Hub[Tx]
NewHub() returns a hub configured with a transaction provider.
func (*Hub[Tx]) BeginOperation ¶
Begin a new operation and returns its context. User code will usually not call BeginOperation directly; use Invoke().
func (*Hub[Tx]) RegisterEventHandler ¶
RegisterEventHandler() registers a handler to handle events whose type matches reflect.TypeOf(event).
The event handler hnd must be a function conforming to one of the following signatures, wherein *OpContext[Tx] must be assignable to C (this includes context.Context), and the event type must be assignable to E:
func(E) func(C, E) func(E) error func(C, E) error
Event handlers are invoked *after* the operation has returned, but before the transaction is committed. If an event handler returns an error, the transaction aborts and is rolled back - this is by design; event handlers are not intended for "fire and forget" use - use AfterFunc() for that.
type OpContext ¶
type OpContext[T Transaction] struct { context.Context // contains filtered or unexported fields }
OpContext represents the context of in-process operation including its current state, associated Hub, and active transaction (if any).
An OpContext is responsible for managing the lifecycle of its associated operation, including commit/rollback handling, event dispatch, and AfterFunc invocation. This functionality is not exposed publicly, instead, coordination is delegated to Invoke().
OpContext wraps context.Context so can be passed to any method that expects one of these.
func (*OpContext[T]) AfterFunc ¶
Register a function to be invoked upon completion of the operation. The callback is invoked after the transaction (if any) is committed. After callbacks can be registered by the main operation, as well as any triggered event handlers.
type Operation ¶
type Operation[Tx Transaction, I any, O any] func(ctx *OpContext[Tx], input *I) (*O, error)
Operation represents a single operation with defined input/output parameters.
type Transaction ¶
type TransactionProvider ¶
type TransactionProvider[Tx Transaction] func(context.Context) (Tx, error)
TransactionProvider is a transaction factory
type TxOperation ¶
type TxOperation[Tx Transaction, I any, O any] func(ctx *OpContext[Tx], tx Tx, input *I) (*O, error)
TxOperation represents a single operation with an implied transaction, and defined input/output parameters. Use TxOperation to reduce boilerplate if your operation is guaranteed to start a transaction.