Skip to content

Commit aa736a0

Browse files
authored
Add queue microtask to host configs (#20668)
* Queue discrete events in microtask * Fix flow types * Add to createReactNoop * More flow types * Remove import * Add to custom HostConfig as well
1 parent deeeaf1 commit aa736a0

7 files changed

Lines changed: 53 additions & 0 deletions

File tree

‎packages/react-art/src/ReactARTHostConfig.js‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,9 @@ export function getChildHostContext() {
330330
exportconstscheduleTimeout=setTimeout;
331331
exportconstcancelTimeout=clearTimeout;
332332
exportconstnoTimeout=-1;
333+
exportfunctionqueueMicrotask(callback: Function){
334+
invariant(false,'Not implemented.');
335+
}
333336

334337
exportfunctionshouldSetTextContent(type,props){
335338
return(

‎packages/react-dom/src/client/ReactDOMHostConfig.js‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,21 @@ export const scheduleTimeout: any =
384384
exportconstcancelTimeout: any=
385385
typeofclearTimeout==='function' ? clearTimeout : (undefined: any);
386386
exportconstnoTimeout=-1;
387+
exportconstqueueMicrotask: any=
388+
typeofglobal.queueMicrotask==='function'
389+
? global.queueMicrotask
390+
: typeofPromise!=='undefined'
391+
? callback=>
392+
Promise.resolve(null)
393+
.then(callback)
394+
.catch(handleErrorInNextTick)
395+
: scheduleTimeout;
396+
397+
functionhandleErrorInNextTick(error){
398+
setTimeout(()=>{
399+
throwerror;
400+
});
401+
}
387402

388403
// -------------------
389404
// Mutation

‎packages/react-native-renderer/src/ReactFabricHostConfig.js‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,9 @@ export const warnsIfNotActing = false;
348348
exportconstscheduleTimeout=setTimeout;
349349
exportconstcancelTimeout=clearTimeout;
350350
exportconstnoTimeout=-1;
351+
exportfunctionqueueMicrotask(callback: Function){
352+
invariant(false,'Not implemented.');
353+
}
351354

352355
// -------------------
353356
// Persistence

‎packages/react-native-renderer/src/ReactNativeHostConfig.js‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,9 @@ export const warnsIfNotActing = true;
247247
exportconstscheduleTimeout=setTimeout;
248248
exportconstcancelTimeout=clearTimeout;
249249
exportconstnoTimeout=-1;
250+
exportfunctionqueueMicrotask(callback: Function){
251+
invariant(false,'Not implemented.');
252+
}
250253

251254
exportfunctionshouldSetTextContent(type: string,props: Props): boolean{
252255
// TODO (bvaughn) Revisit this decision.

‎packages/react-noop-renderer/src/createReactNoop.js‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,19 @@ function createReactNoop(reconciler: Function, useMutation: boolean) {
371371
scheduleTimeout: setTimeout,
372372
cancelTimeout: clearTimeout,
373373
noTimeout: -1,
374+
queueMicrotask:
375+
typeofqueueMicrotask==='function'
376+
? queueMicrotask
377+
: typeofPromise!=='undefined'
378+
? callback=>
379+
Promise.resolve(null)
380+
.then(callback)
381+
.catch(error=>{
382+
setTimeout(()=>{
383+
throwerror;
384+
});
385+
})
386+
: setTimeout,
374387

375388
prepareForCommit(): null|Object{
376389
returnnull;

‎packages/react-reconciler/src/forks/ReactFiberHostConfig.custom.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ export const shouldSetTextContent = $$$hostConfig.shouldSetTextContent;
5454
exportconstcreateTextInstance=$$$hostConfig.createTextInstance;
5555
exportconstscheduleTimeout=$$$hostConfig.scheduleTimeout;
5656
exportconstcancelTimeout=$$$hostConfig.cancelTimeout;
57+
exportconstqueueMicrotask=$$$hostConfig.queueMicrotask;
5758
exportconstnoTimeout=$$$hostConfig.noTimeout;
5859
exportconstnow=$$$hostConfig.now;
5960
exportconstisPrimaryRenderer=$$$hostConfig.isPrimaryRenderer;

‎packages/react-test-renderer/src/ReactTestHostConfig.js‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,21 @@ export const warnsIfNotActing = true;
220220

221221
export const scheduleTimeout = setTimeout;
222222
export const cancelTimeout = clearTimeout;
223+
export const queueMicrotask =
224+
typeof global.queueMicrotask === 'function'
225+
? global.queueMicrotask
226+
: typeof Promise !== 'undefined'
227+
? (callback: Function) =>
228+
Promise.resolve(null)
229+
.then(callback)
230+
.catch(handleErrorInNextTick)
231+
: scheduleTimeout;
232+
233+
function handleErrorInNextTick(error) {
234+
setTimeout(() => {
235+
throw error;
236+
});
237+
}
223238
export const noTimeout = -1;
224239

225240
// -------------------

0 commit comments

Comments
 (0)