Coder Social home page Coder Social logo

event's Introduction

OpenMix 出品:https://openmix.org

Mix Event

参考 PHP PSR-14 标准的事件调度库

Event dispatch library refer to PHP PSR-14 standard

该库还有 php 版本:https://github.com/mix-php/event

Overview

事件调度是一种常见机制,允许开发人员轻松,一致地将逻辑注入应用程序,这在 PHP 中非常常见,于是我打造了这个 Go 版本的事件调度库,整体实现基于 PSR-14

  • 事件(Event):一个自定义结构体,事件数据的载体
  • 监听器(Listener):负责处理对应的事件数据
  • 事件调度器(Dispatcher):用于触发某个事件

Installation

  • 安装
go get -u github.com/mix-go/event

Usage

定义事件 Event,事件可以为任意结构体,只需要继承 event.EventTrait 即可

type CommandBeforeExecuteEvent struct {
    event.EventTrait
    Command interface{}
}

定义监听器 Listener,监听器是用户编写处理事件逻辑代码的地方,Events 方法返回一个要监听的事件类的数组,当这些事件触发时,会调用 Process 方法:

type CommandListener struct {
}

func (t *CommandListener) Events() []event.Event {
    return []event.Event{
        &event2.CommandBeforeExecuteEvent{},
    }
}

func (t *CommandListener) Process(e event.Event) {
    switch e.(type) {
    case *event2.CommandBeforeExecuteEvent:
        // 初始化全局对象
        globals.Init()

        // 设置守护
        if flag.Match("d", "daemon").Bool() {
            process.Daemon()
        }
        break
    }
}

触发某个事件 Dispatcher

e := &CommandBeforeExecuteEvent{
    Command: "foo",
}
d := event.NewDispatcher(&CommandListener)
d.Dispatch(e)

License

Apache License Version 2.0, http://www.apache.org/licenses/

event's People

Contributors

onanying avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.