eventemitter

package module
v1.4.1 Latest Latest
Warning

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

Go to latest
Published: Jul 4, 2024 License: MIT Imports: 5 Imported by: 4

README

EventEmitter - a simple event emitter

PkgGoDev Build Status Go Report Card Coverage Status GitHub issues Release

Installation

To install the package, run:

go get github.com/go-zoox/eventemitter

Getting Started

import (
  "testing"
  "github.com/go-zoox/eventemitter"
)

func main(t *testing.T) {
	e := eventemitter.New()
	count := 0
	e.On("send.notify", eventemitter.HandleFunc(func(payload any) {
		count++
		t.Log("send.notify", payload)
	}))

	e.Start()

	wg := &sync.WaitGroup{}
	for i := 0; i < 10; i++ {
		index := i
		wg.Add(1)
		go func() {
			e.Emit("send.notify", index)
			wg.Done()
		}()
	}

	wg.Wait()
}

License

GoZoox is released under the MIT License.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Version = "1.4.1"

Version is the current version of the package.

Functions

This section is empty.

Types

type EventEmitter

type EventEmitter interface {
	On(event string, handler Handler)
	Once(event string, handler Handler)
	Off(event string, handler Handler)
	Emit(event string, payload any)
}

EventEmitter is a simple event emitter.

func New

func New(opts ...func(opt *Option)) EventEmitter

New creates a new EventEmitter.

type Handler added in v1.2.5

type Handler interface {
	ID() string
	Serve(payload any)
}

Handler is a function that can be registered to an event.

func HandleFunc

func HandleFunc(handler func(payload any)) Handler

HandleFunc creates a Handle from a function.

type Option added in v1.0.5

type Option struct {
	Context context.Context
	//
	BufferSize int
}

Option is the option for the event emitter.

Jump to

Keyboard shortcuts

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