예제 #1
0
파일: join.py 프로젝트: phaufe/RxPY
            def on_next_right(value):
                duration = None
                current_id = right_id[0]
                right_id[0] += 1
                md = SingleAssignmentDisposable()
                right_map[current_id] = value
                group.add(md)

                def expire():
                    if current_id in right_map:
                        del right_map[current_id]
                    if not len(right_map) and right_done[0]:
                        observer.on_completed()

                    return group.remove(md)

                try:
                    duration = right_duration_selector(value)
                except Exception as exception:
                    log.error("*** Exception: %s" % exception)
                    observer.on_error(exception)
                    return

                md.disposable = duration.take(1).subscribe(noop, observer.on_error, lambda: expire())

                for val in left_map.values():
                    try:
                        result = result_selector(val, value)
                    except Exception as exception:
                        log.error("*** Exception: %s" % exception)
                        observer.on_error(exception)
                        return

                    observer.on_next(result)
class AutoDetachObserver(AbstractObserver):

    def __init__(self, observer):
        super(AutoDetachObserver, self).__init__()
        
        self.observer = observer
        self.m = SingleAssignmentDisposable()

    def next(self, value):
        try:
            self.observer.on_next(value)
        except Exception as ex:
            traceback.print_exc(file=sys.stdout)
            self.dispose()
        
    def error(self, exn):
        try:
            self.observer.on_error(exn)
        finally:
            self.dispose()
        
    def completed(self):
        try:
            self.observer.on_completed()
        finally:
            self.dispose()
    
    def set_disposable(self, value):
        self.m.disposable = value
    
    disposable = property(fset=set_disposable)

    def dispose(self):
        super(AutoDetachObserver, self).dispose()
        self.m.dispose()
예제 #3
0
        def action(action1, state=None):
            current = None

            def on_error(exn):
                nonlocal last_exception
                last_exception = exn
                action1()

            if is_disposed:
                return
            try:
                current = next(e)
            except StopIteration:
                if last_exception:
                    observer.on_error(last_exception)
                else:
                    observer.on_completed()
            except Exception as ex:
                observer.on_error(ex)
            else:
                d = SingleAssignmentDisposable()
                subscription.disposable = d

                d.disposable = current.subscribe(observer.on_next, on_error,
                                                 observer.on_completed)
예제 #4
0
        def action(action1, state=None):
            current = None
            
            def on_error(exn):
                nonlocal last_exception
                last_exception = exn
                action1()

            if is_disposed:
                return
            try:
                current = next(e)
            except StopIteration:
                if last_exception:
                    observer.on_error(last_exception)
                else:
                    observer.on_completed()    
            except Exception as ex:
                observer.on_error(ex)
            else:
                d = SingleAssignmentDisposable()
                subscription.disposable = d
                
                d.disposable = current.subscribe(
                    observer.on_next,
                    on_error,
                    observer.on_completed
                )
예제 #5
0
파일: expand.py 프로젝트: ESSL-CQQ/RxPY
                def action(this, state):
                    if len(q) > 0:
                        work = q.pop(0)
                    else:
                        is_acquired[0] = False
                        return

                    m1 = SingleAssignmentDisposable()
                    d.add(m1)

                    def on_next(x):
                        observer.on_next(x)
                        result = None
                        try:
                            result = selector(x)
                        except Exception as ex:
                            observer.on_error(ex)

                        q.append(result)
                        active_count[0] += 1
                        ensure_active()

                    def on_complete():
                        d.remove(m1)
                        active_count[0] -= 1
                        if active_count[0] == 0:
                            observer.on_completed()

                    m1.disposable = work.subscribe(on_next, observer.on_error, on_complete)
                    this()
예제 #6
0
        def subscribe_all(parent, *children):

            values = [NO_VALUE for _ in children]

            def subscribe_child(i, child):
                subscription = SingleAssignmentDisposable()

                def on_next(value):
                    with parent.lock:
                        values[i] = value

                subscription.disposable = child.subscribe(
                    on_next, observer.on_error)
                return subscription

            parent_subscription = SingleAssignmentDisposable()

            def on_next(value):
                with parent.lock:
                    if NO_VALUE not in values:
                        try:
                            result = result_selector(value, *values)
                        except Exception as error:
                            observer.on_error(error)
                        else:
                            observer.on_next(result)

            parent_subscription.disposable = parent.subscribe(
                on_next, observer.on_error, observer.on_completed)

            return listify_args(
                parent_subscription,
                *(subscribe_child(*a) for a in enumerate(children)))
예제 #7
0
class AutoDetachObserver(AbstractObserver):
    def __init__(self, observer):
        super(AutoDetachObserver, self).__init__(self._next, self._error,
                                                 self._completed)

        self.observer = observer
        self.m = SingleAssignmentDisposable()

    def _next(self, value):
        try:
            self.observer.on_next(value)
        except Exception as ex:
            self.dispose()
            raise ex

    def _error(self, exn):
        try:
            self.observer.on_error(exn)
        finally:
            self.dispose()

    def _completed(self):
        try:
            self.observer.on_completed()
        finally:
            self.dispose()

    def set_disposable(self, value):
        self.m.disposable = value

    disposable = property(fset=set_disposable)

    def dispose(self):
        super(AutoDetachObserver, self).dispose()
        self.m.dispose()
예제 #8
0
class AutoDetachObserver(AbstractObserver):

    def __init__(self, observer):
        super(AutoDetachObserver, self).__init__(self._next, self._error, self._completed)

        self.observer = observer
        self.m = SingleAssignmentDisposable()

    def _next(self, value):
        try:
            self.observer.on_next(value)
        except Exception:
            self.dispose()
            raise

    def _error(self, exn):
        try:
            self.observer.on_error(exn)
        finally:
            self.dispose()

    def _completed(self):
        try:
            self.observer.on_completed()
        finally:
            self.dispose()

    def set_disposable(self, value):
        self.m.disposable = value

    disposable = property(fset=set_disposable)

    def dispose(self):
        super(AutoDetachObserver, self).dispose()
        self.m.dispose()
예제 #9
0
class ScheduledItem(object):
    def __init__(self, scheduler, state, action, duetime, comparer=None):
        self.scheduler = scheduler
        self.state = state
        self.action = action
        self.duetime = duetime
        self.comparer = comparer or default_sub_comparer
        self.disposable = SingleAssignmentDisposable()

    def invoke(self):
        self.disposable.disposable = self.invoke_core()

    def compare_to(self, other):
        return self.comparer(self.duetime, other.duetime)

    def cancel(self):
        """Cancels the work item by disposing the resource returned by
        invoke_core as soon as possible."""

        self.disposable.dispose()

    def is_cancelled(self):
        return self.disposable.is_disposed

    def invoke_core(self):
        return self.action(self.scheduler, self.state)

    def __lt__(self, other):
        return self.compare_to(other) < 0

    def __gt__(self, other):
        return self.compare_to(other) > 0

    def __eq__(self, other):
        return self.compare_to(other) == 0
예제 #10
0
        def subscribe(observer):
            is_open = [False]

            def on_next(left):
                if is_open[0]:
                    observer.on_next(left)

            def on_completed():
                if is_open[0]:
                    observer.on_completed()

            disposables = CompositeDisposable(
                source.subscribe(on_next, observer.on_error, on_completed))

            right_subscription = SingleAssignmentDisposable()
            disposables.add(right_subscription)

            def on_next2(x):
                is_open[0] = True
                right_subscription.dispose()

            def on_completed2():
                right_subscription.dispose()

            right_subscription.disposable = other.subscribe(
                on_next2, observer.on_error, on_completed2)

            return disposables
예제 #11
0
            def on_next(x):
                nonlocal value, has_value, _id

                throttle = None
                try:
                    throttle = throttle_duration_selector(x)
                except Exception as e:
                    observer.on_error(e)
                    return
                
                has_value = True
                value = x
                _id += 1
                current_id = _id
                d = SingleAssignmentDisposable()
                cancelable.disposable = d

                def on_next(x):
                    nonlocal has_value
                    if has_value and _id == current_id:
                        observer.on_next(value)
                    
                    has_value = False
                    d.dispose()
                
                def on_completed():
                    nonlocal has_value
                    if has_value and _id == current_id:
                        observer.on_next(value)
                    
                    has_value = False
                    d.dispose()
                
                d.disposable = throttle.subscribe(on_next, observer.on_error, on_completed)
예제 #12
0
파일: catch.py 프로젝트: indigos33k3r/RxPY
    def subscribe(observer, scheduler=None):
        d1 = SingleAssignmentDisposable()
        subscription = SerialDisposable()

        subscription.disposable = d1

        def on_error(exception):
            try:
                result = handler(exception)
            except Exception as ex:  # By design. pylint: disable=W0703
                observer.on_error(ex)
                return

            result = Observable.from_future(result) if is_future(result) else result
            d = SingleAssignmentDisposable()
            subscription.disposable = d
            d.disposable = result.subscribe(observer, scheduler)

        d1.disposable = source.subscribe_(
            observer.on_next,
            on_error,
            observer.on_completed,
            scheduler
        )
        return subscription
예제 #13
0
            def action(scheduler, state):
                if queue:
                    work = queue.pop(0)
                else:
                    is_acquired[0] = False
                    return

                sad = SingleAssignmentDisposable()
                d.add(sad)

                def on_next(value):
                    observer.on_next(value)
                    result = None
                    try:
                        result = mapper(value)
                    except Exception as ex:
                        observer.on_error(ex)
                        return

                    queue.append(result)
                    active_count[0] += 1
                    ensure_active()

                def on_complete():
                    d.remove(sad)
                    active_count[0] -= 1
                    if active_count[0] == 0:
                        observer.on_completed()

                sad.disposable = work.subscribe_(on_next, observer.on_error,
                                                 on_complete, scheduler)
                m.disposable = scheduler.schedule(action)
예제 #14
0
        def on_next_right(value):
            duration = None
            current_id = right_id[0]
            right_id[0] += 1
            md = SingleAssignmentDisposable()
            right_map[current_id] = value
            group.add(md)

            def expire():
                if current_id in right_map:
                    del right_map[current_id]
                if not len(right_map) and right_done[0]:
                    observer.on_completed()

                return group.remove(md)

            try:
                duration = right_duration_selector(value)
            except Exception as exception:
                log.error("*** Exception: %s" % exception)
                observer.on_error(exception)
                return

            md.disposable = duration.take(1).subscribe(noop, observer.on_error,
                                                       lambda: expire())

            for val in left_map.values():
                try:
                    result = result_selector(val, value)
                except Exception as exception:
                    log.error("*** Exception: %s" % exception)
                    observer.on_error(exception)
                    return

                observer.on_next(result)
예제 #15
0
class ScheduledItem(object):
    def __init__(self, scheduler, state, action, duetime, comparer=None):
        self.scheduler = scheduler
        self.state = state
        self.action = action
        self.duetime = duetime
        self.comparer = comparer or default_sub_comparer
        self.disposable = SingleAssignmentDisposable()

    def invoke(self):
        self.disposable.disposable = self.invoke_core()

    def compare_to(self, other):
        return self.comparer(self.duetime, other.duetime)

    def cancel(self):
        """Cancels the work item by disposing the resource returned by
        invoke_core as soon as possible."""

        self.disposable.dispose()

    def is_cancelled(self):
        return self.disposable.is_disposed

    def invoke_core(self):
        return self.action(self.scheduler, self.state)

    def __lt__(self, other):
        return self.compare_to(other) < 0

    def __gt__(self, other):
        return self.compare_to(other) > 0

    def __eq__(self, other):
        return self.compare_to(other) == 0
        def subscribe(observer):
            m = SingleAssignmentDisposable()
            group = CompositeDisposable()
            is_stopped = False
            group.add(m)
            
            def on_next(inner_source):
                inner_subscription = SingleAssignmentDisposable()
                group.add(inner_subscription)

                def on_complete():
                    nonlocal group
                    
                    group.remove(inner_subscription)
                    if is_stopped and group.length == 1:
                        observer.on_completed()
                    
                disposable = inner_source.subscribe(
                    observer.on_next,
                    observer.on_error, 
                    on_complete)
                
                inner_subscription.disposable = disposable
            
            def on_complete():
                nonlocal is_stopped

                is_stopped = True
                if group.length == 1:
                    observer.on_completed()
            
            m.disposable = sources.subscribe(on_next, observer.on_error, on_complete)
            return group
예제 #17
0
파일: merge.py 프로젝트: mvschaik/RxPY
        def subscribe(observer):
            m = SingleAssignmentDisposable()
            group = CompositeDisposable()
            is_stopped = [False]
            group.add(m)

            def on_next(inner_source):
                inner_subscription = SingleAssignmentDisposable()
                group.add(inner_subscription)

                inner_source = Observable.from_future(inner_source)

                def on_complete():
                    group.remove(inner_subscription)
                    if is_stopped[0] and group.length == 1:
                        observer.on_completed()

                disposable = inner_source.subscribe(
                    observer.on_next,
                    observer.on_error,
                    on_complete)

                inner_subscription.disposable = disposable

            def on_complete():
                is_stopped[0] = True
                if group.length == 1:
                    observer.on_completed()

            m.disposable = sources.subscribe(on_next, observer.on_error, on_complete)
            return group
예제 #18
0
파일: delay.py 프로젝트: jesonjn/RxPY
            def on_next(notification):
                log.debug("observable_delay_timespan:subscribe:on_next()")
                should_run = False
                
                with self.lock:
                    if notification.value.kind == 'E':
                        del queue[:]
                        queue.append(notification)
                        exception[0] = notification.value.exception
                        should_run = not running[0]
                    else:
                        queue.append(Timestamp(value=notification.value, timestamp=notification.timestamp + duetime))
                        should_run = not active[0]
                        active[0] = True

                if should_run:
                    if exception[0]:
                        log.error("*** Exception: %s", exception[0])
                        observer.on_error(exception[0])
                    else:
                        d = SingleAssignmentDisposable()
                        cancelable.disposable = d

                        def action(this):
                            if exception[0]:
                                log.error("observable_delay_timespan:subscribe:on_next:action(), exception: %s", exception[0])
                                return
                            
                            with self.lock:
                                running[0] = True
                                while True:
                                    result = None
                                    if len(queue) and queue[0].timestamp <= scheduler.now():
                                        result = queue.pop(0).value
    
                                    if result:
                                        result.accept(observer)
    
                                    if not result:
                                        break
    
                                should_recurse = False
                                recurse_duetime = 0
                                if len(queue) :
                                    should_recurse = True
                                    diff = queue[0].timestamp - scheduler.now()
                                    zero = timedelta(0) if isinstance(diff, timedelta) else 0
                                    recurse_duetime = max(zero, diff)
                                else:
                                    active[0] = False
    
                                ex = exception[0]
                                running[0] = False
                            
                            if ex:
                                observer.on_error(ex)
                            elif should_recurse:
                                this(recurse_duetime)

                        d.disposable = scheduler.schedule_recursive_with_relative(duetime, action)
