We are executing a time taking function on web worker. Also I have a Dispatcher Class that create a single instance for other classes to use looks like below:
class Dispatcher {
constructor() {
console.log("calling");
this.events = {};
}
//some code
}
const dispatcher = new Dispatcher();
export default dispatcher;
I have imported this module in another file named as DataManager Class at the top:
import dispatcher from '../../utility/dispatcher';
export class DataManager {
notifyRenderer = (data: ResultData): void => {
dispatcher.dispatch(NOTIFY_EVENT, data);
}
}
and my web worker is creating a new instance of this class and from here we are triggering a notifyRenderer method written in DataManager Class.
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);
Also I am attaching a screenshot of my console showing console.log("calling"); two times. I have no idea why Dispatch class constructor is getting called two times.
Is there any mistake i am doing. Need help on this please.
I feel like module imports might be the problem. Is it?
Adding screenshot trace from worker:
Thank you in Advance!