Commit 1234bdaa authored by hujiebin's avatar hujiebin

Update event.go

parent 8d8ceb62
package domain package domain
//异步执行的接口 import "git.hilo.cn/hilo-common/utils"
type AsyncEvent interface {
AsyncDo(model *Model, eventData interface{}, n int) error //异步执行的接口-老方式-废弃
AsyncSize() int //type AsyncEvent interface {
AsyncNoTxDo(model *Model, eventData interface{}, n int) error // AsyncDo(model *Model, eventData interface{}, n int) error
AsyncNoTxSize() int // AsyncSize() int
// AsyncNoTxDo(model *Model, eventData interface{}, n int) error
// AsyncNoTxSize() int
//}
// 程序内部事件
type EventBase struct {
//同步执行
syncList []func(model *Model, event interface{}) error
//异步执行
asyncList []func(model *Model, event interface{}) error
}
// 添加同步事件
func AddEventSync(event *EventBase, callback func(model *Model, event interface{}) error) {
event.syncList = append(event.syncList, callback)
}
// 添加异步事件
func AddEventAsync(event *EventBase, callback func(model *Model, event interface{}) error) {
event.asyncList = append(event.asyncList, callback)
}
// 发布事件
func PublishEvent(event *EventBase, model *Model, data interface{}) error {
// 执行同步的领域事件
for _, callback := range event.syncList {
if err := callback(model, data); err != nil {
return err
}
}
// 执行异步的领域事件
if len(event.asyncList) > 0 {
go func() {
defer utils.CheckGoPanic()
for _, callback := range event.asyncList {
// 异步事件需要用新model,主要是db
var newModel = CreateModelContext(model.MyContext)
if err := callback(newModel, data); err != nil {
model.Log.Errorf("aysnc fail:%v", err)
}
}
}()
}
return nil
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment