-
Notifications
You must be signed in to change notification settings - Fork 24.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Summary: Changelog: [General][Added] - Add global.queueMicrotask `queueMicrotask` is a relatively recent API defined in the WHATWG HTML spec and it's been widely adopted by all web browsers and Node.js. This diff introduced it to React Native by polyfilling it via a lazily-allocated resolved Promise, or calling directly into a fast path provided by Hermes. Reviewed By: RSNara Differential Revision: D29838852 fbshipit-source-id: 8c4378b1b713fb8b0da5e67f92ba2ea9838766f7
- Loading branch information
1 parent
3081db2
commit be189cd
Showing
2 changed files
with
69 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @format | ||
* @flow | ||
*/ | ||
|
||
'use strict'; | ||
|
||
let resolvedPromise; | ||
|
||
/** | ||
* Polyfill for the microtask queuening API defined by WHATWG HTMP spec. | ||
* https://html.spec.whatwg.org/multipage/timers-and-user-prompts.html#dom-queuemicrotask | ||
* | ||
* The method must queue a microtask to invoke @param {function} callback, and | ||
* if the callback throws an exception, report the exception. | ||
*/ | ||
export default function queueMicrotask(callback: Function) { | ||
if (arguments.length < 1) { | ||
throw new TypeError( | ||
'queueMicrotask must be called with at least one argument (a function to call)', | ||
); | ||
} | ||
if (typeof callback !== 'function') { | ||
throw new TypeError('The argument to queueMicrotask must be a function.'); | ||
} | ||
|
||
// Try to reuse a lazily allocated resolved promise from closure. | ||
(resolvedPromise || (resolvedPromise = Promise.resolve())) | ||
.then(callback) | ||
.catch(error => | ||
// Report the exception until the next tick. | ||
setTimeout(() => { | ||
throw error; | ||
}, 0), | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters