|
26 | 26 | use Rx\Disposable\SingleAssignmentDisposable;
|
27 | 27 | use Rx\SchedulerInterface;
|
28 | 28 | use Rx\Subject\AsyncSubject;
|
| 29 | +use Rx\Subject\BehaviorSubject; |
| 30 | +use Rx\Subject\ReplaySubject; |
29 | 31 | use Rx\Subject\Subject;
|
30 | 32 | use Rx\Disposable\RefCountDisposable;
|
31 | 33 | use Rx\Disposable\EmptyDisposable;
|
@@ -636,4 +638,125 @@ public static function defer($factory){
|
636 | 638 | return (new EmptyObservable())->lift(new DeferOperator($factory));
|
637 | 639 | }
|
638 | 640 |
|
| 641 | + /** |
| 642 | + * Multicasts the source sequence notifications through an instantiated subject into all uses of the sequence within a selector function. Each |
| 643 | + * subscription to the resulting sequence causes a separate multicast invocation, exposing the sequence resulting from the selector function's |
| 644 | + * invocation. For specializations with fixed subject types, see Publish, PublishLast, and Replay. |
| 645 | + * |
| 646 | + * @param $subjectOrSubjectSelector |
| 647 | + * @param null $selector |
| 648 | + * @return \Rx\Observable\ConnectableObservable|\Rx\Observable\MulticastObservable |
| 649 | + */ |
| 650 | + public function multicast($subjectOrSubjectSelector, $selector = null) |
| 651 | + { |
| 652 | + return is_callable($subjectOrSubjectSelector) ? |
| 653 | + new MulticastObservable($this, $subjectOrSubjectSelector, $selector) : |
| 654 | + new ConnectableObservable($this, $subjectOrSubjectSelector); |
| 655 | + } |
| 656 | + |
| 657 | + /** |
| 658 | + * Returns an observable sequence that is the result of invoking the selector on a connectable observable sequence that shares a single subscription to the underlying sequence. |
| 659 | + * This operator is a specialization of Multicast using a regular Subject. |
| 660 | + * |
| 661 | + * @param callable|null $selector |
| 662 | + * @return \Rx\Observable\ConnectableObservable|\Rx\Observable\MulticastObservable |
| 663 | + */ |
| 664 | + public function publish(callable $selector = null) |
| 665 | + { |
| 666 | + return $selector ? |
| 667 | + new MulticastObservable($this, function () { |
| 668 | + return new Subject(); |
| 669 | + }, $selector) : |
| 670 | + $this->multicast(new Subject()); |
| 671 | + } |
| 672 | + |
| 673 | + /** |
| 674 | + * Returns an observable sequence that is the result of invoking the selector on a connectable observable sequence that shares a single subscription to the underlying sequence containing only the last notification. |
| 675 | + * This operator is a specialization of Multicast using a AsyncSubject. |
| 676 | + * |
| 677 | + * @param callable|null $selector |
| 678 | + * @return \Rx\Observable\ConnectableObservable|\Rx\Observable\MulticastObservable |
| 679 | + */ |
| 680 | + public function publishLast(callable $selector = null) |
| 681 | + { |
| 682 | + return $selector ? |
| 683 | + new MulticastObservable($this, function () { |
| 684 | + return new AsyncSubject(); |
| 685 | + }, $selector) : |
| 686 | + $this->multicast(new AsyncSubject()); |
| 687 | + } |
| 688 | + |
| 689 | + /** |
| 690 | + * Returns an observable sequence that is the result of invoking the selector on a connectable observable sequence that shares a single subscription to the underlying sequence and starts with initialValue. |
| 691 | + * This operator is a specialization of Multicast using a BehaviorSubject. |
| 692 | + * |
| 693 | + * @param $initialValueOrSelector |
| 694 | + * @param null $initialValue |
| 695 | + * @return \Rx\Observable\ConnectableObservable|\Rx\Observable\MulticastObservable |
| 696 | + */ |
| 697 | + public function publishValue($initialValueOrSelector, $initialValue = null) |
| 698 | + { |
| 699 | + return $initialValue ? |
| 700 | + $this->multicast(function () use ($initialValue) { |
| 701 | + return new BehaviorSubject($initialValue); |
| 702 | + }, $initialValueOrSelector) : |
| 703 | + $this->multicast(new BehaviorSubject($initialValueOrSelector)); |
| 704 | + } |
| 705 | + |
| 706 | + /** |
| 707 | + * Returns an observable sequence that shares a single subscription to the underlying sequence. |
| 708 | + * This operator is a specialization of publish which creates a subscription when the number of observers goes from zero to one, then shares that subscription with all subsequent observers until the number of observers returns to zero, at which point the subscription is disposed. |
| 709 | + * |
| 710 | + * @return \Rx\Observable\RefCountObservable An observable sequence that contains the elements of a sequence produced by multicasting the source sequence.,mk |
| 711 | + */ |
| 712 | + public function share() |
| 713 | + { |
| 714 | + return $this->publish()->refCount(); |
| 715 | + } |
| 716 | + |
| 717 | + /** |
| 718 | + * Returns an observable sequence that shares a single subscription to the underlying sequence and starts with an initialValue. |
| 719 | + * This operator is a specialization of publishValue which creates a subscription when the number of observers goes from zero to one, then shares that subscription with all subsequent observers until the number of observers returns to zero, at which point the subscription is disposed. |
| 720 | + * |
| 721 | + * @param $initialValue |
| 722 | + * @return \Rx\Observable\RefCountObservable |
| 723 | + */ |
| 724 | + public function shareValue($initialValue) |
| 725 | + { |
| 726 | + return $this->publish($initialValue)->refCount(); |
| 727 | + } |
| 728 | + |
| 729 | + /** |
| 730 | + * Returns an observable sequence that shares a single subscription to the underlying sequence replaying notifications subject to a maximum time length for the replay buffer. |
| 731 | + * This operator is a specialization of replay which creates a subscription when the number of observers goes from zero to one, then shares that subscription with all subsequent observers until the number of observers returns to zero, at which point the subscription is disposed. |
| 732 | + * |
| 733 | + * @param $bufferSize |
| 734 | + * @param $windowSize |
| 735 | + * @param $scheduler |
| 736 | + * @return \Rx\Observable\RefCountObservable |
| 737 | + */ |
| 738 | + public function shareReplay($bufferSize, $windowSize, $scheduler) |
| 739 | + { |
| 740 | + return $this->replay(null, $bufferSize, $windowSize, $scheduler)->refCount(); |
| 741 | + } |
| 742 | + |
| 743 | + /** |
| 744 | + * Returns an observable sequence that is the result of invoking the selector on a connectable observable sequence that shares a single subscription to the underlying sequence replaying notifications subject to a maximum time length for the replay buffer. |
| 745 | + * This operator is a specialization of Multicast using a ReplaySubject. |
| 746 | + * |
| 747 | + * @param callable|null $selector |
| 748 | + * @param null $bufferSize |
| 749 | + * @param null $windowSize |
| 750 | + * @param \Rx\SchedulerInterface|null $scheduler |
| 751 | + * @return \Rx\Observable\ConnectableObservable|\Rx\Observable\MulticastObservable |
| 752 | + */ |
| 753 | + public function replay(callable $selector = null, $bufferSize = null, $windowSize = null, SchedulerInterface $scheduler = null) |
| 754 | + { |
| 755 | + return $selector ? |
| 756 | + $this->multicast(function () use ($bufferSize, $windowSize, $scheduler) { |
| 757 | + return new ReplaySubject($bufferSize, $windowSize, $scheduler); |
| 758 | + }, $selector) : |
| 759 | + $this->multicast(new ReplaySubject($bufferSize, $windowSize, $scheduler)); |
| 760 | + } |
| 761 | + |
639 | 762 | }
|
0 commit comments