DEV Community

Discussion on: Observable Web Workers with Angular (8) - Introduction

Collapse
 
bboyle profile image
Ben Boyle

This sounds great Zak, thanks for sharing!

I'm confused though. I think I followed these steps right, but the worker threads aren't being terminated. Did I miss something? github.com/bboyle/observable-workers

Collapse
 
bboyle profile image
Ben Boyle

seems to be the version of observable-workers. I see the worker threads being terminated if I use version 3.0.1. doesn't seem to be an issue with the advanced blog posts

Collapse
 
jzabinskidolios profile image
jzabinski-dolios • Edited

You have to explicitly call subscription.unsubscribe() in order for the workers to be torn down. In other words, this:

  runWorker() {
    const input$: Observable<string> = of('hello');

   fromWorker<string, string>(
      () => new Worker(new URL('./app.worker', import.meta.url), { type: 'module' }),
      input$
    ).subscribe((message) => {
      console.log(`Got message`, message);
    });
Enter fullscreen mode Exit fullscreen mode

Really needs to be this:

subscription = new Subscription();

  runWorker() {
    const input$: Observable<string> = of('hello');

    this.subscription.add(fromWorker<string, string>(
      () => new Worker(new URL('./app.worker', import.meta.url), { type: 'module' }),
      input$
    ).subscribe((message) => {
      console.log(`Got message`, message);
      ==> this.subscription.unsubscribe(); <==
    })
    );
Enter fullscreen mode Exit fullscreen mode