def test_refcountdisposable_singlereference(): d = BooleanDisposable() r = RefCountDisposable(d) assert not d.is_disposed r.dispose() assert d.is_disposed r.dispose() assert d.is_disposed
def test_refcountdisposable_primarydisposesfirst(): d = BooleanDisposable() r = RefCountDisposable(d) assert not d.is_disposed; d1 = r.disposable d2 = r.disposable assert not d.is_disposed d1.dispose() assert not d.is_disposed r.dispose() assert not d.is_disposed d2.dispose() assert d.is_disposed
def test_refcountdisposable_primarydisposesfirst(): d = BooleanDisposable() r = RefCountDisposable(d) assert not d.is_disposed d1 = r.disposable d2 = r.disposable assert not d.is_disposed d1.dispose() assert not d.is_disposed r.dispose() assert not d.is_disposed d2.dispose() assert d.is_disposed
def test_refcountdisposable_refcounting(): d = BooleanDisposable() r = RefCountDisposable(d) assert not d.is_disposed d1 = r.disposable d2 = r.disposable assert not d.is_disposed d1.dispose() assert not d.is_disposed d2.dispose() assert not d.is_disposed r.dispose() assert d.is_disposed d3 = r.disposable d3.dispose()
def subscribe(observer, scheduler_=None): _scheduler = scheduler or scheduler_ or TimeoutScheduler.singleton() n = [0] s = [None] timer_d = SerialDisposable() window_id = [0] group_disposable = CompositeDisposable(timer_d) ref_count_disposable = RefCountDisposable(group_disposable) def create_timer(_id): m = SingleAssignmentDisposable() timer_d.disposable = m def action(scheduler, state): if _id != window_id[0]: return n[0] = 0 window_id[0] += 1 new_id = window_id[0] s[0].on_completed() s[0] = Subject() observer.on_next(add_ref(s[0], ref_count_disposable)) create_timer(new_id) m.disposable = _scheduler.schedule_relative(timespan, action) s[0] = Subject() observer.on_next(add_ref(s[0], ref_count_disposable)) create_timer(0) def on_next(x): new_window = False new_id = 0 s[0].on_next(x) n[0] += 1 if n[0] == count: new_window = True n[0] = 0 window_id[0] += 1 new_id = window_id[0] s[0].on_completed() s[0] = Subject() observer.on_next(add_ref(s[0], ref_count_disposable)) if new_window: create_timer(new_id) def on_error(e): s[0].on_error(e) observer.on_error(e) def on_completed(): s[0].on_completed() observer.on_completed() group_disposable.add(source.subscribe_(on_next, on_error, on_completed, scheduler_)) return ref_count_disposable
def subscribe(observer, scheduler=None): window_subject = Subject() d = CompositeDisposable() r = RefCountDisposable(d) observer.on_next(add_ref(window_subject, r)) def on_next_window(x): window_subject.on_next(x) def on_error(err): window_subject.on_error(err) observer.on_error(err) def on_completed(): window_subject.on_completed() observer.on_completed() d.add( source.subscribe_(on_next_window, on_error, on_completed, scheduler)) def on_next_observer(w): nonlocal window_subject window_subject.on_completed() window_subject = Subject() observer.on_next(add_ref(window_subject, r)) d.add( boundaries.subscribe_(on_next_observer, on_error, on_completed, scheduler)) return r
def run(self, observer, cancel, setSink): self.groupDisposable = CompositeDisposable() self.refCountDisposable = RefCountDisposable(self.groupDisposable) sink = self.Sink(self, observer, cancel) setSink(sink) self.groupDisposable.add(self.source.subscribeSafe(sink)) return self.refCountDisposable
def run(self): self.queue = deque() self.n = 0 self.m = SingleAssignmentDisposable() self.refCountDisposable = RefCountDisposable(self.m) firstWindow = self.createWindow() self.observer.onNext(firstWindow) self.m.disposable = self.parent.source.subscribeSafe(self) return self.refCountDisposable
def run(self): self.gate = RLock() groupDisposable = CompositeDisposable() self.refCountDisposable = RefCountDisposable(groupDisposable) self.createWindow() groupDisposable.add( self.parent.scheduler.schedulePeriodic(self.parent.timeSpan, self.tick)) groupDisposable.add(self.parent.source.subscribeSafe(self)) return self.refCountDisposable
def run(self): self.gate = RLock() self.s = Subject() self.n = 0 self.windowId = 0 self.timerDisposable = SerialDisposable() groupDisposable = CompositeDisposable(self.timerDisposable) self.refCountDisposable = RefCountDisposable(groupDisposable) # AddRef was originally WindowObservable but this is just an alias for AddRef self.observer.onNext(AddRef(self.s, self.refCountDisposable)) self.createTimer(0) groupDisposable.add(self.parent.source.subscribeSafe(self)) return self.refCountDisposable
def observe(self, observer_info: MultiCastObserverInfo) -> rx.typing.Disposable: disposable = SingleAssignmentDisposable() # dispose source if MultiCast sink gets disposed and all inner Flowable sinks # are disposed ref_count_disposable = RefCountDisposable(disposable) disposable.disposable = self.source.observe( observer_info.copy(observer=CollectFlowablesMultiCastObserver( next_observer=observer_info.observer, ref_count_disposable=ref_count_disposable, maintain_order=self.maintain_order, stack=self.stack, subscriber=self.subscriber, ), )) return ref_count_disposable
def subscribe(observer, scheduler=None): m = SerialDisposable() d = CompositeDisposable(m) r = RefCountDisposable(d) window = Subject() observer.on_next(add_ref(window, r)) def on_next(value): window.on_next(value) def on_error(error): window.on_error(error) observer.on_error(error) def on_completed(): window.on_completed() observer.on_completed() d.add(source.subscribe_(on_next, on_error, on_completed, 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) create_window_on_completed() return r
def run(self): self.totalTime = 0 self.nextShift = self.parent.timeShift self.nextSpan = self.parent.timeSpan self.gate = RLock() self.queue = deque() self.timerDisposable = SerialDisposable() groupDisposable = CompositeDisposable(self.timerDisposable) self.refCountDisposable = RefCountDisposable(groupDisposable) self.createWindow() self.createTimer() groupDisposable.add(self.parent.source.subscribeSafe(self)) return self.refCountDisposable
def run(self): self.gate = RLock() self.group = CompositeDisposable() self.refCount = RefCountDisposable(self.group) leftSubscription = SingleAssignmentDisposable() self.group.add(leftSubscription) self.leftID = 0 self.leftMap = {} rightSubscription = SingleAssignmentDisposable() self.group.add(rightSubscription) self.rightID = 0 self.rightMap = {} leftSubscription.disposable = self.parent.left.subscribeSafe( self.Left(self, leftSubscription)) rightSubscription.disposable = self.parent.right.subscribeSafe( self.Right(self, rightSubscription)) return self.refCount
def subscribe(observer, scheduler=None): m = SingleAssignmentDisposable() refCountDisposable = RefCountDisposable(m) n = [0] q = [] def create_window(): s = Subject() q.append(s) observer.on_next(add_ref(s, refCountDisposable)) create_window() def on_next(x): for item in q: item.on_next(x) c = n[0] - count + 1 if c >= 0 and c % skip == 0: s = q.pop(0) s.on_completed() n[0] += 1 if (n[0] % skip) == 0: create_window() def on_error(exception): while q: q.pop(0).on_error(exception) observer.on_error(exception) def on_completed(): while q: q.pop(0).on_completed() observer.on_completed() m.disposable = source.subscribe_(on_next, on_error, on_completed, scheduler) return refCountDisposable
def subscribe(observer, scheduler=None): group = CompositeDisposable() rcd = RefCountDisposable(group) left_map = OrderedDict() right_map = OrderedDict() left_id = [0] right_id = [0] def on_next_left(value): subject = 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): 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) def on_error_left(error): for left_value in left_map.values(): left_value.on_error(error) observer.on_error(error) group.add( left.subscribe_(on_next_left, on_error_left, observer.on_completed, scheduler)) def send_right(value): 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): 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) with left.lock: for left_value in left_map.values(): left_value.on_next(value) def on_error_right(error): for left_value in left_map.values(): left_value.on_error(error) observer.on_error(error) group.add( right.subscribe_(send_right, on_error_right, scheduler=scheduler)) return rcd
def subscribe(observer, scheduler=None): writers = OrderedDict() group_disposable = CompositeDisposable() ref_count_disposable = RefCountDisposable(group_disposable) def on_next(x): 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(key, writer, ref_count_disposable) duration_group = 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): pass def on_error(exn): 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) 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 on_error(ex): for wrt in writers.values(): wrt.on_error(ex) observer.on_error(ex) def on_completed(): for wrt in writers.values(): wrt.on_completed() observer.on_completed() group_disposable.add(source.subscribe_(on_next, on_error, on_completed, scheduler)) return ref_count_disposable
def subscribe(observer, scheduler_=None): _scheduler = scheduler or scheduler_ or timeout_scheduler timer_d = SerialDisposable() next_shift = [timeshift] next_span = [timespan] total_time = [DELTA_ZERO] q = [] group_disposable = CompositeDisposable(timer_d) ref_count_disposable = RefCountDisposable(group_disposable) def create_timer(): m = SingleAssignmentDisposable() timer_d.disposable = m is_span = False is_shift = False if next_span[0] == next_shift[0]: is_span = True is_shift = True elif next_span[0] < next_shift[0]: is_span = True else: is_shift = True new_total_time = next_span[0] if is_span else next_shift[0] ts = new_total_time - total_time[0] total_time[0] = new_total_time if is_span: next_span[0] += timeshift if is_shift: next_shift[0] += timeshift def action(scheduler, state=None): s = None if is_shift: s = Subject() q.append(s) observer.on_next(add_ref(s, ref_count_disposable)) if is_span: s = q.pop(0) s.on_completed() create_timer() m.disposable = _scheduler.schedule_relative(ts, action) q.append(Subject()) observer.on_next(add_ref(q[0], ref_count_disposable)) create_timer() def on_next(x): for s in q: s.on_next(x) def on_error(e): for s in q: s.on_error(e) observer.on_error(e) def on_completed(): for s in q: s.on_completed() observer.on_completed() group_disposable.add( source.subscribe_(on_next, on_error, on_completed, scheduler_)) return ref_count_disposable