-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathevent.go
50 lines (43 loc) · 1.21 KB
/
event.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package goofy
import (
"fmt"
"github.com/urionz/goofy/contracts"
"github.com/urionz/goofy/event"
"github.com/urionz/goofy/log"
)
func (app *Application) AddListeners(eventListeners event.Listeners) contracts.Application {
for name, listeners := range eventListeners {
for _, listener := range listeners {
app.Manager.AddListener(name, listener)
}
}
return app
}
func (app *Application) MustEmit(name string, payload event.M) event.Event {
return app.Manager.MustFire(name, payload)
}
func (app *Application) Emit(name string, payload event.M) (error, event.Event) {
return app.Manager.Fire(name, payload)
}
func (app *Application) Dispatch(name string, payload event.M, fn ...func(e error)) contracts.Application {
go func() {
err, _ := app.Manager.Fire(name, payload)
if len(fn) > 0 && err != nil {
fn[0](err)
}
}()
return app
}
func Listeners(listeners ...event.Listener) []event.Listener {
return listeners
}
func ListenerFunc(fn event.ArgsListenerFunc, args ...interface{}) event.ListenerFunc {
return func(e event.Event) error {
defer func() {
if err := recover(); err != nil {
log.Error(fmt.Errorf("%s event = %s data = %+v", err, e.Name(), e.Data()))
}
}()
return fn(e, args...)
}
}