def __init__(
            self,
            source: Flowable,
            scheduler: Scheduler = None,
    ):
        self._cache = []
        self._exception = None

        outer_self = self

        class ToCacheObserver(Observer):
            def on_next(self, elem: ElementType) -> Ack:
                try:
                    for value in elem:
                        outer_self._cache.append(value)
                except Exception as exc:
                    outer_self._exception = exc
                    return stop_ack

                return continue_ack

            def on_error(self, exc: Exception):
                outer_self._exception = exc

            def on_completed(self):
                pass

        self._observer = ToCacheObserver()

        source.subscribe(
            observer=self._observer,
            scheduler=scheduler,
            subscribe_scheduler=scheduler,
        )
    def test_reduce_single_flowables_with_maintaining_order(self):
        reduce_multicast = CollectFlowablesMultiCast(
            source=self.source_multicast,
            maintain_order=True,
        )
        reduce_multicast.get_source(self.info).subscribe(self.rx_sink)
        self.source_multicast.on_next(Flowable(self.source1))
        self.source_multicast.on_next(Flowable(self.source2))
        self.source_multicast.on_completed()

        sink = TObserver()
        subscription = self.rx_sink.received[0].unsafe_subscribe(Subscriber(
            scheduler=self.source_scheduler,
            subscribe_scheduler=self.source_scheduler
        ))
        subscription.observable.observe(init_observer_info(observer=sink))

        # sending the lifted flowable is scheduled on the multicast_scheduler
        self.multicast_scheduler.advance_by(1)
        self.source_scheduler.advance_by(1)

        self.source1.on_next_single(1)
        self.source2.on_next_single('a')
        self.source1.on_next_single(2)
        self.source1.on_completed()
        self.source2.on_next_single('b')
        self.source2.on_completed()

        self.assertEqual([1, 2, 'a', 'b'], sink.received)
        self.assertTrue(sink.is_completed)
    def test_send_dictionary(self):
        reduce_multicast = CollectFlowablesMultiCast(source=self.source_multicast)
        reduce_multicast.get_source(self.info).subscribe(self.rx_sink)

        self.source_multicast.on_next({'f1': Flowable(self.source1)})

        self.assertEqual(1, len(self.rx_sink.received))
예제 #4
0
 def op_func(left: Flowable):
     return left.do_action(
         on_next=on_next,
         on_completed=on_completed,
         on_error=on_error,
         on_disposed=on_disposed,
     )
    def subscribe_to(self, source: Flowable, scheduler: Scheduler = None):
        scheduler = scheduler or self.scheduler

        subscription = source.unsafe_subscribe(
            init_subscriber(
                scheduler=scheduler,
                subscribe_scheduler=self.subscribe_scheduler,
            ))

        outer_self = self

        class SafeFlowableObserver(Observer):
            def on_next(self, elem: Any):
                if not outer_self.is_stopped:
                    outer_self.is_first = False

                    if outer_self._observable_subject is not None:
                        return outer_self._observable_subject.on_next(elem)

                return continue_ack

            def on_error(self, exc: Exception):
                outer_self.on_error(exc)

            def on_completed(self):
                outer_self.on_completed()

        observer = SafeFlowableObserver()

        disposable = subscription.observable.observe(
            init_observer_info(observer=observer, ))

        self.composite_diposable.add(disposable)
예제 #6
0
    def test_subscribe_single_flowable(self):
        reduce_multicast = LoopFlowableMultiCast(
            source=self.source_multicast,
            func=lambda m: MultiCast(m).pipe(
                rxbp.multicast.op.map(lambda t: t[0]), ),
            initial=[10],
        )
        reduce_multicast.get_source(self.info).subscribe(self.rx_sink)
        self.source_multicast.on_next(Flowable(self.source1))

        sink = TObserver(immediate_continue=0)
        subscription = self.rx_sink.received[0].unsafe_subscribe(
            Subscriber(
                scheduler=self.source_scheduler,
                subscribe_scheduler=self.source_scheduler,
            ))
        subscription.observable.observe(init_observer_info(sink))

        self.multicast_scheduler.advance_by(1)
        self.source_scheduler.advance_by(1)

        # self.source1.on_next_single(0)

        print(sink.received)

        self.assertEqual([10], sink.received)
