Exemplo n.º 1
0
            def on_next(inner_source):
                d = SingleAssignmentDisposable()
                latest[0] += 1
                _id = latest[0]
                has_latest[0] = True
                inner_subscription.disposable = d

                # Check if Future or Observable
                inner_source = Observable.from_future(inner_source)

                def on_next(x):
                    if latest[0] == _id:
                        observer.on_next(x)

                def on_error(e):
                    if latest[0] == _id:
                        observer.on_error(e)

                def on_completed():
                    if latest[0] == _id:
                        has_latest[0] = False
                        if is_stopped[0]:
                            observer.on_completed()

                d.disposable = inner_source.subscribe(on_next, on_error,
                                                      on_completed)
Exemplo n.º 2
0
def take_until(self, other):
    """Returns the values from the source observable sequence until the
    other observable sequence produces a value.

    Keyword arguments:
    other -- Observable sequence that terminates propagation of elements of
        the source sequence.

    Returns an observable sequence containing the elements of the source
    sequence up to the point the other sequence interrupted further
    propagation.
    """

    source = self
    other = Observable.from_future(other)

    def subscribe(observer):
        def on_completed(x):
            observer.on_completed()

        return CompositeDisposable(
            source.subscribe(observer),
            other.subscribe(on_completed, observer.on_error, noop))

    return AnonymousObservable(subscribe)
Exemplo n.º 3
0
    def take_until(self, other):
        """Returns the values from the source observable sequence until the
        other observable sequence produces a value.

        Keyword arguments:
        other -- Observable sequence that terminates propagation of elements of
            the source sequence.

        Returns an observable sequence containing the elements of the source
        sequence up to the point the other sequence interrupted further
        propagation.
        """

        source = self
        other = Observable.from_future(other)

        def subscribe(observer):

            def on_completed(x):
                observer.on_completed()

            return CompositeDisposable(
                source.subscribe(observer),
                other.subscribe(on_completed, observer.on_error, noop)
            )
        return AnonymousObservable(subscribe)
Exemplo n.º 4
0
        def on_next(inner_source):
            d = SingleAssignmentDisposable()
            with self.lock:
                latest[0] += 1
                _id = latest[0]
            has_latest[0] = True
            inner_subscription.disposable = d

            # Check if Future or Observable
            inner_source = Observable.from_future(inner_source)

            def on_next(x):
                if latest[0] == _id:
                    observer.on_next(x)

            def on_error(e):
                if latest[0] == _id:
                    observer.on_error(e)

            def on_completed():
                if latest[0] == _id:
                    has_latest[0] = False
                    if is_stopped[0]:
                        observer.on_completed()

            d.disposable = inner_source.subscribe(on_next, on_error, on_completed)
Exemplo n.º 5
0
    def subscribe(observer):
        try:
            result = observable_factory()
        except Exception as ex:
            return Observable.throw_exception(ex).subscribe(observer)

        result = Observable.from_future(result)
        return result.subscribe(observer)
Exemplo n.º 6
0
    def subscribe(observer):
        try:
            result = observable_factory()
        except Exception as ex:
            return Observable.throw_exception(ex).subscribe(observer)

        result = Observable.from_future(result)
        return result.subscribe(observer)
Exemplo n.º 7
0
 def action(this, state=None):
     if pos[0] < len(sources):
         current = Observable.from_future(sources[pos[0]])
         pos[0] += 1
         d = SingleAssignmentDisposable()
         subscription.disposable = d
         d.disposable = current.subscribe(observer.on_next, lambda ex: this(), lambda: this())
     else:
         observer.on_completed()
Exemplo n.º 8
0
        def on_error(exception):
            try:
                result = handler(exception)
            except Exception as ex:
                observer.on_error(ex)
                return

            result = Observable.from_future(result)
            d = SingleAssignmentDisposable()
            subscription.disposable = d
            d.disposable = result.subscribe(observer)
Exemplo n.º 9
0
            def func(i):
                source = sources[i]
                sad = SingleAssignmentDisposable()
                source = Observable.from_future(source)

                def on_next(x):
                    queues[i].append(x)
                    next(i)

                sad.disposable = source.subscribe(on_next, observer.on_error, lambda: done(i))
                subscriptions[i] = sad
