docs: update and edit web-worker page (#36026)

PR Close #36026
This commit is contained in:
Judy Bogart 2020-03-11 10:14:10 -07:00 committed by Alex Rickabaugh
parent e1ac2efe4a
commit 51a89c32c4
2 changed files with 28 additions and 15 deletions

View File

@ -1,17 +1,31 @@
# Using web workers with Angular CLI # Background processing using web workers
[Web workers](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API) allow you to run CPU intensive computations in a background thread, freeing the main thread to update the user interface. [Web workers](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API) allow you to run CPU-intensive computations in a background thread,
freeing the main thread to update the user interface.
If you find your application performs a lot of computations, such as generating CAD drawings or doing heavy geometrical calculations, using web workers can help increase your application's performance.
If you find your application becomes unresponsive while processing data, using web workers can help. <div class="alert is-helpful>
The CLI does not support running Angular itself in a web worker.
</div>
## Adding a web worker ## Adding a web worker
You can add a web worker anywhere in your application. If the file that contains your expensive computation is `src/app/app.component.ts`, you can add a web worker using `ng generate web-worker app`. To add a web worker to an existing project, use the Angular CLI `ng generate` command.
Running this command will: `ng generate web-worker` *location*
- configure your project to use web workers, if it isn't already. You can add a web worker anywhere in your application.
- add `src/app/app.worker.ts` with scaffolded code to receive messages: For example, to add a web worker to the root component, `src/app/app.component.ts`, run the following command.
`ng generate web-worker app`.
The command performs the following actions.
- Configures your project to use web workers, if it isn't already.
- Adds the following scaffold code to `src/app/app.worker.ts` to receive messages.
<code-example language="typescript" header="src/app/app.worker.ts"> <code-example language="typescript" header="src/app/app.worker.ts">
addEventListener('message', ({ data }) => { addEventListener('message', ({ data }) => {
@ -20,7 +34,7 @@ Running this command will:
}); });
</code-example> </code-example>
- add scaffolded code to `src/app/app.component.ts` to use the worker: - Adds the following scaffold code to `src/app/app.component.ts` to use the worker.
<code-example language="typescript" header="src/app/app.component.ts"> <code-example language="typescript" header="src/app/app.component.ts">
if (typeof Worker !== 'undefined') { if (typeof Worker !== 'undefined') {
@ -36,11 +50,10 @@ Running this command will:
} }
</code-example> </code-example>
After the initial scaffolding, you will need to refactor your code to use the web worker by sending messages to and from. After you generate this initial scaffold, you must refactor your code to use the web worker by sending messages to and from the worker.
## Caveats <div class="alert is-important>
There are two important things to keep in mind when using web workers in Angular projects: Some environments or platforms, such as `@angular/platform-server` used in [Server-side Rendering](guide/universal), don't support web workers. To ensure that your application will work in these environments, you must provide a fallback mechanism to perform the computations that the worker would otherwise perform.
- Some environments or platforms, like `@angular/platform-server` used in [Server-side Rendering](guide/universal), don't support web workers. You have to provide a fallback mechanism to perform the computations that the worker would perform to ensure your application will work in these environments. </div>
- Running Angular itself in a web worker via [**@angular/platform-webworker**](api/platform-webworker) is not yet supported in Angular CLI.

View File

@ -583,8 +583,8 @@
}, },
{ {
"url": "guide/web-worker", "url": "guide/web-worker",
"title": "Web Workers", "title": "Web Workers in Angular",
"tooltip": "Using Web Workers with Angular CLI." "tooltip": "Implement background processing with web workers."
} }
] ]
}, },