angular services and web workers

Viewed 263

I have a service that is being used for heavy computation in an Angular app. I'm using @injectable with the services' class. I'd like to use the class in a web worker as well, but it's throwing dom related errors. What's the best way to reuse the service code? I could write a plain class but would like to avoid duplicating the code as it's still needed inside the app.

I'd like to do something like this:

// web worker
import {Service} from 'app.service.ts';

addEventListener('message', ({ data }) => {
  const response = new Service().callServiceMethod(data);
  postMessage(response);
});
1 Answers

There may be a better answer than this as it's a bit of a workaround, but also requires minimal upkeep and the functionality is kept in the code:

I'm using a non-dom class and an angular class

..............
class NonDom {

exampleMethod() {
// do stuff
 }
}
.............

import NonDom from './NonDom';

class AngularWithDom {
const nonDomClass = new NonDom();

exampleMethod() {
 return nonDomClass.exampleMethod();
 }

domMethod() {
 // do stuff with dom here 
 }
}
Related