def firstOrDefaultInternal(source, throwOnEmpty, default): state = Struct(value=None, hasValue=False, ex=None, event=Event()) def onNext(value): if not state.hasValue: state.value = value state.hasValue = True state.event.set() def onError(exception): state.ex = exception state.event.set() def onCompleted(): state.event.set() with source.subscribe(AnonymousObserver(onNext, onError, onCompleted)): state.event.wait() if state.ex != None: raise state.ex if not state.hasValue: if throwOnEmpty: raise InvalidOperationException("No elements in observable") else: return default return state.value
def createTimer(self): m = SingleAssignmentDisposable() self.timerDisposable.disposable = m isSpan = False isShift = False if self.nextSpan == self.nextShift: isSpan = True isShift = True elif self.nextShift < self.nextShift: isSpan = True else: isShift = True newTotalTime = self.nextSpan if isSpan else self.nextShift ts = newTotalTime - self.totalTime self.totalTime = newTotalTime if isSpan: self.nextSpan += self.parent.timeShift if isShift: self.nextShift += self.parent.timeShift m.disposable = self.parent.scheduler.scheduleWithRelativeAndState( Struct(isSpan=isSpan, isShift=isShift), ts, self.tick)
def start(self, factory, created=TIME_CREATE, subscribed=TIME_SUBSCRIBE, disposed=TIME_DISPOSE): state = Struct(source=None, subscription=None, observer=self.createObserver()) def scheduledCreate(): state.source = factory() return Disposable.empty() def scheduledSubscribe(): state.subscription = state.source.subscribe(state.observer) return Disposable.empty() def scheduledDispose(): state.subscription.dispose() return Disposable.empty() self.scheduleWithAbsolute(created, scheduledCreate) self.scheduleWithAbsolute(subscribed, scheduledSubscribe) self.scheduleWithAbsolute(disposed, scheduledDispose) super(TestScheduler, self).start() return state.observer
def onNext(self, value): next = self.elapsed() + self.delay shouldRun = False with self.gate: self.queue.append(Struct(value=value, interval=next)) shouldRun = self.ready and (not self.active) self.active = True if shouldRun: self.cancelTimer.disposable = self.scheduler.scheduleRecursiveWithRelative( self.delay, self.drainQueue)
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 onNext(self, value): now = self.elapsed() self.queue.append(Struct(value=value, timeStamp=now)) while len(self.queue) > 0: current = self.queue.popleft() if now - current.timeStamp >= self.parent.duration: self.observer.onNext(current.value) else: self.queue.appendleft(current) break
def onNext(self, value): os = [] with self.gate: errorIfDisposed(self) if not self.isStopped: os = list(self.observers) now = self.scheduler.now() self.q.append(Struct(interval=now, value=value)) for observer in os: observer.onNext(value) for observer in os: observer.ensureActive()
def run(self): it = None try: it = iter(self.parent.source) except Exception as e: self.observer.onError(e) self.dispose() return Disposable.empty() scheduler = self.parent.scheduler if scheduler.isLongRunning: return scheduler.scheduleLongRunningWithState(it, self.loop) else: flag = BooleanDisposable() scheduler.scheduleRecursiveWithState(Struct(flag=flag, it=it), self.loopRec) return flag
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 onNext(self, value): now = self.elapsed() self.queue.append(Struct(value=value, interval=now)) self.trim(now)
def onNext(self, value): next = self.elapsed() + self.delay with self.gate: self.queue.append(Struct(value=value, interval=next)) self.evt.release()
def onNext(self, value): self.observer.onNext( Struct(value=value, timestamp=self.parent.scheduler.now()))
def onNext(self, value): now = self.elapsed() span = now - self.last self.last = now self.observer.onNext(Struct(value=value, interval=span))