-
Notifications
You must be signed in to change notification settings - Fork 0
/
context_options.go
103 lines (89 loc) · 2.85 KB
/
context_options.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
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
100
101
102
103
package service
import (
"context"
"git.golaxy.org/core/plugin"
"git.golaxy.org/core/pt"
"git.golaxy.org/core/util/generic"
"git.golaxy.org/core/util/iface"
"git.golaxy.org/core/util/option"
"git.golaxy.org/core/util/uid"
)
type (
RunningHandler = generic.DelegateAction2[Context, RunningState] // 运行状态变化处理器
)
// ContextOptions 创建服务上下文的所有选项
type ContextOptions struct {
CompositeFace iface.Face[Context] // 扩展者,在扩展服务上下文自身能力时使用
Context context.Context // 父Context
AutoRecover bool // 是否开启panic时自动恢复
ReportError chan error // panic时错误写入的error channel
Name string // 服务名称
PersistId uid.Id // 服务持久化Id
EntityLib pt.EntityLib // 实体原型库
PluginBundle plugin.PluginBundle // 插件包
RunningHandler RunningHandler // 运行状态变化处理器
}
var With _Option
type _Option struct{}
// Default 默认值
func (_Option) Default() option.Setting[ContextOptions] {
return func(o *ContextOptions) {
With.CompositeFace(iface.Face[Context]{})(o)
With.Context(nil)(o)
With.PanicHandling(false, nil)(o)
With.Name("")(o)
With.PersistId(uid.Nil)(o)
With.EntityLib(pt.DefaultEntityLib())(o)
With.PluginBundle(plugin.NewPluginBundle())(o)
With.RunningHandler(nil)(o)
}
}
// CompositeFace 扩展者,在扩展服务上下文自身能力时使用
func (_Option) CompositeFace(face iface.Face[Context]) option.Setting[ContextOptions] {
return func(o *ContextOptions) {
o.CompositeFace = face
}
}
// Context 父Context
func (_Option) Context(ctx context.Context) option.Setting[ContextOptions] {
return func(o *ContextOptions) {
o.Context = ctx
}
}
// PanicHandling panic时的处理方式
func (_Option) PanicHandling(autoRecover bool, reportError chan error) option.Setting[ContextOptions] {
return func(o *ContextOptions) {
o.AutoRecover = autoRecover
o.ReportError = reportError
}
}
// Name 服务名称
func (_Option) Name(name string) option.Setting[ContextOptions] {
return func(o *ContextOptions) {
o.Name = name
}
}
// PersistId 服务持久化Id
func (_Option) PersistId(id uid.Id) option.Setting[ContextOptions] {
return func(o *ContextOptions) {
o.PersistId = id
}
}
// EntityLib 实体原型库
func (_Option) EntityLib(lib pt.EntityLib) option.Setting[ContextOptions] {
return func(o *ContextOptions) {
o.EntityLib = lib
}
}
// PluginBundle 插件包
func (_Option) PluginBundle(bundle plugin.PluginBundle) option.Setting[ContextOptions] {
return func(o *ContextOptions) {
o.PluginBundle = bundle
}
}
// RunningHandler 运行状态变化处理器
func (_Option) RunningHandler(handler RunningHandler) option.Setting[ContextOptions] {
return func(o *ContextOptions) {
o.RunningHandler = handler
}
}