hooks

package
v1.39.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 21, 2026 License: MIT Imports: 3 Imported by: 0

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

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

func (*AppEvent) Next

func (e *AppEvent) Next() error

Next calls the next handler in the chain

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.

func (*Event) Next

func (e *Event) Next() error

Next calls the next handler in the chain.

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

func NewHook

func NewHook[T any]() *Hook[T]

NewHook creates a new hook

func (*Hook[T]) Bind

func (h *Hook[T]) Bind(handler *Handler[T])

Bind registers a handler (replaces if ID exists)

func (*Hook[T]) BindFunc

func (h *Hook[T]) BindFunc(fn func(T) error)

BindFunc registers a handler function with auto-generated ID

func (*Hook[T]) Len

func (h *Hook[T]) Len() int

Len returns the number of handlers

func (*Hook[T]) Trigger

func (h *Hook[T]) Trigger(event T, defaultFn func(T) error) error

Trigger executes all handlers in order

func (*Hook[T]) Unbind

func (h *Hook[T]) Unbind(ids ...string)

Unbind removes handlers by ID

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 NewRegistry

func NewRegistry() *Registry

NewRegistry creates a new hook registry

func (*Registry) OnBootstrap

func (r *Registry) OnBootstrap() *Hook[*AppEvent]

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) OnServe

func (r *Registry) OnServe() *Hook[*AppEvent]

OnServe returns the serve hook

func (*Registry) OnTerminate

func (r *Registry) OnTerminate() *Hook[*AppEvent]

OnTerminate returns the terminate hook

func (*Registry) TriggerBootstrap

func (r *Registry) TriggerBootstrap(app interface{}) error

TriggerBootstrap triggers the bootstrap hook

func (*Registry) TriggerModelCreate

func (r *Registry) TriggerModelCreate(kind string, model interface{}) error

TriggerModelCreate triggers the model create hook

func (*Registry) TriggerModelDelete

func (r *Registry) TriggerModelDelete(kind string, model interface{}) error

TriggerModelDelete triggers the model delete hook

func (*Registry) TriggerModelUpdate

func (r *Registry) TriggerModelUpdate(kind string, model interface{}) error

TriggerModelUpdate triggers the model update hook

func (*Registry) TriggerModelValidate

func (r *Registry) TriggerModelValidate(kind string, model interface{}, isNew bool) error

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

func (r *Registry) TriggerServe(app interface{}) error

TriggerServe triggers the serve hook

func (*Registry) TriggerTerminate

func (r *Registry) TriggerTerminate(app interface{}) error

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.

func (*TaggedEvent) Tags

func (e *TaggedEvent) Tags() []string

Tags returns 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.

type Tagger

type Tagger interface {
	Resolver
	Tags() []string
}

Tagger is the interface for events that support tag-based filtering. Events implementing this can be selectively triggered based on tags.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL