
class Worker {

    constructor(script) {
        if (window.Worker) {
            this.worker = new window.Worker(script);
            this.worker.onmessage = this.onmessage()
            this.listeners = {}

            this.post({
                msg: 'init'
            })



        }
    }

    listenTo(ev, callback) {
        this.listeners[ev] = callback
    }


    onmessage() {
        let self = this
        return function(e) {
            if ((e.data.msg in self.listeners) === true) {
                self.listeners[e.data.msg](e)

            }
        }
    }

    post(m) {
        this.worker.postMessage(m)
    }
}

const workers = {
    'main': new Worker('./worker.js')
}





function getWorker(name){
    if (name in workers) {
        return workers[name];
    } else {
        console.log(`unknown worker ${name}`)
    }
}


export default getWorker;
