/
context_options.go
91 lines (79 loc) · 2.68 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
package runtime
import (
"context"
"git.golaxy.org/core/plugin"
"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
PluginBundle plugin.PluginBundle // 插件包
RunningHandler RunningHandler // 运行状态变化处理器
}
type _ContextOption struct{}
// Default 默认值
func (_ContextOption) Default() option.Setting[ContextOptions] {
return func(o *ContextOptions) {
With.Context.CompositeFace(iface.Face[Context]{})(o)
With.Context.Context(nil)(o)
With.Context.PanicHandling(false, nil)(o)
With.Context.Name("")(o)
With.Context.PersistId(uid.Nil)(o)
With.Context.PluginBundle(plugin.NewPluginBundle())(o)
With.Context.RunningHandler(nil)(o)
}
}
// CompositeFace 扩展者,在扩展运行时上下文自身能力时使用
func (_ContextOption) CompositeFace(face iface.Face[Context]) option.Setting[ContextOptions] {
return func(o *ContextOptions) {
o.CompositeFace = face
}
}
// Context 父Context
func (_ContextOption) Context(ctx context.Context) option.Setting[ContextOptions] {
return func(o *ContextOptions) {
o.Context = ctx
}
}
// PanicHandling panic时的处理方式
func (_ContextOption) PanicHandling(autoRecover bool, reportError chan error) option.Setting[ContextOptions] {
return func(o *ContextOptions) {
o.AutoRecover = autoRecover
o.ReportError = reportError
}
}
// Name 运行时名称
func (_ContextOption) Name(name string) option.Setting[ContextOptions] {
return func(o *ContextOptions) {
o.Name = name
}
}
// PersistId 运行时持久化Id
func (_ContextOption) PersistId(id uid.Id) option.Setting[ContextOptions] {
return func(o *ContextOptions) {
o.PersistId = id
}
}
// PluginBundle 插件包
func (_ContextOption) PluginBundle(bundle plugin.PluginBundle) option.Setting[ContextOptions] {
return func(o *ContextOptions) {
o.PluginBundle = bundle
}
}
// RunningHandler 运行状态变化处理器
func (_ContextOption) RunningHandler(handler RunningHandler) option.Setting[ContextOptions] {
return func(o *ContextOptions) {
o.RunningHandler = handler
}
}