Example #1
0
        def _subscribe(observer):
            def fix_subscriber(subscriber):
                """Fix subscriber to check for None or function returned to 
                decorate as Disposable"""
            
                if subscriber is None:
                    subscriber = Disposable.empty()
                elif type(subscriber) == types.FunctionType:
                    subscriber = Disposable(subscriber)

                return subscriber

            def set_disposable(scheduler=None, value=None):
                try:
                    auto_detach_observer.disposable = fix_subscriber(subscribe(auto_detach_observer))
                except Exception as ex:
                    if not auto_detach_observer.fail(ex):
                        raise ex

            auto_detach_observer = AutoDetachObserver(observer)

            if current_thread_scheduler.schedule_required():
                current_thread_scheduler.schedule(set_disposable)
            else:
                set_disposable()

            return auto_detach_observer
    def subscribe(self, on_next=None, on_error=None, on_completed=None, observer=None):

        if isinstance(on_next, Observer):
            observer = on_next
        elif hasattr(on_next, "on_next") and callable(on_next.on_next):
            observer = on_next
        elif not observer:
            observer = AnonymousObserver(on_next, on_error, on_completed)

        auto_detach_observer = AsyncgenObserver(self._asyncgen, observer)

        def fix_subscriber(subscriber):
            """Fixes subscriber to make sure it returns a Disposable instead
            of None or a dispose function"""

            if not hasattr(subscriber, "dispose"):
                subscriber = Disposable.create(subscriber)

            return subscriber

        def set_disposable(scheduler=None, value=None):
            try:
                subscriber = self._subscribe_core(auto_detach_observer)
            except Exception as ex:
                if not auto_detach_observer.fail(ex):
                    raise
            else:
                auto_detach_observer.disposable = fix_subscriber(subscriber)

        # Subscribe needs to set up the trampoline before for subscribing.
        # Actually, the first call to Subscribe creates the trampoline so
        # that it may assign its disposable before any observer executes
        # OnNext over the CurrentThreadScheduler. This enables single-
        # threaded cancellation
        # https://social.msdn.microsoft.com/Forums/en-US/eb82f593-9684-4e27-
        # 97b9-8b8886da5c33/whats-the-rationale-behind-how-currentthreadsche
        # dulerschedulerequired-behaves?forum=rx
        if current_thread_scheduler.schedule_required():
            current_thread_scheduler.schedule(set_disposable)
        else:
            set_disposable()

        # Hide the identity of the auto detach observer
        return Disposable.create(auto_detach_observer.dispose)
        def _subscribe(observer):
            auto_detach_observer = AutoDetachObserver(observer)

            # Backup. Kick queue if there's anything left: TODO: can we move this somewhere else?
            if current_thread_scheduler.schedule_required():
                def action(scheduler, state=None):
                    try:
                        auto_detach_observer.disposable = subscribe(auto_detach_observer)
                    except Exception as ex:
                        if not auto_detach_observer.fail(ex):
                            raise ex
                current_thread_scheduler.schedule(action)
            else:
                try:
                    auto_detach_observer.disposable = subscribe(auto_detach_observer)
                except Exception as ex:
                    if not auto_detach_observer.fail(ex):
                        raise ex

            return auto_detach_observer
Example #4
0
        def _subscribe(observer):
            """Decorator for subscribe. It wraps the observer in an
            AutoDetachObserver and fixes the returned disposable"""
            def fix_subscriber(subscriber):
                """Fixes subscriber to make sure it returns a Disposable instead
                of None or a dispose function"""

                if not hasattr(subscriber, "dispose"):
                    subscriber = Disposable(subscriber)

                return subscriber

            def set_disposable(scheduler=None, value=None):
                try:
                    auto_detach_observer.disposable = fix_subscriber(
                        subscribe(auto_detach_observer))
                except Exception as ex:
                    if not auto_detach_observer.fail(ex):
                        raise ex

            auto_detach_observer = AutoDetachObserver(observer)

            # Subscribe needs to set up the trampoline before for subscribing.
            # Actually, the first call to Subscribe creates the trampoline so
            # that it may assign its disposable before any observer executes
            # OnNext over the CurrentThreadScheduler. This enables single-
            # threaded cancellation
            # https://social.msdn.microsoft.com/Forums/en-US/eb82f593-9684-4e27-
            # 97b9-8b8886da5c33/whats-the-rationale-behind-how-currentthreadsche
            # dulerschedulerequired-behaves?forum=rx
            if current_thread_scheduler.schedule_required():
                current_thread_scheduler.schedule(set_disposable)
            else:
                set_disposable()

            return auto_detach_observer
