event

package
v0.0.0-...-89aa834 Latest Latest
Warning

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

Go to latest
Published: Oct 29, 2024 License: MIT Imports: 6 Imported by: 8

Documentation

Overview

Package event provides a generic event handling system.

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrUnexpectedEventType = errors.New("unexpected event type")

ErrUnexpectedEventType is the error returned when an unexpected event type is received.

Functions

func Decode

func Decode[T comparable](dec *Decoder, event Event[T]) error

Decode decodes an event from the given reader.

func DecodeFrom

func DecodeFrom[T comparable](r io.Reader, event Event[T]) error

DecodeFrom decodes an event from the given reader.

func Encode

func Encode[T comparable](enc *Encoder, event Event[T]) error

Encode encodes an event to the given encoder.

func EncodeTo

func EncodeTo[T comparable](w io.Writer, event Event[T]) error

EncodeTo encodes an event to the given writer.

Example
package main

import (
	"bytes"
	"fmt"

	"github.com/gopherd/core/event"
)

type testEncodableEvent struct {
	Type    int
	Message string
}

func (e testEncodableEvent) Typeof() int {
	return e.Type
}

func main() {
	event.Register(testEncodableEvent{})

	testEvent := testEncodableEvent{Type: 4, Message: "Example Event"}
	buffer := &bytes.Buffer{}

	err := event.EncodeTo(buffer, testEvent)
	if err != nil {
		panic(err)
	}

	var decoded testEncodableEvent
	err = event.DecodeFrom(buffer, &decoded)
	if err != nil {
		panic(err)
	}

	fmt.Println(decoded.Type, decoded.Message)
}
Output:

4 Example Event

func Register

func Register[T comparable](event Event[T])

Register registers an event type for encoding and decoding.

Types

type Decoder

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

Decoder decodes events from a reader.

func NewDecoder

func NewDecoder(r io.Reader) *Decoder

NewDecoder creates a new Decoder instance.

type Dispatcher

type Dispatcher[T comparable] interface {
	DispatchEvent(context.Context, Event[T]) error
}

Dispatcher dispatches events.

type Encoder

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

Encoder encodes events to a writer.

func NewEncoder

func NewEncoder(w io.Writer) *Encoder

NewEncoder creates a new Encoder instance.

type Event

type Event[T comparable] interface {
	// Typeof returns the type of the event.
	Typeof() T
}

Event is the interface that wraps the basic Typeof method.

type EventSystem

type EventSystem[T comparable] interface {
	ListenerAdder[T]
	ListenerRemover
	ListenerChecker
	Dispatcher[T]
}

EventSystem is the interface that manages listeners and dispatches events.

func NewEventSystem

func NewEventSystem[T comparable](ordered bool) EventSystem[T]

NewEventSystem creates a new EventSystem instance.

type Listener

type Listener[T comparable] interface {
	// EventType returns the type of event this listener handles.
	EventType() T
	// HandleEvent processes the fired event.
	HandleEvent(context.Context, Event[T]) error
}

Listener handles fired events.

func Listen

func Listen[H ~func(context.Context, E) error, E Event[T], T comparable](eventType T, handler H) Listener[T]

Listen creates a Listener for the given event type and handler function.

type ListenerAdder

type ListenerAdder[T comparable] interface {
	AddListener(Listener[T]) ListenerID
}

ListenerAdder adds a new listener and returns its ID.

type ListenerChecker

type ListenerChecker interface {
	HasListener(ListenerID) bool
}

ListenerChecker checks if a listener exists by its ID.

type ListenerID

type ListenerID int

ListenerID represents a unique identifier for listeners.

type ListenerRemover

type ListenerRemover interface {
	RemoveListener(ListenerID) bool
}

ListenerRemover removes a listener by its ID.

Jump to

Keyboard shortcuts

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