Esempio n. 1
0
 def setUp(self) -> None:
     self.scheduler = TScheduler()
     self.sources = [TestFlowable(), TestFlowable(), TestFlowable()]
     self.subscriber = init_subscriber(
         scheduler=self.scheduler,
         subscribe_scheduler=self.scheduler,
     )
Esempio n. 2
0
 def setUp(self) -> None:
     self.scheduler = TScheduler()
     self.left_base = ObjectRefBase('left')
     self.right_base = ObjectRefBase('right')
     self.left = TestFlowable(base=self.left_base)
     self.right = TestFlowable(base=self.right_base)
     self.subscriber = init_subscriber(self.scheduler, self.scheduler)
    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)
 def unsafe_subscribe(
         self, subscriber: MultiCastSubscriber) -> MultiCastSubscription:
     subscription = self.source.unsafe_subscribe(
         init_subscriber(
             scheduler=subscriber.subscribe_schedulers[0],
             subscribe_scheduler=subscriber.subscribe_schedulers[0],
         ))
     return init_multicast_subscription(
         observable=FromFlowableMultiCastObservable(
             source=subscription.observable,
             subscriber=subscriber,
         ), )
    def subscribe(
            self,
            on_next: Callable[[Any], None] = None,
            on_error: Callable[[Any], None] = None,
            on_completed: Callable[[], None] = None,
            scheduler: Scheduler = None,
            subscribe_scheduler: Scheduler = None,
            observer: Observer = None,
    ) -> rx.typing.Disposable:
        """ Calling `subscribe` method starts some kind of process that

        start a chain reaction where downsream `Flowables`
        call the `subscribe` method of their linked upstream `Flowable` until
        the sources start emitting data. Once a `Flowable` is subscribed, we
        allow it to have mutable states where it make sense.
        """

        assert isinstance(self, SharedFlowableMixin) is False, \
            'a shared Flowable cannot be subscribed, use Flowable inside MultiCast instead'

        subscribe_scheduler_ = subscribe_scheduler or TrampolineScheduler()
        scheduler_ = scheduler or subscribe_scheduler_

        subscriber = init_subscriber(
            scheduler=scheduler_,
            subscribe_scheduler=subscribe_scheduler_,
        )

        subscription = self.unsafe_subscribe(subscriber=subscriber)

        assert isinstance(subscription, Subscription), \
            f'"{subscription}" must be of type Subscription'

        disposable = self._observe(
            observable=subscription.observable,
            on_next=on_next,
            on_completed=on_completed,
            on_error=on_error,
            observer=observer,
            subscribe_scheduler=subscribe_scheduler_,
        )

        assert isinstance(disposable, rx.typing.Disposable), \
            f'"{disposable}" must be of type Disposable'

        return disposable
Esempio n. 6
0
    def unsafe_subscribe(self, subscriber: Subscriber):
        scheduler = self._scheduler or TrampolineScheduler()

        updated_subscriber = init_subscriber(
            scheduler=subscriber.scheduler,
            subscribe_scheduler=scheduler,
        )

        subscription = self._source.unsafe_subscribe(updated_subscriber)

        class SubscribeOnObservable(Observable):
            def observe(_, observer_info: ObserverInfo):
                def action(_, __):
                    return subscription.observable.observe(observer_info)

                disposable = scheduler.schedule(action)

                return disposable

        observable = SubscribeOnObservable()

        return init_subscription(observable=observable)
 def setUp(self) -> None:
     self.scheduler = TScheduler()
     self.subscriber = init_subscriber(
         scheduler=self.scheduler,
         subscribe_scheduler=self.scheduler,
     )
Esempio n. 8
0
        def _subscribe_core(self,
                            observer: typing.Observer,
                            scheduler: typing.Scheduler = None):
            class RxBPScheduler(SchedulerBase):
                def __init__(self, underlying):
                    super().__init__()

                    self.underlying = underlying

                def sleep(self, seconds: float) -> None:
                    pass

                @property
                def now(self) -> datetime:
                    return self.underlying.now

                @property
                def is_order_guaranteed(self) -> bool:
                    # unknown property, therefore select pessimistically
                    return False

                def schedule(self,
                             action: ScheduledAction,
                             state: TState = None) -> Disposable:
                    return self.underlying.schedule(action=action, state=state)

                def schedule_relative(self,
                                      duetime: RelativeTime,
                                      action: ScheduledAction,
                                      state: TState = None) -> Disposable:
                    return self.underlying.schedule_relative(duetime=duetime,
                                                             action=action,
                                                             state=state)

                def schedule_absolute(self,
                                      duetime: AbsoluteTime,
                                      action: ScheduledAction,
                                      state: TState = None) -> Disposable:
                    return self.underlying.schedule_absolute(duetime=duetime,
                                                             action=action,
                                                             state=state)

                def schedule_periodic(
                        self,
                        period: RelativeTime,
                        action: ScheduledPeriodicAction,
                        state: Optional[TState] = None) -> Disposable:
                    raise NotImplementedError

            class ToRxObserver(Observer):
                @property
                def is_volatile(self):
                    return False

                def on_next(self, elem: ElementType):
                    for e in elem:
                        observer.on_next(e)
                    return continue_ack

                def on_error(self, err):
                    observer.on_error(err)

                def on_completed(self):
                    observer.on_completed()

            to_rx_observer = ToRxObserver()

            if batched is True:

                def on_next(v):
                    batch = list(v())
                    observer.on_next(batch)
                    return continue_ack

                to_rx_observer.on_next = on_next

            trampoline_scheduler = subscribe_schduler or TrampolineScheduler()
            scheduler_ = RxBPScheduler(
                underlying=scheduler
            ) if scheduler is not None else trampoline_scheduler
            subscriber = init_subscriber(
                scheduler=scheduler_, subscribe_scheduler=trampoline_scheduler)
            # observer_info = init_observer_info(observer=to_rx_observer)
            return source.subscribe(
                observer=to_rx_observer,
                subscribe_scheduler=subscriber.subscribe_scheduler,
                scheduler=subscriber.subscribe_scheduler,
            )