emitter

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Sep 26, 2020 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Example (FileByPath)
package main

import (
	"fmt"

	"github.com/recallsong/go-utils/emitter"
)

func main() {
	emit := emitter.New()
	w1 := emit.Watch("connect")
	w1.Callback(func(e *emitter.Event) {
		fmt.Println("w1", e.Name)
	})

	w2 := emit.Watch("connect")
	w2.Callback(func(e *emitter.Event) {
		fmt.Println("w2", e.Name)
	})

	w3 := emit.Watch("disconnect")
	w3.Callback(func(e *emitter.Event) {
		fmt.Println("w3", e.Name)
	})

	w1.Close()

	w4 := emit.Watch("connect")
	w4.Callback(func(e *emitter.Event) {
		fmt.Println("w4", e.Name)
	})

	emit.Emit(&emitter.Event{
		Name: "connect",
		Data: 123,
	})
	emit.Emit(&emitter.Event{
		Name: "disconnect",
		Data: 123,
	})
}
Output:
w4 connect
w2 connect
w3 disconnect

Index

Examples

Constants

View Source
const MaxListenersPerEvent = math.MaxUint32

MaxListenersPerEvent .

Variables

This section is empty.

Functions

This section is empty.

Types

type Emitter

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

Emitter .

func New

func New() *Emitter

New .

func (*Emitter) Emit

func (e *Emitter) Emit(event *Event) int

Emit .

func (*Emitter) String

func (e *Emitter) String() string

func (*Emitter) Watch

func (e *Emitter) Watch(name string) Watcher

Watch .

type Event

type Event struct {
	Name string      `json:"name"`
	Data interface{} `json:"data"`
}

Event .

type Listener

type Listener func(e *Event)

Listener .

type Watcher

type Watcher interface {
	Channel(size int) (<-chan *Event, error)
	Callback(listener Listener, first ...func(string) error) error
	Close() error
	Stop(last ...func(name string))
}

Watcher .

Jump to

Keyboard shortcuts

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