-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobserver.go
184 lines (150 loc) · 3.95 KB
/
observer.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package observer
import (
"reflect"
"sync"
)
const (
event = "event"
)
// Observer 观察者 模式
type Observer[E any] interface {
Subscriber
Publisher[E]
Wait()
}
// Subscriber 订阅者
//
// param observer must be a reflect.Struct
//
// observer can provide Topic or Function interface
//
// or probide event field.
//
// for example:
/*
type person struct {
event string `topic:"pain" notice:"Say"`
}
func(p*person) Say() {
// do sth
}
*/
type Subscriber interface {
// 订阅, 去重
//
// 返回的func是为 取消订阅
Subscribe(observer interface{}) func()
// // 取消订阅
// Unsubscribe(observer interface{})
// 订阅 topic 函数, 不去重
//
// 返回的func是为 取消订阅
SubscribeByTopicFunc(topic string, fc interface{}) func()
}
// Publisher 发布者
type Publisher[E any] interface {
// 异步推送
Publish(topic string, extra E, args ...interface{})
// 异步推送,监听返回值
//
// @params: retfc 其参数类型和顺序与消费者保持一致, 如没有返回值可为空
PublishWithRet(topic string, extra E, retfc interface{}, args ...interface{})
// 同步推送
SyncPublish(topic string, extra E, args ...interface{})
// 同步推送,监听返回值
//
// @params: retfc 其参数类型和顺序与消费者保持一致, 如没有返回值可为空
SyncPublishWithRet(topic string, extra E, retfc interface{}, args ...interface{})
}
// Topic 主题名
type Topic interface {
Topic() string
}
// Function 通知函数
type Function interface {
Function() string
}
type observer[E any] struct {
handler *syncHandler
wait sync.WaitGroup
opt *Options[E]
}
// NewObserver Observer
func NewObserver[E any](opts ...Option[E]) Observer[E] {
opt := _defaultOpt[E]()
for _, v := range opts {
v(opt)
}
return &observer[E]{
handler: &syncHandler{
rwlock: new(sync.RWMutex),
m: map[string]handlers{},
},
opt: opt,
}
}
func (o *observer[E]) Subscribe(observer interface{}) func() {
t, topic, fc := checkObserver(observer, o.opt.eventField)
h := newHandler(t, observer, fc)
o.handler.Append(topic, true, h)
return func() {
o.handler.Del(topic, h)
}
}
// func (o *observer[E]) Unsubscribe(observer interface{}) {
// t, topic, fc := checkObserver(observer, event)
// o.handler.Del(topic, newHandler(t, observer, fc))
// }
func (o *observer[E]) SubscribeByTopicFunc(topic string, fc interface{}) func() {
t, v := checkFunc(fc)
h := newHandler(t, fc, v)
o.handler.Append(topic, false, h)
return func() {
o.handler.Del(topic, h)
}
}
func (o *observer[E]) Publish(topic string, extra E, args ...interface{}) {
o.publish(false, topic, extra, nil, args...)
}
func (o *observer[E]) PublishWithRet(topic string, extra E, retfc interface{}, args ...interface{}) {
o.publish(false, topic, extra, retfc, args...)
}
func (o *observer[E]) SyncPublish(topic string, extra E, args ...interface{}) {
o.publish(true, topic, extra, nil, args...)
}
func (o *observer[E]) SyncPublishWithRet(topic string, extra E, retfc interface{}, args ...interface{}) {
o.publish(true, topic, extra, retfc, args...)
}
func (o *observer[E]) publish(sync bool, topic string, extra E, retfc interface{}, args ...interface{}) {
fc := reflect.Zero(reflect.TypeOf(0))
if retfc != nil {
_, fc = checkFunc(retfc)
}
handlers := o.handler.Get(topic)
params := []reflect.Value{}
for _, arg := range args {
params = append(params, reflect.ValueOf(arg))
}
for _, handler := range handlers {
o.onNotice(sync, topic, extra, handler, fc, params, args)
}
}
func (o *observer[E]) onNotice(sync bool, topic string, extra E, handler *handler, fc reflect.Value, params []reflect.Value, args ...interface{}) {
h := func() {
defer o.opt.recover(topic, extra, handler.callback, args)
defer o.wait.Done()
ret := handler.Call(params...)
if !fc.IsZero() {
fc.Call(ret)
}
}
o.wait.Add(1)
if sync {
o.opt.syncExec(topic, extra, h)
return
}
o.opt.asyncExec(topic, extra, h)
}
func (o *observer[E]) Wait() {
o.wait.Wait()
}