Files
react/packages/internal-test-utils/enqueueTask.js
Andrew Clark 39d4b93657 [Internal tests] Close MessageChannel port to prevent leak (#26357)
Node's MessageChannel implementation will leak if you don't explicitly
close the port. This updates the enqueueTask function we use in our
internal testing helpers to close the port once it's no longer needed.
2023-03-09 11:20:07 -05:00

20 lines
486 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 = () => {
channel.port1.close();
task();
};
channel.port2.postMessage(undefined);
}