Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 27 additions & 23 deletions packages/react-native/Libraries/Core/setUpRegeneratorRuntime.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,29 +18,33 @@ const {polyfillGlobal} = require('../Utilities/PolyfillFunctions');
* You can use this module directly, or just require InitializeCore.
*/

let hasNativeGenerator;
try {
// If this function was lowered by regenerator-transform, it will try to
// access `global.regeneratorRuntime` which doesn't exist yet and will throw.
hasNativeGenerator = hasNativeConstructor(
function* () {},
'GeneratorFunction',
);
} catch {
// In this case, we know generators are not provided natively.
hasNativeGenerator = false;
}
// The preset only lowers generators in development builds, so the runtime is
// only needed there. Gating on __DEV__ keeps it out of production bundles.
if (__DEV__) {
let hasNativeGenerator;
try {
// If this function was lowered by regenerator-transform, it will try to
// access `global.regeneratorRuntime` which doesn't exist yet and will throw.
hasNativeGenerator = hasNativeConstructor(
function* () {},
'GeneratorFunction',
);
} catch {
// In this case, we know generators are not provided natively.
hasNativeGenerator = false;
}

// If generators are provided natively, which suggests that there was no
// regenerator-transform, then there is no need to set up the runtime.
if (!hasNativeGenerator) {
polyfillGlobal('regeneratorRuntime', () => {
// The require just sets up the global, so make sure when we first
// invoke it the global does not exist
delete global.regeneratorRuntime;
// If generators are provided natively, which suggests that there was no
// regenerator-transform, then there is no need to set up the runtime.
if (!hasNativeGenerator) {
polyfillGlobal('regeneratorRuntime', () => {
// The require just sets up the global, so make sure when we first
// invoke it the global does not exist
delete global.regeneratorRuntime;

// regenerator-runtime/runtime exports the regeneratorRuntime object, so we
// can return it safely.
return require('regenerator-runtime/runtime'); // flowlint-line untyped-import:off
});
// regenerator-runtime/runtime exports the regeneratorRuntime object, so we
// can return it safely.
return require('regenerator-runtime/runtime'); // flowlint-line untyped-import:off
});
}
}