events

package
v1.5.12 Latest Latest
Warning

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

Go to latest
Published: Oct 27, 2023 License: MIT Imports: 5 Imported by: 4

Documentation

Overview

Source: https://github.com/kataras/go-events Package events provides simple EventEmitter support for Go Programming Language

Index

Examples

Constants

View Source
const (
	// Version current version number
	Version = "0.0.3"
	// DefaultMaxListeners is the number of max listeners per event
	// default EventEmitters will print a warning if more than x listeners are
	// added to it. This is a useful default which helps finding memory leaks.
	// Defaults to 0, which means unlimited
	DefaultMaxListeners = 0
)

Variables

This section is empty.

Functions

This section is empty.

Types

type EventEmitter

type EventEmitter interface {
	// AddListener is an alias for .On(eventName, listener).
	AddListener(EventName, ...Listener) error
	// Emit fires a particular event,
	// Synchronously calls each of the listeners registered for the event named
	// eventName, in the order they were registered,
	// passing the supplied arguments to each.
	Emit(EventName, ...any)
	// EventNames returns an array listing the events for which the emitter has registered listeners.
	// The values in the array will be strings.
	EventNames() []EventName
	// GetMaxListeners returns the max listeners for this emmiter
	// see SetMaxListeners
	GetMaxListeners() uint
	// ListenerCount returns the length of all registered listeners to a particular event
	ListenerCount(EventName) int
	// Listeners returns a copy of the array of listeners for the event named eventName.
	Listeners(EventName) []Listener
	// On registers a particular listener for an event, func receiver parameter(s) is/are optional
	On(EventName, ...Listener) error
	// Once adds a one time listener function for the event named eventName.
	// The next time eventName is triggered, this listener is removed and then invoked.
	Once(EventName, ...Listener) error
	// RemoveAllListeners removes all listeners, or those of the specified eventName.
	// Note that it will remove the event itself.
	// Returns an indicator if event and listeners were found before the remove.
	RemoveAllListeners(EventName) bool
	// RemoveListener removes given listener from the event named eventName.
	// Returns an indicator whether listener was removed
	RemoveListener(EventName, Listener) bool
	// Clear removes all events and all listeners, restores Events to an empty value
	Clear()
	// SetMaxListeners obviously this function allows the MaxListeners
	// to be decrease or increase. Set to zero for unlimited
	SetMaxListeners(uint)
	// Len returns the length of all registered events
	Len() int
}

EventEmitter is the message/or/event manager

func New

func New() EventEmitter

New returns a new, empty, EventEmitter

type EventName

type EventName string

EventName is just a type of string, it's the event name

type Events

type Events map[EventName][]Listener

Events the type for registered listeners, it's just a map[string][]func(...any)

Example
// regiter our events to the default event emmiter
for evt, listeners := range testEvents {
	_event.On(evt, listeners...)
}

user := "user1"
room := "room1"

createUser(user)
joinUserTo(user, room)
leaveFromRoom(user, room)
Output:

A new User just created!
A new User just created, *from second event listener
user1 joined to room: room1
user1 left from the room: room1

func (Events) CopyTo

func (e Events) CopyTo(emmiter EventEmitter)

CopyTo copies the event listeners to an EventEmitter

type Listener

type Listener func(...any)

Listener is the type of a Listener, it's a func which receives any,optional, arguments from the caller/emmiter

Jump to

Keyboard shortcuts

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