diff --git a/src/views/AuthApp.jsx b/src/views/AuthApp.jsx index 994fa03..66054c3 100644 --- a/src/views/AuthApp.jsx +++ b/src/views/AuthApp.jsx @@ -25,9 +25,11 @@ import AppLogo from '@/assets/highlights_travel_300_300.png' import '@/assets/App.css' import 'react-chat-elements/dist/main.css' import EmailFetch from './Conversations/Online/Components/EmailFetch' -import { getEmailFetchAction } from '@/actions/EmailActions' +import FetchEmailWorker from './../workers/fetchEmailWorker?worker&url' + +// const fetchEmailWorkerURL = new URL('/src/workers/fetchEmailWorker.js', import.meta.url); +const fetchEmailWorker = new Worker(FetchEmailWorker, { type: 'module' }); -const fetchEmailWorkerURL = new URL('./../workers/fetchEmailWorker.js', import.meta.url); function AuthApp() { const navigate = useNavigate() @@ -54,28 +56,34 @@ function AuthApp() { } else { Notification.requestPermission() } - let fetchEmailWorker; + let _fetchEmailWorker; if (loginUser.userId > 0) { appendRequestHeader('X-User-Id', loginUser.userId) loadPageSpy(loginUser.username) connectWebsocket(loginUser.userId) fetchInitialData(loginUser) - let fetchEmailWorker; + let _fetchEmailWorker; if (isPermitted(PERM_USE_EMAL)) { - fetchEmailWorker = startEmailInterval(loginUser.userId) + _fetchEmailWorker = startEmailInterval(loginUser.userId) } } return () => { disconnectWebsocket() - if (fetchEmailWorker) { - fetchEmailWorker.terminate(); + if (_fetchEmailWorker) { + _fetchEmailWorker.terminate(); } } }, []) const startEmailInterval = (userId) => { - const fetchEmailWorker = new Worker(fetchEmailWorkerURL, { type: 'module' }); + // const fetchEmailWorker = new Worker(fetchEmailWorkerURL, { type: 'module' }); + fetchEmailWorker.onerror = function(error) { + console.error('There was an error in the worker', error); + }; + fetchEmailWorker.onmessage = function(event) { + // console.log('Received message from worker', event.data, event.message); + }; fetchEmailWorker.postMessage({ command: 'fetchEmail', param: { opi_sn: userId } }); return fetchEmailWorker; // setInterval(() => { diff --git a/src/workers/fetchEmailWorker.js b/src/workers/fetchEmailWorker.js index 1c97d30..24ea289 100644 --- a/src/workers/fetchEmailWorker.js +++ b/src/workers/fetchEmailWorker.js @@ -1,10 +1,10 @@ -import { getEmailFetchAction } from '@/actions/EmailActions' +import { getEmailFetchAction } from './../actions/EmailActions' self.onmessage = (event) => { if (event.data.command === 'fetchEmail') { // Get the parameter const param = event.data.param // { opi_sn: userId } - // console.log('Worker: Received command start with param:', param); + console.log('Worker: Received command `fetchEmail` with param:', param) // Do something...... setInterval(() => { @@ -12,3 +12,8 @@ self.onmessage = (event) => { }, 10 * 1000) // prod: 30 minutes } } +self.onerror = function (error) { + console.error('Error in worker', error) + // You can also post the error back to the main thread + postMessage({ error: error.message }) +}