18 lines
536 B
TypeScript
18 lines
536 B
TypeScript
|
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);
|
||
|
});
|
||
|
}
|
||
|
}
|