From 38f668f95f82c7bf1fb4a893edf220d1320ebfcc Mon Sep 17 00:00:00 2001 From: Rick Hanlon Date: Tue, 26 Jan 2021 17:09:51 -0700 Subject: [PATCH] Queue discrete events in microtask --- packages/react-art/src/ReactARTHostConfig.js | 3 +++ .../react-dom/src/client/ReactDOMHostConfig.js | 15 +++++++++++++++ .../src/ReactFabricHostConfig.js | 3 +++ .../src/ReactNativeHostConfig.js | 3 +++ .../src/ReactTestHostConfig.js | 15 +++++++++++++++ 5 files changed, 39 insertions(+) diff --git a/packages/react-art/src/ReactARTHostConfig.js b/packages/react-art/src/ReactARTHostConfig.js index 43a563e90aa23..ad2d73062240e 100644 --- a/packages/react-art/src/ReactARTHostConfig.js +++ b/packages/react-art/src/ReactARTHostConfig.js @@ -330,6 +330,9 @@ export function getChildHostContext() { export const scheduleTimeout = setTimeout; export const cancelTimeout = clearTimeout; export const noTimeout = -1; +export function queueMicrotask() { + invariant(false, 'Not implemented.'); +} export function shouldSetTextContent(type, props) { return ( diff --git a/packages/react-dom/src/client/ReactDOMHostConfig.js b/packages/react-dom/src/client/ReactDOMHostConfig.js index 8de2f2c5c7ca4..c0f0c52fdbb86 100644 --- a/packages/react-dom/src/client/ReactDOMHostConfig.js +++ b/packages/react-dom/src/client/ReactDOMHostConfig.js @@ -384,6 +384,21 @@ export const scheduleTimeout: any = export const cancelTimeout: any = typeof clearTimeout === 'function' ? clearTimeout : (undefined: any); export const noTimeout = -1; +export const queueMicrotask: (Function => void) | undefined = + typeof queueMicrotask === 'function' + ? queueMicrotask + : typeof Promise !== 'undefined' + ? callback => + Promise.resolve(null) + .then(callback) + .catch(handleErrorInNextTick) + : scheduleTimeout; + +function handleErrorInNextTick(error) { + setTimeout(() => { + throw error; + }); +} // ------------------- // Mutation diff --git a/packages/react-native-renderer/src/ReactFabricHostConfig.js b/packages/react-native-renderer/src/ReactFabricHostConfig.js index 636542e8d426b..a960f26c5fd0a 100644 --- a/packages/react-native-renderer/src/ReactFabricHostConfig.js +++ b/packages/react-native-renderer/src/ReactFabricHostConfig.js @@ -348,6 +348,9 @@ export const warnsIfNotActing = false; export const scheduleTimeout = setTimeout; export const cancelTimeout = clearTimeout; export const noTimeout = -1; +export function queueMicrotask() { + invariant(false, 'Not implemented.'); +} // ------------------- // Persistence diff --git a/packages/react-native-renderer/src/ReactNativeHostConfig.js b/packages/react-native-renderer/src/ReactNativeHostConfig.js index eba0a2b90462b..69cb665d26d03 100644 --- a/packages/react-native-renderer/src/ReactNativeHostConfig.js +++ b/packages/react-native-renderer/src/ReactNativeHostConfig.js @@ -247,6 +247,9 @@ export const warnsIfNotActing = true; export const scheduleTimeout = setTimeout; export const cancelTimeout = clearTimeout; export const noTimeout = -1; +export function queueMicrotask() { + invariant(false, 'Not implemented.'); +} export function shouldSetTextContent(type: string, props: Props): boolean { // TODO (bvaughn) Revisit this decision. diff --git a/packages/react-test-renderer/src/ReactTestHostConfig.js b/packages/react-test-renderer/src/ReactTestHostConfig.js index a6d661d5fe9cc..eb5ceaa51404b 100644 --- a/packages/react-test-renderer/src/ReactTestHostConfig.js +++ b/packages/react-test-renderer/src/ReactTestHostConfig.js @@ -220,6 +220,21 @@ export const warnsIfNotActing = true; export const scheduleTimeout = setTimeout; export const cancelTimeout = clearTimeout; +export const queueMicrotask = + typeof queueMicrotask === 'function' + ? queueMicrotask + : typeof Promise !== 'undefined' + ? callback => + Promise.resolve(null) + .then(callback) + .catch(handleErrorInNextTick) + : scheduleTimeout; + +function handleErrorInNextTick(error) { + setTimeout(() => { + throw error; + }); +} export const noTimeout = -1; // -------------------