Exemplo n.º 10
0
    def while_do(cls, condition, source):
        """Repeats source as long as condition holds emulating a while loop.
        
        Keyword arguments:
        condition -- {Function} The condition which determines if the source 
            will be repeated.
        source -- {Observable} The observable sequence that will be run if the 
            condition function returns true.
   
        Returns an observable {Observable} sequence which is repeated as long 
        as the condition holds."""

        source = Observable.from_future(source)
        return Observable.concat(Enumerable.while_do(condition, source))
Exemplo n.º 11
0
        def action(this, state=None):
            try:
                source = next(sources)
            except StopIteration:
                observer.on_completed()
                return

            # Allow source to be a factory method taking an error
            source = source(state) if callable(source) else source
            current = Observable.from_future(source)

            d = SingleAssignmentDisposable()
            subscription.disposable = d
            d.disposable = current.subscribe(observer.on_next, lambda ex: this(ex), this)
Exemplo n.º 12
0
  def while_do(cls, condition, source):
      """Repeats source as long as condition holds emulating a while loop.
      
      Keyword arguments:
      condition -- {Function} The condition which determines if the source 
          will be repeated.
      source -- {Observable} The observable sequence that will be run if the 
          condition function returns true.
 
      Returns an observable {Observable} sequence which is repeated as long 
      as the condition holds."""
      
      source = Observable.from_future(source)
      return Observable.concat(Enumerable.while_do(condition, source))    
Exemplo n.º 13
0
            def action(this, state=None):
                try:
                    source = next(sources)
                except StopIteration:
                    observer.on_completed()
                    return

                # Allow source to be a factory method taking an error
                source = source(state) if callable(source) else source
                current = Observable.from_future(source)

                d = SingleAssignmentDisposable()
                subscription.disposable = d
                d.disposable = current.subscribe(observer.on_next,
                                                 lambda ex: this(ex), this)
Exemplo n.º 14
0
        def on_next(inner_source):
            inner_subscription = SingleAssignmentDisposable()
            group.add(inner_subscription)

            inner_source = Observable.from_future(inner_source)

            def on_complete():
                group.remove(inner_subscription)
                if is_stopped[0] and len(group) == 1:
                    observer.on_completed()

            disposable = inner_source.subscribe(observer.on_next,
                                                observer.on_error, on_complete)

            inner_subscription.disposable = disposable
Exemplo n.º 15
0
            def on_next(inner_source):
                inner_subscription = SingleAssignmentDisposable()
                group.add(inner_subscription)

                inner_source = Observable.from_future(inner_source)

                def on_next(x):
                    observer.on_next(x)

                def on_completed():
                    group.remove(inner_subscription)
                    if is_stopped and group.length == 1:
                        observer.on_completed()

                inner_subscription.disposable = inner_source.subscribe(on_next, observer.on_error, on_completed)
Exemplo n.º 16
0
def while_do(cls, condition, source):
    """Repeats source as long as condition holds emulating a while loop.

    Keyword arguments:
    :param types.FunctionType condition: The condition which determines if the
        source will be repeated.
    :param Observable source: The observable sequence that will be run if the
        condition function returns true.

    :returns: An observable sequence which is repeated as long as the condition
        holds.
    :rtype: Observable
    """

    source = Observable.from_future(source)
    return Observable.concat(Enumerable.while_do(condition, source))
Exemplo n.º 17
0
    def start_async(cls, function_async):
        """Invokes the asynchronous function, surfacing the result through an
        observable sequence.

        Keyword arguments:
        function_async -- {Function} Asynchronous function which returns a
            Future to run.

        Returns {Observable} An observable sequence exposing the function's
        result value, or an exception."""

        try:
            future = function_async()
        except Exception as ex:
            return Observable.throw(ex)

        return Observable.from_future(future)
Exemplo n.º 18
0
    def start_async(cls, function_async):
        """Invokes the asynchronous function, surfacing the result through an
        observable sequence.

        Keyword arguments:
        function_async -- {Function} Asynchronous function which returns a
            Future to run.

        Returns {Observable} An observable sequence exposing the function's
        result value, or an exception."""

        try:
            future = function_async()
        except Exception as ex:
            return Observable.throw(ex)

        return Observable.from_future(future)
