1. /**
    
  2.  * Copyright (c) Meta Platforms, Inc. and affiliates.
    
  3.  *
    
  4.  * This source code is licensed under the MIT license found in the
    
  5.  * LICENSE file in the root directory of this source tree.
    
  6.  *
    
  7.  * @flow
    
  8.  */
    
  9. 
    
  10. let didWarnAboutMessageChannel = false;
    
  11. let enqueueTaskImpl = null;
    
  12. 
    
  13. export default function enqueueTask(task: () => void): void {
    
  14.   if (enqueueTaskImpl === null) {
    
  15.     try {
    
  16.       // read require off the module object to get around the bundlers.
    
  17.       // we don't want them to detect a require and bundle a Node polyfill.
    
  18.       const requireString = ('require' + Math.random()).slice(0, 7);
    
  19.       const nodeRequire = module && module[requireString];
    
  20.       // assuming we're in node, let's try to get node's
    
  21.       // version of setImmediate, bypassing fake timers if any.
    
  22.       enqueueTaskImpl = nodeRequire.call(module, 'timers').setImmediate;
    
  23.     } catch (_err) {
    
  24.       // we're in a browser
    
  25.       // we can't use regular timers because they may still be faked
    
  26.       // so we try MessageChannel+postMessage instead
    
  27.       enqueueTaskImpl = function (callback: () => void) {
    
  28.         if (__DEV__) {
    
  29.           if (didWarnAboutMessageChannel === false) {
    
  30.             didWarnAboutMessageChannel = true;
    
  31.             if (typeof MessageChannel === 'undefined') {
    
  32.               console.error(
    
  33.                 'This browser does not have a MessageChannel implementation, ' +
    
  34.                   'so enqueuing tasks via await act(async () => ...) will fail. ' +
    
  35.                   'Please file an issue at https://github.com/facebook/react/issues ' +
    
  36.                   'if you encounter this warning.',
    
  37.               );
    
  38.             }
    
  39.           }
    
  40.         }
    
  41.         const channel = new MessageChannel();
    
  42.         channel.port1.onmessage = callback;
    
  43.         channel.port2.postMessage(undefined);
    
  44.       };
    
  45.     }
    
  46.   }
    
  47.   return enqueueTaskImpl(task);
    
  48. }