Documentation
¶
Overview ¶
Package hooks provides event types for the hook system.
Package hooks provides an extensible hook system for Commerce.
Hooks allow extensions and plugins to tap into various lifecycle events and modify behavior without changing core code.
Available Hooks:
- OnBootstrap: Called during application initialization
- OnServe: Called when the server starts
- OnTerminate: Called during graceful shutdown
- OnRouteSetup: Called when setting up HTTP routes
- OnModelValidate: Called before model validation
- OnModelCreate: Called before creating a model
- OnModelUpdate: Called before updating a model
- OnModelDelete: Called before deleting a model
Usage:
app.Hooks.OnModelCreate("Order").Bind(&hooks.Handler{
ID: "validateInventory",
Func: func(e *hooks.ModelEvent) error {
// Check inventory before creating order
return e.Next()
},
})
Package hooks provides tagged hooks for selective event handling.
Index ¶
- type AppEvent
- type Event
- type Handler
- type Hook
- type ModelEvent
- type Registry
- func (r *Registry) OnBootstrap() *Hook[*AppEvent]
- func (r *Registry) OnModelCreate(kind string) *Hook[*ModelEvent]
- func (r *Registry) OnModelDelete(kind string) *Hook[*ModelEvent]
- func (r *Registry) OnModelUpdate(kind string) *Hook[*ModelEvent]
- func (r *Registry) OnModelValidate(kind string) *Hook[*ModelEvent]
- func (r *Registry) OnRouteSetup() *Hook[*RouteEvent]
- func (r *Registry) OnServe() *Hook[*AppEvent]
- func (r *Registry) OnTerminate() *Hook[*AppEvent]
- func (r *Registry) TriggerBootstrap(app interface{}) error
- func (r *Registry) TriggerModelCreate(kind string, model interface{}) error
- func (r *Registry) TriggerModelDelete(kind string, model interface{}) error
- func (r *Registry) TriggerModelUpdate(kind string, model interface{}) error
- func (r *Registry) TriggerModelValidate(kind string, model interface{}, isNew bool) error
- func (r *Registry) TriggerRouteSetup(router *gin.RouterGroup) error
- func (r *Registry) TriggerServe(app interface{}) error
- func (r *Registry) TriggerTerminate(app interface{}) error
- type Resolver
- type RouteEvent
- type TaggedEvent
- type TaggedHook
- func (th *TaggedHook[T]) Bind(handler *Handler[T]) string
- func (th *TaggedHook[T]) BindFunc(fn func(T) error) string
- func (th *TaggedHook[T]) CanTriggerOn(eventTags []string) bool
- func (th *TaggedHook[T]) SetTags(tags ...string)
- func (th *TaggedHook[T]) Tags() []string
- func (th *TaggedHook[T]) Unbind(ids ...string)
- type Tagger
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type AppEvent ¶
type AppEvent struct {
App interface{}
// contains filtered or unexported fields
}
AppEvent is the base event type containing the app reference
type Event ¶
type Event struct {
// contains filtered or unexported fields
}
Event is the base event type that implements Resolver. Embed this in custom event types to get chaining support.
type Handler ¶
type Handler[T any] struct { // ID is a unique identifier for this handler ID string // Priority determines execution order (lower = earlier) Priority int // Func is the handler function Func func(T) error }
Handler represents a hook handler
type Hook ¶
type Hook[T any] struct { // contains filtered or unexported fields }
Hook is a thread-safe collection of handlers
type ModelEvent ¶
type ModelEvent struct {
App interface{}
Kind string
Model interface{}
IsNew bool
// contains filtered or unexported fields
}
ModelEvent is emitted for model operations
func (*ModelEvent) Next ¶
func (e *ModelEvent) Next() error
Next calls the next handler in the chain
type Registry ¶
type Registry struct {
// contains filtered or unexported fields
}
Registry manages all hooks for an application
func (*Registry) OnBootstrap ¶
OnBootstrap returns the bootstrap hook
func (*Registry) OnModelCreate ¶
func (r *Registry) OnModelCreate(kind string) *Hook[*ModelEvent]
OnModelCreate returns the model create hook for a kind
func (*Registry) OnModelDelete ¶
func (r *Registry) OnModelDelete(kind string) *Hook[*ModelEvent]
OnModelDelete returns the model delete hook for a kind
func (*Registry) OnModelUpdate ¶
func (r *Registry) OnModelUpdate(kind string) *Hook[*ModelEvent]
OnModelUpdate returns the model update hook for a kind
func (*Registry) OnModelValidate ¶
func (r *Registry) OnModelValidate(kind string) *Hook[*ModelEvent]
OnModelValidate returns the model validate hook for a kind
func (*Registry) OnRouteSetup ¶
func (r *Registry) OnRouteSetup() *Hook[*RouteEvent]
OnRouteSetup returns the route setup hook
func (*Registry) OnTerminate ¶
OnTerminate returns the terminate hook
func (*Registry) TriggerBootstrap ¶
TriggerBootstrap triggers the bootstrap hook
func (*Registry) TriggerModelCreate ¶
TriggerModelCreate triggers the model create hook
func (*Registry) TriggerModelDelete ¶
TriggerModelDelete triggers the model delete hook
func (*Registry) TriggerModelUpdate ¶
TriggerModelUpdate triggers the model update hook
func (*Registry) TriggerModelValidate ¶
TriggerModelValidate triggers the model validate hook
func (*Registry) TriggerRouteSetup ¶
func (r *Registry) TriggerRouteSetup(router *gin.RouterGroup) error
TriggerRouteSetup triggers the route setup hook
func (*Registry) TriggerServe ¶
TriggerServe triggers the serve hook
func (*Registry) TriggerTerminate ¶
TriggerTerminate triggers the terminate hook
type Resolver ¶
type Resolver interface {
Next() error
// contains filtered or unexported methods
}
Resolver is the interface for events that can chain to the next handler. This matches the Base hook system pattern for consistency.
type RouteEvent ¶
type RouteEvent struct {
App interface{}
Router *gin.RouterGroup
// contains filtered or unexported fields
}
RouteEvent is emitted when setting up routes
func (*RouteEvent) Next ¶
func (e *RouteEvent) Next() error
Next calls the next handler in the chain
type TaggedEvent ¶
type TaggedEvent struct {
Event
// contains filtered or unexported fields
}
TaggedEvent is an event that supports tag-based filtering.
func NewTaggedEvent ¶
func NewTaggedEvent(tags ...string) *TaggedEvent
NewTaggedEvent creates a new tagged event with the given tags.
func (*TaggedEvent) SetTags ¶
func (e *TaggedEvent) SetTags(tags ...string)
SetTags sets the event tags.
type TaggedHook ¶
type TaggedHook[T Tagger] struct { // contains filtered or unexported fields }
TaggedHook wraps a Hook and filters execution based on event tags. This matches the Base hook system pattern for consistency.
func NewTaggedHook ¶
func NewTaggedHook[T Tagger](base *Hook[T], tags ...string) *TaggedHook[T]
NewTaggedHook creates a new TaggedHook wrapping the given base hook. If no tags are provided, the hook matches all events.
func (*TaggedHook[T]) Bind ¶
func (th *TaggedHook[T]) Bind(handler *Handler[T]) string
Bind registers a handler that will only fire for matching tags.
func (*TaggedHook[T]) BindFunc ¶
func (th *TaggedHook[T]) BindFunc(fn func(T) error) string
BindFunc registers a handler function that will only fire for matching tags.
func (*TaggedHook[T]) CanTriggerOn ¶
func (th *TaggedHook[T]) CanTriggerOn(eventTags []string) bool
CanTriggerOn checks if this TaggedHook should trigger for the given event tags.
func (*TaggedHook[T]) SetTags ¶
func (th *TaggedHook[T]) SetTags(tags ...string)
SetTags updates the tags this hook filters on.
func (*TaggedHook[T]) Tags ¶
func (th *TaggedHook[T]) Tags() []string
Tags returns the current filter tags.
func (*TaggedHook[T]) Unbind ¶
func (th *TaggedHook[T]) Unbind(ids ...string)
Unbind removes handlers by ID.