events

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 10, 2019 License: MIT Imports: 2 Imported by: 3

README

events Build Status

简单的事件订阅发布系统

安装
go get github.com/issue9/events
文档

Go Walker GoDoc

版权

本项目采用 MIT 开源授权许可证,完整的授权说明可在 LICENSE 文件中找到。

Documentation

Overview

Package events 提供了简单的事件发布订阅功能

Index

Constants

This section is empty.

Variables

View Source
var ErrStopped = errors.New("该事件已经停止发布新内容")

ErrStopped 表示发布都已经调用 Destory 销毁了事件处理器。

Functions

func New

func New() (*Publisher, *Event)

New 声明一个新的事件处理

Publisher 供事件发布者进行发布新事件; Event 供订阅者订阅事件。

Types

type Event

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

Event 事件

func (*Event) Attach

func (e *Event) Attach(subscriber Subscriber) int

Attach 注册订阅者

返回一个唯一 ID,用户可以使用此 ID 取消订阅

func (*Event) Detach

func (e *Event) Detach(id int)

Detach 取消订阅者

type Publisher

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

Publisher 事件的发布者

func (*Publisher) Destory

func (p *Publisher) Destory()

Destory 销毁当前事件处理程序

func (*Publisher) Publish

func (p *Publisher) Publish(data interface{}) error

Publish 触发事件

type Subscriber

type Subscriber func(data interface{})

Subscriber 订阅者函数

当存在多个订阅者时,通过 go 异步执行每个函数。

data 为事件传递过来的数据,可能存在多个订阅者, 最好不要直接修改 data 数据,否则结果是未知的。

Jump to

Keyboard shortcuts

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