Example #5
0
        def _subscribe(observer):
            """Decorator for subscribe. It wraps the observer in an
            AutoDetachObserver and fixes the returned disposable"""

            def fix_subscriber(subscriber):
                """Fixes subscriber to make sure it returns a Disposable instead
                of None or a dispose function"""

                if not hasattr(subscriber, "dispose"):
                    subscriber = Disposable(subscriber)

                return subscriber

            def set_disposable(scheduler=None, value=None):
                try:
                    auto_detach_observer.disposable = fix_subscriber(subscribe(auto_detach_observer))
                except Exception as ex:
                    if not auto_detach_observer.fail(ex):
                        raise ex

            auto_detach_observer = AutoDetachObserver(observer)

            # Subscribe needs to set up the trampoline before for subscribing.
            # Actually, the first call to Subscribe creates the trampoline so
            # that it may assign its disposable before any observer executes
            # OnNext over the CurrentThreadScheduler. This enables single-
            # threaded cancellation
            # https://social.msdn.microsoft.com/Forums/en-US/eb82f593-9684-4e27-
            # 97b9-8b8886da5c33/whats-the-rationale-behind-how-currentthreadsche
            # dulerschedulerequired-behaves?forum=rx
            if current_thread_scheduler.schedule_required():
                current_thread_scheduler.schedule(set_disposable)
            else:
                set_disposable()

            return auto_detach_observer
Example #6
0
    def subscribe_(self,
                   on_next: typing.OnNext = None,
                   on_error: typing.OnError = None,
                   on_completed: typing.OnCompleted = None,
                   scheduler: typing.Scheduler = None) -> typing.Disposable:
        """Subscribe callbacks to the observable sequence.

        Examples:
            >>> source.subscribe_(on_next)
            >>> source.subscribe_(on_next, on_error)
            >>> source.subscribe_(on_next, on_error, on_completed)

        Args:
            on_next: Action to invoke for each element in the observable
                sequence.
            on_error: Action to invoke upon exceptional termination of
                the observable sequence.
            on_completed: Action to invoke upon graceful termination of
                the observable sequence.
            scheduler: The scheduler to use for this subscription.

        Returns:
            Disposable object representing an observer's subscription
            to the observable sequence.
        """

        auto_detach_observer = AutoDetachObserver(on_next, on_error,
                                                  on_completed)

        def fix_subscriber(subscriber):
            """Fixes subscriber to make sure it returns a Disposable instead
            of None or a dispose function"""
            if not hasattr(subscriber, "dispose"):
                subscriber = Disposable(subscriber)

            return subscriber

        def set_disposable(_: abc.Scheduler = None, __: Any = None):
            try:
                subscriber = self._subscribe_core(auto_detach_observer,
                                                  scheduler)
            except Exception as ex:  # By design. pylint: disable=W0703
                if not auto_detach_observer.fail(ex):
                    raise
            else:
                auto_detach_observer.subscription = fix_subscriber(subscriber)

        # Subscribe needs to set up the trampoline before for subscribing.
        # Actually, the first call to Subscribe creates the trampoline so
        # that it may assign its disposable before any observer executes
        # OnNext over the CurrentThreadScheduler. This enables single-
        # threaded cancellation
        # https://social.msdn.microsoft.com/Forums/en-US/eb82f593-9684-4e27-
        # 97b9-8b8886da5c33/whats-the-rationale-behind-how-currentthreadsche
        # dulerschedulerequired-behaves?forum=rx
        if current_thread_scheduler.schedule_required():
            current_thread_scheduler.schedule(set_disposable)
        else:
            set_disposable()

        # Hide the identity of the auto detach observer
        return Disposable(auto_detach_observer.dispose)