예제 #19
0
        def subscribe(observer):
            m = SingleAssignmentDisposable()
            group = CompositeDisposable()
            is_stopped = [False]
            group.add(m)

            def on_next(inner_source):
                inner_subscription = SingleAssignmentDisposable()
                group.add(inner_subscription)

                inner_source = Observable.from_future(inner_source)

                def on_complete():
                    group.remove(inner_subscription)
                    if is_stopped[0] and group.length == 1:
                        observer.on_completed()

                disposable = inner_source.subscribe(
                    observer.on_next,
                    observer.on_error,
                    on_complete)

                inner_subscription.disposable = disposable

            def on_complete():
                is_stopped[0] = True
                if group.length == 1:
                    observer.on_completed()

            m.disposable = sources.subscribe(on_next, observer.on_error,
                                             on_complete)
            return group
            def on_next(inner_source):
                nonlocal latest, has_latest

                d = SingleAssignmentDisposable()
                latest += 1
                _id = latest
                has_latest = True
                inner_subscription.disposable = d

                def on_next(x):
                    if latest == _id:
                        observer.on_next(x)
                
                def on_error(e):
                    if latest == _id:
                        observer.on_error(e)
                
                def on_completed():
                    nonlocal has_latest

                    if latest == _id:
                        has_latest = False
                        if is_stopped:
                            observer.on_completed()
                        
                d.disposable = inner_source.subscribe(on_next, on_error, on_completed)
예제 #21
0
class AutoDetachObserver(ObserverBase):
    def __init__(self, observer):
        super(AutoDetachObserver, self).__init__()

        self._observer = observer
        self._subscription = SingleAssignmentDisposable()

    def _on_next_core(self, value):
        try:
            self._observer.on_next(value)
        except Exception:
            self.dispose()
            raise

    def _on_error_core(self, error):
        try:
            self._observer.on_error(error)
        finally:
            self.dispose()

    def _on_completed_core(self):
        try:
            self._observer.on_completed()
        finally:
            self.dispose()

    def set_disposable(self, value):
        self._subscription.disposable = value

    subscription = property(fset=set_disposable)

    def dispose(self):
        super().dispose()
        self._subscription.dispose()
예제 #22
0
                def action(this, state):
                    if len(q) > 0:
                        work = q.pop(0)
                    else:
                        is_acquired[0] = False
                        return

                    m1 = SingleAssignmentDisposable()
                    d.add(m1)

                    def on_next(x):
                        observer.on_next(x)
                        result = None
                        try:
                            result = selector(x)
                        except Exception as ex:
                            observer.on_error(ex)

                        q.append(result)
                        active_count[0] += 1
                        ensure_active()

                    def on_complete():
                        d.remove(m1)
                        active_count[0] -= 1
                        if active_count[0] == 0:
                            observer.on_completed()

                    m1.disposable = work.subscribe(on_next, observer.on_error,
                                                   on_complete)
                    this()
예제 #23
0
        def subscribe_all(parent, *children):

            values = [NO_VALUE for _ in children]

            def subscribe_child(i, child):
                subscription = SingleAssignmentDisposable()
                def on_next(value):
                    with parent.lock:
                        values[i] = value
                subscription.disposable = child.subscribe(
                    on_next, observer.on_error)
                return subscription

            parent_subscription = SingleAssignmentDisposable()
            def on_next(value):
                with parent.lock:
                    if NO_VALUE not in values:
                        try:
                            result = result_selector(value, *values)
                        except Exception as error:
                            observer.on_error(error)
                        else:
                            observer.on_next(result)
            parent_subscription.disposable = parent.subscribe(
                on_next, observer.on_error, observer.on_completed)

            return listify_args(
                parent_subscription,
                *(subscribe_child(*a) for a in enumerate(children))
            )
예제 #24
0
            def on_next(x):
                nonlocal value, has_value, _id

                throttle = None
                try:
                    throttle = throttle_duration_selector(x)
                except Exception as e:
                    observer.on_error(e)
                    return

                has_value = True
                value = x
                _id += 1
                current_id = _id
                d = SingleAssignmentDisposable()
                cancelable.disposable = d

                def on_next(x):
                    nonlocal has_value
                    if has_value and _id == current_id:
                        observer.on_next(value)

                    has_value = False
                    d.dispose()

                def on_completed():
                    nonlocal has_value
                    if has_value and _id == current_id:
                        observer.on_next(value)

                    has_value = False
                    d.dispose()

                d.disposable = throttle.subscribe(on_next, observer.on_error,
                                                  on_completed)
예제 #25
0
        def on_next(inner_source):
            d = SingleAssignmentDisposable()
            with self.lock:
                latest[0] += 1
                _id = latest[0]
            has_latest[0] = True
            inner_subscription.disposable = d

            # Check if Future or Observable
            inner_source = Observable.from_future(inner_source)

            def on_next(x):
                if latest[0] == _id:
                    observer.on_next(x)

            def on_error(e):
                if latest[0] == _id:
                    observer.on_error(e)

            def on_completed():
                if latest[0] == _id:
                    has_latest[0] = False
                    if is_stopped[0]:
                        observer.on_completed()

            d.disposable = inner_source.subscribe(on_next, on_error, on_completed)
