diff --git a/src/pyj/read_book/db.pyj b/src/pyj/read_book/db.pyj index 579d47cbd3..da645e24e4 100644 --- a/src/pyj/read_book/db.pyj +++ b/src/pyj/read_book/db.pyj @@ -360,7 +360,7 @@ class DB: mt = fdata.mimetype or 'application/octet-stream' if fdata.encoded: result = Blob([base64decode(result)], {'type':mt}) - proceed({'ok': True, 'result': result, 'name': name, 'mt': mt}) + proceed({'ok': True, 'result': req.result, 'name': name, 'mt': mt}) req.onerror = def(event): details = get_error_details(event) proceed({'ok': False, 'name': name, 'details': details}) diff --git a/src/pyj/read_book/search.pyj b/src/pyj/read_book/search.pyj index a5b874e5bb..25b9785f6c 100644 --- a/src/pyj/read_book/search.pyj +++ b/src/pyj/read_book/search.pyj @@ -53,14 +53,18 @@ class SearchOverlay: self._worker = start_worker('read_book.search') return self._worker - def queue_search(self, book, current_name): + def queue_search(self, query, book, current_name): spine = book.manifest.spine self.request_counter += 1 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 + 'id': self.request_counter, 'stored_files': book.stored_files, 'query': query }) + def clear_caches(self): + if self._worker: + self.worker.postMessage({'type': 'clear_caches'}) + def onkeydown(self, event): if event.key is 'Escape' or event.key is 'Esc': self.hide() diff --git a/src/pyj/read_book/search_worker.pyj b/src/pyj/read_book/search_worker.pyj index d80bfc403e..fd99242aba 100644 --- a/src/pyj/read_book/search_worker.pyj +++ b/src/pyj/read_book/search_worker.pyj @@ -3,6 +3,7 @@ from __python__ import bound_methods, hash_literals from read_book.db import DB +from read_book.resources import text_from_serialized_html GET_SPINE_FAILED = 1 CONNECT_FAILED = 2 @@ -19,11 +20,15 @@ class Worker: self.searching = False self.current_query = None self.current_query_id = None + self.text_cache = {} @property def initialize_error_msg(self): return self.db?.initialize_error_msg + def clear_caches(self): + self.text_cache = {} + wc = Worker() @@ -33,11 +38,19 @@ def send_search_complete(): wc.current_query = wc.current_query_id = None +def search_in_text_of(name): + pass + + def queue_next_spine_item(spine_idx): name = wc.current_query.spine[spine_idx] if not name: send_search_complete() return + if wc.text_cache[name]: + search_in_text_of(name) + setTimeout(queue_next_spine_item.bind(None, spine_idx + 1), 0) + return query = wc.current_query wc.db.get_book_file(query.book_hash, query.stored_files, name, got_spine_item.bind(None, query.id, spine_idx)) @@ -46,7 +59,10 @@ def got_spine_item(query_id, spine_idx, result): if query_id is not wc.current_query_id: return if result.ok: - queue_next_spine_item(spine_idx + 1) + name = wc.current_query.spine[spine_idx] + wc.text_cache[name] = text_from_serialized_html(result.result) + search_in_text_of(name) + setTimeout(queue_next_spine_item.bind(None, spine_idx + 1), 0) else: if result.details is 'ENOENT': queue_next_spine_item(spine_idx + 1) @@ -97,6 +113,8 @@ def worker_main(): perform_search(e.data) else: wc.pending_search = e.data + elif e.data.type is 'clear_caches': + wc.clear_caches() self.onerror = def(e): send_error(UNHANDLED_ERROR, f'{e.line}:{e.message}') diff --git a/src/pyj/read_book/view.pyj b/src/pyj/read_book/view.pyj index 10e5de82a2..f7f43e7d6b 100644 --- a/src/pyj/read_book/view.pyj +++ b/src/pyj/read_book/view.pyj @@ -932,6 +932,7 @@ class View: self.loaded_resources = {} self.content_popup_overlay.loaded_resources = {} self.timers.start_book(book) + self.search_overlay.clear_caches() unkey = username_key(get_interface_data().username) self.book = current_book.book = book hl = None