Exemplo n.º 19
0
        def on_next(inner_source):
            if not has_current[0]:
                has_current[0] = True

                inner_source = Observable.from_future(inner_source)

                inner_subscription = SingleAssignmentDisposable()
                g.add(inner_subscription)

                def on_completed_inner():
                    g.remove(inner_subscription)
                    has_current[0] = False
                    if is_stopped[0] and len(g) == 1:
                        observer.on_completed()

                inner_subscription.disposable = inner_source.subscribe(
                    observer.on_next, observer.on_error, on_completed_inner)
Exemplo n.º 20
0
def skip_until(self, other):
    """Returns the values from the source observable sequence only after
    the other observable sequence produces a value.

    other -- The observable sequence that triggers propagation of elements
        of the source sequence.

    Returns an observable sequence containing the elements of the source
    sequence starting from the point the other sequence triggered
    propagation.
    """

    source = self
    other = Observable.from_future(other)

    def subscribe(observer):
        is_open = [False]

        def on_next(left):
            if is_open[0]:
                observer.on_next(left)

        def on_completed():
            if is_open[0]:
                observer.on_completed()

        subs = source.subscribe(on_next, observer.on_error, on_completed)
        disposables = CompositeDisposable(subs)

        right_subscription = SingleAssignmentDisposable()
        disposables.add(right_subscription)

        def on_next2(x):
            is_open[0] = True
            right_subscription.dispose()

        def on_completed2():
            right_subscription.dispose()

        right_subscription.disposable = other.subscribe(
            on_next2, observer.on_error, on_completed2)

        return disposables

    return AnonymousObservable(subscribe)
Exemplo n.º 21
0
def skip_until(self, other):
    """Returns the values from the source observable sequence only after
    the other observable sequence produces a value.

    other -- The observable sequence that triggers propagation of elements
        of the source sequence.

    Returns an observable sequence containing the elements of the source
    sequence starting from the point the other sequence triggered
    propagation.
    """

    source = self
    other = Observable.from_future(other)

    def subscribe(observer):
        is_open = [False]

        def on_next(left):
            if is_open[0]:
                observer.on_next(left)

        def on_completed():
            if is_open[0]:
                observer.on_completed()

        subs = source.subscribe(on_next, observer.on_error, on_completed)
        disposables = CompositeDisposable(subs)

        right_subscription = SingleAssignmentDisposable()
        disposables.add(right_subscription)

        def on_next2(x):
            is_open[0] = True
            right_subscription.dispose()

        def on_completed2():
            right_subscription.dispose()

        right_subscription.disposable = other.subscribe(on_next2, observer.on_error, on_completed2)

        return disposables
    return AnonymousObservable(subscribe)
Exemplo n.º 22
0
        def on_next(inner_source):
            if not has_current[0]:
                has_current[0] = True

                inner_source = Observable.from_future(inner_source)

                inner_subscription = SingleAssignmentDisposable()
                g.add(inner_subscription)

                def on_completed_inner():
                    g.remove(inner_subscription)
                    has_current[0] = False
                    if is_stopped[0] and len(g) == 1:
                        observer.on_completed()

                inner_subscription.disposable = inner_source.subscribe(
                    observer.on_next,
                    observer.on_error,
                    on_completed_inner
                )
