-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptions.go
More file actions
61 lines (49 loc) · 1.47 KB
/
options.go
File metadata and controls
61 lines (49 loc) · 1.47 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
package observer
import "reflect"
// Options options
type Options[E any] struct {
eventField string // default event
// recover
recover func(topic string, extra E, cb reflect.Value, args ...interface{})
// exec
asyncExec func(topic string, extra E, fn func())
syncExec func(topic string, extra E, fn func())
}
// Option option
type Option[E any] func(o *Options[E])
func _defaultOpt[E any]() *Options[E] {
return &Options[E]{
eventField: event,
recover: func(topic string, extra E, cb reflect.Value, args ...interface{}) {
if err := recover(); err != nil {
panic([]interface{}{topic, extra, cb.Type().PkgPath(), cb.Type().Name(), cb.String(), args, err})
}
},
asyncExec: func(topic string, extra E, fn func()) { go fn() },
syncExec: func(topic string, extra E, fn func()) { fn() },
}
}
// WithEventField event field default event
func WithEventField[E any](event string) Option[E] {
return func(o *Options[E]) {
o.eventField = event
}
}
// WithRecover recover default
func WithRecover[E any](re func(topic string, extra E, cb reflect.Value, args ...interface{})) Option[E] {
return func(o *Options[E]) {
o.recover = re
}
}
// WithAsyncExec async exec
func WithAsyncExec[E any](exec func(topic string, extra E, fn func())) Option[E] {
return func(o *Options[E]) {
o.asyncExec = exec
}
}
// WithSyncExec sync exec
func WithSyncExec[E any](exec func(topic string, extra E, fn func())) Option[E] {
return func(o *Options[E]) {
o.syncExec = exec
}
}