Merge pull request #869 from realm/kd/fix-execution-context-detection

Update index.js to report missing react-native link
This commit is contained in:
blagoev 2017-05-29 09:00:16 +03:00 committed by GitHub
commit 2390f9b084

View File

@ -23,7 +23,6 @@ function nodeRequire(module) {
return require(module);
}
function getContext() {
// If process is an object, we're probably running in Node or Electron
// From: http://stackoverflow.com/a/24279593/1417293
@ -36,14 +35,26 @@ function getContext() {
return 'nodejs';
}
// If we've already injected the Realm class, we are probably
// running in a jscore environment, either directly or via React Native
if (typeof Realm !== 'undefined') {
if (typeof navigator !== 'undefined' && navigator.product === 'ReactNative') { // eslint-disable-line no-undef
// If the navigator.userAgent contains the string "Chrome", we're likely
// running via the chrome debugger.
if (typeof navigator !== 'undefined' &&
navigator.product === 'ReactNative') { // eslint-disable-line no-undef
return 'reactnative';
/Chrome/.test(navigator.userAgent)) { // eslint-disable-line no-undef
return 'chromedebugger';
}
// Otherwise, we must be in a "normal" react native situation.
// In that case, the Realm type should have been injected by the native code.
// If it hasn't, the user likely forgot to run link.
if (typeof Realm === 'undefined'){
throw new Error('Missing Realm constructor. Did you run "react-native link realm"? Please see https://realm.io/docs/react-native/latest/#missing-realm-constructor for troubleshooting');
}
return 'reactnative';
}
// If we're not running in React Native but we already injected the Realm class,
// we are probably running in a pure jscore environment
if (typeof Realm !== 'undefined') {
return 'jscore';
}
@ -53,7 +64,7 @@ function getContext() {
}
// Finally, if the navigator.userAgent contains the string "Chrome", we're likely
// running via the chrome debugger.
// running via the chrome debugger, even if navigator.product isn't set to "ReactNative"
if (typeof navigator !== 'undefined' &&
/Chrome/.test(navigator.userAgent)) { // eslint-disable-line no-undef
return 'chromedebugger';
@ -89,7 +100,7 @@ switch(getContext()) {
}
if (!realmConstructor) {
throw new Error('Missing Realm constructor. Did you run "react-native link realm"? Please see https://realm.io/docs/react-native/latest/#missing-realm-constructor for troubleshooting');
throw Error("Error trying to establish execution context");
}
require('./extensions')(realmConstructor);