Event loop.
Serial and parallel events supported.
You can use them seperately. Also you can also mix using serial and parallel .
go get github.com/slclub/goevents
import github.com/slclub/goevents
events := goevents.Classic()
Defined func follow the type.It is the beanchmark,don't need to appear in your code. Define functions "func" rather than method according to golang standards
There is the function of event defined by yourself. The following definitions are allowed.Maybe you can say that as long as the golang function is allowed
func event1(st ...interface{}) { }//allowed
func event(){}//allowed.
func event(n int, str string){}//allowed.
func main() {
self := goevents.Classic()
self.On("a", func() {
print("serial event added:e1")
})
self.On("b", func(n int) {
print("serial event added:e2; args:", n)
})
self.Bind(22222)
self.Bind("I am string").On("b", func(str string) {
print("serial event added:e3; args:", str)
})
//Can trigger the events that was named belong to On function.
//events.Bind("e2", "do some things by bind").Trigger("b")
//Trigger all the event functions.
events.Emit()
}
func main() {
self := goevents.Classic()
self.GoOn(func() {
print("parallel event added e1")
}, "no", 1)
self.GoOn(func(str string, n int) {
print("parallel event added e2", str, n)
}, "no", 2)
self.Emit()
//trigger all of the events that has not been emited.
self.Emit()
package main
import (
"fmt"
"github.com/slclub/goevents"
"time"
)
var print = fmt.Println
func Test() {
Timer := time.Now()
self := goevents.Classic()
self.On("a", func() {
print("serial event added:e1")
})
self.On("b", func(n int) {
print("serial event added:e2; args:", n)
})
self.Bind(22222)
self.Bind("I am string").On("b", func(str string) {
print("serial event added:e3; args:", str)
})
self.GoOn(func() {
print("parallel event added e1")
}, "no", 1)
self.GoOn(func(str string, n int) {
print("parallel event added e2", str, n)
}, "no", 2)
self.Emit()
excuTimes := time.Since(Timer)
print("event running time:", excuTimes)
}
//instance
ev := events.Classic()
Bind event to the object of events
ev.On("message", func(args ...interface{}){
//do some things
})
Bind param to the current event func
//use mod one
ev.Bind("abc",123,&struct1{1,2})
//you can also coherent usage.
ev.Bind(...).On("message", func(args ...interface{}){
//do something
})
Trigger the events by the first element of the args. it will Emit all events if no argments.
//Just trigger partion of the events by first argment.
ev.Trigger("message")
//If no params it will emit all the serial events
ev.Trigger()
Bind event that need to parallel execution.
ev.GoOn(func(...interface{}){
//event do something
}, args)
Emit all the events. Parallel events execution included.
ev.Emit().
Setting events object running mod. Param:chNum parallel gorountine numbers. Param:safeMod events object running mod.
ev.Conf(3,0)
more contact with slclub