-
Notifications
You must be signed in to change notification settings - Fork 29.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
worker: add ability to unshift message from MessagePort
In combination with Atomics, this makes it possible to implement generic synchronous functionality, e.g. `importScript()`, in Workers purely by communicating with other threads. This is a continuation of #26686, where a preference for a solution was voiced that allowed reading individual messages, rather than emitting all messages through events. PR-URL: #27294 Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Gus Caplan <me@gus.host> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
- Loading branch information
Showing
7 changed files
with
139 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
'use strict'; | ||
const common = require('../common'); | ||
const assert = require('assert'); | ||
const { MessageChannel, receiveMessageOnPort } = require('worker_threads'); | ||
|
||
const { port1, port2 } = new MessageChannel(); | ||
|
||
const message1 = { hello: 'world' }; | ||
const message2 = { foo: 'bar' }; | ||
|
||
// Make sure receiveMessageOnPort() works in a FIFO way, the same way it does | ||
// when we’re using events. | ||
assert.deepStrictEqual(receiveMessageOnPort(port2), undefined); | ||
port1.postMessage(message1); | ||
port1.postMessage(message2); | ||
assert.deepStrictEqual(receiveMessageOnPort(port2), { message: message1 }); | ||
assert.deepStrictEqual(receiveMessageOnPort(port2), { message: message2 }); | ||
assert.deepStrictEqual(receiveMessageOnPort(port2), undefined); | ||
assert.deepStrictEqual(receiveMessageOnPort(port2), undefined); | ||
|
||
// Make sure message handlers aren’t called. | ||
port2.on('message', common.mustNotCall()); | ||
port1.postMessage(message1); | ||
assert.deepStrictEqual(receiveMessageOnPort(port2), { message: message1 }); | ||
port1.close(); |