def run(self): srcs = list(self.parent.sources) N = len(srcs) self.queues = [None] * N self.isDone = [False] * N self.subscriptions = [None] * N self.gate = RLock() for i in range(0, N): self.queues[i] = deque() # Loop twice because subscribing could already yield # a value before all queues are initialized for i in range(0, N): d = SingleAssignmentDisposable() self.subscriptions[i] = d o = self.O(self, i) d.disposable = srcs[i].subscribeSafe(o) c = CompositeDisposable(self.subscriptions) def dispose(): for q in self.queues: q.clear() c.add(Disposable.create(dispose)) return c
def run(self, sources): self.isDisposed = False self.subscription = SerialDisposable() self.gate = AsyncLock() self.stack = [] self.length = [] self.stack.append(iter(sources)) try: length = len(sources) except TypeError: self.length.append(-1) else: self.length.append(length) def scheduled(continuation): self.recurse = continuation self.gate.wait(self.moveNext) cancel = Scheduler.tailRecursion.scheduleRecursive(scheduled) return CompositeDisposable( self.subscription, cancel, Disposable.create(lambda: self.gate.wait(self.dispose)))
def run(self, sources): self.isDisposed = False self.subscription = SerialDisposable() self.gate = AsyncLock() self.stack = [] self.length = [] self.stack.append(iter(sources)) try: length = len(sources) except TypeError: self.length.append(-1) else: self.length.append(length) def scheduled(continuation): self.recurse = continuation self.gate.wait(self.moveNext) cancel = Scheduler.tailRecursion.scheduleRecursive(scheduled) return CompositeDisposable( self.subscription, cancel, Disposable.create(lambda: self.gate.wait(self.dispose)) )
def subscribeCore(self, observer): index = len(self.subscriptions) self.observers.append(observer) self.subscriptions.append(Struct( subscribe=self.scheduler.now(), unsubscribe=0 )) def scheduled(_, message): # time = message[0] notification = message[1] notification.accept(observer) return Disposable.empty() for m in self.messages: self.scheduler.scheduleWithRelativeAndState(m, m[0], scheduled) def dispose(): self.observers.remove(observer) self.subscriptions[index].unsubscribe = self.scheduler.now() return Disposable.create(dispose)
def connect(self, observer): # # We connect the given observer to the subject first, before performing any kind # of initialization which will register an event handler. This is done to ensure # we don't have a time gap between adding the handler and connecting the user's # subject, e.g. when the ImmediateScheduler is used. # # [OK] Use of unsafe Subscribe: called on a known subject implementation. # connection = self.subject.subscribe(observer) self.count += 1 if self.count == 1: try: self.initialize() except Exception as e: self.count -= 1 connection.dispose() observer.onError(e) return Disposable.empty() def dispose(): connection.dispose() with self.parent.gate: self.count -=1 if self.count == 0: self.parent.scheduler.schedule(self.removeHandler.dispose) self.parent.session = None return Disposable.create(dispose)
def start(self): timer = Timer(self.interval, self._execute) self.timerDisposable.disposable = Disposable.create(timer.cancel) timer.start() return self.timerDisposable
def scheduleDrain(self): def cancel(): self.stopped = True self.stop.set() self.evt.release() self.stop.clear() self.cancelTimer.disposable = Disposable.create(cancel) self.scheduler.scheduleLongRunning(self.drainQueue)
def wrapper(observer): a = subscribe(observer) if isinstance(a, Disposable): return a elif callable(a): return Disposable.create(a) else: return Disposable.empty()
def run(self): def dispose(): try: subscription.dispose() finally: self.parent.action() subscription = self.parent.source.subscribeSafe(self) return Disposable.create(dispose)
def _scheduleCore(self, state, action): d = SingleAssignmentDisposable() def scheduled(): if not d.isDisposed: d.disposable = action(self, state) future = self.pool.submit(scheduled) cancel = Disposable.create(future.cancel) return CompositeDisposable(d, cancel)
def ensureDispatcher(self): if self.dispatcherJob != None: return with self.lock: if self.dispatcherJob == None: self.dispatcherJob = self.scheduler.scheduleLongRunning(self.dispatch) self.disposable.disposable = CompositeDisposable( self.dispatcherJob, Disposable.create(self.dispatcherEvent.release) )
def subscribeCore(self, observer): index = len(self.subscriptions) self.observers.append(observer) self.subscriptions.append( Struct(subscribe=self.scheduler.now(), unsubscribe=0)) def dispose(): self.observers.remove(observer) self.subscriptions[index].unsubscribe = self.scheduler.now() return Disposable.create(dispose)
def subscribeCore(self, observer): index = len(self.subscriptions) self.observers.append(observer) self.subscriptions.append(Struct( subscribe=self.scheduler.now(), unsubscribe=0 )) def dispose(): self.observers.remove(observer) self.subscriptions[index].unsubscribe = self.scheduler.now() return Disposable.create(dispose)
def _scheduleRelativeCore(self, state, dueTime, action): dt = Scheduler.normalize(dueTime) if dt == 0: return self.scheduleWithState(state, action) d = SingleAssignmentDisposable() def scheduled(): if not d.isDisposed: d.disposable = action(self, state) timer = Timer(dt, scheduled) cancel = Disposable.create(timer.cancel) return CompositeDisposable(d, cancel)
def run(self): subscription = self.parent.source.subscribeSafe(self) with self.parent.gate: self.parent.count += 1 if self.parent.count == 1: self.parent.connectableSubscription = self.parent.source.connect() def dispose(): subscription.dispose() with self.parent.gate: self.parent.count -= 1 if self.parent.count == 0: self.parent.connectableSubscription.dispose() return Disposable.create(dispose)
def run(self): subscription = self.parent.source.subscribeSafe(self) with self.parent.gate: self.parent.count += 1 if self.parent.count == 1: self.parent.connectableSubscription = self.parent.source.connect( ) def dispose(): subscription.dispose() with self.parent.gate: self.parent.count -= 1 if self.parent.count == 0: self.parent.connectableSubscription.dispose() return Disposable.create(dispose)
def subscribeCore(self, observer): index = len(self.subscriptions) self.observers.append(observer) self.subscriptions.append( Struct(subscribe=self.scheduler.now(), unsubscribe=0)) def scheduled(_, message): # time = message[0] notification = message[1] notification.accept(observer) return Disposable.empty() for m in self.messages: self.scheduler.scheduleWithRelativeAndState(m, m[0], scheduled) def dispose(): self.observers.remove(observer) self.subscriptions[index].unsubscribe = self.scheduler.now() return Disposable.create(dispose)
def addHandler(self, handler): self.addHandlerAction(handler) return Disposable.create(lambda: self.removeHandlerAction(handler))