示例#1
0
文件: amb.py 项目: xyicheng/RxPY
    def subscribe(observer, scheduler=None):
        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 left_source.lock:
                choice_left()
            if choice[0] == left_choice:
                observer.on_next(value)

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

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

        lelf_d = left_source.subscribe_(on_next_left, on_error_left, on_completed_left, scheduler)
        left_subscription.disposable = lelf_d

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

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

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

        right_d = right_source.subscribe_(send_right, on_error_right, on_completed_right, scheduler)
        right_subscription.disposable = right_d
        return CompositeDisposable(left_subscription, right_subscription)
示例#2
0
    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)
        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)
        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)
示例#5
0
    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
示例#6
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)
        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
示例#8
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)
示例#9
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
示例#10
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)
示例#11
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)
            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)
        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
示例#14
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)))
示例#15
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
示例#16
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()
示例#17
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)
示例#18
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
示例#19
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)
示例#20
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)
    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
示例#22
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)
示例#23
0
            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)
示例#24
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
示例#25
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)
示例#26
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)
        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
                )
示例#28
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()
            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)
示例#30
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
示例#31
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)
示例#32
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)
示例#33
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))
            )
示例#34
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)
示例#35
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))
示例#37
0
 def create_window_close():
     try:
         window_close = window_closing_selector()
     except Exception as exception:
         log.error("*** Exception: %s" % exception)
         observer.on_error(exception)
         return
     
     def on_completed():
         window.on_completed()
         window = Subject()
         observer.on_next(add_ref(window, r))
         create_window_close()
     
     m1 = SingleAssignmentDisposable()
     m.disposable(m1)
     m1.disposable(window_close.take(1).subscribe(noop, on_error, on_completed))
示例#38
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)
示例#39
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 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))
示例#41
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)
示例#43
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
示例#44
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
 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)
示例#46
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
示例#47
0
            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
示例#48
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)
示例#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
文件: 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
示例#51
0
        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)
            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()
示例#53
0
            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)
示例#54
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)
示例#55
0
        def subscribe(observer):
            switched = False
            _id = 0

            original = SingleAssignmentDisposable()
            subscription = SerialDisposable()
            timer = SerialDisposable()
            subscription.disposable = original

            def create_timer():
                my_id = _id

                def action(scheduler, state=None):
                    nonlocal switched

                    switched = (_id == my_id)
                    timer_wins = switched
                    if timer_wins:
                        subscription.disposable = other.subscribe(observer)

                timer.disposable = scheduler_method(duetime, action)

            create_timer()

            def on_next(x):
                nonlocal _id

                on_next_wins = not switched
                if on_next_wins:
                    _id += 1
                    observer.on_next(x)
                    create_timer()

            def on_error(e):
                nonlocal _id

                on_error_wins = not switched
                if on_error_wins:
                    _id += 1
                    observer.on_error(e)

            def on_completed():
                nonlocal _id

                on_completed_wins = not switched
                if on_completed_wins:
                    _id += 1
                    observer.on_completed()

            original.disposable = source.subscribe(on_next, on_error,
                                                   on_completed)
            return CompositeDisposable(subscription, timer)
            def on_next(inner_source):
                inner_subscription = SingleAssignmentDisposable()
                group.add(inner_subscription)

                def on_next(x):
                    observer.on_next(x)
                
                def on_completed():
                    group.remove(inner_subscription)
                    if is_stopped and group.length == 1:
                        observer.on_completed()
                    
                inner_subscription.disposable = inner_source.subscribe(on_next, observer.on_error, on_completed)
示例#57
0
        def action(this, state=None):
            try:
                source = next(sources)
            except StopIteration:
                observer.on_completed()
                return

            # Allow source to be a factory method taking an error
            source = source(state) if callable(source) else source
            current = Observable.from_future(source)

            d = SingleAssignmentDisposable()
            subscription.disposable = d
            d.disposable = current.subscribe(observer.on_next, lambda ex: this(ex), this)