Skip to content
This repository was archived by the owner on May 29, 2023. It is now read-only.

Commit a78a26a

Browse files
committed
docs: update examples
1 parent ef49ea9 commit a78a26a

File tree

1 file changed

+27
-2
lines changed

1 file changed

+27
-2
lines changed

README.md

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ npm i @ng-web-apis/workers
2828

2929
## How it use
3030

31-
You can create worker and use it in a template with `AsyncPipe`:
31+
Create a worker and use it in a template with `AsyncPipe`:
3232

3333
```typescript
3434
import {WebWorker} from '@ng-web-apis/workers';
@@ -39,7 +39,8 @@ function compute(data: number): number {
3939

4040
@Component({
4141
template: `
42-
Computed Result: {{ worker | async }}
42+
Computed Result:
43+
<ng-container *ngIf="worker | async as event">{{ event.data }}</ng-container>
4344
<form (ngSubmit)="worker.postMessage(input.value)">
4445
<input #input />
4546
<button type="submit">Send to worker</button>
@@ -51,6 +52,30 @@ class SomeComponent {
5152
}
5253
```
5354

55+
To get data from the worker event, use the toData operator
56+
57+
```typescript
58+
import {toData, WebWorker} from '@ng-web-apis/workers';
59+
60+
function compute(data: number): number {
61+
return data ** 2;
62+
}
63+
64+
@Component({
65+
template: `
66+
Computed Result: {{ workerData$ | async }}
67+
<form (ngSubmit)="worker.postMessage(input.value)">
68+
<input #input />
69+
<button type="submit">Send to worker</button>
70+
</form>
71+
`,
72+
})
73+
class SomeComponent {
74+
readonly worker = WebWorker.fromFunction<number, number>(compute);
75+
readonly workerData$ = this.worker.pipe(toData());
76+
}
77+
```
78+
5479
It's the same with `WorkerPipe` only:
5580

5681
```typescript

0 commit comments

Comments
 (0)