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 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_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 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).""" 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 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 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 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 _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 _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_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 _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_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 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 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 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 test_groupdisposable_addafterdispose(): disp1 = [False] disp2 = [False] def action1(): disp1[0] = True d1 = Disposable(action1) def action2(): disp2[0] = True d2 = Disposable(action2) g = CompositeDisposable(d1) assert g.length == 1 g.dispose() assert disp1[0] assert g.length == 0 g.add(d2) assert disp2[0] assert g.length == 0
def schedule(self, action, state=None): """Schedules an action to be executed.""" if self.is_disposed: raise DisposedException() si = ScheduledItem(self, state, action, None) with self.condition: self.ready_list.append(si) self.condition.notify() # signal that a new item is available self.ensure_thread() return Disposable(si.cancel)
def test_mutabledisposable_replacebeforedispose(): disp1 = [False] disp2 = [False] m = SerialDisposable() def action1(): disp1[0] = True d1 = Disposable(action1) m.disposable = d1 assert d1 == m.disposable assert not disp1[0] def action2(): disp2[0] = True d2 = Disposable(action2) m.disposable = d2 assert d2 == m.disposable assert disp1[0] assert not disp2[0]
def test_mutabledisposable_replaceafterdispose(): disp1 = [False] disp2 = [False] m = SerialDisposable() m.dispose() def action1(): disp1[0] = True d1 = Disposable(action1) m.disposable = d1 assert m.disposable == None assert disp1[0] def action2(): disp2[0] = True d2 = Disposable(action2) m.disposable = d2 assert m.disposable == None assert disp2[0]
def subscribe(observer): try: subscription = source.subscribe(observer) except Exception as ex: action() raise def dispose(): try: subscription.dispose() finally: action() return Disposable(dispose)
def test_groupdisposable_remove(): 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 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[0] assert g.remove(d2) assert not g.contains(d1) assert not g.contains(d2) assert disp2[0] disp3 = [False] def action3(): disp3[0] = True d3 = Disposable(action3) assert not g.remove(d3) assert not disp3[0]
def test_mutabledisposable_dispose(): disp = [False] m = SerialDisposable() def action(): disp[0] = True d = Disposable(action) m.disposable = d assert d == m.disposable assert not disp[0] m.dispose() assert disp[0] assert m.disposable == None
def schedule(self, action, state=None): """Schedules an action to be executed.""" scheduler = self disposable = SingleAssignmentDisposable() def interval(): disposable.disposable = action(scheduler, state) timer = [eventlet.spawn(interval)] def dispose(): timer[0].kill() return CompositeDisposable(disposable, Disposable(dispose))
def test_groupdisposable_addafterdispose(): 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) assert g.length == 1 g.dispose() assert disp1 assert g.length == 0 g.add(d2) assert disp2 assert g.length == 0
def schedule(self, action, state=None): """Schedules an action to be executed.""" scheduler = self disposable = SingleAssignmentDisposable() def interval(): disposable.disposable = action(scheduler, state) handle = [self.loop.call_soon(interval)] def dispose(): # nonlocal handle handle[0].cancel() return CompositeDisposable(disposable, Disposable(dispose))
def test_mutabledisposable_dispose(): disp = False m = SerialDisposable() def action(): nonlocal disp disp = True d = Disposable(action) m.disposable = d assert d == m.disposable assert not disp m.dispose() assert disp assert m.disposable == None