eventemitter

package module
v1.0.9 Latest Latest
Warning

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

Go to latest
Published: Dec 27, 2023 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.0.9"

Version is the current version of the package.

Functions

This section is empty.

Types

type EventEmitter

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

EventEmitter is a simple event emitter.

func New

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

New creates a new EventEmitter.

func (*EventEmitter) Emit

func (e *EventEmitter) Emit(typ string, payload any)

Emit emits an event.

func (*EventEmitter) Off

func (e *EventEmitter) Off(typ string, handler Handle)

Off removes specify the given event type.

func (*EventEmitter) On

func (e *EventEmitter) On(typ string, handler Handle)

On registers a handler for the given event type.

func (*EventEmitter) Once

func (e *EventEmitter) Once(typ string, handler Handle)

Once performs exactly one action.

func (*EventEmitter) Start

func (e *EventEmitter) Start() error

Start starts the event worker.

func (*EventEmitter) Stop

func (e *EventEmitter) Stop() error

Stop stops the event worker.

type Handle

type Handle interface {
	ID() string
	Serve(payload any) error
}

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

func HandleFunc

func HandleFunc(handler func(payload any)) Handle

HandleFunc creates a Handle from a function.

type Option added in v1.0.5

type Option struct {
	DisableAutoStart bool `json:"disable-auto-start"`
}

Jump to

Keyboard shortcuts

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