-
Notifications
You must be signed in to change notification settings - Fork 0
/
baseObservable.go
92 lines (82 loc) · 1.68 KB
/
baseObservable.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
package rx
import (
"fmt"
"sync"
"time"
)
type outputMetadata struct {
ch chan<- interface{}
seq uint64
done uint64
}
type baseObservable struct {
_source Observable
_input chan interface{}
register chan chan<- interface{}
unregister chan chan<- interface{}
outputs map[chan<- interface{}]*outputMetadata
mutex *sync.RWMutex
}
func (o *baseObservable) push(event interface{}, ch chan<- interface{}, seq uint64) {
o.mutex.RLock()
meta, ok := o.outputs[ch]
o.mutex.RUnlock()
if !ok {
return
}
if meta.done+1 < seq {
time.Sleep(time.Duration(seq-meta.done-1) * time.Millisecond)
go o.push(event, ch, seq)
return
}
defer func() {
if err := recover(); err != nil {
fmt.Printf("%#v\n", err)
}
meta.done++
}()
ch <- event
}
func (o *baseObservable) run() {
for {
select {
case event, ok := <-o._input:
if ok {
o.mutex.RLock()
for ch, meta := range o.outputs {
go o.push(event, ch, meta.seq)
meta.seq++
}
o.mutex.RUnlock()
} else {
for ch := range o.outputs {
o.mutex.Lock()
delete(o.outputs, ch)
o.mutex.Unlock()
ch <- closed{}
}
}
case ch, ok := <-o.register:
if ok {
o.mutex.Lock()
o.outputs[ch] = &outputMetadata{ch: ch, seq: 1}
o.mutex.Unlock()
ch <- subscribed{}
}
case ch := <-o.unregister:
o.mutex.Lock()
delete(o.outputs, ch)
o.mutex.Unlock()
ch <- unsubscribed{}
}
}
}
func (o baseObservable) source() Observable {
return o._source
}
func (o baseObservable) input() chan interface{} {
return o._input
}
func (o baseObservable) subscribe(leaf bool, handlers ...EventHandler) *Subscription {
return NewSubscription(o, leaf, handlers...)
}