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)
class PausableBufferedObservable(ObservableBase): def __init__(self, source, pauser=None): self.controller = Subject() if pauser and hasattr(pauser, "subscribe"): self.pauser = self.controller.merge(pauser) else: self.pauser = self.controller super().__init__(source) def _subscribe_core(self, observer, scheduler=None): previous_should_fire = [None] queue = [] def result_mapper(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 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_mapper).subscribe_(on_next, on_error, on_completed, scheduler) return subscription def pause(self): self.controller.on_next(False) def resume(self): self.controller.on_next(True)
return val def start_feed(self): if self.feed_sub is None: self.feed_sub = self.scheduler.schedule_periodic(50, self.acquire) def stop_feed(self): if self.feed_sub is not None: self.feed_sub.dispose() self.feed_sub = None if __name__ == "__main__": misc_log = Subject() print_log = misc_log.subscribe(lambda x: print(str(x))) cam = Camera(0) joint_log = misc_log.merge(cam.log_stream) print_log.dispose() print_log = joint_log.subscribe(lambda x: print(str(x))) cam.start_capture() root = tk.Tk() low = (35, 59, 54) high = (91, 255, 255) app = TrackerGUI(root, cam, low, high) root.mainloop() cam.stop_capture()
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)