예제 #26
0
파일: skipuntil.py 프로젝트: phaufe/RxPY
        def subscribe(observer):
            is_open = [False]

            def on_next(left):
                if is_open[0]:
                    observer.on_next(left)

            def on_completed():
                if is_open[0]:
                    observer.on_completed()

            disposables = CompositeDisposable(source.subscribe(on_next, observer.on_error, on_completed))

            right_subscription = SingleAssignmentDisposable()
            disposables.add(right_subscription)

            def on_next2(x):
                is_open[0] = True
                right_subscription.dispose()

            def on_completed2():
                right_subscription.dispose()

            right_subscription.disposable = other.subscribe(on_next2, observer.on_error, on_completed2)

            return disposables
예제 #27
0
    def subscribe(observer):
        has_current = [False]
        is_stopped = [False]
        m = SingleAssignmentDisposable()
        g = CompositeDisposable()

        g.add(m)

        def on_next(inner_source):
            if not has_current[0]:
                has_current[0] = True

                inner_source = Observable.from_future(inner_source)

                inner_subscription = SingleAssignmentDisposable()
                g.add(inner_subscription)

                def on_completed_inner():
                    g.remove(inner_subscription)
                    has_current[0] = False
                    if is_stopped[0] and len(g) == 1:
                        observer.on_completed()

                inner_subscription.disposable = inner_source.subscribe(
                    observer.on_next, observer.on_error, on_completed_inner)

        def on_completed():
            is_stopped[0] = True
            if not has_current[0] and len(g) == 1:
                observer.on_completed()

        m.disposable = sources.subscribe(on_next, observer.on_error,
                                         on_completed)
        return g
예제 #28
0
class ScheduledItem(object):
    def __init__(self, scheduler, state, action, duetime):
        self.scheduler = scheduler
        self.state = state
        self.action = action
        self.duetime = duetime
        self.disposable = SingleAssignmentDisposable()

    def invoke(self):
        ret = self.scheduler.invoke_action(self.action, self.state)
        self.disposable.disposable = ret

    def cancel(self):
        """Cancels the work item by disposing the resource returned by
        invoke_core as soon as possible."""

        self.disposable.dispose()

    def is_cancelled(self):
        return self.disposable.is_disposed

    def __lt__(self, other):
        return self.duetime < other.duetime

    def __gt__(self, other):
        return self.duetime > other.duetime

    def __eq__(self, other):
        return self.duetime == other.duetime
예제 #29
0
        def set_timer(timeout):
            my_id = _id[0]

            def timer_wins():
                return _id[0] == my_id

            d = SingleAssignmentDisposable()
            timer.disposable = d

            def on_next(x):
                if timer_wins():
                    subscription.disposable = other.subscribe(
                        observer, scheduler)

                d.dispose()

            def on_error(e):
                if timer_wins():
                    observer.on_error(e)

            def on_completed():
                if timer_wins():
                    subscription.disposable = other.subscribe(observer)

            d.disposable = timeout.subscribe_(on_next, on_error, on_completed,
                                              scheduler)
예제 #30
0
파일: groupjoin.py 프로젝트: mvschaik/RxPY
            def on_next_right(value):
                _id = right_id[0]
                right_id[0] += 1
                right_map[_id] = value

                md = SingleAssignmentDisposable()
                group.add(md)

                def expire():
                    del right_map[_id]
                    group.remove(md)

                try:
                    duration = right_duration_selector(value)
                except Exception as e:
                    for left_value in left_map.values():
                        left_value.on_error(e)

                    observer.on_error(e)
                    return

                def on_error(e):
                    for left_value in left_map.values():
                        left_value.on_error(e)

                    observer.on_error(e)

                md.disposable = duration.take(1).subscribe(
                    nothing,
                    on_error,
                    expire)

                for left_value in left_map.values():
                    left_value.on_next(value)
예제 #31
0
파일: debounce.py 프로젝트: AlexMost/RxPY
        def on_next(x):
            throttle = None
            try:
                throttle = throttle_duration_selector(x)
            except Exception as e:
                observer.on_error(e)
                return

            has_value[0] = True
            value[0] = x
            _id[0] += 1
            current_id = _id[0]
            d = SingleAssignmentDisposable()
            cancelable.disposable = d

            def on_next(x):
                if has_value[0] and _id[0] == current_id:
                    observer.on_next(value[0])

                has_value[0] = False
                d.dispose()

            def on_completed():
                if has_value[0] and _id[0] == current_id:
                    observer.on_next(value[0])

                has_value[0] = False
                d.dispose()

            d.disposable = throttle.subscribe(on_next, observer.on_error,
                                              on_completed)
        def subscribe(observer):
            m = SingleAssignmentDisposable()
            group = CompositeDisposable()
            is_stopped = False
            group.add(m)

            def on_next(inner_source):
                inner_subscription = SingleAssignmentDisposable()
                group.add(inner_subscription)

                def on_complete():
                    nonlocal group

                    group.remove(inner_subscription)
                    if is_stopped and group.length == 1:
                        observer.on_completed()

                disposable = inner_source.subscribe(observer.on_next,
                                                    observer.on_error,
                                                    on_complete)

                inner_subscription.disposable = disposable

            def on_complete():
                nonlocal is_stopped

                is_stopped = True
                if group.length == 1:
                    observer.on_completed()

            m.disposable = sources.subscribe(on_next, observer.on_error,
                                             on_complete)
            return group
