예제 #1
0
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
예제 #2
0
파일: buffer.py 프로젝트: aguil/RxPython
        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)
예제 #3
0
파일: reactive.py 프로젝트: aguil/RxPython
    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
예제 #4
0
파일: delay.py 프로젝트: aguil/RxPython
        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)
예제 #5
0
파일: reactive.py 프로젝트: aguil/RxPython
    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)
예제 #6
0
파일: skipLast.py 프로젝트: aguil/RxPython
        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
예제 #7
0
    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()
예제 #8
0
        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
예제 #9
0
파일: reactive.py 프로젝트: aguil/RxPython
    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)
예제 #10
0
파일: takeLast.py 프로젝트: aguil/RxPython
        def onNext(self, value):
            now = self.elapsed()

            self.queue.append(Struct(value=value, interval=now))
            self.trim(now)
예제 #11
0
파일: delay.py 프로젝트: aguil/RxPython
        def onNext(self, value):
            next = self.elapsed() + self.delay

            with self.gate:
                self.queue.append(Struct(value=value, interval=next))
                self.evt.release()
예제 #12
0
파일: timestamp.py 프로젝트: aguil/RxPython
 def onNext(self, value):
     self.observer.onNext(
         Struct(value=value, timestamp=self.parent.scheduler.now()))
예제 #13
0
 def onNext(self, value):
     now = self.elapsed()
     span = now - self.last
     self.last = now
     self.observer.onNext(Struct(value=value, interval=span))