fix: web worker 在构建版本报错

dev/emitter
Lei OT 10 months ago
parent 2f3b594012
commit 3100a3c768

@ -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(() => {

@ -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 })
}

Loading…
Cancel
Save