event

package
v0.1.0-preview.4 Latest Latest
Warning

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

Go to latest
Published: Aug 11, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

Package event provides typed, instance-owned application event topics for generated Spice applications.

Index

Examples

Constants

This section is empty.

Variables

View Source
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

type Definition struct {
	ID     string
	Module string
}

Definition identifies one compiler-owned event contract and its publishing module.

type Handler

type Handler[T any] func(context.Context, T) error

Handler consumes one strongly typed event on the publisher's goroutine.

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 Publisher

type Publisher[T any] interface {
	Publish(context.Context, T) error
}

Publisher is the narrow event dependency injected into producers.

type Result

type Result struct {
	Interaction Interaction
	Duration    time.Duration
	Err         error
	Panicked    bool
}

Result describes one completed event-handler interaction.

type Subscriber

type Subscriber[T any] struct {
	ID     string
	Module string
	Order  int
	Handle Handler[T]
}

Subscriber declares one generated event handler. Lower Order values run first; ties are resolved by module and stable subscriber ID.

type SubscriberMetadata

type SubscriberMetadata struct {
	ID     string
	Module string
	Order  int
}

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.

func (*Topic[T]) Publish

func (topic *Topic[T]) Publish(ctx context.Context, value T) error

Publish delivers an event synchronously in deterministic subscriber order. Delivery stops at cancellation, panic, or the first handler error.

Directories

Path Synopsis
Package outbox provides transport-neutral durable event publication contracts and an explicit at-least-once dispatcher.
Package outbox provides transport-neutral durable event publication contracts and an explicit at-least-once dispatcher.

Jump to

Keyboard shortcuts

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