Example #7
0
    def subscribe(self,
                  on_next=None,
                  on_error=None,
                  on_completed=None,
                  observer=None):
        """Subscribe an observer to the observable sequence.

        Examples:
        1 - source.subscribe()
        2 - source.subscribe(observer)
        3 - source.subscribe(on_next)
        4 - source.subscribe(on_next, on_error)
        5 - source.subscribe(on_next, on_error, on_completed)

        Keyword arguments:
        on_next -- [Optional] Action to invoke for each element in the
            observable sequence.
        on_error -- [Optional] Action to invoke upon exceptional
            termination of the observable sequence.
        on_completed -- [Optional] Action to invoke upon graceful
            termination of the observable sequence.
        observer -- [Optional] The object that is to receive
            notifications. You may subscribe using an observer or
            callbacks, not both.

        Return disposable object representing an observer's subscription
            to the observable sequence.
        """
        # Accept observer as first parameter
        if isinstance(on_next, Observer):
            observer = on_next
        elif hasattr(on_next, "on_next") and callable(on_next.on_next):
            observer = on_next
        elif not observer:
            observer = AnonymousObserver(on_next, on_error, on_completed)

        auto_detach_observer = AutoDetachObserver(observer)

        def fix_subscriber(subscriber):
            """Fixes subscriber to make sure it returns a Disposable instead
            of None or a dispose function"""

            if not hasattr(subscriber, "dispose"):
                subscriber = Disposable.create(subscriber)

            return subscriber

        def set_disposable(scheduler=None, value=None):
            try:
                subscriber = self._subscribe_core(auto_detach_observer)
            except Exception as ex:
                if not auto_detach_observer.fail(ex):
                    raise
            else:
                auto_detach_observer.disposable = fix_subscriber(subscriber)

        # Subscribe needs to set up the trampoline before for subscribing.
        # Actually, the first call to Subscribe creates the trampoline so
        # that it may assign its disposable before any observer executes
        # OnNext over the CurrentThreadScheduler. This enables single-
        # threaded cancellation
        # https://social.msdn.microsoft.com/Forums/en-US/eb82f593-9684-4e27-
        # 97b9-8b8886da5c33/whats-the-rationale-behind-how-currentthreadsche
        # dulerschedulerequired-behaves?forum=rx
        if current_thread_scheduler.schedule_required():
            current_thread_scheduler.schedule(set_disposable)
        else:
            set_disposable()

        # Hide the identity of the auto detach observer
        return Disposable.create(auto_detach_observer.dispose)
Example #8
0
def subscribe(source: ObservableBase,
              observer: abc.Observer = None,
              scheduler: abc.Scheduler = None):
    """Subscribe an observer to the observable sequence.

    Examples:
    1 - source.subscribe()
    2 - source.subscribe(observer)

    Keyword arguments:
    observer -- [Optional] The object that is to receive
        notifications. You may subscribe using an observer or
        callbacks, not both.

    Return disposable object representing an observer's subscription
        to the observable sequence.
    """

    observer = observer or AnonymousObserver()
    assert isinstance(observer, abc.Observer) or isinstance(
        observer, types.GeneratorType)

    if isinstance(observer, types.GeneratorType):
        if inspect.getgeneratorstate(observer) == inspect.GEN_CREATED:
            observer.on_next(None)

    auto_detach_observer = AutoDetachObserver(observer)

    def fix_subscriber(subscriber):
        """Fixes subscriber to make sure it returns a Disposable instead
        of None or a dispose function"""
        if not hasattr(subscriber, "dispose"):
            subscriber = Disposable.create(subscriber)

        return subscriber

    def set_disposable(_: abc.Scheduler = None, __: Any = None):
        try:
            subscriber = source._subscribe_core(auto_detach_observer,
                                                scheduler)
        except Exception as ex:  # By design. pylint: disable=W0703
            if not auto_detach_observer.fail(ex):
                raise
        else:
            auto_detach_observer.subscription = fix_subscriber(subscriber)

    # Subscribe needs to set up the trampoline before for subscribing.
    # Actually, the first call to Subscribe creates the trampoline so
    # that it may assign its disposable before any observer executes
    # OnNext over the CurrentThreadScheduler. This enables single-
    # threaded cancellation
    # https://social.msdn.microsoft.com/Forums/en-US/eb82f593-9684-4e27-
    # 97b9-8b8886da5c33/whats-the-rationale-behind-how-currentthreadsche
    # dulerschedulerequired-behaves?forum=rx
    if current_thread_scheduler.schedule_required():
        current_thread_scheduler.schedule(set_disposable)
    else:
        set_disposable()

    # Hide the identity of the auto detach observer
    return Disposable.create(auto_detach_observer.dispose)
