We are currently running a time-consuming function on a web worker. In addition, we have a Dispatcher Class that creates a single instance for other classes to utilize, as shown below:
class Dispatcher {
constructor() {
console.log("calling");
this.events = {};
}
//some code
}
const dispatcher = new Dispatcher();
export default dispatcher;
The above module has been imported into another file named DataManager Class at the beginning:
import dispatcher from '../../utility/dispatcher';
export class DataManager {
notifyRenderer = (data: ResultData): void => {
dispatcher.dispatch(NOTIFY_EVENT, data);
}
}
Within our web worker, a new instance of the DataManager Class is created, and the notifyRenderer method within the DataManager Class is triggered from there.
import { DataManager } from "./data-manager";
let dm: DataManager;
addEventListener('message', (e) => {
if (!dm) {
dm = new DataManager();
}
const res = dm.addOrUpdateData(e.data.input, true);
dm.notifyRenderer(res);
postMessage({ type: 'Dispatch', res });
}, false);
Additionally, I have included a screenshot displaying the console.log("calling") message appearing twice. The reason behind why the Dispatch class constructor is called multiple times remains unclear.
https://i.sstatic.net/MZuoc.png
I am seeking assistance in resolving this issue. Your help is highly appreciated.
Could the problem be related to how modules are being imported?
Here is a screenshot trace from the worker:
https://i.sstatic.net/IVsUj.png
Your support is greatly valued!