Ejemplo n.º 1
0
    def observe(self, observer_info: ObserverInfo):
        self.observer = observer_info.observer

        class ZipLeftObserver(Observer):
            def on_next(_, elem: ElementType) -> Ack:
                return self._on_next_left(elem)

            def on_error(_, exc: Exception):
                self._on_error(exc)

            def on_completed(_):
                self._on_completed_left()

        class ZipRightObserver(Observer):
            def on_next(_, elem: ElementType) -> Ack:
                return self._on_next_right(elem)

            def on_error(_, exc: Exception):
                self._on_error(exc)

            def on_completed(_):
                self._on_completed_right()

        left_observer = ZipLeftObserver()
        left_subscription = observer_info.copy(observer=left_observer, )
        d1 = self.left.observe(left_subscription)

        right_observer = ZipRightObserver()
        right_subscription = observer_info.copy(observer=right_observer, )
        d2 = self.right.observe(right_subscription)

        return CompositeDisposable(d1, d2)
Ejemplo n.º 2
0
    def observe(self, observer_info: ObserverInfo):
        """
        sources[0] ------------------------> Subject --
                                                       \
        sources[1] -> ConnectableObserver -> Subject -----> ConcatObserver
        ...                                            /
        sources[n] -> ConnectableObserver -> Subject --
        """

        conn_observers = [
            ConnectableObserver(underlying=subject, )
            for subject in self.subjects[1:]
        ]

        connectables = iter(conn_observers)

        concat_observer = ConcatObserver(next_observer=observer_info.observer,
                                         connectables=connectables)

        concat_observer_info = observer_info.copy(observer=concat_observer, )

        for subject in self.subjects:
            subject.observe(concat_observer_info)

        def gen_disposable_from_observer():
            for source, conn_observer in zip(self.sources[1:], conn_observers):
                yield source.observe(
                    observer_info.copy(observer=conn_observer, ))

        others_disposables = gen_disposable_from_observer()

        first_disposable = self.sources[0].observe(
            observer_info.copy(observer=self.subjects[0], ))

        return CompositeDisposable(first_disposable, *others_disposables)
Ejemplo n.º 3
0
 def observe(self, observer_info: ObserverInfo):
     return self.source.observe(
         observer_info=observer_info.copy(observer=RepeatFirstObserver(
             next_observer=observer_info.observer,
             scheduler=self.scheduler,
         ))
     )
Ejemplo n.º 4
0
 def observe(self, observer_info: ObserverInfo):
     return self.source.observe(observer_info.copy(
         observer=MapObserver(
             source=observer_info.observer,
             func=self.func,
         ),
     ))
 def observe(self, observer_info: ObserverInfo):
     subscription = observer_info.copy(observer=IndexedFilterObserver(
         observer=observer_info.observer,
         predicate=self.predicate,
         selector_subject=self.selector_subject,
     ), )
     return self.source.observe(subscription)
Ejemplo n.º 6
0
    def observe(self, observer_info: ObserverInfo):
        self.on_observe(observer_info)

        if self.subscriber.subscribe_scheduler.idle:
            raise Exception(
                to_operator_exception(
                    message=
                    'observe method call should be scheduled on subscribe scheduler',
                    stack=self.stack,
                ))

        observer = DebugObserver(
            source=observer_info.observer,
            name=self.name,
            on_next_func=self.on_next,
            on_completed_func=self.on_completed,
            on_error_func=self.on_error,
            on_sync_ack=self.on_sync_ack,
            on_async_ack=self.on_async_ack,
            on_raw_ack=self.on_raw_ack,
            stack=self.stack,
        )

        # def action(_, __):
        #     observer.has_scheduled_next = True
        # self.subscriber.subscribe_scheduler.schedule(action)

        return self.source.observe(observer_info.copy(observer=observer, ))
 def observe(self, observer_info: ObserverInfo):
     first_observer = DefaultIfEmptyObserver(
         next_observer=observer_info.observer,
         lazy_val=self.lazy_val,
     )
     map_subscription = observer_info.copy(observer=first_observer)
     return self.source.observe(map_subscription)
