| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 1e7479d commit bf9240a
5 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -763,6 +763,18 @@ port2.postMessage(new URL('https://example.org')); | |||
| 763 | 763 | // Prints: { } | |
| 764 | 764 | ``` | |
| 765 | 765 | ||
| 766 | + ### `port.hasRef()` | ||
| 767 | + | ||
| 768 | + <!-- YAML | ||
| 769 | + added: REPLACEME | ||
| 770 | + --> | ||
| 771 | + | ||
| 772 | + > Stability: 1 - Experimental | ||
| 773 | + | ||
| 774 | + * Returns: {boolean} | ||
| 775 | + | ||
| 776 | + If true, the `MessagePort` object will keep the Node.js event loop active. | ||
| 777 | + | ||
| 766 | 778 | ### `port.ref()` | |
| 767 | 779 | ||
| 768 | 780 | <!-- YAML | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -91,7 +91,7 @@ const messageTypes = { | |||
| 91 | 91 | // We have to mess with the MessagePort prototype a bit, so that a) we can make | |
| 92 | 92 | // it inherit from NodeEventTarget, even though it is a C++ class, and b) we do | |
| 93 | 93 | // not provide methods that are not present in the Browser and not documented | |
| 94 | - // on our side (e.g. hasRef). | ||
| 94 | + // on our side (e.g. stopMessagePort). | ||
| 95 | 95 | // Save a copy of the original set of methods as a shallow clone. | |
| 96 | 96 | const MessagePortPrototype = ObjectCreate( | |
| 97 | 97 | ObjectGetPrototypeOf(MessagePort.prototype), | |
@@ -103,6 +103,9 @@ ObjectSetPrototypeOf(MessagePort.prototype, NodeEventTarget.prototype); | |||
| 103 | 103 | // changing the prototype of MessagePort.prototype implicitly removed them. | |
| 104 | 104 | MessagePort.prototype.ref = MessagePortPrototype.ref; | |
| 105 | 105 | MessagePort.prototype.unref = MessagePortPrototype.unref; | |
| 106 | + MessagePort.prototype.hasRef = function() { | ||
| 107 | + return !!FunctionPrototypeCall(MessagePortPrototype.hasRef, this); | ||
| 108 | + }; | ||
| 106 | 109 | ||
| 107 | 110 | function validateMessagePort(port, name) { | |
| 108 | 111 | if (!checkMessagePort(port)) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,57 @@ | |||
| 1 | + 'use strict'; | ||
| 2 | + const common = require('../common'); | ||
| 3 | + | ||
| 4 | + const { MessageChannel } = require('worker_threads'); | ||
| 5 | + const { createHook } = require('async_hooks'); | ||
| 6 | + const { strictEqual } = require('assert'); | ||
| 7 | + | ||
| 8 | + const handles = []; | ||
| 9 | + | ||
| 10 | + createHook({ | ||
| 11 | + init(asyncId, type, triggerAsyncId, resource) { | ||
| 12 | + if (type === 'MESSAGEPORT') { | ||
| 13 | + handles.push(resource); | ||
| 14 | + } | ||
| 15 | + } | ||
| 16 | + }).enable(); | ||
| 17 | + | ||
| 18 | + const { port1, port2 } = new MessageChannel(); | ||
| 19 | + strictEqual(handles[0], port1); | ||
| 20 | + strictEqual(handles[1], port2); | ||
| 21 | + | ||
| 22 | + strictEqual(handles[0].hasRef(), false); | ||
| 23 | + strictEqual(handles[1].hasRef(), false); | ||
| 24 | + | ||
| 25 | + port1.unref(); | ||
| 26 | + strictEqual(handles[0].hasRef(), false); | ||
| 27 | + | ||
| 28 | + port1.ref(); | ||
| 29 | + strictEqual(handles[0].hasRef(), true); | ||
| 30 | + | ||
| 31 | + port1.unref(); | ||
| 32 | + strictEqual(handles[0].hasRef(), false); | ||
| 33 | + | ||
| 34 | + port1.on('message', () => {}); | ||
| 35 | + strictEqual(handles[0].hasRef(), true); | ||
| 36 | + | ||
| 37 | + port2.unref(); | ||
| 38 | + strictEqual(handles[1].hasRef(), false); | ||
| 39 | + | ||
| 40 | + port2.ref(); | ||
| 41 | + strictEqual(handles[1].hasRef(), true); | ||
| 42 | + | ||
| 43 | + port2.unref(); | ||
| 44 | + strictEqual(handles[1].hasRef(), false); | ||
| 45 | + | ||
| 46 | + port2.on('message', () => {}); | ||
| 47 | + strictEqual(handles[0].hasRef(), true); | ||
| 48 | + | ||
| 49 | + port1.on('close', common.mustCall(() => { | ||
| 50 | + strictEqual(handles[0].hasRef(), false); | ||
| 51 | + strictEqual(handles[1].hasRef(), false); | ||
| 52 | + })); | ||
| 53 | + | ||
| 54 | + port2.close(); | ||
| 55 | + | ||
| 56 | + strictEqual(handles[0].hasRef(), true); | ||
| 57 | + strictEqual(handles[1].hasRef(), true); | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -179,7 +179,7 @@ const { MessageChannel, MessagePort } = require('worker_threads'); | |||
| 179 | 179 | assert.deepStrictEqual( | |
| 180 | 180 | Object.getOwnPropertyNames(MessagePort.prototype).sort(), | |
| 181 | 181 | [ | |
| 182 | - 'close', 'constructor', 'onmessage', 'onmessageerror', 'postMessage', | ||
| 183 | - 'ref', 'start', 'unref', | ||
| 182 | + 'close', 'constructor', 'hasRef', 'onmessage', 'onmessageerror', | ||
| 183 | + 'postMessage', 'ref', 'start', 'unref', | ||
| 184 | 184 | ]); | |
| 185 | 185 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,45 @@ | |||
| 1 | + 'use strict'; | ||
| 2 | + const common = require('../common'); | ||
| 3 | + | ||
| 4 | + const { Worker } = require('worker_threads'); | ||
| 5 | + const { createHook } = require('async_hooks'); | ||
| 6 | + const { deepStrictEqual, strictEqual } = require('assert'); | ||
| 7 | + | ||
| 8 | + const m = new Map(); | ||
| 9 | + createHook({ | ||
| 10 | + init(asyncId, type, triggerAsyncId, resource) { | ||
| 11 | + if (['WORKER', 'MESSAGEPORT'].includes(type)) { | ||
| 12 | + m.set(asyncId, { type, resource }); | ||
| 13 | + } | ||
| 14 | + }, | ||
| 15 | + destroy(asyncId) { | ||
| 16 | + m.delete(asyncId); | ||
| 17 | + } | ||
| 18 | + }).enable(); | ||
| 19 | + | ||
| 20 | + function getActiveWorkerAndMessagePortTypes() { | ||
| 21 | + const activeWorkerAndMessagePortTypes = []; | ||
| 22 | + for (const asyncId of m.keys()) { | ||
| 23 | + const { type, resource } = m.get(asyncId); | ||
| 24 | + // Same logic as https://github.com/mafintosh/why-is-node-running/blob/24fb4c878753390a05d00959e6173d0d3c31fddd/index.js#L31-L32. | ||
| 25 | + if (typeof resource.hasRef !== 'function' || resource.hasRef() === true) { | ||
| 26 | + activeWorkerAndMessagePortTypes.push(type); | ||
| 27 | + } | ||
| 28 | + } | ||
| 29 | + return activeWorkerAndMessagePortTypes; | ||
| 30 | + } | ||
| 31 | + | ||
| 32 | + const w = new Worker('', { eval: true }); | ||
| 33 | + deepStrictEqual(getActiveWorkerAndMessagePortTypes(), ['WORKER']); | ||
| 34 | + w.unref(); | ||
| 35 | + deepStrictEqual(getActiveWorkerAndMessagePortTypes(), []); | ||
| 36 | + w.ref(); | ||
| 37 | + deepStrictEqual(getActiveWorkerAndMessagePortTypes(), ['WORKER', 'MESSAGEPORT']); | ||
| 38 | + | ||
| 39 | + w.on('exit', common.mustCall((exitCode) => { | ||
| 40 | + strictEqual(exitCode, 0); | ||
| 41 | + deepStrictEqual(getActiveWorkerAndMessagePortTypes(), ['WORKER']); | ||
| 42 | + setTimeout(common.mustCall(() => { | ||
| 43 | + deepStrictEqual(getActiveWorkerAndMessagePortTypes(), []); | ||
| 44 | + }), 0); | ||
| 45 | + })); | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments