mirror of
https://github.com/facebook/react.git
synced 2026-02-24 20:53:03 +00:00
To wait for the microtask queue to empty, our internal test helpers schedule an arbitrary task using `setImmediate`. It doesn't matter what kind of task it is, only that it's a separate task from the current one, because by the time it fires, the microtasks for the current event will have already been processed. The issue with `setImmediate` is that Jest mocks it. Which can lead to weird behavior. I've changed it to instead use a message event, via the MessageChannel implementation exposed by the `node:worker_threads` module. We should consider doing this in the public implementation of `act`, too.
17 lines
440 B
JavaScript
17 lines
440 B
JavaScript
/**
|
|
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
*
|
|
* This source code is licensed under the MIT license found in the
|
|
* LICENSE file in the root directory of this source tree.
|
|
*
|
|
* @flow
|
|
*/
|
|
|
|
const {MessageChannel} = require('node:worker_threads');
|
|
|
|
export default function enqueueTask(task: () => void): void {
|
|
const channel = new MessageChannel();
|
|
channel.port1.onmessage = task;
|
|
channel.port2.postMessage(undefined);
|
|
}
|