Ejemplo n.º 8
0
 def observe(self, observer_info: ObserverInfo):
     next_observer_info = observer_info.copy(
         observer=TakeWhileObserver(
             observer=observer_info.observer,
             predicate=self.predicate,
         ),
     )
     return self.source.observe(next_observer_info)
 def observe(self, observer_info: ObserverInfo):
     return self.source.observe(
         observer_info.copy(observer=BufferedObserver(
             underlying=observer_info.observer,
             scheduler=self.scheduler,
             subscribe_scheduler=self.subscribe_scheduler,
             buffer_size=self.buffer_size,
         ), ))
Ejemplo n.º 10
0
    def observe(self, observer_info: ObserverInfo):
        func = self.func

        map_subscription = observer_info.copy(observer=MapToIteratorObserver(
            source=observer_info.observer,
            func=func,
        ), )
        return self.source.observe(map_subscription)
 def observe(self, observer_info: ObserverInfo):
     return self.source.observe(observer_info.copy(
         observer=ScanObserver(
             observer=observer_info.observer,
             func=self.func,
             initial=self.initial,
         ),
     ))
Ejemplo n.º 12
0
    def observe(self, observer_info: ObserverInfo) -> Disposable:
        observer = EvictingBufferedObserver(
            observer=observer_info.observer,
            scheduler=self.scheduler,
            subscribe_scheduler=self.subscribe_scheduler,
            strategy=self.overflow_strategy,
        )

        return self.source.observe(observer_info.copy(observer=observer, ))
Ejemplo n.º 13
0
    def observe(self, observer_info: ObserverInfo):
        if self.on_error is not None:

            def on_error(exc):
                self.on_error(exc)
                return observer_info.observer.on_error(exc)

        else:
            on_error = observer_info.observer.on_error

        if self.on_completed is not None:

            def on_completed():
                self.on_completed()
                return observer_info.observer.on_completed()

        else:
            on_completed = observer_info.observer.on_completed

        if self.on_next is not None:

            def on_next(val):
                if not isinstance(val, list):
                    try:
                        val = list(val)
                    except Exception as exc:
                        on_error(exc)
                        return stop_ack

                for item in val:
                    self.on_next(item)

                return observer_info.observer.on_next(val)

        else:
            on_next = observer_info.observer.on_next

        observer = type(
            'DoActionObserver',
            (Observer, object),
            {
                'on_next': on_next,
                'on_completed': on_completed,
                'on_error': on_error
            },
        )

        disposable = self.source.observe(
            observer_info=observer_info.copy(observer=observer), )

        if self.on_disposed is None:
            return disposable
        else:
            return CompositeDisposable(disposable,
                                       Disposable(self.on_disposed))
Ejemplo n.º 14
0
    def test_use_case(self):
        sink = TObserver(immediate_continue=0)
        subscription = rxbp.from_range(1, 4).unsafe_subscribe(
            Subscriber(scheduler=self.scheduler,
                       subscribe_scheduler=self.scheduler))
        subscription.observable.observe(ObserverInfo(observer=sink))

        self.scheduler.advance_by(1)

        self.assertEqual([1, 2, 3], sink.received)
        self.assertTrue(sink.is_completed)
    def observe(self, observer_info: ObserverInfo):
        """ This function ought be called at most once.

        :param observer_info: downstream obseNonerver
        :return: Disposable
        """

        self.observer = observer_info.observer
        source = self

        class LeftControlledZipObserver(Observer):
            def on_next(self, elem: ElementType) -> Ack:
                return source._on_next_left(elem)

            def on_error(self, exc: Exception):
                return source._on_error(exc)

            def on_completed(self):
                source._on_completed_left()

        class RightControlledZipObserver(Observer):
            def on_next(self, elem: ElementType) -> Ack:
                return source._on_next_right(elem)

            def on_error(self, exc: Exception):
                return source._on_error(exc)

            def on_completed(self):
                source._on_completed_right()

        left_observer = LeftControlledZipObserver()
        left_subscription = observer_info.copy(observer=left_observer)
        d1 = self.left_observable.observe(left_subscription)

        right_observer = RightControlledZipObserver()
        left_subscription = observer_info.copy(observer=right_observer)
        d2 = self.right_observable.observe(left_subscription)

        return CompositeDisposable(d1, d2)
Ejemplo n.º 16
0
    def observe(self, observer_info: ObserverInfo):
        """ This function ought be called at most once.

        :param observer_info: downstream obseNonerver
        :return: Disposable
        """

        self.observer = observer_info.observer
        outer_self = self

        class LeftControlledZipObserver(Observer):
            def on_next(self, elem: ElementType) -> Ack:
                return outer_self._iterate_over_batch(elem=elem, is_left=True)

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

            def on_completed(self):
                outer_self._on_completed_left()

        class RightControlledZipObserver(Observer):
            def on_next(self, elem: ElementType) -> Ack:
                return outer_self._iterate_over_batch(elem=elem, is_left=False)

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

            def on_completed(self):
                outer_self._on_completed_right()

        left_observer = LeftControlledZipObserver()
        left_subscription = observer_info.copy(observer=left_observer, )
        d1 = self.left.observe(left_subscription)

        right_observer = RightControlledZipObserver()
        left_subscription = observer_info.copy(observer=right_observer, )
        d2 = self.right.observe(left_subscription)

        return CompositeDisposable(d1, d2)
Ejemplo n.º 17
0
    def observe(self, observer_info: ObserverInfo):
        composite_disposable = CompositeDisposable()

        disposable = self.source.observe(
            observer_info.copy(observer=FlatMapObserver(
                observer_info=observer_info,
                func=self.func,
                scheduler=self.scheduler,
                subscribe_scheduler=self.subscribe_scheduler,
                composite_disposable=composite_disposable,
            )), )
        composite_disposable.add(disposable)

        return composite_disposable
    def observe(self, observer_info: ObserverInfo):
        composite_disposable = CompositeDisposable()

        disposable = self.source.observe(
            observer_info.copy(observer=FlatMergeNoBackpressureObserver(
                observer=observer_info.observer,
                selector=self.selector,
                scheduler=self.scheduler,
                subscribe_scheduler=self.subscribe_scheduler,
                composite_disposable=composite_disposable,
            ), ))
        composite_disposable.add(disposable)

        return composite_disposable
Ejemplo n.º 19
0
    def observe(self, observer_info: ObserverInfo):
        self.observer = observer_info.observer
        source = self

        class LeftObserver(Observer):
            def on_next(self, elem: ElementType):
                next_state = RawMergeStates.OnLeftReceived(elem=elem,
                                                           ack=AckSubject())
                return source._on_next(next_state)

            def on_error(self, exc):
                source._on_error(exc)

            def on_completed(self):
                source._on_completed_left()

        class RightObserver(Observer):
            def on_next(self, elem: ElementType):
                next_state = RawMergeStates.OnRightReceived(elem=elem,
                                                            ack=AckSubject())
                return source._on_next(next_state)

            def on_error(self, exc):
                source._on_error(exc)

            def on_completed(self):
                source._on_completed_right()

        left_observer = LeftObserver()
        left_subscription = observer_info.copy(observer=left_observer)
        d1 = self.left.observe(left_subscription)

        right_observer = RightObserver()
        right_subscription = observer_info.copy(observer=right_observer)
        d2 = self.right.observe(right_subscription)

        return CompositeDisposable(d1, d2)
