Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
H
hilo-common
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
hujiebin
hilo-common
Commits
1234bdaa
Commit
1234bdaa
authored
Feb 24, 2023
by
hujiebin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Update event.go
parent
8d8ceb62
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
50 additions
and
6 deletions
+50
-6
event.go
domain/event.go
+50
-6
No files found.
domain/event.go
View file @
1234bdaa
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
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment