events

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Dec 10, 2025 License: GPL-3.0 Imports: 1 Imported by: 0

Documentation

Overview

Package events provides a simple publish-subscribe mechanism for event handling.

This package does not define specific events; instead, publishers define their own event types by embedding the Event interface in their structs. Subscribers can subscribe to these custom events by providing callback functions that accept the event type as a parameter.

Example:

package somepkg

type SomeEvent struct {
    events.Event // embedding marks this as an event type
    Message string
}

func doSomething() {
	events.Emit(SomeEvent{Message: "hello world"})
}

package other

func doOtherthing() {
	sub := events.Subscribe(func(evt somepkg.SomeEvent) {
	    fmt.Println("Received event:", evt.Message)
	})
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Emit

func Emit[T Event](evt T)

Emit notifies all subscribers of the event, passing event data. Callbacks are invoked asynchronously in separate goroutines.

func Unsubscribe

func Unsubscribe[T Event](sub *Subscription[T])

Unsubscribe removes the given subscription.

Types

type Event

type Event interface {
	// IsEvent is a marker method for the Event interface; it has no runtime use.
	IsEvent()
}

type Subscription

type Subscription[T Event] struct{}

Subscription allows unsubscribing from an event.

func Subscribe

func Subscribe[T Event](callback func(evt T)) *Subscription[T]

Subscribe registers a callback function for the given event type T. Returns a Subscription handle that can be used to unsubscribe later.

Jump to

Keyboard shortcuts

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