Passing a Typescript class to a SharedWorker to create the worker

727 views Asked by At

When creating SharedWorkers you would pass the JS file inside the SharedWorker constructor such as

var worker = new SharedWorker('some_worker_file.js');

That file will contain a line that gets executed right away as its not inside any function. That line would be something like -

this.addEventlistener('connect', function(e) { ... handler code ... } );

Now how do you do something similar with Typescript? You aren't calling new XXX so placing the code inside a constructor is pointless. What's the best way to create a class with Typescript to do this - is it possible?

I'm wondering if the only way to do this is to create a simple JS file with the connect listener to set up the worker and also have that simple JS file load in the transpiled TS files as imports.

0

There are 0 answers