From 3100a3c7682fa785e4ea50151a9d2c394d4df222 Mon Sep 17 00:00:00 2001 From: Lei OT Date: Tue, 10 Dec 2024 10:45:44 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20web=20worker=20=E5=9C=A8=E6=9E=84?= =?UTF-8?q?=E5=BB=BA=E7=89=88=E6=9C=AC=E6=8A=A5=E9=94=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/views/AuthApp.jsx | 24 ++++++++++++++++-------- src/workers/fetchEmailWorker.js | 9 +++++++-- 2 files changed, 23 insertions(+), 10 deletions(-) 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 }) +}