-
Notifications
You must be signed in to change notification settings - Fork 24.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Conditionalize Promise Polyfill for Hermes
Summary: On Hermes, RN can directly use the Promise from global w/o the need of polyfilling it. PromiseRejectionTrackingOptions are extracted to its own file so it can be shared by both codepaths and preserve the behaviors that it's only imported on dev. Some zero-overhead type gymnastics are used to flow-type it properly. Changelog: [General] - made promise polyfill conditionalized on Hermes Reviewed By: cpojer Differential Revision: D24068716 fbshipit-source-id: 3e0b1675493908324f27cc5b7300d8cc42a03acc
- Loading branch information
1 parent
3c154c8
commit 0a28b34
Showing
3 changed files
with
74 additions
and
37 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
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
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,54 @@ | ||
/** | ||
* 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 strict | ||
*/ | ||
|
||
'use strict'; | ||
|
||
import typeof {enable} from 'promise/setimmediate/rejection-tracking'; | ||
|
||
type ExtractOptionsType = <P>((options?: ?P) => void) => P; | ||
|
||
let rejectionTrackingOptions: $Call<ExtractOptionsType, enable> = { | ||
allRejections: true, | ||
onUnhandled: (id, rejection = {}) => { | ||
let message: string; | ||
let stack: ?string; | ||
|
||
const stringValue = Object.prototype.toString.call(rejection); | ||
if (stringValue === '[object Error]') { | ||
message = Error.prototype.toString.call(rejection); | ||
const error: Error = (rejection: $FlowFixMe); | ||
stack = error.stack; | ||
} else { | ||
try { | ||
message = require('pretty-format')(rejection); | ||
} catch { | ||
message = | ||
typeof rejection === 'string' | ||
? rejection | ||
: JSON.stringify((rejection: $FlowFixMe)); | ||
} | ||
} | ||
|
||
const warning = | ||
`Possible Unhandled Promise Rejection (id: ${id}):\n` + | ||
`${message ?? ''}\n` + | ||
(stack == null ? '' : stack); | ||
console.warn(warning); | ||
}, | ||
onHandled: id => { | ||
const warning = | ||
`Promise Rejection Handled (id: ${id})\n` + | ||
'This means you can ignore any previous messages of the form ' + | ||
`"Possible Unhandled Promise Rejection (id: ${id}):"`; | ||
console.warn(warning); | ||
}, | ||
}; | ||
|
||
export default rejectionTrackingOptions; |