def timestamp(source: Observable[Any]) -> Observable[Timestamp[_T]]: """Records the timestamp for each value in an observable sequence. Examples: >>> timestamp(source) Produces objects with attributes `value` and `timestamp`, where value is the original value. Args: source: Observable source to timestamp. Returns: An observable sequence with timestamp information on values. """ def factory(scheduler_: Optional[abc.SchedulerBase] = None): _scheduler = scheduler or scheduler_ or TimeoutScheduler.singleton( ) def mapper(value: _T) -> Timestamp[_T]: return Timestamp(value=value, timestamp=_scheduler.now) return source.pipe(operators.map(mapper)) return defer(factory)
def create(): def defer(scheduler): invoked[0] += 1 xs[0] = scheduler.create_cold_observable( on_next(100, scheduler.clock), on_error(200, ex)) return xs[0] return reactivex.defer(defer)
def if_then_( condition: Callable[[], bool], then_source: Union[Observable[_T], "Future[_T]"], else_source: Union[None, Observable[_T], "Future[_T]"] = None, ) -> Observable[_T]: """Determines whether an observable collection contains values. Example: 1 - res = reactivex.if_then(condition, obs1) 2 - res = reactivex.if_then(condition, obs1, obs2) Args: condition: The condition which determines if the then_source or else_source will be run. then_source: The observable sequence or Promise that will be run if the condition function returns true. else_source: [Optional] The observable sequence or Promise that will be run if the condition function returns False. If this is not provided, it defaults to reactivex.empty Returns: An observable sequence which is either the then_source or else_source. """ else_source_: Union[Observable[_T], "Future[_T]"] = else_source or reactivex.empty() then_source = ( reactivex.from_future(then_source) if isinstance(then_source, Future) else then_source ) else_source_ = ( reactivex.from_future(else_source_) if isinstance(else_source_, Future) else else_source_ ) def factory(_: abc.SchedulerBase) -> Union[Observable[_T], "Future[_T]"]: return then_source if condition() else else_source_ return reactivex.defer(factory)
def test_ref_count_notconnected(self): disconnected = [False] count = [0] def factory(scheduler): count[0] += 1 def create(obs, scheduler=None): def func(): disconnected[0] = True return func return reactivex.create(create) xs = reactivex.defer(factory) subject = MySubject() conn = ConnectableObservable(xs, subject) refd = conn.pipe(ops.ref_count()) dis1 = refd.subscribe() self.assertEqual(1, count[0]) self.assertEqual(1, subject.subscribe_count) assert not disconnected[0] dis2 = refd.subscribe() self.assertEqual(1, count[0]) self.assertEqual(2, subject.subscribe_count) assert not disconnected[0] dis1.dispose() assert not disconnected[0] dis2.dispose() assert disconnected[0] disconnected[0] = False dis3 = refd.subscribe() self.assertEqual(2, count[0]) self.assertEqual(3, subject.subscribe_count) assert not disconnected[0] dis3.dispose() assert disconnected[0]
def scan(source: Observable[_T]) -> Observable[_TState]: """Partially applied scan operator. Applies an accumulator function over an observable sequence and returns each intermediate result. Examples: >>> scanned = scan(source) Args: source: The observable source to scan. Returns: An observable sequence containing the accumulated values. """ def factory(scheduler: abc.SchedulerBase) -> Observable[_TState]: has_accumulation = False accumulation: _TState = cast(_TState, None) def projection(x: _T) -> _TState: nonlocal has_accumulation nonlocal accumulation if has_accumulation: accumulation = accumulator(accumulation, x) else: accumulation = ( accumulator(cast(_TState, seed), x) if has_seed else cast(_TState, x) ) has_accumulation = True return accumulation return source.pipe(ops.map(projection)) return defer(factory)
def case_( mapper: Callable[[], _Key], sources: Mapping[_Key, Observable[_T]], default_source: Optional[Union[Observable[_T], "Future[_T]"]] = None, ) -> Observable[_T]: default_source_: Union[Observable[_T], "Future[_T]"] = default_source or empty() def factory(_: abc.SchedulerBase) -> Observable[_T]: try: result: Union[Observable[_T], "Future[_T]"] = sources[mapper()] except KeyError: result = default_source_ if isinstance(result, Future): result_: Observable[_T] = from_future(result) else: result_ = result return result_ return defer(factory)
def repeat(source: Observable[_T]) -> Observable[_T]: """Repeats the observable sequence a specified number of times. If the repeat count is not specified, the sequence repeats indefinitely. Examples: >>> repeated = source.repeat() >>> repeated = source.repeat(42) Args: source: The observable source to repeat. Returns: The observable sequence producing the elements of the given sequence repeatedly. """ if repeat_count is None: gen = infinite() else: gen = range(repeat_count) return reactivex.defer( lambda _: reactivex.concat_with_iterable(source for _ in gen))
def create(): def defer(scheduler): invoked[0] += 1 raise Exception(ex) return reactivex.defer(defer)