Pass results back from worker to UI

This commit is contained in:
Kovid Goyal 2021-05-17 16:30:38 +05:30
parent 4ad95b38c2
commit a365ac1862
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 14 additions and 3 deletions

View File

@ -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'})

View File

@ -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