|
7 | 7 | * @flow
|
8 | 8 | */
|
9 | 9 |
|
10 |
| -let didWarnAboutMessageChannel = false; |
11 |
| -let enqueueTaskImpl = null; |
| 10 | +const {MessageChannel} = require('node:worker_threads'); |
12 | 11 |
|
13 |
| -// Same as shared/enqeuueTask, but while that one used by the public |
14 |
| -// implementation of `act`, this is only used by our internal testing helpers. |
15 | 12 | export default function enqueueTask(task: () => void): void {
|
16 |
| - if (enqueueTaskImpl === null) { |
17 |
| - try { |
18 |
| - // read require off the module object to get around the bundlers. |
19 |
| - // we don't want them to detect a require and bundle a Node polyfill. |
20 |
| - const requireString = ('require' + Math.random()).slice(0, 7); |
21 |
| - const nodeRequire = module && module[requireString]; |
22 |
| - // assuming we're in node, let's try to get node's |
23 |
| - // version of setImmediate, bypassing fake timers if any. |
24 |
| - enqueueTaskImpl = nodeRequire.call(module, 'timers').setImmediate; |
25 |
| - } catch (_err) { |
26 |
| - // we're in a browser |
27 |
| - // we can't use regular timers because they may still be faked |
28 |
| - // so we try MessageChannel+postMessage instead |
29 |
| - enqueueTaskImpl = function (callback: () => void) { |
30 |
| - if (__DEV__) { |
31 |
| - if (didWarnAboutMessageChannel === false) { |
32 |
| - didWarnAboutMessageChannel = true; |
33 |
| - if (typeof MessageChannel === 'undefined') { |
34 |
| - console['error']( |
35 |
| - 'This browser does not have a MessageChannel implementation, ' + |
36 |
| - 'so enqueuing tasks via await act(async () => ...) will fail. ' + |
37 |
| - 'Please file an issue at https://github.com/facebook/react/issues ' + |
38 |
| - 'if you encounter this warning.', |
39 |
| - ); |
40 |
| - } |
41 |
| - } |
42 |
| - } |
43 |
| - const channel = new MessageChannel(); |
44 |
| - channel.port1.onmessage = callback; |
45 |
| - channel.port2.postMessage(undefined); |
46 |
| - }; |
47 |
| - } |
48 |
| - } |
49 |
| - return enqueueTaskImpl(task); |
| 13 | + const channel = new MessageChannel(); |
| 14 | + channel.port1.onmessage = task; |
| 15 | + channel.port2.postMessage(undefined); |
50 | 16 | }
|
0 commit comments