-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathemitter.go
More file actions
217 lines (186 loc) · 4.37 KB
/
Copy pathemitter.go
File metadata and controls
217 lines (186 loc) · 4.37 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
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package sdk
import (
"fmt"
"github.com/bolaxy/common"
"github.com/bolaxy/common/hexutil"
"github.com/bolaxy/crypto"
)
type Type string
type Kind uint8
type Callback func(*Event)
type Cancel func()
const (
fireOnce Kind = iota
fireAlways
)
type Result struct {
Success bool
ContractAddress *common.Address
IsLog bool
Data []byte
Topics []common.Hash
}
type Event struct {
eventType Type
value interface{}
}
func GenLogType(contractAddr common.Address, eventSig common.Hash) Type {
return Type(hexutil.Encode(crypto.Keccak256(contractAddr.Bytes(), eventSig.Bytes())))
}
func GenHashType(txhash common.Hash) Type {
return Type(txhash.String())
}
func NewEvent(evtType Type, value interface{}) *Event {
return &Event{
eventType: evtType,
value: value,
}
}
func (e *Event) GetValue() interface{} {
return e.value
}
func (e *Event) GetType() Type {
return e.eventType
}
func NewEventEmitter(bufSize int) *Emitter {
ee := &Emitter{
bufSize: bufSize,
events: make(map[uint64]map[Type]Kind),
subscriber: make(map[Type]map[uint64]chan<- *Event),
observer: make(chan *observer),
notify: make(chan *Event), // 如果设置了buf,在cancel阶段会出现竞争问题,然后回调函数中会多次调用cancel
cancellation: make(chan uint64),
}
go ee.run()
return ee
}
type Emitter struct {
counter uint64
bufSize int
events map[uint64]map[Type]Kind
subscriber map[Type]map[uint64]chan<- *Event
observer chan *observer
notify chan *Event
cancellation chan uint64
}
func (ee *Emitter) run() {
for {
select {
case observer := <-ee.observer:
ee.counter += 1
for _, et := range observer.eventTypes {
fmt.Printf("eventType: %s, counter: %d\n", et, ee.counter)
item, ok := ee.events[ee.counter]
if !ok {
item = make(map[Type]Kind)
ee.events[ee.counter] = item
}
item[et] = observer.kind
cb, ok := ee.subscriber[et]
if !ok {
cb = make(map[uint64]chan<- *Event)
ee.subscriber[et] = cb
}
input := make(chan *Event)
newObservable(ee.counter, input, ee.bufSize, observer.fn)
cb[ee.counter] = input
}
observer.signal <- ee.counter
case e := <-ee.notify:
if item, ok := ee.subscriber[e.GetType()]; ok {
for id, ch := range item {
fmt.Printf("%s -> %d\n", e.GetType(), id)
kind := ee.events[id][e.GetType()]
ch <- e
if kind == fireOnce {
fmt.Printf("fireonce ...\n")
delete(ee.events, id)
delete(ee.subscriber[e.GetType()], id)
close(ch)
}
}
}
case id := <-ee.cancellation:
if items, ok := ee.events[id]; ok {
delete(ee.events, id)
for et := range items {
close(ee.subscriber[et][id])
delete(ee.subscriber[et], id)
}
}
}
}
}
func (ee *Emitter) On(fn Callback, eventType ...Type) (cancel Cancel) {
return ee.subscribe(fireAlways, fn, eventType...)
}
func (ee *Emitter) Once(fn Callback, eventType ...Type) (cancel Cancel) {
return ee.subscribe(fireOnce, fn, eventType...)
}
func (ee *Emitter) subscribe(kind Kind, fn Callback, eventType ...Type) (cancel Cancel) {
if len(eventType) == 0 {
return func() {}
}
ch := make(chan uint64, 1)
ee.observer <- &observer{
kind: kind,
fn: fn,
eventTypes: eventType,
signal: ch,
}
id := <-ch
return func() {
ee.cancellation <- id
}
}
func (ee *Emitter) Emit(event *Event) {
ee.notify <- event
}
type observer struct {
kind Kind
fn Callback
eventTypes []Type
signal chan<- uint64
}
type observable struct {
input <-chan *Event
output chan *Event
fn Callback
index uint64
}
// output must buffered channel
func newObservable(index uint64, input <-chan *Event, bufSize int, fn Callback) *observable {
if bufSize <= 0 {
bufSize = 128
}
output := make(chan *Event, bufSize)
r := &observable{
input: input,
output: output,
fn: fn,
index: index,
}
go r.consume()
go r.run()
return r
}
func (r *observable) run() {
for v := range r.input {
select {
case r.output <- v:
default:
x := <-r.output
fmt.Printf("(%d)Warning: drop event: %v\n", r.index, x)
r.output <- v
}
}
fmt.Printf("(%d) quit.\n", r.index)
close(r.output)
}
func (r *observable) consume() {
for e := range r.output {
fmt.Printf("(%d)consume 1----> %v\n", r.index, e)
r.fn(e)
fmt.Printf("(%d)consume 2----> %v\n", r.index, e)
}
}