예제 #33
0
 def __init__(self, scheduler, state, action, duetime, comparer=None):
     self.scheduler = scheduler
     self.state = state
     self.action = action
     self.duetime = duetime
     self.comparer = comparer or default_sub_comparer
     self.disposable = SingleAssignmentDisposable()
            def on_next(inner_source):
                nonlocal latest, has_latest

                d = SingleAssignmentDisposable()
                latest += 1
                _id = latest
                has_latest = True
                inner_subscription.disposable = d

                def on_next(x):
                    if latest == _id:
                        observer.on_next(x)

                def on_error(e):
                    if latest == _id:
                        observer.on_error(e)

                def on_completed():
                    nonlocal has_latest

                    if latest == _id:
                        has_latest = False
                        if is_stopped:
                            observer.on_completed()

                d.disposable = inner_source.subscribe(on_next, on_error,
                                                      on_completed)
예제 #35
0
        def on_next(x):
            throttle = None
            try:
                throttle = throttle_duration_mapper(x)
            except Exception as e:
                observer.on_error(e)
                return

            has_value[0] = True
            value[0] = x
            _id[0] += 1
            current_id = _id[0]
            d = SingleAssignmentDisposable()
            cancelable.disposable = d

            def on_next(x):
                if has_value[0] and _id[0] == current_id:
                    observer.on_next(value[0])

                has_value[0] = False
                d.dispose()

            def on_completed():
                if has_value[0] and _id[0] == current_id:
                    observer.on_next(value[0])

                has_value[0] = False
                d.dispose()

            d.disposable = throttle.subscribe_(on_next, observer.on_error,
                                               on_completed, scheduler)
예제 #36
0
        def on_next(inner_source):
            d = SingleAssignmentDisposable()
            with self.lock:
                latest[0] += 1
                _id = latest[0]
            has_latest[0] = True
            inner_subscription.disposable = d

            # Check if Future or Observable
            inner_source = Observable.from_future(inner_source)

            def on_next(x):
                if latest[0] == _id:
                    observer.on_next(x)

            def on_error(e):
                if latest[0] == _id:
                    observer.on_error(e)

            def on_completed():
                if latest[0] == _id:
                    has_latest[0] = False
                    if is_stopped[0]:
                        observer.on_completed()

            d.disposable = inner_source.subscribe(on_next, on_error,
                                                  on_completed)
예제 #37
0
        def on_next_right(value):
            with self.lock:
                _id = right_id[0]
                right_id[0] += 1
                right_map[_id] = value

            md = SingleAssignmentDisposable()
            group.add(md)

            def expire():
                del right_map[_id]
                group.remove(md)

            try:
                duration = right_duration_selector(value)
            except Exception as e:
                for left_value in left_map.values():
                    left_value.on_error(e)

                observer.on_error(e)
                return

            def on_error(e):
                with self.lock:
                    for left_value in left_map.values():
                        left_value.on_error(e)

                    observer.on_error(e)

            md.disposable = duration.take(1).subscribe(nothing, on_error,
                                                       expire)

            with self.lock:
                for left_value in left_map.values():
                    left_value.on_next(value)
예제 #38
0
    def subscribe(observer, scheduler=None):
        group = CompositeDisposable()
        is_stopped = [False]
        m = SingleAssignmentDisposable()
        group.add(m)

        def on_next(inner_source):
            inner_subscription = SingleAssignmentDisposable()
            group.add(inner_subscription)

            inner_source = Observable.from_future(inner_source) if is_future(
                inner_source) else inner_source

            @synchronized(source.lock)
            def on_completed():
                group.remove(inner_subscription)
                if is_stopped[0] and len(group) == 1:
                    observer.on_completed()

            on_next = synchronized(source.lock)(observer.on_next)
            on_error = synchronized(source.lock)(observer.on_error)
            disposable = inner_source.subscribe_(on_next, on_error,
                                                 on_completed, scheduler)
            inner_subscription.disposable = disposable

        def on_completed():
            is_stopped[0] = True
            if len(group) == 1:
                observer.on_completed()

        m.disposable = source.subscribe_(on_next, observer.on_error,
                                         on_completed, scheduler)
        return group
