Documentation
¶
Overview ¶
Package event provides typed, instance-owned application event topics for generated Spice applications.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ErrPanicked = errors.New("event handler panicked")
ErrPanicked identifies an observed event-handler panic. Publish reports the panic to observers and then re-panics with the original value.
Functions ¶
This section is empty.
Types ¶
type Definition ¶
Definition identifies one compiler-owned event contract and its publishing module.
type Interaction ¶
type Interaction struct {
Event Definition
Subscriber SubscriberMetadata
}
Interaction identifies one delivery from an event contract to a subscriber.
type Observer ¶
type Observer interface {
BeginEvent(context.Context, Interaction) (context.Context, func(Result))
}
Observer receives event interaction begin/end information on the publishing goroutine. Implementations must not panic or block indefinitely.
type Result ¶
type Result struct {
Interaction Interaction
Duration time.Duration
Err error
Panicked bool
}
Result describes one completed event-handler interaction.
type Subscriber ¶
Subscriber declares one generated event handler. Lower Order values run first; ties are resolved by module and stable subscriber ID.
type SubscriberMetadata ¶
SubscriberMetadata is the payload-free identity exposed to observers.
type Topic ¶
type Topic[T any] struct { // contains filtered or unexported fields }
Topic is one immutable typed event contract and its generated subscribers.
Example ¶
package main
import (
"context"
"fmt"
"github.com/spice-framework/spice/event"
)
type orderPlaced struct {
ID string
}
func main() {
topic, err := event.NewTopic(
event.Definition{
ID: "orders.OrderPlaced",
Module: "example.com/shop/orders",
},
[]event.Subscriber[orderPlaced]{
{
ID: "inventory.Reserve",
Module: "example.com/shop/inventory",
Handle: func(_ context.Context, placed orderPlaced) error {
fmt.Printf("inventory reserved %s\n", placed.ID)
return nil
},
},
},
)
if err != nil {
fmt.Printf("construct topic: %v\n", err)
return
}
if err := topic.Publish(context.Background(), orderPlaced{ID: "order-1"}); err != nil {
fmt.Printf("publish: %v\n", err)
}
}
Output: inventory reserved order-1
func NewTopic ¶
func NewTopic[T any]( definition Definition, subscribers []Subscriber[T], observers ...Observer, ) (*Topic[T], error)
NewTopic validates and freezes one generated event topic. It starts no goroutines and installs no global registry.