-
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathoperator_combining.go
More file actions
1638 lines (1408 loc) · 47.6 KB
/
operator_combining.go
File metadata and controls
1638 lines (1408 loc) · 47.6 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
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
// Copyright 2025 samber.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://github.com/samber/ro/blob/main/licenses/LICENSE.apache.md
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//nolint:nestif,funlen,gocyclo
package ro
import (
"context"
"sync"
"sync/atomic"
"github.com/samber/lo"
"github.com/samber/ro/internal/xatomic"
)
// MergeWith merges the values from all observables to a single observable result.
// It subscribes to each inner Observable, and emits all values
// from each inner Observable, maintaining their order. It completes when all
// inner Observables are done.
//
// It is a curried function that takes the first Observable as an argument.
// Play: https://go.dev/play/p/6QpUzcdRWJl
func MergeWith[T any](observables ...Observable[T]) func(Observable[T]) Observable[T] {
return func(obsA Observable[T]) Observable[T] {
list := make([]Observable[T], len(observables)+1)
list[0] = obsA
copy(list[1:], observables)
return MergeAll[T]()(Just(list...))
}
}
// MergeWith1 merges the values from all observables to a single observable result.
// It subscribes to each inner Observable, and emits all values
// from each inner Observable, maintaining their order. It completes when all
// inner Observables are done.
//
// It is a curried function that takes the first Observable as an argument.
// Play: https://go.dev/play/p/P47lkUFpYq7
func MergeWith1[T any](obsB Observable[T]) func(Observable[T]) Observable[T] {
return func(obsA Observable[T]) Observable[T] {
return MergeAll[T]()(Just(obsA, obsB))
}
}
// MergeWith2 merges the values from all observables to a single observable result.
// It subscribes to each inner Observable, and emits all values
// from each inner Observable, maintaining their order. It completes when all
// inner Observables are done.
//
// It is a curried function that takes the first Observable as an argument.
// Play: https://go.dev/play/p/LOQ3YbuDyC9
func MergeWith2[T any](obsB, obsC Observable[T]) func(Observable[T]) Observable[T] {
return func(obsA Observable[T]) Observable[T] {
return MergeAll[T]()(Just(obsA, obsB, obsC))
}
}
// MergeWith3 merges the values from all observables to a single observable result.
// It subscribes to each inner Observable, and emits all values
// from each inner Observable, maintaining their order. It completes when all
// inner Observables are done.
//
// It is a curried function that takes the first Observable as an argument.
// Play: https://go.dev/play/p/pMQ5bNOlWj9
func MergeWith3[T any](obsB, obsC, obsD Observable[T]) func(Observable[T]) Observable[T] {
return func(obsA Observable[T]) Observable[T] {
return MergeAll[T]()(Just(obsA, obsB, obsC, obsD))
}
}
// MergeWith4 merges the values from all observables to a single observable result.
// It subscribes to each inner Observable, and emits all values
// from each inner Observable, maintaining their order. It completes when all
// inner Observables are done.
//
// It is a curried function that takes the first Observable as an argument.
// Play: https://go.dev/play/p/FvJTHVOe52s
func MergeWith4[T any](obsB, obsC, obsD, obsE Observable[T]) func(Observable[T]) Observable[T] {
return func(obsA Observable[T]) Observable[T] {
return MergeAll[T]()(Just(obsA, obsB, obsC, obsD, obsE))
}
}
// MergeWith5 merges the values from all observables to a single observable result.
// It subscribes to each inner Observable, and emits all values
// from each inner Observable, maintaining their order. It completes when all
// inner Observables are done.
//
// It is a curried function that takes the first Observable as an argument.
// Play: https://go.dev/play/p/kR3rFF7Bw-i
func MergeWith5[T any](obsB, obsC, obsD, obsE, obsF Observable[T]) func(Observable[T]) Observable[T] {
return func(obsA Observable[T]) Observable[T] {
return MergeAll[T]()(Just(obsA, obsB, obsC, obsD, obsE, obsF))
}
}
// MergeAll converts a higher-order Observable into a first-order Observable which
// concurrently delivers all values that are emitted on the inner Observables.
// It subscribes to each inner Observable as they arrive, and emits all values
// from each inner Observable, maintaining their order. It completes when all
// inner Observables are done.
// Play: https://go.dev/play/p/m3nHZZJbwMF
func MergeAll[T any]() func(Observable[Observable[T]]) Observable[T] {
return func(sources Observable[Observable[T]]) Observable[T] {
return NewObservableWithContext(func(subscriberCtx context.Context, destination Observer[T]) Teardown {
var parentCtx context.Context
var parentCtxMu sync.Mutex // atomic.Value has been introduced in go 1.19 and this library support go 1.18
subscriptions := NewSubscription(nil)
// default value is not 0, because it counts the outer Observable `sources`
subscriptionsCount := int32(1)
onDone := func() {
newCount := atomic.AddInt32(&subscriptionsCount, -1)
// when equal to 0, it means both the outer and inner Observables are done
if newCount == 0 {
parentCtxMu.Lock()
destination.CompleteWithContext(parentCtx)
parentCtxMu.Unlock()
}
}
subscriptions.AddUnsubscribable(
sources.SubscribeWithContext(
subscriberCtx,
NewObserverWithContext(
func(ctx context.Context, source Observable[T]) {
atomic.AddInt32(&subscriptionsCount, 1)
subscriptions.AddUnsubscribable(
source.SubscribeWithContext(
ctx,
NewObserverWithContext(
destination.NextWithContext,
destination.ErrorWithContext,
func(ctx context.Context) {
onDone()
},
),
),
)
},
destination.ErrorWithContext,
func(ctx context.Context) {
parentCtxMu.Lock()
parentCtx = ctx
parentCtxMu.Unlock()
onDone()
},
),
),
)
return subscriptions.Unsubscribe
})
}
}
// MergeMap applies a projection function to each item emitted by the source
// Observable and then merges the results into a single Observable.
// Play: https://go.dev/play/p/NwEyrLITshG
func MergeMap[T, R any](projection func(item T) Observable[R]) func(Observable[T]) Observable[R] {
return MergeMapIWithContext(func(ctx context.Context, item T, index int64) (context.Context, Observable[R]) {
return ctx, projection(item)
})
}
// MergeMapWithContext applies a projection function to each item emitted by the source
// Observable and then merges the results into a single Observable.
// Play: https://go.dev/play/p/i2Ru9sUdL-x
func MergeMapWithContext[T, R any](projection func(ctx context.Context, item T) Observable[R]) func(Observable[T]) Observable[R] {
return MergeMapIWithContext(func(ctx context.Context, item T, _ int64) (context.Context, Observable[R]) {
return ctx, projection(ctx, item)
})
}
// MergeMapI applies a projection function to each item emitted by the source
// Observable and then merges the results into a single Observable.
// Play: https://go.dev/play/p/dPDI7ch4g0i
func MergeMapI[T, R any](projection func(item T, index int64) Observable[R]) func(Observable[T]) Observable[R] {
return MergeMapIWithContext(func(ctx context.Context, item T, index int64) (context.Context, Observable[R]) {
return ctx, projection(item, index)
})
}
// MergeMapIWithContext applies a projection function to each item emitted by the source
// Observable and then merges the results into a single Observable.
// Play: https://go.dev/play/p/8Ih5mCaDbB8
func MergeMapIWithContext[T, R any](projection func(ctx context.Context, item T, index int64) (context.Context, Observable[R])) func(Observable[T]) Observable[R] {
return func(source Observable[T]) Observable[R] {
i := int64(0)
return MergeAll[R]()(
NewObservableWithContext(func(subscriberCtx context.Context, destination Observer[Observable[R]]) Teardown {
sub := source.SubscribeWithContext(
subscriberCtx,
NewObserverWithContext(
func(ctx context.Context, value T) {
destination.NextWithContext(projection(ctx, value, i))
i++
},
destination.ErrorWithContext,
destination.CompleteWithContext,
),
)
return sub.Unsubscribe
}),
)
}
}
// CombineLatestWith combines the values from the source Observable with the latest
// values from the other Observables. It will only emit when all Observables have
// emitted at least one value. It completes when the source Observable completes.
//
// It is a curried function that takes the other Observable as an argument.
// Play: https://go.dev/play/p/yq7G8eItuzO
func CombineLatestWith[A, B any](obsB Observable[B]) func(Observable[A]) Observable[lo.Tuple2[A, B]] {
return CombineLatestWith1[A](obsB)
}
// CombineLatestWith1 combines the values from the source Observable with the latest
// values from the other Observables. It will only emit when all Observables have
// emitted at least one value. It completes when the source Observable completes.
//
// It is a curried function that takes the other Observable as an argument.
// Play: https://go.dev/play/p/KXb19PPjCb1
func CombineLatestWith1[A, B any](obsB Observable[B]) func(Observable[A]) Observable[lo.Tuple2[A, B]] {
return func(obsA Observable[A]) Observable[lo.Tuple2[A, B]] {
return NewObservableWithContext(func(subscriberCtx context.Context, destination Observer[lo.Tuple2[A, B]]) Teardown {
var valueA xatomic.Pointer[A]
var valueB xatomic.Pointer[B]
// 0: not done
// 1: partially done
// 2: done
// 3: error
var status int32
onUpdate := func(ctx context.Context, a *A, b *B) {
if atomic.LoadInt32(&status) < 2 {
if a == nil {
a = valueA.Load()
}
if b == nil {
b = valueB.Load()
}
if a != nil && b != nil {
destination.NextWithContext(ctx, lo.T2(*a, *b))
}
}
}
onCompleted := func(ctx context.Context) {
if atomic.LoadInt32(&status) == 2 {
destination.CompleteWithContext(ctx)
}
}
subscriptions := NewSubscription(nil)
subscriptions.AddUnsubscribable(
obsA.SubscribeWithContext(
subscriberCtx,
NewObserverWithContext(
func(ctx context.Context, v A) {
valueA.Store(&v)
onUpdate(ctx, &v, nil)
},
func(ctx context.Context, err error) {
atomic.StoreInt32(&status, 3)
destination.ErrorWithContext(ctx, err)
},
func(ctx context.Context) {
atomic.AddInt32(&status, 1)
onCompleted(ctx)
},
),
),
)
subscriptions.AddUnsubscribable(
obsB.SubscribeWithContext(
subscriberCtx,
NewObserverWithContext(
func(ctx context.Context, v B) {
valueB.Store(&v)
onUpdate(ctx, nil, &v)
},
func(ctx context.Context, err error) {
atomic.StoreInt32(&status, 3)
destination.ErrorWithContext(ctx, err)
},
func(ctx context.Context) {
atomic.AddInt32(&status, 1)
onCompleted(ctx)
},
),
),
)
return func() {
atomic.StoreInt32(&status, 2)
subscriptions.Unsubscribe()
}
})
}
}
// CombineLatestWith2 combines the values from the source Observable with the latest
// values from the other Observables. It will only emit when all Observables have
// emitted at least one value. It completes when the source Observable completes.
//
// It is a curried function that takes the other Observable as an argument.
// Play: https://go.dev/play/p/hPDCDwEOB84
func CombineLatestWith2[A, B, C any](obsB Observable[B], obsC Observable[C]) func(Observable[A]) Observable[lo.Tuple3[A, B, C]] {
return func(obsA Observable[A]) Observable[lo.Tuple3[A, B, C]] {
return NewObservableWithContext(func(subscriberCtx context.Context, destination Observer[lo.Tuple3[A, B, C]]) Teardown {
var valueA xatomic.Pointer[A]
var valueB xatomic.Pointer[B]
var valueC xatomic.Pointer[C]
// 0: not done
// 1: partially done
// 2: partially done
// 3: done
// 4: error
var status int32
onUpdate := func(ctx context.Context, a *A, b *B, c *C) {
if atomic.LoadInt32(&status) < 3 {
if a == nil {
a = valueA.Load()
}
if b == nil {
b = valueB.Load()
}
if c == nil {
c = valueC.Load()
}
if a != nil && b != nil && c != nil {
destination.NextWithContext(ctx, lo.T3(*a, *b, *c))
}
}
}
onCompleted := func(ctx context.Context) {
if atomic.LoadInt32(&status) == 3 {
destination.CompleteWithContext(ctx)
}
}
subscriptions := NewSubscription(nil)
subscriptions.AddUnsubscribable(
obsA.SubscribeWithContext(
subscriberCtx,
NewObserverWithContext(
func(ctx context.Context, v A) {
valueA.Store(&v)
onUpdate(ctx, &v, nil, nil)
},
func(ctx context.Context, err error) {
atomic.StoreInt32(&status, 4)
destination.ErrorWithContext(ctx, err)
},
func(ctx context.Context) {
atomic.AddInt32(&status, 1)
onCompleted(ctx)
},
),
),
)
subscriptions.AddUnsubscribable(
obsB.SubscribeWithContext(
subscriberCtx,
NewObserverWithContext(
func(ctx context.Context, v B) {
valueB.Store(&v)
onUpdate(ctx, nil, &v, nil)
},
func(ctx context.Context, err error) {
atomic.StoreInt32(&status, 4)
destination.ErrorWithContext(ctx, err)
},
func(ctx context.Context) {
atomic.AddInt32(&status, 1)
onCompleted(ctx)
},
),
),
)
subscriptions.AddUnsubscribable(
obsC.SubscribeWithContext(
subscriberCtx,
NewObserverWithContext(
func(ctx context.Context, v C) {
valueC.Store(&v)
onUpdate(ctx, nil, nil, &v)
},
func(ctx context.Context, err error) {
atomic.StoreInt32(&status, 4)
destination.ErrorWithContext(ctx, err)
},
func(ctx context.Context) {
atomic.AddInt32(&status, 1)
onCompleted(ctx)
},
),
),
)
return func() {
atomic.StoreInt32(&status, 3)
subscriptions.Unsubscribe()
}
})
}
}
// CombineLatestWith3 combines the values from the source Observable with the latest
// values from the other Observables. It will only emit when all Observables have
// emitted at least one value. It completes when the source Observable completes.
//
// It is a curried function that takes the other Observable as an argument.
// Play: https://go.dev/play/p/PcMxo8yakQq
func CombineLatestWith3[A, B, C, D any](obsB Observable[B], obsC Observable[C], obsD Observable[D]) func(Observable[A]) Observable[lo.Tuple4[A, B, C, D]] {
return func(obsA Observable[A]) Observable[lo.Tuple4[A, B, C, D]] {
return NewObservableWithContext(func(subscriberCtx context.Context, destination Observer[lo.Tuple4[A, B, C, D]]) Teardown {
var valueA xatomic.Pointer[A]
var valueB xatomic.Pointer[B]
var valueC xatomic.Pointer[C]
var valueD xatomic.Pointer[D]
// 0: not done
// 1: partially done
// 2: partially done
// 3: partially done
// 4: done
// 5: error
var status int32
onUpdate := func(ctx context.Context, a *A, b *B, c *C, d *D) {
if atomic.LoadInt32(&status) < 4 {
if a == nil {
a = valueA.Load()
}
if b == nil {
b = valueB.Load()
}
if c == nil {
c = valueC.Load()
}
if d == nil {
d = valueD.Load()
}
if a != nil && b != nil && c != nil && d != nil {
destination.NextWithContext(ctx, lo.T4(*a, *b, *c, *d))
}
}
}
onCompleted := func(ctx context.Context) {
if atomic.LoadInt32(&status) == 4 {
destination.CompleteWithContext(ctx)
}
}
subscriptions := NewSubscription(nil)
subscriptions.AddUnsubscribable(
obsA.SubscribeWithContext(
subscriberCtx,
NewObserverWithContext(
func(ctx context.Context, v A) {
valueA.Store(&v)
onUpdate(ctx, &v, nil, nil, nil)
},
func(ctx context.Context, err error) {
atomic.StoreInt32(&status, 5)
destination.ErrorWithContext(ctx, err)
},
func(ctx context.Context) {
atomic.AddInt32(&status, 1)
onCompleted(ctx)
},
),
),
)
subscriptions.AddUnsubscribable(
obsB.SubscribeWithContext(
subscriberCtx,
NewObserverWithContext(
func(ctx context.Context, v B) {
valueB.Store(&v)
onUpdate(ctx, nil, &v, nil, nil)
},
func(ctx context.Context, err error) {
atomic.StoreInt32(&status, 5)
destination.ErrorWithContext(ctx, err)
},
func(ctx context.Context) {
atomic.AddInt32(&status, 1)
onCompleted(ctx)
},
),
),
)
subscriptions.AddUnsubscribable(
obsC.SubscribeWithContext(
subscriberCtx,
NewObserverWithContext(
func(ctx context.Context, v C) {
valueC.Store(&v)
onUpdate(ctx, nil, nil, &v, nil)
},
func(ctx context.Context, err error) {
atomic.StoreInt32(&status, 5)
destination.ErrorWithContext(ctx, err)
},
func(ctx context.Context) {
atomic.AddInt32(&status, 1)
onCompleted(ctx)
},
),
),
)
subscriptions.AddUnsubscribable(
obsD.SubscribeWithContext(
subscriberCtx,
NewObserverWithContext(
func(ctx context.Context, v D) {
valueD.Store(&v)
onUpdate(ctx, nil, nil, nil, &v)
},
func(ctx context.Context, err error) {
atomic.StoreInt32(&status, 5)
destination.ErrorWithContext(ctx, err)
},
func(ctx context.Context) {
atomic.AddInt32(&status, 1)
onCompleted(ctx)
},
),
),
)
return func() {
atomic.StoreInt32(&status, 4)
subscriptions.Unsubscribe()
}
})
}
}
// CombineLatestWith4 combines the values from the source Observable with the latest
// values from the other Observables. It will only emit when all Observables have
// emitted at least one value. It completes when the source Observable completes.
//
// It is a curried function that takes the other Observable as an argument.
func CombineLatestWith4[A, B, C, D, E any](obsB Observable[B], obsC Observable[C], obsD Observable[D], obsE Observable[E]) func(Observable[A]) Observable[lo.Tuple5[A, B, C, D, E]] {
return func(obsA Observable[A]) Observable[lo.Tuple5[A, B, C, D, E]] {
return NewObservableWithContext(func(subscriberCtx context.Context, destination Observer[lo.Tuple5[A, B, C, D, E]]) Teardown {
var valueA xatomic.Pointer[A]
var valueB xatomic.Pointer[B]
var valueC xatomic.Pointer[C]
var valueD xatomic.Pointer[D]
var valueE xatomic.Pointer[E]
// 0: not done
// 1: partially done
// 2: partially done
// 3: partially done
// 4: partially done
// 5: done
// 6: error
var status int32
onUpdate := func(ctx context.Context, a *A, b *B, c *C, d *D, e *E) {
if atomic.LoadInt32(&status) < 5 {
if a == nil {
a = valueA.Load()
}
if b == nil {
b = valueB.Load()
}
if c == nil {
c = valueC.Load()
}
if d == nil {
d = valueD.Load()
}
if e == nil {
e = valueE.Load()
}
if a != nil && b != nil && c != nil && d != nil && e != nil {
destination.NextWithContext(ctx, lo.T5(*a, *b, *c, *d, *e))
}
}
}
onCompleted := func(ctx context.Context) {
if atomic.LoadInt32(&status) == 5 {
destination.CompleteWithContext(ctx)
}
}
subscriptions := NewSubscription(nil)
subscriptions.AddUnsubscribable(
obsA.SubscribeWithContext(
subscriberCtx,
NewObserverWithContext(
func(ctx context.Context, v A) {
valueA.Store(&v)
onUpdate(ctx, &v, nil, nil, nil, nil)
},
func(ctx context.Context, err error) {
atomic.StoreInt32(&status, 6)
destination.ErrorWithContext(ctx, err)
},
func(ctx context.Context) {
atomic.AddInt32(&status, 1)
onCompleted(ctx)
},
),
),
)
subscriptions.AddUnsubscribable(
obsB.SubscribeWithContext(
subscriberCtx,
NewObserverWithContext(
func(ctx context.Context, v B) {
valueB.Store(&v)
onUpdate(ctx, nil, &v, nil, nil, nil)
},
func(ctx context.Context, err error) {
atomic.StoreInt32(&status, 6)
destination.ErrorWithContext(ctx, err)
},
func(ctx context.Context) {
atomic.AddInt32(&status, 1)
onCompleted(ctx)
},
),
),
)
subscriptions.AddUnsubscribable(
obsC.SubscribeWithContext(
subscriberCtx,
NewObserverWithContext(
func(ctx context.Context, v C) {
valueC.Store(&v)
onUpdate(ctx, nil, nil, &v, nil, nil)
},
func(ctx context.Context, err error) {
atomic.StoreInt32(&status, 6)
destination.ErrorWithContext(ctx, err)
},
func(ctx context.Context) {
atomic.AddInt32(&status, 1)
onCompleted(ctx)
},
),
),
)
subscriptions.AddUnsubscribable(
obsD.SubscribeWithContext(
subscriberCtx,
NewObserverWithContext(
func(ctx context.Context, v D) {
valueD.Store(&v)
onUpdate(ctx, nil, nil, nil, &v, nil)
},
func(ctx context.Context, err error) {
atomic.StoreInt32(&status, 6)
destination.ErrorWithContext(ctx, err)
},
func(ctx context.Context) {
atomic.AddInt32(&status, 1)
onCompleted(ctx)
},
),
),
)
subscriptions.AddUnsubscribable(
obsE.SubscribeWithContext(
subscriberCtx,
NewObserverWithContext(
func(ctx context.Context, v E) {
valueE.Store(&v)
onUpdate(ctx, nil, nil, nil, nil, &v)
},
func(ctx context.Context, err error) {
atomic.StoreInt32(&status, 6)
destination.ErrorWithContext(ctx, err)
},
func(ctx context.Context) {
atomic.AddInt32(&status, 1)
onCompleted(ctx)
},
),
),
)
return func() {
atomic.StoreInt32(&status, 5)
subscriptions.Unsubscribe()
}
})
}
}
// CombineLatestAll combines the values from the source Observable with the latest
// values from the other Observables. It will only emit when all Observables have
// emitted at least one value. It completes when the source Observable completes.
// Play: https://go.dev/play/p/nT1qq9ipwZL
func CombineLatestAll[T any]() func(Observable[Observable[T]]) Observable[[]T] {
return func(sources Observable[Observable[T]]) Observable[[]T] {
return NewObservableWithContext(func(subscriberCtx context.Context, destination Observer[[]T]) Teardown {
subscriptions := NewSubscription(nil)
var observables []Observable[T]
var values []*xatomic.Pointer[T]
// -1: error
// 0: done
// 1: partially done
// 2: partially done
// .: partially done
// .: partially done
// n: not done
var status int32
onUpdate := func(ctx context.Context) {
if atomic.LoadInt32(&status) > 0 {
result := make([]T, len(values))
for i := range values {
v := values[i].Load()
if v == nil {
return
}
result[i] = *v
}
destination.NextWithContext(ctx, result)
}
}
onCompleted := func(ctx context.Context) {
if atomic.LoadInt32(&status) == 0 {
destination.CompleteWithContext(ctx)
}
}
subscribeInner := func() {
// init
atomic.StoreInt32(&status, int32(len(observables)))
values = make([]*xatomic.Pointer[T], len(observables))
for i := range observables {
values[i] = new(xatomic.Pointer[T])
}
// inner subscriptions
for i := range observables {
j := i
subscriptions.AddUnsubscribable(
observables[j].SubscribeWithContext(
subscriberCtx,
NewObserverWithContext(
func(ctx context.Context, v T) {
values[j].Store(&v)
onUpdate(ctx)
},
func(ctx context.Context, err error) {
atomic.StoreInt32(&status, -1)
destination.ErrorWithContext(ctx, err)
},
func(ctx context.Context) {
atomic.AddInt32(&status, -1)
onCompleted(ctx)
},
),
),
)
}
}
// outer subscription
subscriptions.AddUnsubscribable(
sources.SubscribeWithContext(
subscriberCtx,
NewObserverWithContext(
func(ctx context.Context, v Observable[T]) {
observables = append(observables, v)
},
func(ctx context.Context, err error) {
atomic.StoreInt32(&status, -1)
destination.ErrorWithContext(ctx, err)
},
func(ctx context.Context) {
if len(observables) > 0 {
subscribeInner()
} else {
atomic.StoreInt32(&status, 0)
destination.CompleteWithContext(ctx)
}
},
),
),
)
return func() {
atomic.StoreInt32(&status, 0)
subscriptions.Unsubscribe()
}
})
}
}
// CombineLatestAllAny combines the values from the source Observable with the latest
// values from the other Observables. It will only emit when all Observables have
// emitted at least one value. It completes when the source Observable completes.
// Play: https://go.dev/play/p/nKMychGg9KH
func CombineLatestAllAny() func(Observable[Observable[any]]) Observable[[]any] {
return CombineLatestAll[any]()
}
// ConcatWith concatenates the source Observable with other Observables. It subscribes
// to each inner Observable only after the previous one completes, maintaining their
// order. It completes when all inner Observables are done.
//
// It is a curried function that takes the other Observables as arguments.
// Play: https://go.dev/play/p/nRHRSR2yNvd
func ConcatWith[T any](obs ...Observable[T]) func(Observable[T]) Observable[T] {
return func(source Observable[T]) Observable[T] {
return ConcatAll[T]()(Just(append([]Observable[T]{source}, obs...)...))
}
}
// ConcatAll concatenates the source Observable with other Observables. It subscribes
// to each inner Observable only after the previous one completes, maintaining their
// order. It completes when all inner Observables are done.
// Play: https://go.dev/play/p/zygV4Ld9tcv
func ConcatAll[T any]() func(Observable[Observable[T]]) Observable[T] {
return func(sources Observable[Observable[T]]) Observable[T] {
return NewUnsafeObservableWithContext(func(subscriberCtx context.Context, destination Observer[T]) Teardown {
subscriptions := NewSubscription(nil)
subscriptions.AddUnsubscribable(
sources.SubscribeWithContext(
subscriberCtx,
NewObserverWithContext(
func(ctx context.Context, source Observable[T]) {
sub := source.SubscribeWithContext(
ctx,
NewObserverWithContext(
destination.NextWithContext,
func(ctx context.Context, err error) {
subscriptions.Unsubscribe()
destination.ErrorWithContext(ctx, err)
},
func(ctx context.Context) {},
),
)
// `subscriptions` cancels `sub` when it unsubscribes
// but `sub` cannot unsubscribe `subscriptions`
subscriptions.AddUnsubscribable(sub)
sub.Wait()
},
func(ctx context.Context, err error) {
subscriptions.Unsubscribe()
destination.ErrorWithContext(ctx, err)
},
destination.CompleteWithContext,
),
),
)
return subscriptions.Unsubscribe
})
}
}
// StartWith emits the given values before emitting the values from the source Observable.
// Play: https://go.dev/play/p/vS_gIw8Ce1C
func StartWith[T any](prefixes ...T) func(Observable[T]) Observable[T] {
return func(source Observable[T]) Observable[T] {
return NewUnsafeObservableWithContext(func(subscriberCtx context.Context, destination Observer[T]) Teardown {
for i := range prefixes {
destination.NextWithContext(subscriberCtx, prefixes[i])
}
sub := source.SubscribeWithContext(subscriberCtx, destination)
return sub.Unsubscribe
})
}
}
// EndWith emits the given values after emitting the values from the source Observable.
// Play: https://go.dev/play/p/9FPyf3bqJk_n
func EndWith[T any](suffixes ...T) func(Observable[T]) Observable[T] {
return func(source Observable[T]) Observable[T] {
return NewUnsafeObservableWithContext(func(subscriberCtx context.Context, destination Observer[T]) Teardown {
sub := source.SubscribeWithContext(
subscriberCtx,
NewObserverWithContext(
destination.NextWithContext,
destination.ErrorWithContext,
func(ctx context.Context) {
for i := range suffixes {
destination.NextWithContext(ctx, suffixes[i])
}
destination.CompleteWithContext(ctx)
},
),
)
return sub.Unsubscribe
})
}
}
// Pairwise emits the previous and current values as a pair of two values.
// Play: https://go.dev/play/p/0YujgFTL4e0
func Pairwise[T any]() func(Observable[T]) Observable[[]T] {
return func(source Observable[T]) Observable[[]T] {
return NewUnsafeObservableWithContext(func(subscriberCtx context.Context, destination Observer[[]T]) Teardown {
count := int64(0)
var last T
sub := source.SubscribeWithContext(
subscriberCtx,
NewObserverWithContext(
func(ctx context.Context, value T) {
if count > 0 {
destination.NextWithContext(ctx, []T{last, value})
}
count++
last = value
},
destination.ErrorWithContext,
destination.CompleteWithContext,
),
)
return sub.Unsubscribe
})
}
}