doc: fix module customization hook examples

When running these examples, `node` fails to return as this
`MessagePort` keeps the event loop active in the main thread unless
it is `unref()`ed.

Fixes: https://github.com/nodejs/node/issues/52846
PR-URL: https://github.com/nodejs/node/pull/53637
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Chemi Atlow <chemi@atlow.co.il>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Moshe Atlow <moshe@atlow.co.il>
Reviewed-By: Ulises Gascón <ulisesgascongonzalez@gmail.com>
This commit is contained in:
Elliot Goodrich 2024-07-01 14:34:35 +01:00 committed by GitHub
parent c1dc307221
commit dda30f972a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -327,6 +327,7 @@ const { port1, port2 } = new MessageChannel();
port1.on('message', (msg) => { port1.on('message', (msg) => {
console.log(msg); console.log(msg);
}); });
port1.unref();
register('./my-hooks.mjs', { register('./my-hooks.mjs', {
parentURL: import.meta.url, parentURL: import.meta.url,
@ -347,6 +348,7 @@ const { port1, port2 } = new MessageChannel();
port1.on('message', (msg) => { port1.on('message', (msg) => {
console.log(msg); console.log(msg);
}); });
port1.unref();
register('./my-hooks.mjs', { register('./my-hooks.mjs', {
parentURL: pathToFileURL(__filename), parentURL: pathToFileURL(__filename),
@ -439,6 +441,7 @@ const { port1, port2 } = new MessageChannel();
port1.on('message', (msg) => { port1.on('message', (msg) => {
assert.strictEqual(msg, 'increment: 2'); assert.strictEqual(msg, 'increment: 2');
}); });
port1.unref();
register('./path-to-my-hooks.js', { register('./path-to-my-hooks.js', {
parentURL: import.meta.url, parentURL: import.meta.url,
@ -461,6 +464,7 @@ const { port1, port2 } = new MessageChannel();
port1.on('message', (msg) => { port1.on('message', (msg) => {
assert.strictEqual(msg, 'increment: 2'); assert.strictEqual(msg, 'increment: 2');
}); });
port1.unref();
register('./path-to-my-hooks.js', { register('./path-to-my-hooks.js', {
parentURL: pathToFileURL(__filename), parentURL: pathToFileURL(__filename),