def test_when_never_never(self): scheduler = TestScheduler() xs = Observable.never() ys = Observable.never() def create(): def selector(x, y): return x + y return Observable.when(xs.and_(ys).then_do(selector)) results = scheduler.start(create) results.messages.assert_equal()
def create(): def on_next(x): i[0] += 1 def on_completed(): completed = True return Observable.never().do_action(on_next=on_next, on_completed=on_completed)
def test_publish_with_initial_value_multiple_connections(self): xs = Observable.never() ys = xs.publish_value(1979) connection1 = ys.connect() connection2 = ys.connect() assert(connection1 == connection2) connection1.dispose() connection2.dispose() connection3 = ys.connect() assert(connection1 != connection3)
def test_replay_time_multiple_connections(self): xs = Observable.never() ys = xs.replay(None, None, 100) connection1 = ys.connect() connection2 = ys.connect() assert(connection1 == connection2) connection1.dispose() connection2.dispose() connection3 = ys.connect() assert(connection1 != connection3)
def test_publish_with_initial_value_multiple_connections(self): xs = Observable.never() ys = xs.publish_value(1979) connection1 = ys.connect() connection2 = ys.connect() assert (connection1 == connection2) connection1.dispose() connection2.dispose() connection3 = ys.connect() assert (connection1 != connection3)
def timeoutIndividual(self, durationSelector, firstTimeout=None, other=None): assert isinstance(self, Observable) if firstTimeout == None: firstTimeout = Observable.never() if other == None: other = Observable.throw(TimeoutException()) assert isinstance(firstTimeout, Observable) assert isinstance(other, Observable) return TimeoutObservable(self, firstTimeout, durationSelector, other)
def amb(cls, *args): """Propagates the observable sequence that reacts first. E.g. winner = Rx.Observable.amb(xs, ys, zs) Returns an observable sequence that surfaces any of the given sequences, whichever reacted first. """ acc = Observable.never() if isinstance(args[0], list): items = args[0] else: items = list(args) def func(previous, current): return previous.amb(current) for item in items: acc = func(acc, item) return acc
def timeout_with_selector(self, first_timeout=None, timeout_duration_selector=None, other=None): """Returns the source observable sequence, switching to the other observable sequence if a timeout is signaled. 1 - res = source.timeout_with_selector(rx.Observable.timer(500)) 2 - res = source.timeout_with_selector(rx.Observable.timer(500), lambda x: rx.Observable.timer(200)) 3 - res = source.timeout_with_selector(rx.Observable.timer(500), lambda x: rx.Observable.timer(200)), rx.Observable.return_value(42)) first_timeout -- [Optional] Observable sequence that represents the timeout for the first element. If not provided, this defaults to Observable.never(). timeout_Duration_selector -- [Optional] Selector to retrieve an observable sequence that represents the timeout between the current element and the next element. other -- [Optional] Sequence to return in case of a timeout. If not provided, this is set to Observable.throw_exception(). Returns the source sequence switching to the other sequence in case of a timeout. """ first_timeout = first_timeout or Observable.never() other = other or Observable.throw_exception(Exception('Timeout')) source = self def subscribe(observer): subscription = SerialDisposable() timer = SerialDisposable() original = SingleAssignmentDisposable() subscription.disposable = original switched = False _id = [0] def set_timer(timeout): my_id = _id[0] def timer_wins(): return _id[0] == my_id d = SingleAssignmentDisposable() timer.disposable = d def on_next(x): if timer_wins(): subscription.disposable = other.subscribe(observer) d.dispose() def on_error(e): if timer_wins(): observer.on_error(e) def on_completed(): if timer_wins(): subscription.disposable = other.subscribe(observer) d.disposable = timeout.subscribe(on_next, on_error, on_completed) set_timer(first_timeout) def observer_wins(): res = not switched if res: _id[0] += 1 return res def on_next(x): if observer_wins(): observer.on_next(x) timeout = None try: timeout = timeout_duration_selector(x) except Exception as e: observer.on_error(e) return set_timer(timeout) def on_error(e): if observer_wins(): observer.on_error(e) def on_completed(): if observer_wins(): observer.on_completed() original.disposable = source.subscribe(on_next, on_error, on_completed) return CompositeDisposable(subscription, timer) return AnonymousObservable(subscribe)
def create(): return Observable.never().as_observable()