mlpcardgame/src/workers/runner.ts

18 lines
536 B
TypeScript
Raw Normal View History

2019-09-18 12:42:17 +00:00
import EventEmitter from "eventemitter3";
export class TaskRunner extends EventEmitter {
public class: any;
public instance: Worker;
constructor(taskName: string) {
super();
this.class = require(`worker-loader!@/workers/tasks/${taskName}`);
this.instance = new this.class() as Worker;
this.instance.addEventListener("error", ev => this.emit("error", ev));
this.instance.addEventListener("message", ev => {
const message = JSON.parse(ev.data);
this.emit(message.type, message.data);
});
}
}