Skip to content

Commit

Permalink
Add queue microtask to host configs (#20668)
Browse files Browse the repository at this point in the history
* Queue discrete events in microtask

* Fix flow types

* Add to createReactNoop

* More flow types

* Remove import

* Add to custom HostConfig as well
  • Loading branch information
rickhanlonii authored Jan 27, 2021
1 parent deeeaf1 commit aa736a0
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 0 deletions.
3 changes: 3 additions & 0 deletions packages/react-art/src/ReactARTHostConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,9 @@ export function getChildHostContext() {
export const scheduleTimeout = setTimeout;
export const cancelTimeout = clearTimeout;
export const noTimeout = -1;
export function queueMicrotask(callback: Function) {
invariant(false, 'Not implemented.');
}

export function shouldSetTextContent(type, props) {
return (
Expand Down
15 changes: 15 additions & 0 deletions packages/react-dom/src/client/ReactDOMHostConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: any =
typeof global.queueMicrotask === 'function'
? global.queueMicrotask
: typeof Promise !== 'undefined'
? callback =>
Promise.resolve(null)
.then(callback)
.catch(handleErrorInNextTick)
: scheduleTimeout;

function handleErrorInNextTick(error) {
setTimeout(() => {
throw error;
});
}

// -------------------
// Mutation
Expand Down
3 changes: 3 additions & 0 deletions packages/react-native-renderer/src/ReactFabricHostConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,9 @@ export const warnsIfNotActing = false;
export const scheduleTimeout = setTimeout;
export const cancelTimeout = clearTimeout;
export const noTimeout = -1;
export function queueMicrotask(callback: Function) {
invariant(false, 'Not implemented.');
}

// -------------------
// Persistence
Expand Down
3 changes: 3 additions & 0 deletions packages/react-native-renderer/src/ReactNativeHostConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,9 @@ export const warnsIfNotActing = true;
export const scheduleTimeout = setTimeout;
export const cancelTimeout = clearTimeout;
export const noTimeout = -1;
export function queueMicrotask(callback: Function) {
invariant(false, 'Not implemented.');
}

export function shouldSetTextContent(type: string, props: Props): boolean {
// TODO (bvaughn) Revisit this decision.
Expand Down
13 changes: 13 additions & 0 deletions packages/react-noop-renderer/src/createReactNoop.js
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,19 @@ function createReactNoop(reconciler: Function, useMutation: boolean) {
scheduleTimeout: setTimeout,
cancelTimeout: clearTimeout,
noTimeout: -1,
queueMicrotask:
typeof queueMicrotask === 'function'
? queueMicrotask
: typeof Promise !== 'undefined'
? callback =>
Promise.resolve(null)
.then(callback)
.catch(error => {
setTimeout(() => {
throw error;
});
})
: setTimeout,

prepareForCommit(): null | Object {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export const shouldSetTextContent = $$$hostConfig.shouldSetTextContent;
export const createTextInstance = $$$hostConfig.createTextInstance;
export const scheduleTimeout = $$$hostConfig.scheduleTimeout;
export const cancelTimeout = $$$hostConfig.cancelTimeout;
export const queueMicrotask = $$$hostConfig.queueMicrotask;
export const noTimeout = $$$hostConfig.noTimeout;
export const now = $$$hostConfig.now;
export const isPrimaryRenderer = $$$hostConfig.isPrimaryRenderer;
Expand Down
15 changes: 15 additions & 0 deletions packages/react-test-renderer/src/ReactTestHostConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,21 @@ export const warnsIfNotActing = true;

export const scheduleTimeout = setTimeout;
export const cancelTimeout = clearTimeout;
export const queueMicrotask =
typeof global.queueMicrotask === 'function'
? global.queueMicrotask
: typeof Promise !== 'undefined'
? (callback: Function) =>
Promise.resolve(null)
.then(callback)
.catch(handleErrorInNextTick)
: scheduleTimeout;

function handleErrorInNextTick(error) {
setTimeout(() => {
throw error;
});
}
export const noTimeout = -1;

// -------------------
Expand Down

0 comments on commit aa736a0

Please sign in to comment.