|
| 1 | +/* istanbul ignore file */ |
| 2 | +// the part of this file that we need tested is definitely being run |
| 3 | +// and the part that is not cannot easily have useful tests written |
| 4 | +// anyway. So we're just going to ignore coverage for this file |
| 5 | +/** |
| 6 | + * copied from React's enqueueTask.js |
| 7 | + */ |
| 8 | + |
| 9 | +let didWarnAboutMessageChannel = false |
| 10 | +let enqueueTask |
| 11 | +try { |
| 12 | + // read require off the module object to get around the bundlers. |
| 13 | + // we don't want them to detect a require and bundle a Node polyfill. |
| 14 | + const requireString = `require${Math.random()}`.slice(0, 7) |
| 15 | + const nodeRequire = module && module[requireString] |
| 16 | + // assuming we're in node, let's try to get node's |
| 17 | + // version of setImmediate, bypassing fake timers if any. |
| 18 | + enqueueTask = nodeRequire('timers').setImmediate |
| 19 | +} catch (_err) { |
| 20 | + // we're in a browser |
| 21 | + // we can't use regular timers because they may still be faked |
| 22 | + // so we try MessageChannel+postMessage instead |
| 23 | + enqueueTask = callback => { |
| 24 | + if (didWarnAboutMessageChannel === false) { |
| 25 | + didWarnAboutMessageChannel = true |
| 26 | + // eslint-disable-next-line no-console |
| 27 | + console.error( |
| 28 | + typeof MessageChannel !== 'undefined', |
| 29 | + 'This browser does not have a MessageChannel implementation, ' + |
| 30 | + 'so enqueuing tasks via await act(async () => ...) will fail. ' + |
| 31 | + 'Please file an issue at https://github.com/facebook/react/issues ' + |
| 32 | + 'if you encounter this warning.', |
| 33 | + ) |
| 34 | + } |
| 35 | + const channel = new MessageChannel() |
| 36 | + channel.port1.onmessage = callback |
| 37 | + channel.port2.postMessage(undefined) |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +export default function flushMicroTasks() { |
| 42 | + return { |
| 43 | + then(resolve) { |
| 44 | + enqueueTask(resolve) |
| 45 | + }, |
| 46 | + } |
| 47 | +} |
0 commit comments