From 8beb332548a81c266415066fa30d7bdb99b12769 Mon Sep 17 00:00:00 2001 From: FragginWagon Date: Wed, 28 Jan 2026 20:35:25 +0000 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=8D=20Add=20search=20functionality=20i?= =?UTF-8?q?n=20the=20worker=20script?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/workers/search.worker.js | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 code/websites/pokedex.online/src/workers/search.worker.js diff --git a/code/websites/pokedex.online/src/workers/search.worker.js b/code/websites/pokedex.online/src/workers/search.worker.js new file mode 100644 index 0000000..ad2da2e --- /dev/null +++ b/code/websites/pokedex.online/src/workers/search.worker.js @@ -0,0 +1,51 @@ +/** + * Search Worker + * Offloads search operations to a background thread to avoid blocking the UI + */ + +// Listen for search requests from main thread +self.onmessage = (event) => { + const { lines, searchTerm, id } = event.data; + + if (!searchTerm || !lines) { + self.postMessage({ type: 'complete', id, results: [] }); + return; + } + + try { + const results = []; + const lowerSearchTerm = searchTerm.toLowerCase(); + const chunkSize = 1000; // Process in chunks and send updates + + // Process lines and find matches + for (let i = 0; i < lines.length; i++) { + if (lines[i].toLowerCase().includes(lowerSearchTerm)) { + results.push(i); + } + + // Send progress updates every chunk + if ((i + 1) % chunkSize === 0) { + self.postMessage({ + type: 'progress', + id, + percent: ((i + 1) / lines.length) * 100, + foundSoFar: results.length + }); + } + } + + // Send final results + self.postMessage({ + type: 'complete', + id, + results, + totalMatches: results.length + }); + } catch (error) { + self.postMessage({ + type: 'error', + id, + error: error.message + }); + } +};