mirror of
https://github.com/nodejs/node.git
synced 2025-04-30 23:56:58 +00:00

Having an experimental feature behind a flag makes change if we are expecting significant breaking changes to its API. Since the Worker API has been essentially stable since its initial introduction, and no noticeable doubt about possibly not keeping the feature around has been voiced, removing the flag and thereby reducing the barrier to experimentation, and consequently receiving feedback on the implementation, seems like a good idea. PR-URL: https://github.com/nodejs/node/pull/25361 Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Yuta Hiroto <hello@hiroppy.me> Reviewed-By: Shingo Inoue <leko.noor@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com> Reviewed-By: Tobias Nießen <tniessen@tnie.de> Reviewed-By: Masashi Hirano <shisama07@gmail.com> Reviewed-By: Weijia Wang <starkwang@126.com> Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com> Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
46 lines
1.3 KiB
JavaScript
46 lines
1.3 KiB
JavaScript
'use strict';
|
|
const common = require('../common');
|
|
const assert = require('assert');
|
|
const { MessageChannel, MessagePort, Worker } = require('worker_threads');
|
|
|
|
{
|
|
const channel = new MessageChannel();
|
|
|
|
channel.port1.on('message', common.mustCall(({ typedArray }) => {
|
|
assert.deepStrictEqual(typedArray, new Uint8Array([0, 1, 2, 3, 4]));
|
|
}));
|
|
|
|
const typedArray = new Uint8Array([0, 1, 2, 3, 4]);
|
|
channel.port2.postMessage({ typedArray }, [ typedArray.buffer ]);
|
|
assert.strictEqual(typedArray.buffer.byteLength, 0);
|
|
channel.port2.close();
|
|
}
|
|
|
|
{
|
|
const channel = new MessageChannel();
|
|
|
|
channel.port1.on('close', common.mustCall());
|
|
channel.port2.on('close', common.mustCall());
|
|
channel.port2.close();
|
|
}
|
|
|
|
{
|
|
const channel = new MessageChannel();
|
|
|
|
const w = new Worker(`
|
|
const { MessagePort } = require('worker_threads');
|
|
const assert = require('assert');
|
|
require('worker_threads').parentPort.on('message', ({ port }) => {
|
|
assert(port instanceof MessagePort);
|
|
port.postMessage('works');
|
|
});
|
|
`, { eval: true });
|
|
w.postMessage({ port: channel.port2 }, [ channel.port2 ]);
|
|
assert(channel.port1 instanceof MessagePort);
|
|
assert(channel.port2 instanceof MessagePort);
|
|
channel.port1.on('message', common.mustCall((message) => {
|
|
assert.strictEqual(message, 'works');
|
|
w.terminate();
|
|
}));
|
|
}
|