Documentation ¶
Overview ¶
Package event provides a generic event handling system.
Index ¶
- Variables
- func Decode[T comparable](dec *Decoder, event Event[T]) error
- func DecodeFrom[T comparable](r io.Reader, event Event[T]) error
- func Encode[T comparable](enc *Encoder, event Event[T]) error
- func EncodeTo[T comparable](w io.Writer, event Event[T]) error
- func Register[T comparable](event Event[T])
- type Decoder
- type Dispatcher
- type Encoder
- type Event
- type EventSystem
- type Listener
- type ListenerAdder
- type ListenerChecker
- type ListenerID
- type ListenerRemover
Examples ¶
Constants ¶
This section is empty.
Variables ¶
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.
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.
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.
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 ListenerRemover ¶
type ListenerRemover interface {
RemoveListener(ListenerID) bool
}
ListenerRemover removes a listener by its ID.