eventkit

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Jun 19, 2023 License: MIT Imports: 10 Imported by: 0

README

Go GitHub go.mod Go version Go Report Card codecov License Github tag

Overview

The eventkit package provides a simple event-driven programming mechanism in Go. It allows you to create and manage events, subscribe to events using struct methods or functions, and trigger events with optional data.

Usage

Creating a new event instance

To create a new event instance, use the New() function:

eventInstance := eventkit.New()
Subscribing to an event
Using struct methods

To subscribe to an event using struct methods, use the Subscribe() function. It takes a struct instance as the payload and automatically registers all methods starting with a specified prefix (default: "On") as event listeners.

type MyEventPayload struct {
	// struct fields
}

func (m *MyEventPayload) OnEventName() {
	// event handler logic
}

// Subscribe to the event
eventInstance.Subscribe(&MyEventPayload{})
Using functions

To subscribe to an event using a function, use the SubscribeFunc() function. Provide a unique listener identifier and the callback function.

func myEventListener(data ...interface{}) {
	// event handler logic
}

// Subscribe to the event
eventInstance.SubscribeFunc("myListener", myEventListener)
Triggering an event

To trigger an event, use the Trigger() function. Provide the name of the event and optional data as arguments.

err := eventInstance.Trigger("eventName", eventData)
if err != nil {
	// handle error
}

Note: The package utilizes various external dependencies, such as logrus, golang.org/x/text/cases, and sync, among others.

For more details on the package, please refer to the code documentation and comments within the source files.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ErrEventCallbacks

type ErrEventCallbacks interface {
	Error() string
	Errors() []error
}

func NewErrEventCallbacks

func NewErrEventCallbacks(event string, errors []error) ErrEventCallbacks

NewErrEventCallbacks : Create new error event callbacks

type EventKit

type EventKit interface {
	Subscribe(payload any) error
	SubscribeFunc(listener string, callback any) error
	Trigger(name string, data ...any) error
}

func New

func New() EventKit

New : Create a new event instance

Jump to

Keyboard shortcuts

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