-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathcomp_async.go
More file actions
99 lines (86 loc) · 3.46 KB
/
comp_async.go
File metadata and controls
99 lines (86 loc) · 3.46 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*
* This file is part of Golaxy Distributed Service Development Framework.
*
* Golaxy Distributed Service Development Framework is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 2.1 of the License, or
* (at your option) any later version.
*
* Golaxy Distributed Service Development Framework is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Golaxy Distributed Service Development Framework. If not, see <http://www.gnu.org/licenses/>.
*
* Copyright (c) 2024 pangdogs.
*/
package framework
import (
"context"
"time"
"git.golaxy.org/core"
"git.golaxy.org/core/ec"
"git.golaxy.org/core/runtime"
"git.golaxy.org/core/utils/async"
"git.golaxy.org/core/utils/generic"
"git.golaxy.org/core/utils/reinterpret"
)
// CallAsync 异步执行代码,有返回值
func (c *ComponentBehavior) CallAsync(fun generic.FuncVar1[IRuntime, any, async.Result], args ...any) async.Future {
return core.CallAsync(c, func(ctx runtime.Context, args ...any) async.Result {
if !c.isAlive() {
return async.NewResult(nil, ErrAsyncCallerNotAlive)
}
return fun.UnsafeCall(reinterpret.Cast[IRuntime](ctx), args...)
}, args...)
}
// CallVoidAsync 异步执行代码,无返回值
func (c *ComponentBehavior) CallVoidAsync(fun generic.ActionVar1[IRuntime, any], args ...any) async.Future {
return core.CallAsync(c, func(ctx runtime.Context, args ...any) async.Result {
if !c.isAlive() {
return async.NewResult(nil, ErrAsyncCallerNotAlive)
}
fun.UnsafeCall(reinterpret.Cast[IRuntime](ctx), args...)
return async.NewResult(nil, nil)
}, args...)
}
// GoAsync 使用新线程执行代码,有返回值(注意线程安全)
func (c *ComponentBehavior) GoAsync(fun generic.FuncVar1[context.Context, any, async.Result], args ...any) async.Future {
return core.GoAsync(c.Entity(), func(ctx context.Context, args ...any) async.Result {
return fun.UnsafeCall(ctx, args...)
}, args...)
}
// GoVoidAsync 使用新线程执行代码,无返回值(注意线程安全)
func (c *ComponentBehavior) GoVoidAsync(fun generic.ActionVar1[context.Context, any], args ...any) async.Future {
return core.GoVoidAsync(c.Entity(), func(ctx context.Context, args ...any) {
fun.UnsafeCall(ctx, args...)
}, args...)
}
// TimeAfterAsync 定时器,指定时长
func (c *ComponentBehavior) TimeAfterAsync(dur time.Duration) async.Future {
return core.TimeAfterAsync(c.Entity(), dur)
}
// TimeAtAsync 定时器,指定时间点
func (c *ComponentBehavior) TimeAtAsync(at time.Time) async.Future {
return core.TimeAtAsync(c.Entity(), at)
}
// TimeTickAsync 心跳器
func (c *ComponentBehavior) TimeTickAsync(dur time.Duration) async.Future {
return core.TimeTickAsync(c.Entity(), dur)
}
// ReadChanAsync 读取channel
func (c *ComponentBehavior) ReadChanAsync(ch <-chan any) async.Future {
return core.ReadChanAsync(c.Entity(), ch)
}
// Await 异步等待结果返回
func (c *ComponentBehavior) Await(futures ...async.Future) AwaitDirector {
return AwaitDirector{
caller: c,
director: core.Await(c, futures...),
}
}
func (c *ComponentBehavior) isAlive() bool {
return c.State() <= ec.ComponentState_Alive
}