예제 #39
0
파일: amb.py 프로젝트: michaelandersen/RxPY
    def subscribe(observer):
        choice = [None]
        left_choice = "L"
        right_choice = ("R",)
        left_subscription = SingleAssignmentDisposable()
        right_subscription = SingleAssignmentDisposable()

        def choice_left():
            if not choice[0]:
                choice[0] = left_choice
                right_subscription.dispose()

        def choice_right():
            if not choice[0]:
                choice[0] = right_choice
                left_subscription.dispose()

        def on_next_left(value):
            with self.lock:
                choice_left()
            if choice[0] == left_choice:
                observer.on_next(value)

        def on_error_left(err):
            with self.lock:
                choice_left()
            if choice[0] == left_choice:
                observer.on_error(err)

        def on_completed_left():
            with self.lock:
                choice_left()
            if choice[0] == left_choice:
                observer.on_completed()

        ld = left_source.subscribe(on_next_left, on_error_left, on_completed_left)
        left_subscription.disposable = ld

        def on_next_right(value):
            with self.lock:
                choice_right()
            if choice[0] == right_choice:
                observer.on_next(value)

        def on_error_right(err):
            with self.lock:
                choice_right()
            if choice[0] == right_choice:
                observer.on_error(err)

        def on_completed_right():
            with self.lock:
                choice_right()
            if choice[0] == right_choice:
                observer.on_completed()

        rd = right_source.subscribe(on_next_right, on_error_right, on_completed_right)
        right_subscription.disposable = rd
        return CompositeDisposable(left_subscription, right_subscription)
예제 #40
0
 def subscribe_child(i, child):
     subscription = SingleAssignmentDisposable()
     def on_next(value):
         with parent.lock:
             values[i] = value
     subscription.disposable = child.subscribe(
         on_next, observer.on_error)
     return subscription
            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))
예제 #42
0
            def on_next(notification):
                log.debug("observable_delay_timespan:subscribe:on_next()")
                nonlocal q, active, cancelable, exception, running
                should_run = False
                d = None
                
                if notification.value.kind == 'E':
                    q = []
                    q.append(notification)
                    exception = notification.value.exception
                    should_run = not running
                else:
                    q.append(Timestamp(value=notification.value, timestamp=notification.timestamp + duetime))
                    should_run = not active
                    active = True
                
                if should_run:
                    if exception:
                        log.error("*** Exception: %s" % exception)
                        observer.on_error(exception)
                    else:
                        d = SingleAssignmentDisposable()
                        cancelable.disposable = d

                        def action(self):
                            nonlocal q, active, running
                            if exception:
                                log.error("observable_delay_timespan:subscribe:on_next:action(), exception: %s" % exception)
                                return
                            
                            running = True
                            while True:
                                result = None
                                if len(q) and q[0].timestamp <= scheduler.now():
                                    result = q.pop(0).value
                                
                                if result:
                                    result.accept(observer)
                                
                                if not result:
                                    break
                            
                            should_recurse = False
                            recurse_duetime = 0
                            if len(q) > 0:
                                should_recurse = True
                                recurse_duetime = max(timedelta(0), q[0].timestamp - scheduler.now())
                            else:
                                active = False
                            
                            e = exception
                            running = False
                            if e:
                                observer.on_error(e)
                            elif should_recurse:
                                self(recurse_duetime)

                        d.disposable = scheduler.schedule_recursive_with_relative(duetime, action)
    def __init__(self, source, on_error):
        super(JoinObserver, self).__init__(on_next=self._next)

        self.source = source
        self.on_error = on_error
        self.queue = []
        self.active_plans = []
        self.subscription = SingleAssignmentDisposable()
        self.is_disposed = False
            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))
예제 #45
0
 def action(this, state=None):
     if pos[0] < len(sources):
         current = Observable.from_future(sources[pos[0]])
         pos[0] += 1
         d = SingleAssignmentDisposable()
         subscription.disposable = d
         d.disposable = current.subscribe(observer.on_next, lambda ex: this(), lambda: this())
     else:
         observer.on_completed()
        def subscribe(observer):
            choice = None
            left_choice = 'L'
            right_choice = 'R',
            left_subscription = SingleAssignmentDisposable()
            right_subscription = SingleAssignmentDisposable()

            def choiceL():
                nonlocal choice

                if not choice:
                    choice = left_choice
                    right_subscription.dispose()

            def choiceR():
                nonlocal choice

                if not choice:
                    choice = right_choice
                    left_subscription.dispose()

            def on_left_next(left):
                choiceL()
                if choice == left_choice:
                    observer.on_next(left)
            
            def on_left_error(err):
                choiceL()
                if choice == left_choice:
                    observer.on_error(err)
                

            def on_left_completed():
                choiceL()
                if choice == left_choice:
                    observer.on_completed()

            left_subscription.disposable = left_source.subscribe(on_left_next, on_left_error, on_left_completed)

            def on_right_next(right):
                choiceR()
                if choice == right_choice:
                    observer.on_next(right)
            
            def on_right_error(err):
                choiceR()
                if choice == right_choice:
                    observer.on_error(err)
            
            def on_right_completed():
                choiceR()
                if choice == right_choice:
                    observer.on_completed()

            right_subscription.disposable = right_source.subscribe(on_right_next, on_right_error, on_right_completed)
            return CompositeDisposable(left_subscription, right_subscription)
예제 #47
0
    def subscribe(observer, _=None):
        m = SingleAssignmentDisposable()
        d = SerialDisposable()
        d.disposable = m

        def action(scheduler, state):
            d.disposable = ScheduledDisposable(scheduler, source.subscribe(observer))

        m.disposable = scheduler.schedule(action)
        return d
            def on_error(exception):
                try:
                    result = handler(exception)
                except Exception as ex:
                    observer.on_error(ex)
                    return

                d = SingleAssignmentDisposable()
                subscription.disposable = d
                d.disposable = result.subscribe(observer)