Ejemplo n.º 20
0
    def test_from_list_batch_size_of_two(self):
        sink = TObserver(immediate_continue=0)
        subscription = rxbp.from_range(1, 4, batch_size=2).unsafe_subscribe(
            Subscriber(scheduler=self.scheduler,
                       subscribe_scheduler=self.scheduler))
        subscription.observable.observe(ObserverInfo(observer=sink))

        self.scheduler.advance_by(1)

        self.assertEqual([1, 2], sink.received)

        sink.ack.on_next(continue_ack)
        self.scheduler.advance_by(1)

        self.assertEqual([1, 2, 3], sink.received)
        self.assertTrue(sink.is_completed)
    def test_from_list_batch_size_of_one(self):
        test_list = [1, 2, 3]

        sink = TObserver(immediate_continue=0)
        subscription = rxbp.from_list(
            test_list, batch_size=1).unsafe_subscribe(
                Subscriber(scheduler=self.scheduler,
                           subscribe_scheduler=self.scheduler))
        subscription.observable.observe(ObserverInfo(observer=sink))

        self.scheduler.advance_by(1)

        self.assertEqual(test_list[:1], sink.received)
        self.assertFalse(sink.is_completed)

        sink.ack.on_next(continue_ack)
        self.scheduler.advance_by(1)

        self.assertEqual(test_list[:2], sink.received)
    def observe(self, observer_info: ObserverInfo):
        @dataclass
        class MaterializeObserver(Observer):
            source: Observer

            def on_next(self, elem: ElementType) -> Ack:
                if not isinstance(elem, list):
                    elem = list(elem)

                return self.source.on_next(elem)

            def on_error(self, exc: Exception):
                return self.source.on_error(exc)

            def on_completed(self):
                return self.source.on_completed()

        return self.source.observe(
            observer_info.copy(observer=MaterializeObserver(
                source=observer_info.observer, ), ))
Ejemplo n.º 23
0
    def observe(self, observer_info: ObserverInfo):
        class ToListObserver(Observer):
            def __init__(
                self,
                func: Callable[[Any, Any], Any],
                initial: Any,
            ):
                self.func = func
                self.acc = initial

            def on_next(self, elem: ElementType):
                try:
                    if isinstance(elem, list):
                        materialized_values = elem
                    else:
                        materialized_values = list(elem)

                    for value in materialized_values:
                        self.acc = self.func(self.acc, value)

                except Exception as exc:
                    self.on_error(exc)
                    return stop_ack

                return continue_ack

            def on_error(self, exc):
                return observer_info.observer.on_error(exc)

            def on_completed(self):
                _ = observer_info.observer.on_next([self.acc])
                observer_info.observer.on_completed()

        return self.source.observe(
            observer_info.copy(observer=ToListObserver(
                func=self.func,
                initial=self.initial,
            ), ))
Ejemplo n.º 24
0
    def observe(self, observer_info: ObserverInfo) -> Disposable:
        observer_info = observer_info.observer

        class IdentityObserver(Observer):
            def on_next(self, elem: ElementType):
                def gen_select_msg():
                    for _ in elem:
                        yield select_next
                        yield select_completed

                # val = list(gen_select_msg())
                # ack = observer_info.on_next(val)

                ack = observer_info.on_next(gen_select_msg())
                return ack

            def on_error(self, exc: Exception):
                observer_info.on_next(exc)

            def on_completed(self):
                observer_info.on_completed()

        self._source.observe(observer_info.copy(observer=IdentityObserver(), ))
 def observe(self, observer_info: ObserverInfo):
     to_list_observer = ToListObserver(observer=observer_info.observer)
     return self.source.observe(
         observer_info.copy(
             observer=ToListObserver(observer=observer_info.observer), ))
 def observe(self, observer_info: ObserverInfo):
     return self.source.observe(
         observer_info.copy(observer=FirstOrDefaultObserver(
             observer=observer_info.observer,
             lazy_val=self.lazy_val,
         )))
Ejemplo n.º 27
0
 def observe(self, observer_info: ObserverInfo):
     subscription = observer_info.copy(observer=FilterObserver(
         observer=observer_info.observer,
         predicate=self.predicate,
     ), )
     return self.source.observe(subscription)
 def observe(self, observer_info: ObserverInfo):
     return self.source.observe(
         observer_info.copy(observer=LastObserver(
             observer=observer_info.observer,
             stack=self.stack,
         ), ))
Ejemplo n.º 29
0
 def observe(self, observer_info: ObserverInfo):
     return self.source.observe(
         observer_info.copy(observer=ObserveOnObserver(
             observer=observer_info.observer,
             scheduler=self.scheduler,
         )))