events

package
v1.6.0 Latest Latest
Warning

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

Go to latest
Published: Nov 12, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package events provides a generic, type-keyed in-process event bus so domain event producers and handlers are decoupled within a process. It complements the durable, cross-process outbox package.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Publish

func Publish[T any](ctx context.Context, b *Bus, evt T) error

Publish delivers evt to all handlers registered for type T. In sync mode (default) it runs them in registration order and returns their joined errors; in async mode it dispatches each in a goroutine and returns nil.

Example

Subscribe handlers for a typed event, then publish one. In the default sync mode handlers run inline and Publish returns their joined errors.

package main

import (
	"context"
	"fmt"

	"github.com/rahmadafandi/fibr/events"
)

func main() {
	type OrderCreated struct{ ID int }

	bus := events.New()
	events.Subscribe(bus, func(_ context.Context, e OrderCreated) error {
		fmt.Println("email: order", e.ID)
		return nil
	})
	events.Subscribe(bus, func(_ context.Context, e OrderCreated) error {
		fmt.Println("analytics: order", e.ID)
		return nil
	})

	_ = events.Publish(context.Background(), bus, OrderCreated{ID: 7})
}
Output:
email: order 7
analytics: order 7

func Subscribe

func Subscribe[T any](b *Bus, handler func(context.Context, T) error)

Subscribe registers handler for events of type T.

Types

type Bus

type Bus struct {
	// contains filtered or unexported fields
}

Bus is an in-process event bus. Handlers are keyed by event type.

func New

func New(opts ...Option) *Bus

New returns an event bus. By default it delivers synchronously.

type Option

type Option func(*Bus)

Option configures a Bus.

func WithAsync

func WithAsync() Option

WithAsync dispatches each handler in its own goroutine, so Publish returns immediately and handler errors are logged rather than returned.

func WithLogger

func WithLogger(l *logger.Logger) Option

WithLogger sets the logger used for async handler errors.

Jump to

Keyboard shortcuts

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