Exemplo n.º 23
0
def timeout(self, duetime, other=None, scheduler=None):
    """Returns the source observable sequence or the other observable
    sequence if duetime elapses.

    1 - res = source.timeout(new Date()); # As a date
    2 - res = source.timeout(5000); # 5 seconds
    # As a date and timeout observable
    3 - res = source.timeout(datetime(), rx.Observable.return_value(42))
    # 5 seconds and timeout observable
    4 - res = source.timeout(5000, rx.Observable.return_value(42))
    # As a date and timeout observable
    5 - res = source.timeout(datetime(), rx.Observable.return_value(42),
                             rx.Scheduler.timeout)
    # 5 seconds and timeout observable
    6 - res = source.timeout(5000, rx.Observable.return_value(42),
                             rx.Scheduler.timeout)

    Keyword arguments:
    :param datetime|int duetime: Absolute (specified as a datetime object) or
        relative time (specified as an integer denoting milliseconds) when a
        timeout occurs.
    :param Observable other: Sequence to return in case of a timeout. If not
        specified, a timeout error throwing sequence will be used.
    :param Scheduler scheduler: Scheduler to run the timeout timers on. If not
        specified, the timeout scheduler is used.

    :returns: The source sequence switching to the other sequence in case of
        a timeout.
    :rtype: Observable
    """

    scheduler_method = None
    source = self

    other = other or Observable.throw_exception(Exception("Timeout"))
    other = Observable.from_future(other)

    scheduler = scheduler or timeout_scheduler

    if isinstance(duetime, datetime):
        scheduler_method = scheduler.schedule_absolute
    else:
        scheduler_method = scheduler.schedule_relative

    def subscribe(observer):
        switched = [False]
        _id = [0]

        original = SingleAssignmentDisposable()
        subscription = SerialDisposable()
        timer = SerialDisposable()
        subscription.disposable = original

        def create_timer():
            my_id = _id[0]

            def action(scheduler, state=None):
                switched[0] = (_id[0] == my_id)
                timer_wins = switched[0]
                if timer_wins:

                    subscription.disposable = other.subscribe(observer)

            timer.disposable = scheduler_method(duetime, action)

        create_timer()

        def on_next(x):
            on_next_wins = not switched[0]
            if on_next_wins:
                _id[0] += 1
                observer.on_next(x)
                create_timer()

        def on_error(e):
            on_error_wins = not switched[0]
            if on_error_wins:
                _id[0] += 1
                observer.on_error(e)

        def on_completed():
            on_completed_wins = not switched[0]
            if on_completed_wins:
                _id[0] += 1
                observer.on_completed()

        original.disposable = source.subscribe(on_next, on_error, on_completed)
        return CompositeDisposable(subscription, timer)

    return AnonymousObservable(subscribe)
Exemplo n.º 24
0
def timeout(self, duetime, other=None, scheduler=None):
    """Returns the source observable sequence or the other observable
    sequence if duetime elapses.

    1 - res = source.timeout(new Date()); # As a date
    2 - res = source.timeout(5000); # 5 seconds
    # As a date and timeout observable
    3 - res = source.timeout(datetime(), rx.Observable.return_value(42))
    # 5 seconds and timeout observable
    4 - res = source.timeout(5000, rx.Observable.return_value(42))
    # As a date and timeout observable
    5 - res = source.timeout(datetime(), rx.Observable.return_value(42),
                             rx.Scheduler.timeout)
    # 5 seconds and timeout observable
    6 - res = source.timeout(5000, rx.Observable.return_value(42),
                             rx.Scheduler.timeout)

    Keyword arguments:
    :param datetime|int duetime: Absolute (specified as a datetime object) or
        relative time (specified as an integer denoting milliseconds) when a
        timeout occurs.
    :param Observable other: Sequence to return in case of a timeout. If not
        specified, a timeout error throwing sequence will be used.
    :param Scheduler scheduler: Scheduler to run the timeout timers on. If not
        specified, the timeout scheduler is used.

    :returns: The source sequence switching to the other sequence in case of
        a timeout.
    :rtype: Observable
    """

    scheduler_method = None
    source = self

    other = other or Observable.throw_exception(Exception("Timeout"))
    other = Observable.from_future(other)

    scheduler = scheduler or timeout_scheduler

    if isinstance(duetime, datetime):
        scheduler_method = scheduler.schedule_absolute
    else:
        scheduler_method = scheduler.schedule_relative

    def subscribe(observer):
        switched = [False]
        _id = [0]

        original = SingleAssignmentDisposable()
        subscription = SerialDisposable()
        timer = SerialDisposable()
        subscription.disposable = original

        def create_timer():
            my_id = _id[0]

            def action(scheduler, state=None):
                switched[0] = (_id[0] == my_id)
                timer_wins = switched[0]
                if timer_wins:

                    subscription.disposable = other.subscribe(observer)

            timer.disposable = scheduler_method(duetime, action)

        create_timer()
        def on_next(x):
            on_next_wins = not switched[0]
            if on_next_wins:
                _id[0] += 1
                observer.on_next(x)
                create_timer()

        def on_error(e):
            on_error_wins = not switched[0]
            if on_error_wins:
                _id[0] += 1
                observer.on_error(e)

        def on_completed():
            on_completed_wins = not switched[0]
            if on_completed_wins:
                _id[0] += 1
                observer.on_completed()

        original.disposable = source.subscribe(on_next, on_error, on_completed)
        return CompositeDisposable(subscription, timer)
    return AnonymousObservable(subscribe)