예제 #49
0
    def start(self):
        """Returns the disposable object used to cancel the scheduled recurring
        action (best effort).
        """

        dis = SingleAssignmentDisposable()
        self._cancel = dis
        dis.disposable = self._scheduler.schedule_recursive_with_relative_and_state(self._period, self.tick, 0)

        return dis
예제 #50
0
    def subscribe(observer):
        m = SingleAssignmentDisposable()
        d = SerialDisposable()
        d.disposable = m

        def action(scheduler, state):
            d.disposable = ScheduledDisposable(scheduler, source.subscribe(observer))

        m.disposable = scheduler.schedule(action)
        return d
 def on_error(exception):
     try:
         result = handler(exception)
     except Exception as ex:
         observer.on_error(ex)
         return
     
     d = SingleAssignmentDisposable()
     subscription.disposable = d
     d.disposable = result.subscribe(observer)
예제 #52
0
            def subscribe_child(i, child):
                subscription = SingleAssignmentDisposable()

                def on_next(value):
                    with parent.lock:
                        values[i] = value

                subscription.disposable = child.subscribe(
                    on_next, observer.on_error)
                return subscription
예제 #53
0
    def start(self):
        """Returns the disposable object used to cancel the scheduled recurring
        action (best effort).
        """

        dis = SingleAssignmentDisposable()
        self._cancel = dis
        dis.disposable = self._scheduler.schedule_recursive_with_relative_and_state(
            self._period, self.tick, 0)

        return dis
예제 #54
0
 def func(i):
     subscriptions[i] = SingleAssignmentDisposable()
     
     def on_next(x):
         values[i] = x
         next(i)
     
     def on_completed():
         done(i)
     
     subscriptions[i].disposable = args[i].subscribe(on_next, observer.on_error, on_completed)
예제 #55
0
파일: zip.py 프로젝트: ESSL-CQQ/RxPY
        def func(i):
            source = sources[i]
            sad = SingleAssignmentDisposable()
            source = Observable.from_future(source)

            def on_next(x):
                queues[i].append(x)
                next(i)

            sad.disposable = source.subscribe(on_next, observer.on_error, lambda: done(i))
            subscriptions[i] = sad
            def action(this, state=None):
                nonlocal pos

                if pos < len(sources):
                    current = sources[pos]
                    pos += 1
                    d = SingleAssignmentDisposable()
                    subscription.disposable = d
                    d.disposable = current.subscribe(observer.on_next, lambda ex: this(), lambda: this())
                else:
                    observer.on_completed()
            def on_next_left(value):
                s = Subject()
                _id = left_id
                left_id += 1
                left_map.add(_id, s)
                
                try:
                    result = result_selector(value, add_ref(s, r))
                except Exception as e:
                    log.error("*** Exception: %s" % e)
                    left_values = left_map.getValues()
                    for left_value in left_values:
                        left_value.on_error(e)
                    
                    observer.on_error(e)
                    return
                
                observer.on_next(result)

                right_values = right_map.getValues()
                for right_value in right_values:
                    s.on_next(right_value)
                
                md = SingleAssignmentDisposable()
                group.add(md)

                def expire():
                    if left_map.remove(_id):
                        s.on_completed()
                    
                    group.remove(md)
                
                try:
                    duration = left_duration_selector(value)
                except Exception as e:
                    left_values = left_map.getValues()
                    for left_value in left_values:
                        left_value.on_error(e)
                    
                    observer.on_error(e)
                    return
                
                def on_error(e):
                    left_values = left_map.getValues()
                    for left_value in left_values:
                        left_value.on_error(e)
                    
                    observer.on_error(e)
                    
                md.disposable = duration.take(1).subscribe(
                    nothing,
                    on_error,
                    expire)
예제 #58
0
파일: groupjoin.py 프로젝트: phaufe/RxPY
            def on_next_left(value):
                s = Subject()

                with self.lock:
                    _id = left_id[0]
                    left_id[0] += 1
                    left_map[_id] = s

                try:
                    result = result_selector(value, add_ref(s, r))
                except Exception as e:
                    log.error("*** Exception: %s" % e)
                    for left_value in left_map.values():
                        left_value.on_error(e)

                    observer.on_error(e)
                    return

                observer.on_next(result)

                for right_value in right_map.values():
                    s.on_next(right_value)

                md = SingleAssignmentDisposable()
                group.add(md)

                def expire():
                    if _id in left_map:
                        del left_map[_id]
                        s.on_completed()

                    group.remove(md)

                try:
                    duration = left_duration_selector(value)
                except Exception as e:
                    for left_value in left_map.values():
                        left_value.on_error(e)

                    observer.on_error(e)
                    return

                def on_error(e):
                    for left_value in left_map.values():
                        left_value.on_error(e)

                    observer.on_error(e)

                md.disposable = duration.take(1).subscribe(
                    nothing,
                    on_error,
                    expire)