Is it possible to create a Angular library within a nx workspace that uses a web worker?

Viewed 294

I want to create a Angular library within my nx workspace that utilizes a web worker internally.

e.g. Angular library containing

  • a angular module called FibonacciModule
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { FibonacciService } from './fibonacci.service';

@NgModule({
  imports: [CommonModule],
  providers: [FibonacciService],
})
export class FibonacciModule {}

  • a angular service called FibonacciService
import { Injectable } from '@angular/core';

@Injectable({
  providedIn: 'root',
})
export class FibonacciService {
  private worker: Worker;

  constructor() {
    this.worker = new Worker('./lib.worker', { type: 'module' });
    this.worker.onmessage = () => {
      // ...
    };
  }
  calculate(num: number) {
    this.worker.postMessage(num);
    // ...
  }
}

  • a web worker contains a lib.worker.ts and a fibonacci.ts

If I create a new web worker via Angular schematics the angular.json get updated with a new property "webWorkerTsConfig": "apps/my-app/tsconfig.worker.json".

But if I add this property to a angular library I get an error. Is it possible to use web workers with a Angular library within a NX workspace? In my opinion it makes sense to encapsulate a web worker in a library.

0 Answers
Related