-
Notifications
You must be signed in to change notification settings - Fork 1
/
rx.go
4183 lines (3772 loc) · 119 KB
/
rx.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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Code generated by jig; DO NOT EDIT.
//go:generate jig
package rx
import (
"fmt"
"math"
"runtime"
"sync"
"sync/atomic"
"time"
"github.com/reactivego/scheduler"
"github.com/reactivego/subscriber"
)
//jig:name Subscriber
// Subscriber is an interface that can be passed in when subscribing to an
// Observable. It allows a set of observable subscriptions to be canceled
// from a single subscriber at the root of the subscription tree.
type Subscriber = subscriber.Subscriber
//jig:name NewSubscriber
// NewSubscriber creates a new subscriber.
func NewSubscriber() Subscriber {
return subscriber.New()
}
//jig:name Scheduler
// Scheduler is used to schedule tasks to support subscribing and observing.
type Scheduler = scheduler.Scheduler
//jig:name GoroutineScheduler
func GoroutineScheduler() Scheduler {
return scheduler.Goroutine
}
//jig:name MakeTrampolineScheduler
func MakeTrampolineScheduler() Scheduler {
return scheduler.MakeTrampoline()
}
//jig:name Observer
// Observer is a function that gets called whenever the Observable has
// something to report. The next argument is the item value that is only
// valid when the done argument is false. When done is true and the err
// argument is not nil, then the Observable has terminated with an error.
// When done is true and the err argument is nil, then the Observable has
// completed normally.
type Observer func(next interface{}, err error, done bool)
//jig:name Observable
// Observable is a function taking an Observer, Scheduler and Subscriber.
// Calling it will subscribe the Observer to events from the Observable.
type Observable func(Observer, Scheduler, Subscriber)
//jig:name BoolObserver
// BoolObserver is a function that gets called whenever the Observable has
// something to report. The next argument is the item value that is only
// valid when the done argument is false. When done is true and the err
// argument is not nil, then the Observable has terminated with an error.
// When done is true and the err argument is nil, then the Observable has
// completed normally.
type BoolObserver func(next bool, err error, done bool)
//jig:name ObservableBool
// ObservableBool is a function taking an Observer, Scheduler and Subscriber.
// Calling it will subscribe the Observer to events from the Observable.
type ObservableBool func(BoolObserver, Scheduler, Subscriber)
//jig:name IntObserver
// IntObserver is a function that gets called whenever the Observable has
// something to report. The next argument is the item value that is only
// valid when the done argument is false. When done is true and the err
// argument is not nil, then the Observable has terminated with an error.
// When done is true and the err argument is nil, then the Observable has
// completed normally.
type IntObserver func(next int, err error, done bool)
//jig:name ObservableInt
// ObservableInt is a function taking an Observer, Scheduler and Subscriber.
// Calling it will subscribe the Observer to events from the Observable.
type ObservableInt func(IntObserver, Scheduler, Subscriber)
//jig:name ObservableObserver
// ObservableObserver is a function that gets called whenever the Observable has
// something to report. The next argument is the item value that is only
// valid when the done argument is false. When done is true and the err
// argument is not nil, then the Observable has terminated with an error.
// When done is true and the err argument is nil, then the Observable has
// completed normally.
type ObservableObserver func(next Observable, err error, done bool)
//jig:name ObservableObservable
// ObservableObservable is a function taking an Observer, Scheduler and Subscriber.
// Calling it will subscribe the Observer to events from the Observable.
type ObservableObservable func(ObservableObserver, Scheduler, Subscriber)
//jig:name SliceObserver
// SliceObserver is a function that gets called whenever the Observable has
// something to report. The next argument is the item value that is only
// valid when the done argument is false. When done is true and the err
// argument is not nil, then the Observable has terminated with an error.
// When done is true and the err argument is nil, then the Observable has
// completed normally.
type SliceObserver func(next Slice, err error, done bool)
//jig:name ObservableSlice
// ObservableSlice is a function taking an Observer, Scheduler and Subscriber.
// Calling it will subscribe the Observer to events from the Observable.
type ObservableSlice func(SliceObserver, Scheduler, Subscriber)
//jig:name Error
// Error signals an error condition.
type Error func(error)
//jig:name Complete
// Complete signals that no more data is to be expected.
type Complete func()
//jig:name Canceled
// Canceled returns true when the observer has unsubscribed.
type Canceled func() bool
//jig:name Next
// Next can be called to emit the next value to the IntObserver.
type Next func(interface{})
//jig:name Create
// Create provides a way of creating an Observable from
// scratch by calling observer methods programmatically.
//
// The create function provided to Create will be called once
// to implement the observable. It is provided with a Next, Error,
// Complete and Canceled function that can be called by the code that
// implements the Observable.
func Create(create func(Next, Error, Complete, Canceled)) Observable {
observable := func(observe Observer, scheduler Scheduler, subscriber Subscriber) {
runner := scheduler.Schedule(func() {
if !subscriber.Subscribed() {
return
}
n := func(next interface{}) {
if subscriber.Subscribed() {
observe(next, nil, false)
}
}
e := func(err error) {
if subscriber.Subscribed() {
var zero interface{}
observe(zero, err, true)
}
}
c := func() {
if subscriber.Subscribed() {
var zero interface{}
observe(zero, nil, true)
}
}
x := func() bool {
return !subscriber.Subscribed()
}
create(n, e, c, x)
})
subscriber.OnUnsubscribe(runner.Cancel)
}
return observable
}
//jig:name CreateRecursive
// CreateRecursive provides a way of creating an Observable from
// scratch by calling observer methods programmatically.
//
// The create function provided to CreateRecursive will be called
// repeatedly to implement the observable. It is provided with a Next, Error
// and Complete function that can be called by the code that implements the
// Observable.
func CreateRecursive(create func(Next, Error, Complete)) Observable {
observable := func(observe Observer, scheduler Scheduler, subscriber Subscriber) {
done := false
runner := scheduler.ScheduleRecursive(func(self func()) {
if !subscriber.Subscribed() {
return
}
n := func(next interface{}) {
if subscriber.Subscribed() {
observe(next, nil, false)
}
}
e := func(err error) {
done = true
if subscriber.Subscribed() {
var zero interface{}
observe(zero, err, true)
}
}
c := func() {
done = true
if subscriber.Subscribed() {
var zero interface{}
observe(zero, nil, true)
}
}
create(n, e, c)
if !done && subscriber.Subscribed() {
self()
}
})
subscriber.OnUnsubscribe(runner.Cancel)
}
return observable
}
//jig:name CreateFutureRecursive
// CreateFutureRecursive provides a way of creating an Observable from
// scratch by calling observer methods programmatically.
//
// The create function provided to CreateFutureRecursive will be called
// repeatedly to implement the observable. It is provided with a Next, Error
// and Complete function that can be called by the code that implements the
// Observable.
//
// The timeout passed in determines the time before calling the create
// function. The time.Duration returned by the create function determines how
// long CreateFutureRecursive has to wait before calling the create function
// again.
func CreateFutureRecursive(timeout time.Duration, create func(Next, Error, Complete) time.Duration) Observable {
observable := func(observe Observer, scheduler Scheduler, subscriber Subscriber) {
done := false
runner := scheduler.ScheduleFutureRecursive(timeout, func(self func(time.Duration)) {
if !subscriber.Subscribed() {
return
}
n := func(next interface{}) {
if subscriber.Subscribed() {
observe(next, nil, false)
}
}
e := func(err error) {
done = true
if subscriber.Subscribed() {
var zero interface{}
observe(zero, err, true)
}
}
c := func() {
done = true
if subscriber.Subscribed() {
var zero interface{}
observe(zero, nil, true)
}
}
timeout = create(n, e, c)
if !done && subscriber.Subscribed() {
self(timeout)
}
})
subscriber.OnUnsubscribe(runner.Cancel)
}
return observable
}
//jig:name Defer
// Defer does not create the Observable until the observer subscribes.
// It creates a fresh Observable for each subscribing observer. Use it to
// create observables that maintain separate state per subscription.
func Defer(factory func() Observable) Observable {
observable := func(observe Observer, scheduler Scheduler, subscriber Subscriber) {
factory()(observe, scheduler, subscriber)
}
return observable
}
//jig:name Empty
// Empty creates an Observable that emits no items but terminates normally.
func Empty() Observable {
observable := func(observe Observer, scheduler Scheduler, subscriber Subscriber) {
runner := scheduler.Schedule(func() {
if subscriber.Subscribed() {
var zero interface{}
observe(zero, nil, true)
}
})
subscriber.OnUnsubscribe(runner.Cancel)
}
return observable
}
//jig:name From
// From creates an Observable from multiple interface{} values passed in.
func From(slice ...interface{}) Observable {
observable := func(observe Observer, scheduler Scheduler, subscriber Subscriber) {
i := 0
runner := scheduler.ScheduleRecursive(func(self func()) {
if subscriber.Subscribed() {
if i < len(slice) {
observe(slice[i], nil, false)
if subscriber.Subscribed() {
i++
self()
}
} else {
var zero interface{}
observe(zero, nil, true)
}
}
})
subscriber.OnUnsubscribe(runner.Cancel)
}
return observable
}
//jig:name FromChan
// FromChan creates an Observable from a Go channel of interface{}
// values. This allows the code feeding into the channel to send either an error
// or the next value. The feeding code can send nil or more items and then
// closing the channel will be seen as completion. When the feeding code sends
// an error into the channel, it should close the channel immediately to
// indicate termination with error.
func FromChan(ch <-chan interface{}) Observable {
observable := func(observe Observer, scheduler Scheduler, subscriber Subscriber) {
cancel := make(chan struct{})
runner := scheduler.ScheduleRecursive(func(self func()) {
if !subscriber.Subscribed() {
return
}
select {
case next, ok := <-ch:
if !subscriber.Subscribed() {
return
}
if ok {
err, ok := next.(error)
if !ok {
observe(next, nil, false)
if !subscriber.Subscribed() {
return
}
self()
} else {
observe(nil, err, true)
}
} else {
observe(nil, nil, true)
}
case <-cancel:
return
}
})
subscriber.OnUnsubscribe(func() {
runner.Cancel()
close(cancel)
})
}
return observable
}
//jig:name Of
// Of emits a variable amount of values in a sequence and then emits a
// complete notification.
func Of(slice ...interface{}) Observable {
observable := func(observe Observer, scheduler Scheduler, subscriber Subscriber) {
i := 0
runner := scheduler.ScheduleRecursive(func(self func()) {
if subscriber.Subscribed() {
if i < len(slice) {
observe(slice[i], nil, false)
if subscriber.Subscribed() {
i++
self()
}
} else {
var zero interface{}
observe(zero, nil, true)
}
}
})
subscriber.OnUnsubscribe(runner.Cancel)
}
return observable
}
//jig:name Interval
// Interval creates an Observable that emits a sequence of integers spaced
// by a particular time interval. First integer is not emitted immediately, but
// only after the first time interval has passed. The generated code will do a type
// conversion from int to interface{}.
func Interval(interval time.Duration) Observable {
observable := func(observe Observer, subscribeOn Scheduler, subscriber Subscriber) {
i := 0
runner := subscribeOn.ScheduleFutureRecursive(interval, func(self func(time.Duration)) {
if subscriber.Subscribed() {
observe(interface{}(i), nil, false)
i++
if subscriber.Subscribed() {
self(interval)
}
}
})
subscriber.OnUnsubscribe(runner.Cancel)
}
return observable
}
//jig:name Just
// Just creates an Observable that emits a particular item.
func Just(element interface{}) Observable {
observable := func(observe Observer, scheduler Scheduler, subscriber Subscriber) {
runner := scheduler.Schedule(func() {
if subscriber.Subscribed() {
observe(element, nil, false)
}
if subscriber.Subscribed() {
var zero interface{}
observe(zero, nil, true)
}
})
subscriber.OnUnsubscribe(runner.Cancel)
}
return observable
}
//jig:name Never
// Never creates an Observable that emits no items and does't terminate.
func Never() Observable {
observable := func(observe Observer, scheduler Scheduler, subscriber Subscriber) {
}
return observable
}
//jig:name Range
// Range creates an Observable that emits a range of sequential int values.
// The generated code will do a type conversion from int to interface{}.
func Range(start, count int) Observable {
end := start + count
observable := func(observe Observer, scheduler Scheduler, subscriber Subscriber) {
i := start
runner := scheduler.ScheduleRecursive(func(self func()) {
if subscriber.Subscribed() {
if i < end {
observe(interface{}(i), nil, false)
if subscriber.Subscribed() {
i++
self()
}
} else {
var zero interface{}
observe(zero, nil, true)
}
}
})
subscriber.OnUnsubscribe(runner.Cancel)
}
return observable
}
//jig:name Start
// Start creates an Observable that emits the return value of a function.
// It is designed to be used with a function that returns a (interface{}, error) tuple.
// If the error is non-nil the returned Observable will be an Observable that
// emits and error, otherwise it will be a single-value Observable of the value.
func Start(f func() (interface{}, error)) Observable {
observable := func(observe Observer, scheduler Scheduler, subscriber Subscriber) {
done := false
runner := scheduler.ScheduleRecursive(func(self func()) {
if subscriber.Subscribed() {
if !done {
if next, err := f(); err == nil {
observe(next, nil, false)
if subscriber.Subscribed() {
done = true
self()
}
} else {
var zero interface{}
observe(zero, err, true)
}
} else {
var zero interface{}
observe(zero, nil, true)
}
}
})
subscriber.OnUnsubscribe(runner.Cancel)
}
return observable
}
//jig:name Throw
// Throw creates an Observable that emits no items and terminates with an
// error.
func Throw(err error) Observable {
observable := func(observe Observer, scheduler Scheduler, subscriber Subscriber) {
runner := scheduler.Schedule(func() {
if subscriber.Subscribed() {
var zero interface{}
observe(zero, err, true)
}
})
subscriber.OnUnsubscribe(runner.Cancel)
}
return observable
}
//jig:name RxError
type RxError string
func (e RxError) Error() string { return string(e) }
//jig:name Time
type Time = time.Time
//jig:name TimeObserver
// TimeObserver is a function that gets called whenever the Observable has
// something to report. The next argument is the item value that is only
// valid when the done argument is false. When done is true and the err
// argument is not nil, then the Observable has terminated with an error.
// When done is true and the err argument is nil, then the Observable has
// completed normally.
type TimeObserver func(next Time, err error, done bool)
//jig:name ObservableTime
// ObservableTime is a function taking an Observer, Scheduler and Subscriber.
// Calling it will subscribe the Observer to events from the Observable.
type ObservableTime func(TimeObserver, Scheduler, Subscriber)
//jig:name Ticker
// Ticker creates an ObservableTime that emits a sequence of timestamps after
// an initialDelay has passed. Subsequent timestamps are emitted using a
// schedule of intervals passed in. If only the initialDelay is given, Ticker
// will emit only once.
func Ticker(initialDelay time.Duration, intervals ...time.Duration) ObservableTime {
observable := func(observe TimeObserver, subscribeOn Scheduler, subscriber Subscriber) {
i := 0
runner := subscribeOn.ScheduleFutureRecursive(initialDelay, func(self func(time.Duration)) {
if subscriber.Subscribed() {
if i == 0 || (i > 0 && len(intervals) > 0) {
observe(subscribeOn.Now(), nil, false)
}
if subscriber.Subscribed() {
if len(intervals) > 0 {
self(intervals[i%len(intervals)])
} else {
if i == 0 {
self(0)
} else {
var zero time.Time
observe(zero, nil, true)
}
}
}
i++
}
})
subscriber.OnUnsubscribe(runner.Cancel)
}
return observable
}
//jig:name Timer
// Timer creates an Observable that emits a sequence of integers
// (starting at zero) after an initialDelay has passed. Subsequent values are
// emitted using a schedule of intervals passed in. If only the initialDelay
// is given, Timer will emit only once.
func Timer(initialDelay time.Duration, intervals ...time.Duration) Observable {
observable := func(observe Observer, subscribeOn Scheduler, subscriber Subscriber) {
i := 0
runner := subscribeOn.ScheduleFutureRecursive(initialDelay, func(self func(time.Duration)) {
if subscriber.Subscribed() {
if i == 0 || (i > 0 && len(intervals) > 0) {
observe(interface{}(i), nil, false)
}
if subscriber.Subscribed() {
if len(intervals) > 0 {
self(intervals[i%len(intervals)])
} else {
if i == 0 {
self(0)
} else {
var zero interface{}
observe(zero, nil, true)
}
}
}
i++
}
})
subscriber.OnUnsubscribe(runner.Cancel)
}
return observable
}
//jig:name Slice
type Slice = []interface{}
//jig:name ObservableObservable_CombineLatestAll
// CombineLatestAll flattens a higher order observable
// (e.g. ObservableObservable) by subscribing to
// all emitted observables (ie. Observable entries) until the source
// completes. It will then wait for all of the subscribed Observables
// to emit before emitting the first slice. Whenever any of the subscribed
// observables emits, a new slice will be emitted containing all the latest
// value.
func (o ObservableObservable) CombineLatestAll() ObservableSlice {
observable := func(observe SliceObserver, subscribeOn Scheduler, subscriber Subscriber) {
observables := []Observable(nil)
var observers struct {
sync.Mutex
assigned []bool
values []interface{}
initialized int
active int
}
makeObserver := func(index int) Observer {
observer := func(next interface{}, err error, done bool) {
observers.Lock()
defer observers.Unlock()
if observers.active > 0 {
switch {
case !done:
if !observers.assigned[index] {
observers.assigned[index] = true
observers.initialized++
}
observers.values[index] = next
if observers.initialized == len(observers.values) {
observe(observers.values, nil, false)
}
case err != nil:
observers.active = 0
var zero []interface{}
observe(zero, err, true)
default:
if observers.active--; observers.active == 0 {
var zero []interface{}
observe(zero, nil, true)
}
}
}
}
return observer
}
observer := func(next Observable, err error, done bool) {
switch {
case !done:
observables = append(observables, next)
case err != nil:
var zero []interface{}
observe(zero, err, true)
default:
subscribeOn.Schedule(func() {
if subscriber.Subscribed() {
numObservables := len(observables)
observers.assigned = make([]bool, numObservables)
observers.values = make([]interface{}, numObservables)
observers.active = numObservables
for i, v := range observables {
if !subscriber.Subscribed() {
return
}
v(makeObserver(i), subscribeOn, subscriber)
}
}
})
}
}
o(observer, subscribeOn, subscriber)
}
return observable
}
//jig:name CombineLatest
// CombineLatest will subscribe to all Observables. It will then wait for
// all of them to emit before emitting the first slice. Whenever any of the
// subscribed observables emits, a new slice will be emitted containing all
// the latest value.
func CombineLatest(observables ...Observable) ObservableSlice {
return FromObservable(observables...).CombineLatestAll()
}
//jig:name Concat
// Concat emits the emissions from two or more Observables without interleaving them.
func Concat(observables ...Observable) Observable {
if len(observables) == 0 {
return Empty()
}
return observables[0].ConcatWith(observables[1:]...)
}
//jig:name Merge
// Merge combines multiple Observables into one by merging their emissions.
// An error from any of the observables will terminate the merged observables.
func Merge(observables ...Observable) Observable {
if len(observables) == 0 {
return Empty()
}
return observables[0].MergeWith(observables[1:]...)
}
//jig:name MergeDelayError
// MergeDelayError combines multiple Observables into one by merging their emissions.
// Any error will be deferred until all observables terminate.
func MergeDelayError(observables ...Observable) Observable {
if len(observables) == 0 {
return Empty()
}
return observables[0].MergeDelayErrorWith(observables[1:]...)
}
//jig:name Println
func Println(a ...interface{}) Observer {
observer := func(next interface{}, err error, done bool) {
if !done {
fmt.Println(append(a, next)...)
}
}
return observer
}
//jig:name Subscription
// Subscription is an alias for the subscriber.Subscription interface type.
type Subscription = subscriber.Subscription
//jig:name Connectable
// Connectable provides the Connect method for a Multicaster.
type Connectable func(Scheduler, Subscriber)
// Connect instructs a multicaster to subscribe to its source and begin
// multicasting items to its subscribers. Connect accepts an optional
// scheduler argument.
func (c Connectable) Connect(schedulers ...Scheduler) Subscription {
subscriber := subscriber.New()
schedulers = append(schedulers, scheduler.MakeTrampoline())
if !schedulers[0].IsConcurrent() {
subscriber.OnWait(schedulers[0].Wait)
}
c(schedulers[0], subscriber)
return subscriber
}
//jig:name Multicaster
// Multicaster is a multicasting connectable observable. One or more
// Observers can subscribe to it simultaneously. It will subscribe to the
// source Observable when Connect is called. After that, every emission
// from the source is multcast to all subscribed Observers.
type Multicaster struct {
Observable
Connectable
}
//jig:name FromObservable
// FromObservable creates an ObservableObservable from multiple Observable values passed in.
func FromObservable(slice ...Observable) ObservableObservable {
observable := func(observe ObservableObserver, scheduler Scheduler, subscriber Subscriber) {
i := 0
runner := scheduler.ScheduleRecursive(func(self func()) {
if subscriber.Subscribed() {
if i < len(slice) {
observe(slice[i], nil, false)
if subscriber.Subscribed() {
i++
self()
}
} else {
var zero Observable
observe(zero, nil, true)
}
}
})
subscriber.OnUnsubscribe(runner.Cancel)
}
return observable
}
//jig:name Observable_ConcatWith
// ConcatWith emits the emissions from two or more Observables without interleaving them.
func (o Observable) ConcatWith(other ...Observable) Observable {
if len(other) == 0 {
return o
}
observable := func(observe Observer, subscribeOn Scheduler, subscriber Subscriber) {
var (
observables = append([]Observable{}, other...)
observer Observer
)
observer = func(next interface{}, err error, done bool) {
if !done || err != nil {
observe(next, err, done)
} else {
if len(observables) == 0 {
var zero interface{}
observe(zero, nil, true)
} else {
o := observables[0]
observables = observables[1:]
o(observer, subscribeOn, subscriber)
}
}
}
o(observer, subscribeOn, subscriber)
}
return observable
}
//jig:name Observable_MergeWith
// MergeWith combines multiple Observables into one by merging their emissions.
// An error from any of the observables will terminate the merged observables.
func (o Observable) MergeWith(other ...Observable) Observable {
if len(other) == 0 {
return o
}
observable := func(observe Observer, subscribeOn Scheduler, subscriber Subscriber) {
var observers struct {
sync.Mutex
done bool
len int
}
observer := func(next interface{}, err error, done bool) {
observers.Lock()
defer observers.Unlock()
if !observers.done {
switch {
case !done:
observe(next, nil, false)
case err != nil:
observers.done = true
var zero interface{}
observe(zero, err, true)
default:
if observers.len--; observers.len == 0 {
var zero interface{}
observe(zero, nil, true)
}
}
}
}
observers.len = 1 + len(other)
o.AutoUnsubscribe()(observer, subscribeOn, subscriber)
for _, o := range other {
if subscriber.Subscribed() {
o.AutoUnsubscribe()(observer, subscribeOn, subscriber)
}
}
}
return observable
}
//jig:name Observable_MergeDelayErrorWith
// MergeDelayError combines multiple Observables into one by merging their emissions.
// Any error will be deferred until all observables terminate.
func (o Observable) MergeDelayErrorWith(other ...Observable) Observable {
if len(other) == 0 {
return o
}
observable := func(observe Observer, subscribeOn Scheduler, subscriber Subscriber) {
var observers struct {
sync.Mutex
len int
err error
}
observer := func(next interface{}, err error, done bool) {
observers.Lock()
defer observers.Unlock()
if !done {
observe(next, nil, false)
} else {
if err != nil {
observers.err = err
}
if observers.len--; observers.len == 0 {
var zero interface{}
observe(zero, observers.err, true)
}
}
}
observers.len = 1 + len(other)
o.AutoUnsubscribe()(observer, subscribeOn, subscriber)
for _, o := range other {
if subscriber.Subscribed() {
o.AutoUnsubscribe()(observer, subscribeOn, subscriber)
}
}
}
return observable
}
//jig:name Observable_CombineLatestWith
// CombineLatestWith will subscribe to its Observable and all other
// Observables passed in. It will then wait for all of the ObservableBars
// to emit before emitting the first slice. Whenever any of the subscribed
// observables emits, a new slice will be emitted containing all the latest
// value.
func (o Observable) CombineLatestWith(other ...Observable) ObservableSlice {
return FromObservable(append([]Observable{o}, other...)...).CombineLatestAll()
}
//jig:name Observable_CombineLatestMap
// CombinesLatestMap maps every entry emitted by the Observable into an
// Observable, and then subscribe to it, until the source observable
// completes. It will then wait for all of the Observables to emit before
// emitting the first slice. Whenever any of the subscribed observables emits,
// a new slice will be emitted containing all the latest value.
func (o Observable) CombineLatestMap(project func(interface{}) Observable) ObservableSlice {
return o.MapObservable(project).CombineLatestAll()
}
//jig:name Observable_CombineLatestMapTo
// CombinesLatestMapTo maps every entry emitted by the Observable into a
// single Observable, and then subscribe to it, until the source
// observable completes. It will then wait for all of the Observables
// to emit before emitting the first slice. Whenever any of the subscribed
// observables emits, a new slice will be emitted containing all the latest
// value.
func (o Observable) CombineLatestMapTo(inner Observable) ObservableSlice {
project := func(interface{}) Observable { return inner }
return o.MapObservable(project).CombineLatestAll()
}
//jig:name Observable_ConcatMap
// ConcatMap transforms the items emitted by an Observable by applying a
// function to each item and returning an Observable. The stream of
// Observable items is then flattened by concattenating the emissions from
// the observables without interleaving.
func (o Observable) ConcatMap(project func(interface{}) Observable) Observable {
return o.MapObservable(project).ConcatAll()
}
//jig:name ObservableObservable_ConcatAll
// ConcatAll flattens a higher order observable by concattenating the observables it emits.
func (o ObservableObservable) ConcatAll() Observable {
observable := func(observe Observer, subscribeOn Scheduler, subscriber Subscriber) {
var concat struct {
sync.Mutex
observables []Observable
observer Observer
subscriber Subscriber
}
var source struct {