def publishLast(self, selector=None): assert isinstance(self, Observable) if selector == None: return self.multicast(AsyncSubject()) else: assert callable(selector) return self.multicastIndividual(lambda: AsyncSubject(), selector)
def wrapper(*args) -> Observable: subject = AsyncSubject() def action(scheduler, state): try: result = func(*args) except Exception as ex: # pylint: disable=broad-except subject.on_error(ex) return subject.on_next(result) subject.on_completed() _scheduler.schedule(action) return subject.pipe(ops.as_observable())
def start(action, scheduler=Scheduler.default): assert isinstance(scheduler, Scheduler) subject = AsyncSubject() def scheduled(): try: subject.onNext(action()) subject.onCompleted() except Exception as e: subject.onError(e) return Disposable.empty() scheduler.schedule(scheduled) return subject.asObservable()
def fromFuture(future): subject = AsyncSubject() def callback(f): if f.cancelled(): subject.onError(FutureCanceledException()) elif f.exception() != None: subject.onError(f.exception()) else: subject.onNext(f.result()) subject.onCompleted() if future.done(): callback(future) else: future.add_done_callback(callback) return subject
def action1(scheduler, state=None): subject[0] = AsyncSubject()
# ReplaySubject会缓存所有值,如果指定参数的话只会缓存最近的几个值 print('--------ReplaySubject---------') subject = ReplaySubject() subject.on_next(1) subject.subscribe(lambda i: print(i)) subject.on_next(2) subject.on_next(3) subject.on_next(4) subject.on_completed() # BehaviorSubject会缓存上次发射的值,除非Observable已经关闭 print('--------BehaviorSubject---------') subject = BehaviorSubject(0) subject.on_next(1) subject.on_next(2) subject.subscribe(lambda i: print(i)) subject.on_next(3) subject.on_next(4) subject.on_completed() # AsyncSubject会缓存上次发射的值,而且仅会在Observable关闭后开始发射 print('--------AsyncSubject---------') subject = AsyncSubject() subject.on_next(1) subject.on_next(2) subject.subscribe(lambda i: print(i)) subject.on_next(3) subject.on_next(4) subject.on_completed()
def KeyPress(observable: AsyncSubject): observable.subscribe(lambda key: PressKeyWin(winKeyWords[key]), on_next = lambda: ReleaseKeyWin(), on_completed = lambda: ReleaseKeyWin())