def on_next_right(value: _T2): nonlocal right_id duration = None current_id = right_id right_id += 1 md = SingleAssignmentDisposable() right_map[current_id] = value group.add(md) def expire(): if current_id in right_map: del right_map[current_id] if not len(right_map) and right_done: observer.on_completed() group.remove(md) try: duration = right_duration_mapper(value) except Exception as exception: observer.on_error(exception) return md.disposable = duration.pipe(take(1)).subscribe( noop, observer.on_error, lambda: expire(), scheduler=scheduler) for val in left_map.values(): result = (val, value) observer.on_next(result)
def send_right(value: _TRight) -> None: with left.lock: _id = right_id[0] right_id[0] += 1 right_map[_id] = value md = SingleAssignmentDisposable() group.add(md) def expire(): del right_map[_id] group.remove(md) try: duration = right_duration_mapper(value) except Exception as e: for left_value in left_map.values(): left_value.on_error(e) observer.on_error(e) return def on_error(error: Exception): with left.lock: for left_value in left_map.values(): left_value.on_error(error) observer.on_error(error) md.disposable = duration.pipe(ops.take(1)).subscribe( nothing, on_error, expire, scheduler=scheduler) with left.lock: for left_value in left_map.values(): left_value.on_next(value)
def slice(source: Observable[_T]) -> Observable[_T]: """The partially applied slice operator. Slices the given observable. It is basically a wrapper around the operators :func:`skip <reactivex.operators.skip>`, :func:`skip_last <reactivex.operators.skip_last>`, :func:`take <reactivex.operators.take>`, :func:`take_last <reactivex.operators.take_last>` and :func:`filter <reactivex.operators.filter>`. The following diagram helps you remember how slices works with streams. Positive numbers are relative to the start of the events, while negative numbers are relative to the end (close) of the stream. .. code:: r---e---a---c---t---i---v---e---! 0 1 2 3 4 5 6 7 8 -8 -7 -6 -5 -4 -3 -2 -1 0 Examples: >>> result = source.slice(1, 10) >>> result = source.slice(1, -2) >>> result = source.slice(1, -1, 2) Args: source: Observable to slice Returns: A sliced observable sequence. """ if _stop >= 0: pipeline.append(ops.take(_stop)) if _start > 0: pipeline.append(ops.skip(_start)) elif _start < 0: pipeline.append(ops.take_last(-_start)) if _stop < 0: pipeline.append(ops.skip_last(-_stop)) if _step > 1: pipeline.append(ops.filter_indexed(lambda x, i: i % _step == 0)) elif _step < 0: # Reversing events is not supported raise TypeError("Negative step not supported.") return source.pipe(*pipeline)
def on_next_left(value: _TLeft) -> None: subject: Subject[_TRight] = Subject() with left.lock: _id = left_id[0] left_id[0] += 1 left_map[_id] = subject try: result = (value, add_ref(subject, rcd)) except Exception as e: log.error("*** Exception: %s" % e) for left_value in left_map.values(): left_value.on_error(e) observer.on_error(e) return observer.on_next(result) for right_value in right_map.values(): subject.on_next(right_value) md = SingleAssignmentDisposable() group.add(md) def expire(): if _id in left_map: del left_map[_id] subject.on_completed() group.remove(md) try: duration = left_duration_mapper(value) except Exception as e: for left_value in left_map.values(): left_value.on_error(e) observer.on_error(e) return def on_error(error: Exception) -> Any: for left_value in left_map.values(): left_value.on_error(error) observer.on_error(error) md.disposable = duration.pipe(ops.take(1)).subscribe( nothing, on_error, expire, scheduler=scheduler)
def create_window_on_completed(): try: window_close = closing_mapper() except Exception as exception: observer.on_error(exception) return def on_completed(): nonlocal window window.on_completed() window = Subject() observer.on_next(add_ref(window, r)) create_window_on_completed() m1 = SingleAssignmentDisposable() m.disposable = m1 m1.disposable = window_close.pipe(ops.take(1)).subscribe( noop, on_error, on_completed, scheduler=scheduler)
def projection(x): return reactivex.interval(10).pipe( ops.map_indexed(lambda a, b: x), ops.take(x))
def on_next(x: _T) -> None: writer = None key = None try: key = key_mapper(x) except Exception as e: for wrt in writers.values(): wrt.on_error(e) observer.on_error(e) return fire_new_map_entry = False writer = writers.get(key) if not writer: try: writer = subject_mapper_() except Exception as e: for wrt in writers.values(): wrt.on_error(e) observer.on_error(e) return writers[key] = writer fire_new_map_entry = True if fire_new_map_entry: group: GroupedObservable[_TKey, _TValue] = GroupedObservable( key, writer, ref_count_disposable) duration_group: GroupedObservable[_TKey, Any] = GroupedObservable( key, writer) try: duration = duration_mapper(duration_group) except Exception as e: for wrt in writers.values(): wrt.on_error(e) observer.on_error(e) return observer.on_next(group) sad = SingleAssignmentDisposable() group_disposable.add(sad) def expire(): if writers[key]: del writers[key] writer.on_completed() group_disposable.remove(sad) def on_next(value: Any) -> None: pass def on_error(exn: Exception) -> None: for wrt in writers.values(): wrt.on_error(exn) observer.on_error(exn) def on_completed(): expire() sad.disposable = duration.pipe(ops.take(1)).subscribe( on_next, on_error, on_completed, scheduler=scheduler) try: element = element_mapper_(x) except Exception as error: for wrt in writers.values(): wrt.on_error(error) observer.on_error(error) return writer.on_next(element)
def create(): return xs.pipe(ops.take(17))