Skip to content

Commit

Permalink
Consistent worker iterations (#37208)
Browse files Browse the repository at this point in the history
* Consistent worker iterations

* Update files/en-us/web/api/worker/postmessage/index.md

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

* Update files/en-us/web/api/worker/postmessage/index.md

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

* Update files/en-us/web/api/web_workers_api/using_web_workers/index.md

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

* Update files/en-us/web/api/web_workers_api/using_web_workers/index.md

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

* Update files/en-us/web/api/sharedworker/sharedworker/index.md

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

* Update files/en-us/web/api/sharedworker/sharedworker/index.md

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

* Update files/en-us/web/api/messageevent/index.md

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

* Update files/en-us/web/api/messageevent/index.md

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

* Update files/en-us/web/api/sharedworker/index.md

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

* Update files/en-us/web/api/sharedworker/index.md

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

* Apply suggestions from code review

---------

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: Brian Smith <[email protected]>
  • Loading branch information
3 people authored Dec 31, 2024
1 parent 76ca8e9 commit 875215d
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 45 deletions.
15 changes: 6 additions & 9 deletions files/en-us/web/api/messageevent/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,12 @@ myWorker.port.start();
When the port is started, both scripts post messages to the worker and handle messages sent from it using `port.postMessage()` and `port.onmessage`, respectively:

```js
first.onchange = () => {
myWorker.port.postMessage([first.value, second.value]);
console.log("Message posted to worker");
};

second.onchange = () => {
myWorker.port.postMessage([first.value, second.value]);
console.log("Message posted to worker");
};
[first, second].forEach((input) => {
input.onchange = () => {
myWorker.port.postMessage([first.value, second.value]);
console.log("Message posted to worker");
};
});

myWorker.port.onmessage = (e) => {
result1.textContent = e.data;
Expand Down
15 changes: 6 additions & 9 deletions files/en-us/web/api/sharedworker/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,12 @@ When the port is started, both scripts post messages to the worker and handle me
> You can use browser devtools to debug your SharedWorker, by entering a URL in your browser address bar to access the devtools workers inspector; for example, in Chrome, the URL `chrome://inspect/#workers`, and in Firefox, the URL `about:debugging#workers`.
```js
first.onchange = () => {
myWorker.port.postMessage([first.value, second.value]);
console.log("Message posted to worker");
};

second.onchange = () => {
myWorker.port.postMessage([first.value, second.value]);
console.log("Message posted to worker");
};
[first, second].forEach((input) => {
input.onchange = () => {
myWorker.port.postMessage([first.value, second.value]);
console.log("Message posted to worker");
};
});

myWorker.port.onmessage = (e) => {
result1.textContent = e.data;
Expand Down
15 changes: 6 additions & 9 deletions files/en-us/web/api/sharedworker/sharedworker/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,12 @@ const myWorker = new SharedWorker("worker.js");

myWorker.port.start();

first.onchange = () => {
myWorker.port.postMessage([first.value, second.value]);
console.log("Message posted to worker");
};

second.onchange = () => {
myWorker.port.postMessage([first.value, second.value]);
console.log("Message posted to worker");
};
[first, second].forEach((input) => {
input.onchange = () => {
myWorker.port.postMessage([first.value, second.value]);
console.log("Message posted to worker");
};
});

myWorker.port.onmessage = (e) => {
result1.textContent = e.data;
Expand Down
15 changes: 6 additions & 9 deletions files/en-us/web/api/web_workers_api/using_web_workers/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,12 @@ const myWorker = new Worker("worker.js");
The magic of workers happens via the {{domxref("Worker.postMessage", "postMessage()")}} method and the {{domxref("Worker.message_event", "onmessage")}} event handler. When you want to send a message to the worker, you post messages to it like this ([main.js](https://github.com/mdn/dom-examples/blob/main/web-workers/simple-web-worker/main.js)):
```js
first.onchange = () => {
myWorker.postMessage([first.value, second.value]);
console.log("Message posted to worker");
};
second.onchange = () => {
myWorker.postMessage([first.value, second.value]);
console.log("Message posted to worker");
};
[first, second].forEach((input) => {
input.onchange = () => {
myWorker.port.postMessage([first.value, second.value]);
console.log("Message posted to worker");
};
});
```
So here we have two {{htmlelement("input")}} elements represented by the variables `first` and `second`; when the value of either is changed, `myWorker.postMessage([first.value,second.value])` is used to send the value inside both to the worker, as an array. You can send pretty much anything you like in the message.
Expand Down
15 changes: 6 additions & 9 deletions files/en-us/web/api/worker/postmessage/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,12 @@ The following code snippet shows the creation of a {{domxref("Worker")}} object
```js
const myWorker = new Worker("worker.js");

first.onchange = () => {
myWorker.postMessage([first.value, second.value]);
console.log("Message posted to worker");
};

second.onchange = () => {
myWorker.postMessage([first.value, second.value]);
console.log("Message posted to worker");
};
[first, second].forEach((input) => {
input.onchange = () => {
myWorker.port.postMessage([first.value, second.value]);
console.log("Message posted to worker");
};
});
```

For a full example, see our [simple worker example](https://github.com/mdn/dom-examples/tree/main/web-workers/simple-web-worker) ([run example](https://mdn.github.io/dom-examples/web-workers/simple-web-worker/)).
Expand Down

0 comments on commit 875215d

Please sign in to comment.