Example #9
0
    def subscribe(self, on_next=None, on_error=None, on_completed=None, observer=None):
        """Subscribe an observer to the observable sequence.

        Examples:
        1 - source.subscribe()
        2 - source.subscribe(observer)
        3 - source.subscribe(on_next)
        4 - source.subscribe(on_next, on_error)
        5 - source.subscribe(on_next, on_error, on_completed)

        Keyword arguments:
        on_next -- [Optional] Action to invoke for each element in the
            observable sequence.
        on_error -- [Optional] Action to invoke upon exceptional
            termination of the observable sequence.
        on_completed -- [Optional] Action to invoke upon graceful
            termination of the observable sequence.
        observer -- [Optional] The object that is to receive
            notifications. You may subscribe using an observer or
            callbacks, not both.

        Return disposable object representing an observer's subscription
            to the observable sequence.
        """
        # Accept observer as first parameter
        if isinstance(on_next, Observer):
            observer = on_next
        elif hasattr(on_next, "on_next") and callable(on_next.on_next):
            observer = on_next
        elif not observer:
            observer = AnonymousObserver(on_next, on_error, on_completed)

        auto_detach_observer = AutoDetachObserver(observer)

        def fix_subscriber(subscriber):
            """Fixes subscriber to make sure it returns a Disposable instead
            of None or a dispose function"""

            if not hasattr(subscriber, "dispose"):
                subscriber = Disposable.create(subscriber)

            return subscriber

        def set_disposable(scheduler=None, value=None):
            try:
                subscriber = self._subscribe_core(auto_detach_observer)
            except Exception as ex:
                if not auto_detach_observer.fail(ex):
                    raise
            else:
                auto_detach_observer.disposable = fix_subscriber(subscriber)

        # Subscribe needs to set up the trampoline before for subscribing.
        # Actually, the first call to Subscribe creates the trampoline so
        # that it may assign its disposable before any observer executes
        # OnNext over the CurrentThreadScheduler. This enables single-
        # threaded cancellation
        # https://social.msdn.microsoft.com/Forums/en-US/eb82f593-9684-4e27-
        # 97b9-8b8886da5c33/whats-the-rationale-behind-how-currentthreadsche
        # dulerschedulerequired-behaves?forum=rx
        if current_thread_scheduler.schedule_required():
            current_thread_scheduler.schedule(set_disposable)
        else:
            set_disposable()

        # Hide the identity of the auto detach observer
        return Disposable.create(auto_detach_observer.dispose)
Example #10
0
    def subscribe_(self,
                   on_next: typing.OnNext = None,
                   on_error: typing.OnError = None,
                   on_completed: typing.OnCompleted = None,
                   scheduler: typing.Scheduler = None
                  ) -> typing.Disposable:
        """Subscribe callbacks to the observable sequence.

        Examples:
            >>> source.subscribe_(on_next)
            >>> source.subscribe_(on_next, on_error)
            >>> source.subscribe_(on_next, on_error, on_completed)

        Args:
            on_next: Action to invoke for each element in the observable
                sequence.
            on_error: Action to invoke upon exceptional termination of
                the observable sequence.
            on_completed: Action to invoke upon graceful termination of
                the observable sequence.
            scheduler: The scheduler to use for this subscription.

        Returns:
            Disposable object representing an observer's subscription
            to the observable sequence.
        """

        auto_detach_observer = AutoDetachObserver(on_next, on_error, on_completed)

        def fix_subscriber(subscriber):
            """Fixes subscriber to make sure it returns a Disposable instead
            of None or a dispose function"""
            if not hasattr(subscriber, "dispose"):
                subscriber = Disposable(subscriber)

            return subscriber

        def set_disposable(_: abc.Scheduler = None, __: Any = None):
            try:
                subscriber = self._subscribe_core(auto_detach_observer, scheduler)
            except Exception as ex:  # By design. pylint: disable=W0703
                if not auto_detach_observer.fail(ex):
                    raise
            else:
                auto_detach_observer.subscription = fix_subscriber(subscriber)

        # Subscribe needs to set up the trampoline before for subscribing.
        # Actually, the first call to Subscribe creates the trampoline so
        # that it may assign its disposable before any observer executes
        # OnNext over the CurrentThreadScheduler. This enables single-
        # threaded cancellation
        # https://social.msdn.microsoft.com/Forums/en-US/eb82f593-9684-4e27-
        # 97b9-8b8886da5c33/whats-the-rationale-behind-how-currentthreadsche
        # dulerschedulerequired-behaves?forum=rx
        if current_thread_scheduler.schedule_required():
            current_thread_scheduler.schedule(set_disposable)
        else:
            set_disposable()

        # Hide the identity of the auto detach observer
        return Disposable(auto_detach_observer.dispose)