def test_connectable_observable_forward_scheduler(self): scheduler = TestScheduler() subscribe_scheduler = 'unknown' def subscribe(observer, scheduler=None): nonlocal subscribe_scheduler subscribe_scheduler = scheduler xs = rx.create(subscribe) subject = MySubject() conn = ConnectableObservable(xs, subject) conn.connect(scheduler) assert subscribe_scheduler is scheduler
def test_connectable_observable_disconnected(self): scheduler = TestScheduler() xs = scheduler.create_hot_observable(on_next(210, 1), on_next(220, 2), on_next(230, 3), on_next(240, 4), on_completed(250)) subject = MySubject() conn = ConnectableObservable(xs, subject) disconnect = conn.connect(scheduler) disconnect.dispose() res = scheduler.start(lambda: conn) assert res.messages == []
def test_connectable_observable_disconnected(self): scheduler = TestScheduler() xs = scheduler.create_hot_observable( on_next(210, 1), on_next(220, 2), on_next(230, 3), on_next(240, 4), on_completed(250) ) subject = MySubject() conn = ConnectableObservable(xs, subject) disconnect = conn.connect(scheduler) disconnect.dispose() res = scheduler.start(lambda: conn) assert res.messages == []
def test_ref_count_notconnected(self): disconnected = [False] count = [0] def factory(scheduler): count[0] += 1 def create(obs): def func(): disconnected[0] = True return func return Observable.create(create) xs = Observable.defer(factory) subject = MySubject() conn = ConnectableObservable(xs, subject) refd = conn.ref_count() dis1 = refd.subscribe() self.assertEqual(1, count[0]) self.assertEqual(1, subject.subscribe_count) assert (not disconnected[0]) dis2 = refd.subscribe() self.assertEqual(1, count[0]) self.assertEqual(2, subject.subscribe_count) assert (not disconnected[0]) dis1.dispose() assert (not disconnected[0]) dis2.dispose() assert (disconnected[0]) disconnected[0] = False dis3 = refd.subscribe() self.assertEqual(2, count[0]) self.assertEqual(3, subject.subscribe_count) assert (not disconnected[0]) dis3.dispose() assert (disconnected[0])
def test_ref_count_notconnected(self): disconnected = [False] count = [0] def factory(scheduler): count[0] += 1 def create(obs, scheduler=None): def func(): disconnected[0] = True return func return rx.create(create) xs = rx.defer(factory) subject = MySubject() conn = ConnectableObservable(xs, subject) refd = conn.pipe(ops.ref_count()) dis1 = refd.subscribe() self.assertEqual(1, count[0]) self.assertEqual(1, subject.subscribe_count) assert not disconnected[0] dis2 = refd.subscribe() self.assertEqual(1, count[0]) self.assertEqual(2, subject.subscribe_count) assert not disconnected[0] dis1.dispose() assert not disconnected[0] dis2.dispose() assert disconnected[0] disconnected[0] = False dis3 = refd.subscribe() self.assertEqual(2, count[0]) self.assertEqual(3, subject.subscribe_count) assert not disconnected[0] dis3.dispose() assert disconnected[0]
def multicast( source: Observable) -> Union[Observable, ConnectableObservable]: if subject_factory: def subscribe(observer, scheduler=None): connectable = source.pipe( _multicast(subject=subject_factory(scheduler))) subscription = mapper(connectable).subscribe( observer, scheduler=scheduler) return CompositeDisposable(subscription, connectable.connect(scheduler)) return Observable(subscribe) return ConnectableObservable(source, subject)
def test_connectable_observable_creation(self): y = [0] s2 = Subject() co2 = ConnectableObservable(rx.return_value(1), s2) def on_next(x): y[0] = x co2.subscribe(on_next=on_next) self.assertNotEqual(1, y[0]) co2.connect() self.assertEqual(1, y[0])
def multicast( source: ObservableBase, subject: Subject = None, subject_factory: Callable[[], Subject] = None, mapper: Mapper = None) -> Union[ObservableBase, ConnectableObservable]: """Multicasts the source sequence notifications through an instantiated subject into all uses of the sequence within a mapper function. Each subscription to the resulting sequence causes a separate multicast invocation, exposing the sequence resulting from the mapper function's invocation. For specializations with fixed subject types, see Publish, PublishLast, and Replay. Example: 1 - res = source.multicast(observable) 2 - res = source.multicast(subject_factory=lambda scheduler: Subject(), mapper=lambda x: x) Keyword arguments: subject_factory -- Factory function to create an intermediate subject through which the source sequence's elements will be multicast to the mapper function. subject -- Subject to push source elements into. mapper -- [Optional] Optional mapper function which can use the multicasted source sequence subject to the policies enforced by the created subject. Specified only if subject_factory" is a factory function. Returns an observable sequence that contains the elements of a sequence produced by multicasting the source sequence within a mapper function. """ if subject_factory: def subscribe(observer, scheduler=None): connectable = source.multicast(subject=subject_factory(scheduler)) subscription = mapper(connectable).subscribe(observer, scheduler) return CompositeDisposable(subscription, connectable.connect(scheduler)) return AnonymousObservable(subscribe) return ConnectableObservable(source, subject)
def test_ref_count_connects_on_first(self): scheduler = TestScheduler() xs = scheduler.create_hot_observable(on_next(210, 1), on_next(220, 2), on_next(230, 3), on_next(240, 4), on_completed(250)) subject = MySubject() conn = ConnectableObservable(xs, subject) def create(): return conn.ref_count() res = scheduler.start(create) assert res.messages == [ on_next(210, 1), on_next(220, 2), on_next(230, 3), on_next(240, 4), on_completed(250) ] assert (subject.disposed)