Spaces:
Sleeping
Sleeping
Upload guessedNamesWorker.js
Browse files- static/guessedNamesWorker.js +31 -0
static/guessedNamesWorker.js
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Log when the worker is loaded to confirm it's running
|
| 2 |
+
console.log('Worker script loaded');
|
| 3 |
+
|
| 4 |
+
// Function to handle messages sent to the worker
|
| 5 |
+
onmessage = function(event) {
|
| 6 |
+
console.log('Worker received message:', event.data); // Log when the worker receives a message
|
| 7 |
+
|
| 8 |
+
if (event.data === 'fetch') {
|
| 9 |
+
console.log('Worker is now fetching data...');
|
| 10 |
+
|
| 11 |
+
// Fetch data from the server
|
| 12 |
+
fetch('/getguessednamepath/')
|
| 13 |
+
.then((response) => {
|
| 14 |
+
if (!response.ok) {
|
| 15 |
+
console.error('Network response was not ok');
|
| 16 |
+
throw new Error('Network response was not ok');
|
| 17 |
+
}
|
| 18 |
+
return response.json(); // Parse the response as JSON
|
| 19 |
+
})
|
| 20 |
+
.then((data) => {
|
| 21 |
+
console.log('Data fetched by worker:', data); // Log the fetched data
|
| 22 |
+
postMessage({ status: 'success', data }); // Send the fetched data back to the main thread
|
| 23 |
+
})
|
| 24 |
+
.catch((error) => {
|
| 25 |
+
console.error('Error fetching data in worker:', error); // Log any errors in the worker
|
| 26 |
+
postMessage({ status: 'error', error: error.message }); // Send error message back to the main thread
|
| 27 |
+
});
|
| 28 |
+
} else {
|
| 29 |
+
console.log('Unknown message received by worker:', event.data); // Log any unknown messages
|
| 30 |
+
}
|
| 31 |
+
};
|