-
Notifications
You must be signed in to change notification settings - Fork 338
/
factory.go
363 lines (326 loc) · 7.74 KB
/
factory.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
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
package rxgo
import (
"context"
"math"
"sync"
"sync/atomic"
"time"
)
// Amb takes several Observables, emit all of the items from only the first of these Observables
// to emit an item or notification.
func Amb(observables []Observable, opts ...Option) Observable {
option := parseOptions(opts...)
ctx := option.buildContext(emptyContext)
next := option.buildChannel()
once := sync.Once{}
f := func(o Observable) {
it := o.Observe(opts...)
select {
case <-ctx.Done():
return
case item, ok := <-it:
if !ok {
return
}
once.Do(func() {
defer close(next)
if item.Error() {
next <- item
return
}
next <- item
for {
select {
case <-ctx.Done():
return
case item, ok := <-it:
if !ok {
return
}
if item.Error() {
next <- item
return
}
next <- item
}
}
})
}
}
for _, o := range observables {
go f(o)
}
return &ObservableImpl{
iterable: newChannelIterable(next),
}
}
// CombineLatest combines the latest item emitted by each Observable via a specified function
// and emit items based on the results of this function.
func CombineLatest(f FuncN, observables []Observable, opts ...Option) Observable {
option := parseOptions(opts...)
ctx := option.buildContext(emptyContext)
next := option.buildChannel()
go func() {
size := uint32(len(observables))
var counter uint32
s := make([]interface{}, size)
mutex := sync.Mutex{}
wg := sync.WaitGroup{}
wg.Add(int(size))
errCh := make(chan struct{})
handler := func(ctx context.Context, it Iterable, i int) {
defer wg.Done()
observe := it.Observe(opts...)
for {
select {
case <-ctx.Done():
return
case item, ok := <-observe:
if !ok {
return
}
if item.Error() {
next <- item
errCh <- struct{}{}
return
}
if s[i] == nil {
atomic.AddUint32(&counter, 1)
}
mutex.Lock()
s[i] = item.V
if atomic.LoadUint32(&counter) == size {
next <- Of(f(s...))
}
mutex.Unlock()
}
}
}
ctx, cancel := context.WithCancel(ctx)
for i, o := range observables {
go handler(ctx, o, i)
}
go func() {
for range errCh {
cancel()
}
}()
wg.Wait()
close(next)
close(errCh)
}()
return &ObservableImpl{
iterable: newChannelIterable(next),
}
}
// Concat emits the emissions from two or more Observables without interleaving them.
func Concat(observables []Observable, opts ...Option) Observable {
option := parseOptions(opts...)
ctx := option.buildContext(emptyContext)
next := option.buildChannel()
go func() {
defer close(next)
for _, obs := range observables {
observe := obs.Observe(opts...)
loop:
for {
select {
case <-ctx.Done():
return
case item, ok := <-observe:
if !ok {
break loop
}
if item.Error() {
next <- item
return
}
next <- item
}
}
}
}()
return &ObservableImpl{
iterable: newChannelIterable(next),
}
}
// Create creates an Observable from scratch by calling observer methods programmatically.
func Create(f []Producer, opts ...Option) Observable {
return &ObservableImpl{
iterable: newCreateIterable(f, opts...),
}
}
// Defer does not create the Observable until the observer subscribes,
// and creates a fresh Observable for each observer.
func Defer(f []Producer, opts ...Option) Observable {
return &ObservableImpl{
iterable: newDeferIterable(f, opts...),
}
}
// Empty creates an Observable with no item and terminate immediately.
func Empty() Observable {
next := make(chan Item)
close(next)
return &ObservableImpl{
iterable: newChannelIterable(next),
}
}
// FromChannel creates a cold observable from a channel.
func FromChannel(next <-chan Item, opts ...Option) Observable {
option := parseOptions(opts...)
ctx := option.buildContext(emptyContext)
return &ObservableImpl{
parent: ctx,
iterable: newChannelIterable(next, opts...),
}
}
// FromEventSource creates a hot observable from a channel.
func FromEventSource(next <-chan Item, opts ...Option) Observable {
option := parseOptions(opts...)
return &ObservableImpl{
iterable: newEventSourceIterable(option.buildContext(emptyContext), next, option.getBackPressureStrategy()),
}
}
// Interval creates an Observable emitting incremental integers infinitely between
// each given time interval.
func Interval(interval Duration, opts ...Option) Observable {
option := parseOptions(opts...)
next := option.buildChannel()
ctx := option.buildContext(emptyContext)
go func() {
i := 0
for {
select {
case <-time.After(interval.duration()):
if !Of(i).SendContext(ctx, next) {
return
}
i++
case <-ctx.Done():
close(next)
return
}
}
}()
return &ObservableImpl{
iterable: newEventSourceIterable(ctx, next, option.getBackPressureStrategy()),
}
}
// Just creates an Observable with the provided items.
func Just(items ...interface{}) func(opts ...Option) Observable {
return func(opts ...Option) Observable {
return &ObservableImpl{
iterable: newJustIterable(items...)(opts...),
}
}
}
// JustItem creates a single from one item.
func JustItem(item interface{}, opts ...Option) Single {
return &SingleImpl{
iterable: newJustIterable(item)(opts...),
}
}
// Merge combines multiple Observables into one by merging their emissions
func Merge(observables []Observable, opts ...Option) Observable {
option := parseOptions(opts...)
ctx := option.buildContext(emptyContext)
next := option.buildChannel()
wg := sync.WaitGroup{}
wg.Add(len(observables))
f := func(o Observable) {
defer wg.Done()
observe := o.Observe(opts...)
for {
select {
case <-ctx.Done():
return
case item, ok := <-observe:
if !ok {
return
}
if item.Error() {
next <- item
return
}
next <- item
}
}
}
for _, o := range observables {
go f(o)
}
go func() {
wg.Wait()
close(next)
}()
return &ObservableImpl{
iterable: newChannelIterable(next),
}
}
// Never creates an Observable that emits no items and does not terminate.
func Never() Observable {
next := make(chan Item)
return &ObservableImpl{
iterable: newChannelIterable(next),
}
}
// Range creates an Observable that emits count sequential integers beginning
// at start.
func Range(start, count int, opts ...Option) Observable {
if count < 0 {
return Thrown(IllegalInputError{error: "count must be positive"})
}
if start+count-1 > math.MaxInt32 {
return Thrown(IllegalInputError{error: "max value is bigger than math.MaxInt32"})
}
return &ObservableImpl{
iterable: newRangeIterable(start, count, opts...),
}
}
// Start creates an Observable from one or more directive-like Supplier
// and emits the result of each operation asynchronously on a new Observable.
func Start(fs []Supplier, opts ...Option) Observable {
option := parseOptions(opts...)
next := option.buildChannel()
ctx := option.buildContext(emptyContext)
go func() {
defer close(next)
for _, f := range fs {
select {
case <-ctx.Done():
return
case next <- f(ctx):
}
}
}()
return &ObservableImpl{
iterable: newChannelIterable(next),
}
}
// Thrown creates an Observable that emits no items and terminates with an error.
func Thrown(err error) Observable {
next := make(chan Item, 1)
next <- Error(err)
close(next)
return &ObservableImpl{
iterable: newChannelIterable(next),
}
}
// Timer returns an Observable that completes after a specified delay.
func Timer(d Duration, opts ...Option) Observable {
option := parseOptions(opts...)
next := make(chan Item, 1)
ctx := option.buildContext(emptyContext)
go func() {
defer close(next)
select {
case <-ctx.Done():
return
case <-time.After(d.duration()):
return
}
}()
return &ObservableImpl{
iterable: newChannelIterable(next),
}
}