Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Internal test helpers: Use Node's MessageChannel to queue task #26345

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 4 additions & 38 deletions packages/internal-test-utils/enqueueTask.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,44 +7,10 @@
* @flow
*/

let didWarnAboutMessageChannel = false;
let enqueueTaskImpl = null;
const {MessageChannel} = require('node:worker_threads');

// Same as shared/enqeuueTask, but while that one used by the public
// implementation of `act`, this is only used by our internal testing helpers.
export default function enqueueTask(task: () => void): void {
if (enqueueTaskImpl === null) {
try {
// read require off the module object to get around the bundlers.
// we don't want them to detect a require and bundle a Node polyfill.
const requireString = ('require' + Math.random()).slice(0, 7);
const nodeRequire = module && module[requireString];
// assuming we're in node, let's try to get node's
// version of setImmediate, bypassing fake timers if any.
enqueueTaskImpl = nodeRequire.call(module, 'timers').setImmediate;
} catch (_err) {
// we're in a browser
// we can't use regular timers because they may still be faked
// so we try MessageChannel+postMessage instead
enqueueTaskImpl = function (callback: () => void) {
if (__DEV__) {
if (didWarnAboutMessageChannel === false) {
didWarnAboutMessageChannel = true;
if (typeof MessageChannel === 'undefined') {
console['error'](
'This browser does not have a MessageChannel implementation, ' +
'so enqueuing tasks via await act(async () => ...) will fail. ' +
'Please file an issue at https://github.com/facebook/react/issues ' +
'if you encounter this warning.',
);
}
}
}
const channel = new MessageChannel();
channel.port1.onmessage = callback;
channel.port2.postMessage(undefined);
};
}
}
return enqueueTaskImpl(task);
const channel = new MessageChannel();
channel.port1.onmessage = task;
channel.port2.postMessage(undefined);
}
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,7 @@ Task 1 [Normal] │ █████████
taskId++;
const task = scheduleCallback(NormalPriority, () => {});
cancelCallback(task);
await waitForAll([]);
Scheduler.unstable_flushAll();
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Making this async (which I did during one of the recent codemods) caused the test to time out sometimes. Not consistently, though, which is why I didn't catch it before. Since this is a test of Scheduler itself it's fine to use the lower level, synchronous flushing method.

}

expect(console.error).toHaveBeenCalledTimes(1);
Expand Down
7 changes: 7 additions & 0 deletions scripts/flow/environment.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,3 +190,10 @@ declare module 'async_hooks' {
enterWith(store: T): void;
}
}

declare module 'node:worker_threads' {
declare class MessageChannel {
port1: MessagePort;
port2: MessagePort;
}
}