If some page A sends a sequence of messages to page B that listens for and handles them, does the event listener process those messages in a serial fashion? If it does so in concurrent fashion, I have to be careful about data races, don't I?
In particular, consider some page A (at https://a.example.com/index.html
):
<script>
const win = open('https://b.example.com/index.html');
setTimeout(() => {
for (let i = 0; i < 1024; i++) {
win.postMessage(i, '*');
}
}, 1000);
</script>
and page B (at https://b.example.com/index.html
):
<script>
let count = 0;
window.addEventListener('message', () => {count++});
</script>
When all messages have been processed, is page B's count
variable guaranteed to have value 1024
? Or is there no such guarantee, because messages are being processed in a concurrent manner and updates to the count
variable may be lost?
The Web Messaging standard is not the easiest to read... It does mention queues, which seem to indicate serial (not concurrent) handling of messages, but I'd like to be sure.