예제 #7
0
 def op_func(left: Flowable):
     return left.controlled_zip(
         right=right,
         stack=stack,
         request_left=request_left,
         request_right=request_right,
         match_func=match_func,
     )
    def test_send_single_flowable(self):
        reduce_multicast = SharedMultiCast(source=self.source_multicast)
        observable = reduce_multicast.get_source(self.info)

        observable.subscribe(self.rx_sink1)
        observable.subscribe(self.rx_sink2)

        self.source_multicast.on_next(Flowable(self.source1))

        self.assertEqual(1, len(self.rx_sink1.received))
        self.assertEqual(1, len(self.rx_sink2.received))
예제 #9
0
    def test_send_single_flowable(self):
        reduce_multicast = LoopFlowableMultiCast(
            source=self.source_multicast,
            func=lambda m: MultiCast(m).pipe(
                rxbp.multicast.op.map(lambda t: t[0]), ),
            initial=[0],
        )
        reduce_multicast.get_source(self.info).subscribe(self.rx_sink)

        self.source_multicast.on_next(Flowable(self.source1))

        self.assertEqual(1, len(self.rx_sink.received))
예제 #10
0
 def op_func(source: Flowable):
     return source.debug(
         name=name,
         on_next=on_next,
         on_completed=on_completed,
         on_error=on_error,
         on_observe=on_observe,
         on_subscribe=on_subscribe,
         on_sync_ack=on_sync_ack,
         on_async_ack=on_async_ack,
         on_raw_ack=on_raw_ack,
         stack=stack,
         verbose=verbose,
     )
    def test_use_case(self):
        sink = TObserver()
        subscription = rxbp.merge(
            *[Flowable(e) for e in self.sources]).unsafe_subscribe(
                Subscriber(scheduler=self.scheduler,
                           subscribe_scheduler=self.scheduler))
        subscription.observable.observe(init_observer_info(observer=sink))

        self.sources[0].observable.on_next_single(1)
        self.sources[1].observable.on_next_single(2)
        self.sources[2].observable.on_next_single(3)

        self.scheduler.advance_by(1)

        self.assertEqual([1, 2, 3], sink.received)
예제 #12
0
        def func(builder: ImperativeMultiCastBuilder):
            builder = ImperativeMultiCastBuilder(
                source_scheduler=self.scheduler,
                composite_disposable=self.composite_disposable,
            )

            s1[0] = builder.create_multicast_subject()
            s2[0] = builder.create_flowable_subject()
            s3[0] = builder.create_flowable_subject()

            # s1[0].to_flowable().subscribe(observer=self.sink1, scheduler=self.scheduler)
            s2[0].subscribe(observer=self.sink2, scheduler=self.scheduler)

            blocking_flowable = Flowable(self.source)

            def output_selector(flowable: Flowable):
                return rxbp.multicast.return_flowable(flowable)

            return builder.return_(
                blocking_flowable=blocking_flowable,
                output_selector=output_selector,
            )
예제 #13
0
 def op_func(left: Flowable):
     return left.filter(predicate=predicate, stack=stack)
예제 #14
0
 def op_func(source: Flowable):
     return source.flat_map(func=func, stack=stack)
예제 #15
0
 def op_func(source: Flowable):
     return source.first_or_default(lazy_val=lazy_val)
예제 #16
0
 def op_func(source: Flowable):
     return source.last(stack=stack)
예제 #17
0
 def op_func(source: Flowable):
     return source.flat_map(func=lambda v: v, stack=stack)
예제 #18
0
 def op_func(left: Flowable):
     return left.merge(*others)
예제 #19
0
 def op_func(left: Flowable):
     return left.zip_with_index()
예제 #20
0
 def op_func(source: Flowable):
     return source.scan(func=func, initial=initial)
예제 #21
0
 def op_func(source: Flowable):
     return source.to_list()
예제 #22
0
 def op_func(source: Flowable):
     return source.reduce(func=func, initial=initial)
예제 #23
0
 def op_func(source: Flowable):
     return source.repeat_first()
예제 #24
0
 def op_func(source: Flowable):
     return source.pairwise()
예제 #25
0
 def op_func(source: Flowable):
     return source.map(func=func)
예제 #26
0
 def op_func(left: Flowable):
     return left.concat(*sources)
예제 #27
0
 def op_func(source: Flowable):
     return source.buffer(buffer_size=buffer_size)
예제 #28
0
 def op_func(left: Flowable):
     return left.zip(others=others, stack=stack)
예제 #29
0
 def op_func(source: Flowable):
     return source.observe_on(scheduler=scheduler)
예제 #30
0
 def op_func(source: Flowable):
     return source.map_to_iterator(func=func)