Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Importing multiple Web Workers mixes them up #15188

Closed
ghost opened this issue Jul 29, 2019 · 3 comments · Fixed by GoogleChromeLabs/worker-plugin#35 or #15456
Closed

Importing multiple Web Workers mixes them up #15188

ghost opened this issue Jul 29, 2019 · 3 comments · Fixed by GoogleChromeLabs/worker-plugin#35 or #15456

Comments

@ghost
Copy link

ghost commented Jul 29, 2019

🐞 bug report

Affected Package

Maybe the bundler.

Is this a regression?

Angular versions <7 didn't have built-in support for Web Workers.

Description

I import multiple Web Workers but when messaging one specific, the messages gets sent and received to/from the wrong Web Worker.

Simple code repro from fresh install:

// foo.worker.ts
/// <reference lib="webworker" />

addEventListener('message', () => postMessage('Hello from FooWorker'));
// bar.worker.ts
/// <reference lib="webworker" />

addEventListener('message', () => postMessage('Hello from BarWorker'));
// baz.worker.ts
/// <reference lib="webworker" />

addEventListener('message', () => postMessage('Hello from BazWorker'));
// app.component.ts (library imports omitted)
const fooWorker = new Worker('./foo.worker', { type: 'module' });
const barWorker = new Worker('./bar.worker', { type: 'module' });
const bazWorker = new Worker('./baz.worker', { type: 'module' });

@Component({
  selector: 'app-root',
  template: '<h1>foo: {{ foo }}</h1>',
})
export class AppComponent implements OnInit {
  foo: string;

  ngOnInit() {
    fooWorker.addEventListener('message', msg => {
    	this.foo = msg.data;
    });
    fooWorker.postMessage('ANY INPUT VALUE');
  }
}

Expected output:

foo: Hello from FooWorker

Actual output:

foo: Hello from BazWorker
  • Commenting out the line const bazWorker.... causes the output to become foo: Hello from BarWorker (still wrong)
  • Commenting out both the lines const bazWorker.... and const barWorker.... yields the correct results

🔬 Minimal Reproduction

https://github.com/monocl-oskar/angular-web-worker-problem

🌍 Your Environment

Angular Version:


Angular CLI: 8.1.2
Node: 11.15.0
OS: darwin x64
Angular: 8.1.3
... animations, common, compiler, compiler-cli, core, forms
... language-service, platform-browser, platform-browser-dynamic
... router

Package                           Version
-----------------------------------------------------------
@angular-devkit/architect         0.801.2
@angular-devkit/build-angular     0.801.2
@angular-devkit/build-optimizer   0.801.2
@angular-devkit/build-webpack     0.801.2
@angular-devkit/core              8.1.2
@angular-devkit/schematics        8.1.2
@angular/cli                      8.1.2
@ngtools/webpack                  8.1.2
@schematics/angular               8.1.2
@schematics/update                0.801.2
rxjs                              6.4.0
typescript                        3.4.5
webpack                           4.35.2

Anything else relevant?

  • I tried putting the Web Worker imports in three dedicated services to test a theory that the bundler didn't like them all imported in the same file - Same results
  • I tried moving the instantiation from the "root scope" (above the class declaration) into the constructor (using the Web Workers as private members instead) - Same results

Hoping I'm missing something obvious, thanks!

@ghost
Copy link
Author

ghost commented Jul 31, 2019

Found a workaround here #14582

const fooWorker = new Worker('./foo.worker', { name: 'foo', type: 'module' });
const barWorker = new Worker('./bar.worker', { name: 'bar', type: 'module' });
const bazWorker = new Worker('./baz.worker', { name: 'baz', type: 'module' });

The unique names solve the problem.

@developit
Copy link
Contributor

Worker Plugin 3.2.0 has been published and includes a fix for this.

@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Sep 29, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.