-
Notifications
You must be signed in to change notification settings - Fork 11
/
async_sequence.go
157 lines (123 loc) · 3.21 KB
/
async_sequence.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
package co
import (
"sync"
syncx "go.tempura.ink/co/internal/syncx"
)
type asyncSequence[R any] struct {
async AsyncSequenceable[R]
_defaultIterator Iterator[R]
}
func NewAsyncSequence[R any](it AsyncSequenceable[R]) *asyncSequence[R] {
return &asyncSequence[R]{async: it}
}
func (a *asyncSequence[R]) defaultIterator() Iterator[R] {
if a._defaultIterator != nil {
return a._defaultIterator
}
a._defaultIterator = a.async.iterator()
return a._defaultIterator
}
func (a *asyncSequence[R]) Iter() <-chan R {
return a.defaultIterator().Iter()
}
func (a *asyncSequence[R]) EIter() <-chan *data[R] {
return a.defaultIterator().EIter()
}
func (a *asyncSequence[R]) AdjacentFilter(fn func(R, R) bool) *AsyncAdjacentFilterSequence[R] {
return NewAsyncAdjacentFilterSequence(a.async, fn)
}
func (a *asyncSequence[R]) Merge(its ...AsyncSequenceable[R]) *AsyncMergedSequence[R] {
its = append([]AsyncSequenceable[R]{a.async}, its...)
return NewAsyncMergedSequence(its...)
}
type ErrorMode int
const (
ErrorModeSkip ErrorMode = iota
ErrorModeStop
)
func (e ErrorMode) shouldSkip() bool {
return e == ErrorModeSkip
}
func (e ErrorMode) shouldStop() bool {
return e == ErrorModeStop
}
type asyncSequenceIterator[T any] struct {
delegated Iterator[T]
mux sync.RWMutex
runOnce sync.Once
emitCh []chan T
emitECh []chan *data[T]
dataPool sync.Pool
errorMode ErrorMode
successFn func(T)
failedFn func(error)
}
func NewAsyncSequenceIterator[T any](it Iterator[T]) *asyncSequenceIterator[T] {
a := &asyncSequenceIterator[T]{
delegated: it,
emitCh: make([]chan T, 0),
emitECh: make([]chan *data[T], 0),
dataPool: sync.Pool{},
}
a.dataPool.New = func() any { return NewData[T]() }
return a
}
func (it *asyncSequenceIterator[T]) nextAny() *Optional[any] {
return it.delegated.next().AsOptional()
}
func (it *asyncSequenceIterator[T]) handleError(err error) {
it.failedFn(err)
d := it.dataPool.Get().(*data[T]).set(*new(T), err)
it.emitData(d)
}
func (it *asyncSequenceIterator[T]) emitData(d *data[T]) {
defer func() { it.dataPool.Put(d) }()
it.mux.RLock()
defer it.mux.RUnlock()
for i := range it.emitECh {
syncx.SafeSend(it.emitECh[i], d)
}
if d.err != nil {
return
}
for i := range it.emitCh {
syncx.SafeSend(it.emitCh[i], d.GetValue())
}
}
func (it *asyncSequenceIterator[T]) startListening() {
it.runOnce.Do(func() {
syncx.SafeGo(func() {
for op := it.delegated.next(); op.valid; op = it.delegated.next() {
d := it.dataPool.Get().(*data[T]).set(op.data, nil)
it.emitData(d)
if it.successFn != nil {
syncx.SafeGo(func() { it.successFn(op.data) })
}
}
for _, ch := range it.emitCh {
syncx.SafeClose(ch)
}
})
})
}
func (it *asyncSequenceIterator[T]) Iter() <-chan T {
it.mux.Lock()
defer it.mux.Unlock()
eCh := make(chan T)
it.emitCh = append(it.emitCh, eCh)
it.startListening()
return eCh
}
func (it *asyncSequenceIterator[T]) EIter() <-chan *data[T] {
it.mux.Lock()
defer it.mux.Unlock()
eCh := make(chan *data[T])
it.emitECh = append(it.emitECh, eCh)
it.startListening()
return eCh
}
func (it *asyncSequenceIterator[T]) ForEach(success func(T), failed func(error)) {
it.successFn = success
it.failedFn = failed
it.startListening()
}