event

package module
v0.0.0-...-fabed83 Latest Latest
Warning

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

Go to latest
Published: Sep 11, 2017 License: MIT Imports: 4 Imported by: 1

README

Event - A simple event emitter for Go

GoDoc Go Report Card

This project was inspired by the emission package. Instead of passing strings as event keys, I prefer to have one struct per event.

Sample

package main

import (
	"fmt"

	"github.com/desertbit/event"
)

func main() {
	e := event.New()
	e.On(onEvent)
	e.Once(onceEvent)

	e.TriggerWait("Hello World", 1)
	e.TriggerWait("Hello World", 2)

	e.Off(onEvent)
	e.TriggerWait("Hello World", 3)
}

func onEvent(s string, i int) {
	fmt.Println("on:", s, i)
}

func onceEvent(s string, i int) {
	fmt.Println("once:", s, i)
}

Documentation

Overview

Package event is a simple event emitter for Go.

Index

Constants

This section is empty.

Variables

View Source
var ErrNotFunc = errors.New("listener value is not a function")

ErrNotFunc presented when an invalid argument is provided as a listener function.

Functions

This section is empty.

Types

type Event

type Event struct {
	Recoverer Recoverer
	// contains filtered or unexported fields
}

Event implementation.

func New

func New(r ...Recoverer) *Event

New creates a new single event. Optionally pass a recoverer.

func (*Event) Off

func (e *Event) Off(listener interface{})

Off unsubscribes a function listener.

func (*Event) On

func (e *Event) On(listener interface{})

On subscribes a function as new event listener.

func (*Event) Once

func (e *Event) Once(listener interface{})

Once subscribes a function as new event listener. This function will be called only once.

func (*Event) Trigger

func (e *Event) Trigger(arguments ...interface{})

Trigger the event. This will call all function listeners.

func (*Event) TriggerWait

func (e *Event) TriggerWait(arguments ...interface{})

TriggerWait triggers the event and waits for all functions to execute.

type Recoverer

type Recoverer func(interface{}, error)

Recoverer for panics. The interface defines the listener function.

Jump to

Keyboard shortcuts

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