/
Observable.php
1945 lines (1828 loc) · 63.2 KB
/
Observable.php
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
<?php
namespace Rx;
use Interop\Async\Promise;
use Rx\Observable\AnonymousObservable;
use Rx\Observable\ArrayObservable;
use Rx\Observable\ConnectableObservable;
use Rx\Observable\EmptyObservable;
use Rx\Observable\ErrorObservable;
use Rx\Observable\FromPromiseObservable;
use Rx\Observable\ForkJoinObservable;
use Rx\Observable\IntervalObservable;
use Rx\Observable\IteratorObservable;
use Rx\Observable\MulticastObservable;
use Rx\Observable\NeverObservable;
use Rx\Observable\RangeObservable;
use Rx\Observable\RefCountObservable;
use Rx\Observable\ReturnObservable;
use Rx\Observable\TimerObservable;
use Rx\Observer\CallbackObserver;
use Rx\Observer\DoObserver;
use Rx\Operator\AsObservableOperator;
use Rx\Operator\BufferWithCountOperator;
use Rx\Operator\CatchErrorOperator;
use Rx\Operator\CombineLatestOperator;
use Rx\Operator\ConcatAllOperator;
use Rx\Operator\ConcatMapOperator;
use Rx\Operator\ConcatOperator;
use Rx\Operator\CountOperator;
use Rx\Operator\DefaultIfEmptyOperator;
use Rx\Operator\DeferOperator;
use Rx\Operator\DelayOperator;
use Rx\Operator\DematerializeOperator;
use Rx\Operator\DistinctOperator;
use Rx\Operator\DistinctUntilChangedOperator;
use Rx\Operator\DoOnEachOperator;
use Rx\Operator\GroupByUntilOperator;
use Rx\Operator\MapOperator;
use Rx\Operator\FilterOperator;
use Rx\Operator\MinOperator;
use Rx\Operator\MaxOperator;
use Rx\Operator\MaterializeOperator;
use Rx\Operator\MergeAllOperator;
use Rx\Operator\RaceOperator;
use Rx\Operator\ReduceOperator;
use Rx\Operator\RepeatOperator;
use Rx\Operator\RepeatWhenOperator;
use Rx\Operator\RetryOperator;
use Rx\Operator\RetryWhenOperator;
use Rx\Operator\ScanOperator;
use Rx\Operator\SkipLastOperator;
use Rx\Operator\SkipOperator;
use Rx\Operator\SkipUntilOperator;
use Rx\Operator\StartWithArrayOperator;
use Rx\Operator\SkipWhileOperator;
use Rx\Operator\SubscribeOnOperator;
use Rx\Operator\SwitchFirstOperator;
use Rx\Operator\SwitchLatestOperator;
use Rx\Operator\TakeLastOperator;
use Rx\Operator\TakeOperator;
use Rx\Operator\TakeUntilOperator;
use Rx\Operator\TakeWhileOperator;
use Rx\Operator\ThrottleOperator;
use Rx\Operator\TimeoutOperator;
use Rx\Operator\TimestampOperator;
use Rx\Operator\ToArrayOperator;
use Rx\Operator\ZipOperator;
use Rx\Promise\Promise as RxPromise;
use Rx\Subject\AsyncSubject;
use Rx\Subject\BehaviorSubject;
use Rx\Subject\ReplaySubject;
use Rx\Subject\Subject;
abstract class Observable implements ObservableInterface
{
/**
* @param callable|ObserverInterface|null $onNextOrObserver
* @param callable|null $onError
* @param callable|null $onCompleted
* @return DisposableInterface
* @throws \InvalidArgumentException
*
* @operator
* @reactivex subscribe
*/
public function subscribe($onNextOrObserver = null, callable $onError = null, callable $onCompleted = null): DisposableInterface
{
if ($onNextOrObserver instanceof ObserverInterface) {
return $this->_subscribe($onNextOrObserver);
} elseif ($onNextOrObserver !== null && !is_callable($onNextOrObserver)) {
throw new \InvalidArgumentException('The first argument needs to be a "callable" or "Observer"');
}
$observer = new CallbackObserver($onNextOrObserver, $onError, $onCompleted);
return $this->_subscribe($observer);
}
/**
* @param ObserverInterface $observer
* @return DisposableInterface
*/
protected abstract function _subscribe(ObserverInterface $observer): DisposableInterface;
/**
* @deprecated
*
* @param callable|null $onNext
* @param callable|null $onError
* @param callable|null $onCompleted
* @return DisposableInterface
*/
public function subscribeCallback(callable $onNext = null, callable $onError = null, callable $onCompleted = null): DisposableInterface
{
$observer = new CallbackObserver($onNext, $onError, $onCompleted);
return $this->subscribe($observer);
}
/**
* Creates an observable sequence from a specified subscribeAction callable implementation.
*
* @param callable $subscribeAction Implementation of the resulting observable sequence's subscribe method.
* @return Observable The observable sequence with the specified implementation for the subscribe method.
*
* @demo create/create.php
* @operator
* @reactivex create
*/
public static function create(callable $subscribeAction): Observable
{
return new AnonymousObservable($subscribeAction);
}
/**
* Returns an Observable that emits an infinite sequence of ascending integers starting at 0, with a constant interval of time of your choosing between emissions.
*
* @param $interval int Period for producing the values in the resulting sequence (specified as an integer denoting milliseconds).
* @param SchedulerInterface|null $scheduler
* @return IntervalObservable An observable sequence that produces a value after each period.
*
* @demo interval/interval.php
* @operator
* @reactivex interval
*/
public static function interval(int $interval, $scheduler = null): IntervalObservable
{
return new IntervalObservable($interval, $scheduler ?: Scheduler::getAsync());
}
/**
* Returns an observable sequence that contains a single element.
*
* @param mixed $value Single element in the resulting observable sequence.
* @param SchedulerInterface $scheduler
* @return ReturnObservable An observable sequence with the single element.
*
* @demo of/of.php
* @operator
* @reactivex just
*/
public static function of($value, SchedulerInterface $scheduler = null): ReturnObservable
{
return new ReturnObservable($value, $scheduler ?: Scheduler::getDefault());
}
/**
* Alias for of
*
* @param $value
* @param SchedulerInterface|null $scheduler
* @return ReturnObservable
*/
public static function just($value, SchedulerInterface $scheduler = null): ReturnObservable
{
return static::of($value, $scheduler);
}
/**
* Returns an empty observable sequence.
*
* @param SchedulerInterface $scheduler
* @return EmptyObservable An observable sequence with no elements.
*
* @demo empty/empty.php
* @operator
* @reactivex empty-never-throw
*/
public static function empty(SchedulerInterface $scheduler = null): EmptyObservable
{
return new EmptyObservable($scheduler ?: Scheduler::getDefault());
}
/**
* Alias for empty
*
* @param SchedulerInterface|null $scheduler
* @return EmptyObservable
*/
public static function emptyObservable(SchedulerInterface $scheduler = null): EmptyObservable
{
return static::empty($scheduler);
}
/**
* Returns a non-terminating observable sequence, which can be used to denote an infinite duration.
*
* @return NeverObservable An observable sequence whose observers will never get called.
*
* @demo never/never.php
* @operator
* @reactivex empty-never-throw
*/
public static function never(): NeverObservable
{
return new NeverObservable();
}
/**
* Returns an observable sequence that terminates with an exception.
*
* @param \Throwable $error
* @param SchedulerInterface $scheduler
* @return ErrorObservable The observable sequence that terminates exceptionally with the specified exception object.
*
* @demo error-observable/error-observable.php
* @operator
* @reactivex empty-never-throw
*/
public static function error(\Throwable $error, SchedulerInterface $scheduler = null): ErrorObservable
{
return new ErrorObservable($error, $scheduler ?: Scheduler::getImmediate());
}
/**
* Combine an Observable together with another Observable by merging their emissions into a single Observable.
*
* @param ObservableInterface $otherObservable
* @return Observable
*
* @demo merge/merge.php
* @operator
* @reactivex merge
*/
public function merge(ObservableInterface $otherObservable): Observable
{
return (new AnonymousObservable(function (ObserverInterface $observer) use ($otherObservable) {
$observer->onNext($this);
$observer->onNext($otherObservable);
$observer->onCompleted();
}))->mergeAll();
}
/**
* Merges an observable sequence of observables into an observable sequence.
*
* @return Observable
*
* @demo merge/merge-all.php
* @operator
* @reactivex merge
*/
public function mergeAll(): Observable
{
return $this->lift(function () {
return new MergeAllOperator($this);
});
}
/**
* Runs all observable sequences in parallel and collect their last elements.
*
* @param array $observables
* @param callable|null $resultSelector
* @return ForkJoinObservable
*
* @demo forkJoin-observable/forkJoin-observable.php
* @operator
* @reactivex forkJoin
*/
public static function forkJoin(array $observables = [], callable $resultSelector = null): ForkJoinObservable
{
return new ForkJoinObservable($observables, $resultSelector);
}
/**
* Converts an array to an observable sequence
*
* @param array $array
* @param SchedulerInterface $scheduler
* @return ArrayObservable
*
* @demo fromArray/fromArray.php
* @operator
* @reactivex from
*/
public static function fromArray(array $array, SchedulerInterface $scheduler = null): ArrayObservable
{
return new ArrayObservable($array, $scheduler ?: Scheduler::getDefault());
}
/**
* Converts an Iterator into an observable sequence
*
* @param \Iterator $iterator
* @param SchedulerInterface $scheduler
* @return IteratorObservable
*
* @demo iterator/iterator.php
* @operator
* @reactivex from
*/
public static function fromIterator(\Iterator $iterator, SchedulerInterface $scheduler = null): IteratorObservable
{
return new IteratorObservable($iterator, $scheduler ?: Scheduler::getDefault());
}
/**
* Returns an observable sequence that invokes the specified factory function whenever a new observer subscribes.
*
* @param callable $factory
* @param SchedulerInterface $scheduler
* @return Observable
*
* @demo defer/defer.php
* @operator
* @reactivex defer
*/
public static function defer(callable $factory, SchedulerInterface $scheduler = null): Observable
{
return static::empty($scheduler)
->lift(function () use ($factory) {
return new DeferOperator(new ObservableFactoryWrapper($factory));
});
}
/**
* Generates an observable sequence of integral numbers within a specified range, using the specified scheduler to
* send out observer messages.
*
* @param $start
* @param $count
* @param SchedulerInterface $scheduler
* @return RangeObservable
* @throws \InvalidArgumentException
*
* @demo range/range.php
* @operator
* @reactivex range
*/
public static function range(int $start, int $count, SchedulerInterface $scheduler = null): RangeObservable
{
return new RangeObservable($start, $count, $scheduler ?: Scheduler::getDefault());
}
/**
* Invokes the specified function asynchronously on the specified scheduler, surfacing the result through an
* observable sequence.
*
* @param callable $action
* @param SchedulerInterface $scheduler
* @return Observable
*
* @demo start/start.php
* @operator
* @reactivex start
*/
public static function start(callable $action, SchedulerInterface $scheduler = null): Observable
{
$scheduler = $scheduler ?? Scheduler::getDefault();
$subject = new AsyncSubject();
$scheduler->schedule(function () use ($subject, $action) {
$result = null;
try {
$result = $action();
} catch (\Throwable $e) {
$subject->onError($e);
return;
}
$subject->onNext($result);
$subject->onCompleted();
});
return $subject->asObservable();
}
/**
* Takes a transforming function that operates on each element.
*
* @param callable $selector
* @return Observable
*
* @demo map/map.php
* @operator
* @reactivex map
*/
public function map(callable $selector): Observable
{
return $this->lift(function () use ($selector) {
return new MapOperator($selector);
});
}
/**
* Maps operator variant that calls the map selector with the index and value
*
* @param callable $selector
* @return Observable
*
* @demo map/mapWithIndex.php
* @operator
* @reactivex map
*/
public function mapWithIndex(callable $selector): Observable
{
$index = 0;
return $this->map(function ($value) use ($selector, &$index) {
return $selector($index++, $value);
});
}
/**
* Maps every value to the same value every time
*
* @param $value
* @return Observable
*
* @demo map/mapTo.php
* @operator
* @reactivex map
*/
public function mapTo($value): Observable
{
return $this->map(function () use ($value) {
return $value;
});
}
/**
* Alias for Map
*
* @param callable $selector
* @return Observable
*
* @operator
* @reactivex map
*/
public function select(callable $selector): Observable
{
return $this->map($selector);
}
/**
* Emit only those items from an Observable that pass a predicate test.
*
* @param callable $predicate
* @return Observable
*
* @demo filter/filter.php
* @operator
* @reactivex filter
*/
public function filter(callable $predicate): Observable
{
return $this->lift(function () use ($predicate) {
return new FilterOperator($predicate);
});
}
/**
* Alias for filter
*
* @param callable $predicate
* @return Observable
*
* @operator
* @reactivex filter
*/
public function where(callable $predicate): Observable
{
return $this->filter($predicate);
}
/**
* Projects each element of an observable sequence to an observable sequence and merges the resulting observable sequences into one observable sequence.
*
* @param callable $selector
* @return Observable
*
* @demo flatMap/flatMap.php
* @operator
* @reactivex flatMap
*/
public function flatMap(callable $selector): Observable
{
return $this->map(new ObservableFactoryWrapper($selector))->mergeAll();
}
/**
* Projects each element of the source observable sequence to the other observable sequence and merges the
* resulting observable sequences into one observable sequence.
*
* @param ObservableInterface $observable - An an observable sequence to project each element from the source
* sequence onto.
*
* @return Observable
*
* @demo concat/flatMapTo.php
* @operator
* @reactivex flatMap
*/
public function flatMapTo(ObservableInterface $observable): Observable
{
return $this->flatMap(function () use ($observable) {
return $observable;
});
}
/**
* Alias for flatMap
*
* @param $selector
* @return Observable
*
* @operator
* @reactivex flatMap
*/
public function selectMany($selector): Observable
{
return $this->flatMap($selector);
}
/**
* Bypasses a specified number of elements in an observable sequence and then returns the remaining elements.
*
* Transform the items emitted by an Observable into Observables, and mirror those items emitted by the
* most-recently transformed Observable.
*
* The flatMapLatest operator is similar to the flatMap and concatMap methods described above, however, rather than
* emitting all of the items emitted by all of the Observables that the operator generates by transforming items
* from the source Observable, flatMapLatest instead emits items from each such transformed Observable only until
* the next such Observable is emitted, then it ignores the previous one and begins emitting items emitted by the
* new one.
*
* @param callable $selector - A transform function to apply to each source element.
* @return Observable - An observable sequence which transforms the items emitted by an Observable into
* Observables, and mirror those items emitted by the most-recently transformed Observable.
*
* @demo flatMap/flatMapLatest.php
* @operator
* @reactivex flatMap
*/
public function flatMapLatest(callable $selector): Observable
{
return $this->map(new ObservableFactoryWrapper($selector))->switch();
}
/**
* @param integer $count
* @return Observable
*
* @demo skip/skip.php
* @operator
* @reactivex skip
*/
public function skip(int $count): Observable
{
return $this->lift(function () use ($count) {
return new SkipOperator($count);
});
}
/**
* Bypasses elements in an observable sequence as long as a specified condition is true and then returns the
* remaining elements.
*
* @param callable $predicate A function to test each element for a condition.
*
* @return Observable An observable sequence that contains the elements from the input sequence starting
* at the first element in the linear series that does not pass the test specified by predicate.
*
* @demo skip/skipWhile.php
* @operator
* @reactivex skipWhile
*/
public function skipWhile(callable $predicate): Observable
{
return $this->lift(function () use ($predicate) {
return new SkipWhileOperator($predicate);
});
}
/**
* Bypasses elements in an observable sequence as long as a specified condition is true and then returns the
* remaining elements. The element's index is used in the logic of the predicate function.
*
* @param callable $predicate A function to test each element for a condition; the first parameter of the
* function represents the index of the source element, the second parameter is the value.
*
* @return Observable An observable sequence that contains the elements from the input sequence starting
* at the first element in the linear series that does not pass the test specified by predicate.
*
* @demo skip/skipWhileWithIndex.php
* @operator
* @reactivex skipWhile
*/
public function skipWhileWithIndex(callable $predicate): Observable
{
$index = 0;
return $this->skipWhile(function ($value) use ($predicate, &$index) {
return $predicate($index++, $value);
});
}
/**
* Returns a specified number of contiguous elements from the start of an observable sequence
*
* @param integer $count
* @return Observable|EmptyObservable
*
* @demo take/take.php
* @operator
* @reactivex take
*/
public function take(int $count): Observable
{
if ($count === 0) {
return self::empty();
}
return $this->lift(function () use ($count) {
return new TakeOperator($count);
});
}
/**
* Returns the values from the source observable sequence until the other observable sequence produces a value.
*
* @param ObservableInterface $other - other Observable sequence that terminates propagation of elements of
* the source sequence.
* @return Observable - An observable sequence containing the elements of the source sequence up to the
* point the other sequence interrupted further propagation.
*
* @demo take/takeUntil.php
* @operator
* @reactivex take
*/
public function takeUntil(ObservableInterface $other): Observable
{
return $this->lift(function () use ($other) {
return new TakeUntilOperator($other);
});
}
/**
* Returns elements from an observable sequence as long as a specified condition is true. It takes as a parameter a
* a callback to test each source element for a condition. The callback predicate is called with the value of the
* element.
*
* @param callable $predicate
* @return Observable
*
* @demo take/takeWhile.php
* @operator
* @reactivex takeWhile
*/
public function takeWhile(callable $predicate): Observable
{
return $this->lift(function () use ($predicate) {
return new TakeWhileOperator($predicate);
});
}
/**
* Returns elements from an observable sequence as long as a specified condition is true. It takes as a parameter a
* a callback to test each source element for a condition. The callback predicate is called with the index and the
* value of the element.
*
* @param callable $predicate
* @return Observable
*
* @demo take/takeWhileWithIndex.php
* @operator
* @reactivex takeWhile
*/
public function takeWhileWithIndex(callable $predicate): Observable
{
$index = 0;
return $this->takeWhile(function ($value) use ($predicate, &$index) {
return $predicate($index++, $value);
});
}
/**
* Returns a specified number of contiguous elements from the end of an observable sequence.
*
* @param $count
* @return Observable
*
* @demo take/takeLast.php
* @operator
* @reactivex takeLast
*/
public function takeLast(int $count): Observable
{
return $this->lift(function () use ($count) {
return new TakeLastOperator($count);
});
}
/**
* Groups the elements of an observable sequence according to a specified key selector function and comparer and selects the resulting elements by using a specified function.
*
* @param callable $keySelector
* @param callable|null $elementSelector
* @param callable|null $keySerializer
* @return Observable
*
* @demo groupBy/groupBy.php
* @operator
* @reactivex groupBy
*/
public function groupBy(callable $keySelector, callable $elementSelector = null, callable $keySerializer = null): Observable
{
return $this->groupByUntil($keySelector, $elementSelector, function () {
return static::never();
}, $keySerializer);
}
/**
* Groups the elements of an observable sequence according to a specified key selector function and comparer and selects the resulting elements by using a specified function.
*
* @param callable $keySelector
* @param callable|null $elementSelector
* @param callable|null $durationSelector
* @param callable|null $keySerializer
* @return Observable
*
* @demo groupBy/groupByUntil.php
* @operator
* @reactivex groupBy
*/
public function groupByUntil(callable $keySelector, callable $elementSelector = null, callable $durationSelector = null, callable $keySerializer = null): Observable
{
return $this->lift(function () use ($keySelector, $elementSelector, $durationSelector, $keySerializer) {
return new GroupByUntilOperator($keySelector, $elementSelector, $durationSelector, $keySerializer);
});
}
/**
* Lifts a function to the current Observable and returns a new Observable that when subscribed to will pass
* the values of the current Observable through the Operator function.
*
* @param callable $operatorFactory
* @return Observable
*/
public function lift(callable $operatorFactory): Observable
{
return new AnonymousObservable(function (ObserverInterface $observer) use ($operatorFactory) {
$operator = $operatorFactory();
return $operator($this, $observer);
});
}
/**
* This method allows the use of extra operators with the namespace:
* Rx\Operator
* and also custom operators by adding an operator class with the
* namespace format:
* CustomNamespace\Rx\Operator\OperatorNameOperator
*
* @param $name
* @param $arguments
* @return Observable
*
* @demo custom-operator/rot13.php
*/
public function __call($name, array $arguments): Observable
{
$fullNamespace = 'Rx\\Operator\\';
if ($name[0] === '_') {
list($_, $namespace, $methodName) = explode('_', $name);
$name = $methodName;
$fullNamespace = $namespace . '\\' . $fullNamespace;
}
$className = $fullNamespace . ucfirst($name) . 'Operator';
return $this->lift(function () use ($className, $arguments) {
return (new \ReflectionClass($className))->newInstanceArgs($arguments);
});
}
/**
* Applies an accumulator function over an observable sequence,
* returning the result of the aggregation as a single element in the result sequence.
* The specified seed value is used as the initial accumulator value.
*
* @param callable $accumulator - An accumulator function to be invoked on each element.
* @param mixed $seed [optional] - The initial accumulator value.
* @return Observable - An observable sequence containing a single element with the final
* accumulator value.
*
* @demo reduce/reduce.php
* @demo reduce/reduce-with-seed.php
* @operator
* @reactivex reduce
*/
public function reduce(callable $accumulator, $seed = null): Observable
{
return $this->lift(function () use ($accumulator, $seed) {
return new ReduceOperator($accumulator, $seed);
});
}
/**
* Returns an observable sequence that contains only distinct elements according to the keySelector and the
* comparer. Usage of this operator should be considered carefully due to the maintenance of an internal lookup
* structure which can grow large.
*
* @param callable|null $comparer
* @return Observable
*
* @demo distinct/distinct.php
* @operator
* @reactivex distinct
*/
public function distinct(callable $comparer = null): Observable
{
return $this->lift(function () use ($comparer) {
return new DistinctOperator(null, $comparer);
});
}
/**
* Variant of distinct that takes a key selector
*
* @param callable|null $keySelector
* @param callable|null $comparer
* @return Observable
*
* @demo distinct/distinctKey.php
* @operator
* @reactivex distinct
*/
public function distinctKey(callable $keySelector, callable $comparer = null): Observable
{
return $this->lift(function () use ($keySelector, $comparer) {
return new DistinctOperator($keySelector, $comparer);
});
}
/**
* A variant of distinct that only compares emitted items from the source Observable against their immediate predecessors in order to determine whether or not they are distinct.
*
* @param callable $comparer
* @return Observable
*
* @demo distinct/distinctUntilChanged.php
* @operator
* @reactivex distinct
*/
public function distinctUntilChanged(callable $comparer = null): Observable
{
return $this->lift(function () use ($comparer) {
return new DistinctUntilChangedOperator(null, $comparer);
});
}
/**
* Variant of distinctUntilChanged that takes a key selector
* and the comparer.
*
* @param callable $keySelector
* @param callable $comparer
* @return Observable
*
* @demo distinct/distinctUntilKeyChanged.php
* @operator
* @reactivex distinct
*/
public function distinctUntilKeyChanged(callable $keySelector = null, callable $comparer = null): Observable
{
return $this->lift(function () use ($keySelector, $comparer) {
return new DistinctUntilChangedOperator($keySelector, $comparer);
});
}
/**
* Invokes an action for each element in the observable sequence and invokes an action upon graceful
* or exceptional termination of the observable sequence.
* This method can be used for debugging, logging, etc. of query behavior by intercepting the message stream to
* run arbitrary actions for messages on the pipeline.
*
* When using do, it is important to note that the Observer may receive additional
* events after a stream has completed or errored (such as when using a repeat or resubscribing).
* If you are using an Observable that extends the AbstractObservable, you will not receive these
* events. For this special case, use the DoObserver.
*
* doOnNext, doOnError, and doOnCompleted uses the DoObserver internally and will receive these
* additional events.
*
* @param callable|ObserverInterface $onNextOrObserver
* @param callable $onError
* @param callable $onCompleted
* @return Observable
* @throws \InvalidArgumentException
*
* @demo do/do.php
* @operator
* @reactivex do
*/
public function do($onNextOrObserver = null, callable $onError = null, callable $onCompleted = null): Observable
{
if ($onNextOrObserver instanceof ObserverInterface) {
$observer = $onNextOrObserver;
} elseif (is_callable($onNextOrObserver)) {
$observer = new DoObserver($onNextOrObserver, $onError, $onCompleted);
} else {
throw new \InvalidArgumentException('The first argument needs to be a "callable" or "Observer"');
}
return $this->lift(function () use ($observer) {
return new DoOnEachOperator($observer);
});
}
/**
* Alias for do
*
* @param ObserverInterface $observer
* @return mixed
*/
public function doOnEach(ObserverInterface $observer): Observable
{
return $this->do($observer);
}
/**
* @param callable $onNext
* @return Observable
*
* @demo do/doOnNext.php
* @operator
* @reactivex do
*/
public function doOnNext(callable $onNext): Observable
{
return $this->do(new DoObserver(
$onNext
));
}
/**
* @param callable $onError
* @return Observable
*
* @demo do/doOnError.php
* @operator
* @reactivex do
*/
public function doOnError(callable $onError): Observable
{
return $this->do(new DoObserver(
null,
$onError
));
}
/**
* @param callable $onCompleted
* @return Observable
*
* @demo do/doOnCompleted.php
* @operator
* @reactivex do
*/
public function doOnCompleted(callable $onCompleted): Observable
{
return $this->do(new DoObserver(
null,
null,
$onCompleted
));
}
/**
* Applies an accumulator function over an observable sequence and returns each intermediate result.
* The optional seed value is used as the initial accumulator value.
*
* @param $accumulator
* @param null $seed
* @return Observable
*
* @demo scan/scan.php
* @demo scan/scan-with-seed.php
* @operator
* @reactivex scan
*/