Files
memory-infrastructure-palace/code/websites/pokedex.online/src/workers/search.worker.js

67 lines
1.7 KiB
JavaScript

/**
* Search Worker
* Offloads search operations to a background thread to avoid blocking the UI
*/
console.log('🔧 Search worker initialized');
// Listen for search requests from main thread
self.onmessage = event => {
const { lines, searchTerm, id } = event.data;
console.log('📨 Worker received message:', {
linesCount: lines?.length,
searchTerm,
id
});
if (!searchTerm || !lines) {
console.warn('⚠️ Invalid search data received');
self.postMessage({ type: 'complete', id, results: [] });
return;
}
try {
console.log('🔍 Starting search for:', searchTerm);
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) {
const percent = ((i + 1) / lines.length) * 100;
console.log(
`📊 Progress: ${Math.round(percent)}% (${results.length} matches found)`
);
self.postMessage({
type: 'progress',
id,
percent,
foundSoFar: results.length
});
}
}
// Send final results
console.log(`✅ Search complete: Found ${results.length} matches`);
self.postMessage({
type: 'complete',
id,
results,
totalMatches: results.length
});
} catch (error) {
console.error('❌ Worker error during search:', error);
self.postMessage({
type: 'error',
id,
error: error.message
});
}
};