当前位置: 首页>>代码示例>>Python>>正文


Python Subject.merge方法代码示例

本文整理汇总了Python中rx.subjects.Subject.merge方法的典型用法代码示例。如果您正苦于以下问题:Python Subject.merge方法的具体用法?Python Subject.merge怎么用?Python Subject.merge使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在rx.subjects.Subject的用法示例。


在下文中一共展示了Subject.merge方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: PausableObservable

# 需要导入模块: from rx.subjects import Subject [as 别名]
# 或者: from rx.subjects.Subject import merge [as 别名]
class PausableObservable(ObservableBase):
    def __init__(self, source, pauser=None):
        self.source = source
        self.controller = Subject()

        if pauser and hasattr(pauser, "subscribe"):
            self.pauser = self.controller.merge(pauser)
        else:
            self.pauser = self.controller

        super(PausableObservable, self).__init__()

    def _subscribe_core(self, observer):
        conn = self.source.publish()
        subscription = conn.subscribe(observer)
        connection = [Disposable.empty()]

        def on_next(b):
            if b:
                connection[0] = conn.connect()
            else:
                connection[0].dispose()
                connection[0] = Disposable.empty()

        pausable = self.pauser.distinct_until_changed().subscribe(on_next)
        return CompositeDisposable(subscription, connection[0], pausable)

    def pause(self):
        self.controller.on_next(False)

    def resume(self):
        self.controller.on_next(True)
开发者ID:ESSL-CQQ,项目名称:RxPY,代码行数:34,代码来源:pausable.py

示例2: PausableBufferedObservable

# 需要导入模块: from rx.subjects import Subject [as 别名]
# 或者: from rx.subjects.Subject import merge [as 别名]
class PausableBufferedObservable(Observable):

    def __init__(self, source, pauser=None):
        self.source = source
        self.controller = Subject()

        if pauser and hasattr(pauser, "subscribe"):
            self.pauser = self.controller.merge(pauser)
        else:
            self.pauser = self.controller

        super(PausableBufferedObservable, self).__init__(self._subscribe)

    def _subscribe(self, observer):
        previous_should_fire = [None]
        queue = []

        def result_selector(data, should_fire=False):
            return {"data": data, "should_fire": should_fire}

        def on_next(results):
            should_fire = results.get("should_fire")
            if (not previous_should_fire[0] is None) and should_fire != previous_should_fire[0]:
                previous_should_fire[0] = should_fire
                # change in shouldFire
                if should_fire:
                    while len(queue):
                        b = queue.pop(0)
                        observer.on_next(b)
            else:
                previous_should_fire[0] = should_fire
                # new data
                if should_fire:
                    observer.on_next(results["data"])
                else:
                    queue.append(results["data"])

        def on_error(err):
            # Empty buffer before sending error
            while len(queue):
                observer.on_next(queue.pop(0))
            observer.on_error(err)

        def on_completed():
            # Empty buffer before sending completion
            while len(queue):
                observer.on_next(queue.pop(0))
            observer.on_completed()

        subscription = combine_latest_source(
            self.source,
            self.pauser.distinct_until_changed().start_with(False),
            result_selector
        ).subscribe(on_next, on_error, on_completed)

        return subscription

    def pause(self):
        self.controller.on_next(False)

    def resume(self):
        self.controller.on_next(True)
开发者ID:AlexMost,项目名称:RxPY,代码行数:64,代码来源:pausablebuffered.py


注:本文中的rx.subjects.Subject.merge方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。