30 lines
626 B
JavaScript
30 lines
626 B
JavaScript
|
class EventHandler {
|
||
|
constructor(worker) {
|
||
|
this.events = {};
|
||
|
this.worker = worker;
|
||
|
this.worker.addEventListener('message', this.handleEvent.bind(this));
|
||
|
}
|
||
|
|
||
|
addEvent(name, cb) {
|
||
|
this.events[name] = cb;
|
||
|
}
|
||
|
|
||
|
sendData(name, status, data) {
|
||
|
this.worker.postMessage({
|
||
|
cmd: name,
|
||
|
status: status,
|
||
|
data: data
|
||
|
});
|
||
|
}
|
||
|
|
||
|
handleEvent(event) {
|
||
|
let data = event.data;
|
||
|
if (!data.cmd) {
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
if (this.events[data.cmd]) {
|
||
|
this.events[data.cmd](data.data);
|
||
|
}
|
||
|
}
|
||
|
}
|