From 0a20cffa0949312d85ec9e696d601427bb4c48c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krist=C3=B3f=20T=C3=B3th?= Date: Mon, 5 Mar 2018 16:23:01 +0100 Subject: [PATCH] Implement callback subscription logic in HistoryMonitor --- lib/tfw/components/history_monitor.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/lib/tfw/components/history_monitor.py b/lib/tfw/components/history_monitor.py index 4f0eba8..395149d 100644 --- a/lib/tfw/components/history_monitor.py +++ b/lib/tfw/components/history_monitor.py @@ -17,17 +17,31 @@ class HistoryMonitor: def __init__(self, histfile): self.histfile = histfile self._history = [] + self._last_length = len(self._history) + self._callbacks = [] self.observer = Observer() - self.observer.schedule(CallbackEventHandler(self._fetch_history), dirname(self.histfile)) + self.observer.schedule(CallbackEventHandler(self._fetch_history, + self._invoke_callbacks), + dirname(self.histfile)) @property def history(self): return self._history + @property + def callbacks(self): + return self._callbacks + def _fetch_history(self): + self._last_length = len(self._history) with open(self.histfile, 'r') as ifile: self._history = [line.rstrip() for line in ifile.readlines()] + def _invoke_callbacks(self): + if self._last_length < len(self._history): + for callback in self.callbacks: + callback(self.history) + def watch(self): self.observer.start()