def test_groupdisposable_clear(): disp1 = False disp2 = False def action1(): nonlocal disp1 disp1 = True d1 = Disposable(action1) def action2(): nonlocal disp2 disp2 = True d2 = Disposable(action2) g = CompositeDisposable(d1, d2) assert g.length == 2 g.clear() assert disp1 assert disp2 assert not g.length disp3 = False def action3(): nonlocal disp3 disp3 = True d3 = Disposable(action3) g.add(d3) assert not disp3 assert g.length == 1
def test_groupdisposable_clear(): disp1 = [False] disp2 = [False] def action1(): disp1[0] = True d1 = Disposable(action1) def action2(): disp2[0] = True d2 = Disposable(action2) g = CompositeDisposable(d1, d2) assert g.length == 2 g.clear() assert disp1[0] assert disp2[0] assert not g.length disp3 = [False] def action3(): disp3[0] = True d3 = Disposable(action3) g.add(d3) assert not disp3[0] assert g.length == 1
def test_mutabledisposable_replaceafterdispose(): disp1 = False disp2 = False m = SerialDisposable() m.dispose() def action1(): nonlocal disp1 disp1 = True d1 = Disposable(action1) m.disposable = d1 assert m.disposable == None assert disp1 def action2(): nonlocal disp2 disp2 = True d2 = Disposable(action2) m.disposable = d2 m.disposable == None assert disp2
def test_mutabledisposable_replacebeforedispose(): disp1 = False disp2 = False m = SerialDisposable() def action1(): nonlocal disp1 disp1 = True d1 = Disposable(action1) m.disposable = d1 assert d1 == m.disposable assert not disp1 def action2(): nonlocal disp2 disp2 = True d2 = Disposable(action2) m.disposable = d2 assert d2 == m.disposable assert disp1 assert not disp2
def _process_request(self, number_of_items): if self.enable_queue: #console.log('queue length', self.queue.length) while len(self.queue) >= number_of_items and number_of_items > 0: # console.log('number of items', number_of_items) self.subject.on_next(self.queue.shift()) number_of_items -= 1 if len(self.queue): return { "number_of_items": number_of_items, "return_value": True } else: return { "number_of_items": number_of_items, "return_value": False } if self.has_failed: self.subject.on_error(self.error) self.controlled_disposable.dispose() self.controlled_disposable = Disposable.empty() elif self.has_completed: self.subject.on_completed() self.controlled_disposable.dispose() self.controlled_disposable = Disposable.empty() return {"number_of_items": number_of_items, "return_value": False}
def test_groupdisposable_contains(): d1 = Disposable.empty() d2 = Disposable.empty() g = CompositeDisposable(d1, d2) assert g.length == 2 assert g.contains(d1) assert g.contains(d2)
def __init__(self, conn): super(HttpParser, self).__init__() self.conn = conn self.buf = StringIO() self.requests_in = Subject() self.responses_out = HttpWriter(conn) self.keep_alive_timeout_dispose = Disposable.empty() self.read_timeout_dispose = Disposable.empty() self.keep_alive_timer_on()
def test_anonymousdisposable_dispose(): disposed = [False] def action(): disposed[0] = True d = Disposable(action) assert not disposed[0] d.dispose() assert disposed[0]
def test_anonymousdisposable_dispose(): disposed = False def action(): nonlocal disposed disposed = True d = Disposable(action) assert not disposed d.dispose() assert disposed
def __init__(self, enable_queue=True): super(ControlledSubject, self).__init__(self._subscribe) self.subject = Subject() self.enable_queue = enable_queue self.queue = [] if enable_queue else None self.requested_count = 0 self.requested_disposable = Disposable.empty() self.error = None self.has_failed = False self.has_completed = False self.controlled_disposable = Disposable.empty()
def _subscribe(self, observer): with self.lock: self.check_disposed() if not self.is_stopped: self.observers.append(observer) return InnerSubscription(self, observer) if self.exception: observer.on_error(self.exception) return Disposable.empty() observer.on_completed() return Disposable.empty()
def __subscribe(self, observer): self.check_disposed() if not self.is_stopped: self.observers.append(observer) return InnerSubscription(self, observer) if self.exception: observer.on_error(self.exception) return Disposable.empty() observer.on_completed() return Disposable.empty()
def schedule_relative(self, duetime, action, state=None): """Schedules an action to be executed after duetime. Keyword arguments: duetime -- {timedelta} Relative time after which to execute the action. action -- {Function} Action to be executed. Returns {Disposable} The disposable object used to cancel the scheduled action (best effort).""" scheduler = self msecs = self.to_relative(duetime) if msecs == 0: return scheduler.schedule(action, state) disposable = SingleAssignmentDisposable() def interval(): disposable.disposable = action(scheduler, state) log.debug("timeout: %s", msecs) alarm = self.master.after(msecs, interval) def dispose(): # nonlocal alarm self.master.after_cancel(alarm) return CompositeDisposable(disposable, Disposable(dispose))
def schedule_periodic(self, period, action, state=None): """Schedules a periodic piece of work by dynamically discovering the scheduler's capabilities. Keyword parameters: period -- Period for running the work periodically. action -- Action to be executed. state -- [Optional] Initial state passed to the action upon the first iteration. Returns the disposable object used to cancel the scheduled recurring action (best effort). """ period /= 1000.0 timer = None s = state def interval(): nonlocal timer, s s = action(s) timer = Timer(period, interval) timer.start() timer = Timer(period, interval) timer.start() def dispose(): timer.cancel() return Disposable(dispose)
def schedule_periodic(self, period, action, state=None): """Schedules a periodic piece of work by dynamically discovering the schedulers capabilities. Keyword arguments: period -- Period for running the work periodically. action -- Action to be executed. state -- [Optional] Initial state passed to the action upon the first iteration. Returns the disposable object used to cancel the scheduled recurring action (best effort).""" scheduler = self seconds = self.to_relative(period)/1000.0 if not seconds: return scheduler.schedule(action, state) def interval(): new_state = action(scheduler, state) scheduler.schedule_periodic(period, action, new_state) log.debug("timeout: %s", seconds) timer = [eventlet.spawn_after(seconds, interval)] def dispose(): timer[0].kill() return Disposable.create(dispose)
def schedule_periodic(self, period, action, state=None): """Schedules an action to be executed periodically. Keyword arguments: period -- Period for running the work periodically. action -- {Function} Action to be executed. state -- [Optional] Initial state passed to the action upon the first iteration. Returns {Disposable} The disposable object used to cancel the scheduled action (best effort).""" scheduler = self seconds = self.to_relative(period) / 1000.0 if seconds == 0: return scheduler.schedule(action, state) disposable = SingleAssignmentDisposable() def interval(): disposable.disposable = action(state) scheduler.schedule_periodic(period, action, state) handle = [self.loop.call_later(seconds, interval)] def dispose(): # nonlocal handle handle[0].cancel() return CompositeDisposable(disposable, Disposable(dispose))
def subscribe(observer): enum = iter(sources) is_disposed = [False] subscription = SerialDisposable() def action(action1, state=None): if is_disposed[0]: return try: current = next(enum) except StopIteration: observer.on_completed() except Exception as ex: observer.on_error(ex) else: d = SingleAssignmentDisposable() subscription.disposable = d d.disposable = current.subscribe(observer.on_next, observer.on_error, lambda: action1()) cancelable = immediate_scheduler.schedule_recursive(action) def dispose(): is_disposed[0] = True return CompositeDisposable(subscription, cancelable, Disposable(dispose))
def schedule_periodic(self, period, action, state=None): """Schedules a periodic piece of work by dynamically discovering the schedulers capabilities. Keyword arguments: period -- Period for running the work periodically. action -- Action to be executed. state -- [Optional] Initial state passed to the action upon the first iteration. Returns the disposable object used to cancel the scheduled recurring action (best effort).""" scheduler = self seconds = self.to_relative(period) / 1000.0 if not seconds: return scheduler.schedule(action, state) disposable = SingleAssignmentDisposable() def interval(): disposable.disposable = action(scheduler, state) scheduler.schedule_periodic(period, action, state) log.debug("timeout: %s", seconds) timer = [eventlet.spawn_after(seconds, interval)] def dispose(): timer[0].kill() return CompositeDisposable(disposable, Disposable(dispose))
def wrapped_action(self, state): try: return action(parent._get_recursive_wrapper(self), state) except Exception as ex: if not parent._handler(ex): raise Exception(ex) return Disposable.empty()
def request(self, number): check_disposed(self) self.dispose_current_request() r = self._process_request(number) number = r["number_of_items"] if not r["return_value"]: self.requested_count = number def action(): self.requested_count = 0 self.requested_disposable = Disposable(action) return self.requested_disposable else: return Disposable.empty()
def _wxtimer_schedule(self, time, action, state, periodic=False): scheduler = self msecs = self.to_relative(time) disposable = SingleAssignmentDisposable() periodic_state = [state] def interval(): if periodic: periodic_state[0] = action(periodic_state[0]) else: disposable.disposable = action(scheduler, state) log.debug("timeout: %s", msecs) if msecs == 0: msecs = 1 # wx.Timer doesn't support zero. timer = self._timer_class(interval) timer.Start( msecs, self.wx.TIMER_CONTINUOUS if periodic else self.wx.TIMER_ONE_SHOT) self._timers.add(timer) def dispose(): timer.Stop() self._timers.remove(timer) return CompositeDisposable(disposable, Disposable(dispose))
def schedule_relative(self, duetime, action, state=None): """Schedules an action to be executed after duetime. Keyword arguments: duetime -- {timedelta} Relative time after which to execute the action. action -- {Function} Action to be executed. Returns {Disposable} The disposable object used to cancel the scheduled action (best effort).""" scheduler = self seconds = self.to_relative(duetime) / 1000.0 disposable = SingleAssignmentDisposable() def interval(): disposable.disposable = action(scheduler, state) log.debug("timeout: %s", seconds) handle = [self.reactor.callLater(seconds, interval)] def dispose(): handle[0].cancel() return CompositeDisposable(disposable, Disposable(dispose))
def _subscribe(self, observer): clock = self.scheduler.to_relative(self.scheduler.now()) self.subscriptions.append(Subscription(clock)) index = len(self.subscriptions) - 1 disposable = CompositeDisposable() def get_action(notification): def action(scheduler, state): notification.accept(observer) return Disposable.empty() return action for message in self.messages: notification = message.value # Don't make closures within a loop action = get_action(notification) disposable.add( self.scheduler.schedule_relative(message.time, action)) def dispose(): start = self.subscriptions[index].subscribe end = self.scheduler.to_relative(self.scheduler.now()) self.subscriptions[index] = Subscription(start, end) disposable.dispose() return Disposable(dispose)
def schedule_periodic(self, period, action, state=None): """Schedules an action to be executed periodically. Keyword arguments: period -- Period for running the work periodically. action -- {Function} Action to be executed. state -- [Optional] Initial state passed to the action upon the first iteration. Returns {Disposable} The disposable object used to cancel the scheduled action (best effort).""" scheduler = self seconds = self.to_relative(period)/1000.0 if seconds == 0: return scheduler.schedule(action, state) def interval(): new_state = action(state) scheduler.schedule_periodic(period, action, new_state) handle = [self.loop.call_later(seconds, interval)] def dispose(): # nonlocal handle handle[0].cancel() return Disposable.create(dispose)
def schedule_periodic(self, period, action, state=None): """Schedules a periodic piece of work to be executed in the tkinter mainloop. Keyword arguments: period -- Period in milliseconds for running the work periodically. action -- Action to be executed. state -- [Optional] Initial state passed to the action upon the first iteration. Returns the disposable object used to cancel the scheduled recurring action (best effort).""" state = [state] def interval(): state[0] = action(state[0]) alarm[0] = self.master.after(period, interval) log.debug("timeout: %s", period) alarm = [self.master.after(period, interval)] def dispose(): # nonlocal alarm self.master.after_cancel(alarm[0]) return Disposable(dispose)
def _qtimer_schedule(self, time, action, state, periodic=False): scheduler = self msecs = self.to_relative(time) disposable = SingleAssignmentDisposable() periodic_state = [state] def interval(): if periodic: periodic_state[0] = action(periodic_state[0]) else: disposable.disposable = action(scheduler, state) log.debug("timeout: %s", msecs) timer = self.qtcore.QTimer() timer.setSingleShot(not periodic) timer.timeout.connect(interval) timer.setInterval(msecs) timer.start() self._timers.add(timer) def dispose(): timer.stop() self._timers.remove(timer) return CompositeDisposable(disposable, Disposable(dispose))
def schedule_periodic(self, period, action, state=None): """Schedules a periodic piece of work by dynamically discovering the schedulers capabilities. Keyword arguments: period -- Period for running the work periodically. action -- Action to be executed. state -- [Optional] Initial state passed to the action upon the first iteration. Returns the disposable object used to cancel the scheduled recurring action (best effort).""" period /= 1000.0 timer = [None] s = [state] def interval(): new_state = action(s[0]) if new_state is not None: # Update state if other than None s[0] = new_state timer[0] = Timer(period, interval) timer[0].start() timer[0] = Timer(period, interval) timer[0].start() def dispose(): timer[0].cancel() return Disposable(dispose)
def action(scheduler, state=None): if is_added: group.remove(d) else: is_done[0] = True recursive_action(state) return Disposable.empty()
def schedule_work(_, state3): action(state3, inner_action) if is_added: group.remove(d) else: is_done[0] = True return Disposable.empty()
def action2(scheduler1, state3): if is_added: group.remove(d) else: is_done[0] = True recursive_action(state3) return Disposable.empty()
def fix_subscriber(subscriber): """Fixes subscriber to make sure it returns a Disposable instead of None or a dispose function""" if not hasattr(subscriber, "dispose"): subscriber = Disposable(subscriber) return subscriber
def action2(scheduler1, state3): nonlocal is_done if is_added: group.remove(d) else: is_done = True recursive_action(state3) return Disposable.empty()
def fix_subscriber(subscriber): """Fix subscriber to check for None or function returned to decorate as Disposable""" if subscriber is None: subscriber = Disposable.empty() elif type(subscriber) == types.FunctionType: subscriber = Disposable(subscriber) return subscriber
def action(scheduler, state=None): nonlocal is_done #print "action", scheduler1, state3 if is_added: group.remove(d) else: is_done = True recursive_action(state) return Disposable.empty()
def connect(self): if not self.has_subscription: self.has_subscription = True def dispose(): self.has_subscription = False disposable = self.source.subscribe(self.subject) self.subscription = CompositeDisposable(disposable, Disposable.create(dispose)) return self.subscription
def test_groupdisposable_remove(): disp1 = False disp2 = False def action1(): nonlocal disp1 disp1 = True d1 = Disposable(action1) def action2(): nonlocal disp2 disp2 = True d2 = Disposable(action2) g = CompositeDisposable(d1, d2) assert g.length == 2 assert g.contains(d1) assert g.contains(d2) assert g.remove(d1) assert g.length == 1 assert not g.contains(d1) assert g.contains(d2) assert disp1 assert g.remove(d2) assert not g.contains(d1) assert not g.contains(d2) assert disp2 disp3 = False def action3(): nonlocal disp3 disp3 = True d3 = Disposable(action3) assert not g.remove(d3) assert not disp3
def subscribe(observer): disposable = Disposable.empty() try: resource = resource_factory() if resource: disposable = resource source = observable_factory(resource) except Exception as exception: d = Observable.throw_exception(exception).subscribe(observer) return CompositeDisposable(d, disposable) return CompositeDisposable(source.subscribe(observer), disposable)
def subscribe(self, observer): conn = self.source.publish() subscription = conn.subscribe(observer) connection = [Disposable.empty()] def on_next(b): if b: connection[0] = conn.connect() else: connection[0].dispose() connection[0] = Disposable.empty() pausable = self.subject.distinct_until_changed().subscribe(on_next) return CompositeDisposable(subscription, connection[0], pausable)
def __subscribe(self, observer): self.check_disposed() if not self.is_stopped: self.observers.append(observer) observer.on_next(self.value) return InnerSubscription(self, observer) ex = self.exception if ex: observer.on_error(ex) else: observer.on_completed() return Disposable.empty()
def subscribe(observer): count[0] += 1 should_connect = count[0] == 1 subscription = source.subscribe(observer) if should_connect: connectable_subscription[0] = source.connect() def dispose(): subscription.dispose() count[0] -= 1 if not count[0]: connectable_subscription[0].dispose() return Disposable.create(dispose)
def _process_request(self, number_of_items): if self.enable_queue: #console.log('queue length', self.queue.length) while len(self.queue) >= number_of_items and number_of_items > 0: # console.log('number of items', number_of_items) self.subject.on_next(self.queue.shift()) number_of_items -= 1 if len(self.queue): return { "number_of_items": number_of_items, "return_value": True } else: return { "number_of_items": number_of_items, "return_value": False } if self.has_failed: self.subject.on_error(self.error) self.controlled_disposable.dispose() self.controlled_disposable = Disposable.empty() elif self.has_completed: self.subject.on_completed() self.controlled_disposable.dispose() self.controlled_disposable = Disposable.empty() return { "number_of_items": number_of_items, "return_value": False }
def _subscribe(self, observer): with self.lock: self.check_disposed() if not self.is_stopped: self.observers.append(observer) return InnerSubscription(self, observer) ex = self.exception hv = self.has_value v = self.value if ex: observer.on_error(ex) elif hv: observer.on_next(v) observer.on_completed() else: observer.on_completed() return Disposable.empty()
def subscribe(observer): n = len(sources) queues = [[] for _ in range(n)] is_done = [False] * n def next(i): if all([len(q) for q in queues]): res = [x.pop(0) for x in queues] observer.on_next(res) elif all([x for j, x in enumerate(is_done) if j != i]): observer.on_completed() return def done(i): is_done[i] = True if all(is_done): observer.on_completed() return subscriptions = [None]*n def func(i): subscriptions[i] = SingleAssignmentDisposable() def on_next(x): queues[i].append(x) next(i) subscriptions[i].disposable = sources[i].subscribe(on_next, observer.on_error, lambda: done(i)) for idx in range(n): func(idx) composite_disposable = CompositeDisposable(subscriptions) def action(): for _ in queues: queues[n] = [] composite_disposable.add(Disposable.create(action)) return composite_disposable
def subscribe2(o): o.on_error('exception') return Disposable.empty()
def inner_action(scheduler, state=None): yy[0] = state return Disposable.empty()
def action(scheduler, state=None): xx[0] = state return Disposable.empty()
def inner_action(scheduler, y): yy[0] = y return Disposable.empty()
def subscribe3(o): o.on_completed() return Disposable.empty()
def action(scheduler, state): for observer in observable.observers: notification.accept(observer) return Disposable.empty()
def subscribe1(o): o.on_next(1) return Disposable.empty()
def subscribe(o): o.on_error(ex) o.on_next(100) o.on_error('foo') o.on_completed() return Disposable.empty()
def subscribe(o): o.on_completed() o.on_next(100) o.on_error("ex") o.on_completed() return Disposable.empty()