From a365ac18625c0ce3fb4c194994b7d299c713b6fe Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Mon, 17 May 2021 16:30:38 +0530 Subject: [PATCH] Pass results back from worker to UI --- src/pyj/read_book/search.pyj | 13 ++++++++++++- src/pyj/read_book/search_worker.pyj | 4 ++-- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/src/pyj/read_book/search.pyj b/src/pyj/read_book/search.pyj index 25b9785f6c..c0476b8e25 100644 --- a/src/pyj/read_book/search.pyj +++ b/src/pyj/read_book/search.pyj @@ -29,6 +29,7 @@ class SearchOverlay: def __init__(self, view): self.view = view + self.search_in_flight_id = None c = self.container c.classList.add(CLASS_NAME) next_button = E.div(class_='simple-link', svgicon('chevron-down'), title=_('Next match')) @@ -51,16 +52,26 @@ class SearchOverlay: def worker(self): if not self._worker: self._worker = start_worker('read_book.search') + self._worker.onmessage = self.on_worker_message return self._worker def queue_search(self, query, book, current_name): spine = book.manifest.spine self.request_counter += 1 + self.search_in_flight_id = self.request_counter self.worker.postMessage({ 'type': 'search', 'book_hash': book.book_hash, 'spine': spine, 'current_name': current_name, - 'id': self.request_counter, 'stored_files': book.stored_files, 'query': query + 'id': self.request_counter, 'stored_files': book.stored_files, 'query': query }) + def on_worker_message(self, evt): + msg = evt.data + if msg.type is 'error': + pass + elif msg.id is self.search_in_flight_id: + if msg.type is 'search_complete': + self.search_in_flight_id = None + def clear_caches(self): if self._worker: self.worker.postMessage({'type': 'clear_caches'}) diff --git a/src/pyj/read_book/search_worker.pyj b/src/pyj/read_book/search_worker.pyj index 3a9ffb8438..ea863c56d5 100644 --- a/src/pyj/read_book/search_worker.pyj +++ b/src/pyj/read_book/search_worker.pyj @@ -100,9 +100,9 @@ def search_in_text_of(name): result = { 'file_name': name, 'spine_idx': spine_idx, 'index': match_counts[q], 'text': text, 'before': before, 'after': after, 'mode': wc.current_query.query.mode, - 'q': q, 'result_num': wc.result_num, 'on_discovery': wc.query_id, 'query_id': wc.query_id + 'q': q, 'result_num': wc.result_num, 'on_discovery': wc.current_query_id, } - self.postMessage({'type': 'search_result', 'result': result}) + self.postMessage({'type': 'search_result', 'id': wc.current_query_id, 'result': result}) match_counts[q] += 1