eventemitter

package module
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Mar 8, 2021 License: ISC Imports: 8 Imported by: 8

README

EventEmitter in Golang

Synopsis

EventEmitter is an implementation of the Event-based architecture in Golang.

Installation

import "github.com/jiyeyuran/go-eventemitter"

Examples

Creating an instance.
em := eventemitter.NewEventEmitter()
An usual example.
em.On("foo", func(){
    // Some code...
})
em.Emit("foo")
It will be triggered only once and then callbacks will be removed.
em.Once("foo", func() {
  // some code...
});

em.Emit("foo");
// Nothing happend.
em.Emit("foo");
Callback with parameters.
em.On("foo", func(bar, baz string) {
  // some code...
})

em.Emit("foo", "var 1 for bar", "var 2 for baz")
// Missing parameters
ee.Emit("foo")
// With extra parameters
ee.Emit("foo", "bar", "baz", "extra parameter")
Callback's call can be ordered by "weight" parameter.
em.On("foo", func() {
    fmt.Println("3")
})

em.On("foo", func() {
    fmt.Println("2")
})

em.On("foo", func() {
    fmt.Println("1")
})

em.Emit("foo")
// 3
// 2
// 1
Set maxNumberOfListeners as a parameter when creating new instance.
em := eventemitter.NewEventEmitter(eventemitter.WithMaxListeners(1))


em.On("foo", func(){
    // Some code...
})

// Note: it will show warn in console.
em.On("foo", func(){
    // Some other code...
})
Asynchronous vs. Synchronous
wait := make(chan struct{})

em.On("foo", func(bar int){
    // Some block code...
    <-wait
})

// synchronous and panic
em.Emit("foo", "bad parameter")
// Emit asynchronously
em.SaftEmit("foo")
// Note: it will show error in console and no panic will be trigged.
em.SaftEmit("foo", "bad parameter")
// Wait result
em.SafeEmit("foo").Wait()

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AysncResult added in v1.1.0

type AysncResult interface {
	Wait()
	WaitCtx(ctx context.Context) error
}

Waiting all aysnc listeners to be done.

func NewAysncResultImpl added in v1.1.0

func NewAysncResultImpl(wg *sync.WaitGroup) AysncResult

type AysncResultImpl added in v1.1.0

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

func (*AysncResultImpl) Wait added in v1.1.0

func (s *AysncResultImpl) Wait()

func (*AysncResultImpl) WaitCtx added in v1.1.0

func (s *AysncResultImpl) WaitCtx(ctx context.Context) error

type Decoder

type Decoder interface {
	Decode(data []byte, result interface{}) error
}

Decoder defines interface to decode bytes into golang type

type EventEmitter

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

The EventEmitter implements IEventEmitter

func (*EventEmitter) AddListener

func (e *EventEmitter) AddListener(evt string, listener interface{}) IEventEmitter

func (*EventEmitter) Emit

func (e *EventEmitter) Emit(evt string, args ...interface{}) bool

Emit fires a particular event

func (*EventEmitter) Len

func (e *EventEmitter) Len() int

func (*EventEmitter) ListenerCount

func (e *EventEmitter) ListenerCount(evt string) int

func (*EventEmitter) Off

func (e *EventEmitter) Off(evt string, listener interface{}) IEventEmitter

func (*EventEmitter) On

func (e *EventEmitter) On(evt string, listener interface{}) IEventEmitter

func (*EventEmitter) Once

func (e *EventEmitter) Once(evt string, listener interface{}) IEventEmitter

func (*EventEmitter) RemoveAllListeners

func (e *EventEmitter) RemoveAllListeners(evts ...string) IEventEmitter

func (*EventEmitter) RemoveListener

func (e *EventEmitter) RemoveListener(evt string, listener interface{}) IEventEmitter

func (*EventEmitter) SafeEmit

func (e *EventEmitter) SafeEmit(evt string, args ...interface{}) AysncResult

SafaEmit fires a particular event asynchronously.

type IEventEmitter

type IEventEmitter interface {
	// AddListener is the alias for emitter.On(eventName, listener).
	AddListener(evt string, listener interface{}) IEventEmitter

	// 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(evt string, listener interface{}) IEventEmitter

	// Emit synchronously calls each of the listeners registered for the event named eventName,
	// in the order they were registered, passing the supplied arguments to each.
	// Returns true if the event had listeners, false otherwise.
	Emit(evt string, argv ...interface{}) bool

	// SafeEmit asynchronously calls each of the listeners registered for the event named eventName.
	// By default, a maximum of 128 events can be buffered.
	// Panic will be catched and logged as error.
	// Returns AysncResult.
	SafeEmit(evt string, argv ...interface{}) AysncResult

	// RemoveListener is the alias for emitter.Off(eventName, listener).
	RemoveListener(evt string, listener interface{}) IEventEmitter

	// RemoveAllListeners removes all listeners, or those of the specified eventNames.
	RemoveAllListeners(evts ...string) IEventEmitter

	// On adds the listener function to the end of the listeners array for the event named eventName.
	// No checks are made to see if the listener has already been added.
	// Multiple calls passing the same combination of eventName and listener will result in the listener
	// being added, and called, multiple times.
	// By default, a maximum of 10 listeners can be registered for any single event.
	// This is a useful default that helps finding memory leaks. Note that this is not a hard limit.
	// The EventEmitter instance will allow more listeners to be added but will output a trace warning
	// to log indicating that a "possible EventEmitter memory leak" has been detected.
	On(evt string, listener interface{}) IEventEmitter

	// Off removes the specified listener from the listener array for the event named eventName.
	Off(evt string, listener interface{}) IEventEmitter

	// ListenerCount returns the number of listeners listening to the event named eventName.
	ListenerCount(evt string) int
}

IEventEmitter defines event emitter interface

func NewEventEmitter

func NewEventEmitter(options ...Option) IEventEmitter

type JsonDecoder

type JsonDecoder struct{}

JsonDecoder implements Decoder with standard json decoder.

func (JsonDecoder) Decode

func (JsonDecoder) Decode(data []byte, result interface{}) error

type Logger

type Logger interface {
	Error(format string, v ...interface{})
	Warn(format string, v ...interface{})
}

Logger defines interface to print log information.

type Option

type Option func(*EventEmitter)

Option defines variadic parameter to create EventEmitter

func WithDecoder

func WithDecoder(decoder Decoder) Option

WithDecoder set bytes decode. By default, bytes will be decoded by json decoder.

func WithLogger

func WithLogger(logger Logger) Option

WithLogger set logger. By default, golang standard log will be used.

func WithMaxListeners

func WithMaxListeners(num int) Option

WithMaxListeners set max listeners for single event. By default, a maximum of 10 listeners can be registered for any single event.

func WithQueueSize

func WithQueueSize(queueSize int) Option

WithQueueSize set max queue buffer size for asynchronous event. By default, a maximum of 128 buffer size for any asynchronous event.

Jump to

Keyboard shortcuts

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