Help us shape the future of Go! Take the 2026 Go Developer Survey

tinyevent

package module
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: May 8, 2019 License: MIT Imports: 2 Imported by: 0

README

TinyEvent

极简的事件系统

Install

// x.y.z 是版本号
go get github.com/zjxpcyc/tinyevent@vx.y.z

Useage


// 初始化一个中控中心
// 或者自己实现一个 满足接口 EventBus 即可
var evtBus := &tinyevent.DefaultBus{}

// 声明一个事件
const (
  EvtStart = "event_start"
)

// 注册事件
evtID := evtBus.On(
  // 事件的名称
  EvtStart,
  // 事件需要执行的动作
  func(data interface{}) error {

    // 下面是事件动作的具体内容
    someStr, _ := data.(string)
    fmt.Println(someStr)
    
    return nil
  }
)

// 触发事件执行 - 此处是 go 线程执行
evt := tinyevent.Event {
  Name: EvtStart,
  Payload: "这个字符串将会被打印-在本示例中",
}
evtBus.Emit(evt)

// 取消事件注册
evtBus.Off(evtID)

// 更多信息请查阅 event.go 文件

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Action

type Action func(interface{}) error

Action 动作

type Actions

type Actions = []Action

Actions 动作集合

type DefaultBus

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

DefaultBus 默认事件BUS

func (*DefaultBus) Emit

func (t *DefaultBus) Emit(evt Event)

Emit 执行事件

func (*DefaultBus) Off

func (t *DefaultBus) Off(id EventID)

Off 取消监听

func (*DefaultBus) On

func (t *DefaultBus) On(evtName EventName, act Action) EventID

On 监听

type Event

type Event struct {
	Name    EventName
	Payload interface{}
}

Event 事件

type EventBus

type EventBus interface {
	Emit(Event)
	On(EventName, Action) EventID
	Off(EventID)
}

EventBus 中控

type EventID

type EventID = string

EventID 事件ID

type EventName

type EventName = string

EventName 事件名称

Jump to

Keyboard shortcuts

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