4.11.1 • Published 8 months ago

@ng-web-apis/workers v4.11.1

Weekly downloads
-
License
Apache-2.0
Repository
github
Last release
8 months ago

ng-web-apis logo Web Workers API for Angular

npm version npm bundle size codecov

This is a library for comfortable use of Web Workers API with Angular.

Install

If you do not have @ng-web-apis/common:

npm i @ng-web-apis/common

Now install the package:

npm i @ng-web-apis/workers

How it use

Create a worker and use it in a template with AsyncPipe:

import {WebWorker} from '@ng-web-apis/workers';

function compute(data: number): number {
  return data ** 2;
}

@Component({
  template: `
    Computed Result:
    <ng-container *ngIf="worker | async as event">{{ event.data }}</ng-container>
    <form (ngSubmit)="worker.postMessage(input.value)">
      <input #input />
      <button type="submit">Send to worker</button>
    </form>
  `,
})
class My {
  readonly worker = WebWorker.fromFunction<number, number>(compute);
}

To get data from the worker event, use the toData operator

import {toData, WebWorker} from '@ng-web-apis/workers';

function compute(data: number): number {
  return data ** 2;
}

@Component({
  template: `
    Computed Result: {{ workerData$ | async }}
    <form (ngSubmit)="worker.postMessage(input.value)">
      <input #input />
      <button type="submit">Send to worker</button>
    </form>
  `,
})
class My {
  readonly worker = WebWorker.fromFunction<number, number>(compute);
  readonly workerData$ = this.worker.pipe(toData());
}

It's the same with WorkerPipe only:

import {WorkerModule} from '@ng-web-apis/workers';
import {NgModule} from '@angular/core';

@NgModule({
  imports: [WorkerModule],
  declarations: [My],
})
class SomeModule {}
import {WorkerExecutor, WebWorker} from '@ng-web-apis/workers';
import {FormControl} from '@angular/forms';

@Component({
  template: `
    Computed Result: {{ value | waWorker: changeData | async }}

    <input [(ngModel)]="value" />
  `,
})
class My {
  value: string;

  changeData(data: string): string {
    return `${data} (changed)`;
  }
}

See also

Other Web APIs for Angular by @ng-web-apis

4.11.0

8 months ago

4.11.1

8 months ago

4.9.0

8 months ago

4.8.0

8 months ago

4.6.5

9 months ago

4.7.0

8 months ago

4.10.1

8 months ago

4.10.2

8 months ago

4.10.0

8 months ago

4.6.4

9 months ago

4.6.3

9 months ago

4.6.2

9 months ago

4.6.1

9 months ago

4.6.0

9 months ago

4.5.1

9 months ago

4.5.0

9 months ago

4.3.0

9 months ago

3.2.2

11 months ago

3.2.1

12 months ago

3.2.3

10 months ago

4.1.3

11 months ago

4.1.0

12 months ago

4.0.1

12 months ago

4.0.0

12 months ago

4.2.1

10 months ago

4.1.2

12 months ago

4.2.0

11 months ago

4.1.1

12 months ago

3.0.3

2 years ago

3.0.2

2 years ago

3.0.6

2 years ago

3.0.5

2 years ago

1.1.0

2 years ago

3.0.1

2 years ago

3.0.0

2 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago