|
1 | 1 | # WorkerDOM
|
2 | 2 |
|
3 |
| -An in-progress implementation of the DOM API intended to run within a Web Worker. |
| 3 | +## What's difference from Original? |
4 | 4 |
|
5 |
| -**Purpose**: Move complexity of intermediate work related to DOM mutations to a background thread, sending only the necessary manipulations to a foreground thread. |
| 5 | +- Define the useful APIs from [@mizchi/worker-dom](https://github.com/mizchi/worker-dom) |
| 6 | +- Can be modified the DOM with your Web Worker |
6 | 7 |
|
7 |
| -**Use Cases**: |
8 |
| -1. Embedded content from a third party living side by side with first party code. |
9 |
| -2. Mitigation of expensive rendering for content not requiring synchronous updates to user actions. |
10 |
| -3. Retaining main thread availablity for high priority updates by async updating elsewhere in a document. |
| 8 | +### Installation |
11 | 9 |
|
12 |
| -For more information, visit our [blog post](https://bit.ly/worker-dom-blog) announcing WorkerDOM or checkout the [slides](https://bit.ly/worker-dom-slides) from the announcement at JSConf US. |
13 |
| - |
14 |
| -## Installation |
15 |
| - |
16 |
| -```bash |
17 |
| -npm install @ampproject/worker-dom |
| 10 | +```sh |
| 11 | +npm install @intlify/worker-dom --save |
18 | 12 | ```
|
19 | 13 |
|
20 |
| -## Usage |
| 14 | +### APIs |
| 15 | +- `attachWorker(element: HTMLElement, worker: Worker): Promise<ExportedWoker>` on main-thread |
| 16 | +- `ready` on worker to connect `attachWorker`. |
21 | 17 |
|
22 |
| -WorkerDOM comes in two flavours, a global variant and a module variant. It is possible to include the WorkerDOM main thread code within your document directly or via a bundler. Here's how you might do so directly: |
| 18 | +### Example for Vite |
23 | 19 |
|
24 |
| -```html |
25 |
| -<script src="path/to/workerdom/dist/main.mjs" type="module"></script> |
26 |
| -<script src="path/to/workerdom/dist/main.js" nomodule defer></script> |
27 |
| -``` |
| 20 | +```js |
| 21 | +// worker.js |
| 22 | +import { ready, exportFunction } from '@intlify/worker-dom/dist/lib/worker'; |
28 | 23 |
|
29 |
| -WorkerDOM allows us to upgrade a specific section of the document to be driven by a worker. For example, imagine a `div` node in the page like so: |
| 24 | +// define your functions on worker |
| 25 | +function add(a, b) { |
| 26 | + const ret = a + b |
| 27 | + const el = document.createElement('p') |
| 28 | + el.textContent = ret.toString() |
| 29 | + document.body.appendChild(el) |
| 30 | + return ret |
| 31 | +} |
30 | 32 |
|
31 |
| -```html |
32 |
| -<div src="hello-world.js" id="upgrade-me"></div> |
33 |
| -``` |
| 33 | +// export worker functions |
| 34 | +[add].map(fn => exportFunction(fn.name, fn)) |
34 | 35 |
|
35 |
| -To upgrade this node using the module version of the code, we can directly import `upgradeElement` and use it like this: |
| 36 | +// wait for ready |
| 37 | +await ready; |
36 | 38 |
|
37 |
| -```html |
38 |
| -<script type="module"> |
39 |
| - import {upgradeElement} from './dist/main.mjs'; |
40 |
| - upgradeElement(document.getElementById('upgrade-me'), './dist/worker/worker.mjs'); |
41 |
| -</script> |
| 39 | +// should keep same content with main-thread on init. |
| 40 | +document.body.firstChild.textContent = 'hello from worker mutate'; |
42 | 41 | ```
|
43 | 42 |
|
44 |
| -The nomodule format exposes the global `MainThread`, and could upgrade the `div` in the following way: |
45 |
| - |
46 |
| -```html |
47 |
| -<script nomodule async=false defer> |
48 |
| - document.addEventListener('DOMContentLoaded', function() { |
49 |
| - MainThread.upgradeElement(document.getElementById('upgrade-me'), './dist/worker/worker.js'); |
50 |
| - }, false); |
51 |
| -</script> |
52 |
| -``` |
53 |
| - |
54 |
| -### AMP Distribution for `amp-script` |
| 43 | +```js |
| 44 | +// main.js |
| 45 | +import { attachWorker } from '@intlify/worker-dom/dist/lib/main'; |
| 46 | +import Worker from './woker?worker'; |
55 | 47 |
|
56 |
| -WorkerDOM has a special output variant that supplies additional hooks for safety features e.g. HTML sanitization. This variant is distributed under the amp folder for main and worker thread binaries: |
| 48 | +// attach worker to dom |
| 49 | +const worker = await attachWorker(document.querySelector('#root'), new Worker()); |
57 | 50 |
|
| 51 | +// call function that is exported from worker |
| 52 | +const result = await woker.callFunction('add', 1, 1) |
58 | 53 | ```
|
59 |
| -amp/main.mjs |
60 |
| -amp/worker/worker.mjs |
61 |
| -``` |
62 |
| - |
63 |
| -This output assumes the consumer will compile this distributed JavaScript to ensure it works with older `user-agent`s. |
64 | 54 |
|
65 |
| -### Debug Distribution |
66 | 55 |
|
67 |
| -WorkerDOM also has an output variant that includes additional debugging messages. This variant is distributed in the debug folder. |
| 56 | +### Example for Webpack |
68 | 57 |
|
69 |
| -``` |
70 |
| -debug/main.mjs |
71 |
| -debug/main.js |
72 |
| -debug/worker/worker.mjs |
73 |
| -debug/worker/worker.js |
| 58 | +```js |
| 59 | +// webpack.config.js |
| 60 | +const WorkerPlugin = require("worker-plugin"); |
| 61 | +module.exports = { |
| 62 | + // ... |
| 63 | + plugins: [new WorkerPlugin()] |
| 64 | +} |
74 | 65 | ```
|
75 | 66 |
|
76 |
| -## Running Demos Locally |
| 67 | +```js |
| 68 | +// worker.js |
| 69 | +import { ready } from "@intlify/worker-dom/dist/lib/worker"; |
77 | 70 |
|
78 |
| -After cloning the repository, you can try out the debug demos with the following. |
79 |
| - |
80 |
| -```bash |
81 |
| -npm run demo |
| 71 | +ready.then(() =>{ |
| 72 | + // should keep same content with main-thread on init. |
| 73 | + document.body.firstChild.textContent = 'hello from worker mutate'; |
| 74 | +}); |
82 | 75 | ```
|
83 | 76 |
|
84 |
| -This script will build the current version of WorkerDOM and start up a local [webserver](http://localhost:3001). |
85 |
| - |
86 |
| -## Which JavaScript APIs can I use? |
87 |
| - |
88 |
| -Currently, most DOM elements and their properties are supported. DOM query APIs like `querySelector` have partial support. Browser APIs like History are not implemented yet. Please see the API support matrix [here](web_compat_table.md). |
| 77 | +```js |
| 78 | +// main.js |
| 79 | +import { attachWorker } from "@intlify/worker-dom/dist/lib/main"; |
89 | 80 |
|
90 |
| -## Which Browsers are supported? |
| 81 | +// Create worker by your own way |
| 82 | +const worker = new Worker("./worker.js", { type: "module" }); |
91 | 83 |
|
92 |
| -In general we support the latest two versions of major browsers like Chrome, Firefox, Edge, Safari, Opera and UC Browser. We support desktop, phone, tablet and the web view version of these respective browsers. |
93 |
| - |
94 |
| -Beyond that, we aim for very wide browser support and we accept fixes for all browsers with market share greater than 1 percent. |
95 |
| - |
96 |
| -In particular, we try to maintain "it might not be perfect but isn't broken"-support for IE 11, iOS 8, the Android 4.0 system browser and Chrome 41. |
97 |
| - |
98 |
| -## Local Development |
99 |
| - |
100 |
| -Local development of WorkerDOM assumes the following: |
101 |
| -1. Familiarity with `npm` or `yarn` |
102 |
| -2. Latest LTS release of Node (10 at time of writing) available. |
103 |
| -3. Comfort with TypeScript, the codebase and tests are entirely written in TypeScript. |
104 |
| - |
105 |
| -## Release Log |
106 |
| - |
107 |
| -Each release includes a log of changes with the newly released version. You can find the log here: https://github.com/ampproject/worker-dom/releases |
108 |
| - |
109 |
| -## Security disclosures |
110 |
| - |
111 |
| -The AMP Project accepts responsible security disclosures through the [Google Application Security program](https://www.google.com/about/appsecurity/). |
112 |
| - |
113 |
| -## Code of conduct |
114 |
| - |
115 |
| -The AMP Project strives for a positive and growing project community that provides a safe environment for everyone. All members, committers and volunteers in the community are required to act according to the [code of conduct](CODE_OF_CONDUCT.md). |
| 84 | +// attach worker to dom |
| 85 | +attachWorker(document.querySelector('#root'), worker); |
| 86 | +``` |
116 | 87 |
|
117 |
| -## License |
| 88 | +## LICENSE |
118 | 89 |
|
119 | 90 | worker-dom is licensed under the [Apache License, Version 2.0](LICENSE).
|
0 commit comments