Improve realm constructor logic (#742)
* Add user file * Clean up initialization and add error message * Revert "Add user file" This reverts commit 2948f4cfc2dfd2d5d75594307b1e89806b817eb7. * Make index.js more robust * Fix review comments
This commit is contained in:
parent
5684fd5aa8
commit
e74a657725
67
lib/index.js
67
lib/index.js
|
@ -18,43 +18,68 @@
|
|||
|
||||
'use strict';
|
||||
|
||||
function node_require(module) {
|
||||
// Prevent React Native packager from seeing modules required with this
|
||||
function nodeRequire(module) {
|
||||
return require(module);
|
||||
}
|
||||
|
||||
// If process is defined, we're running in node.
|
||||
function isNode() {
|
||||
return typeof process == 'object' && (('' + process) == '[object process]' || typeof jest == 'object')
|
||||
|
||||
function getContext() {
|
||||
if (typeof process === 'object' && process + '' === '[object process]') {
|
||||
return process.type === 'renderer' ? 'electron' : 'nodejs';
|
||||
}
|
||||
|
||||
// function isElectronRenderer() {
|
||||
// return isNode() && process.type === 'renderer'
|
||||
// }
|
||||
if (typeof navigator !== 'undefined') {
|
||||
if (navigator.product === 'ReactNative') {
|
||||
return 'reactnative';
|
||||
}
|
||||
|
||||
if (/Chrome/.test(navigator.userAgent)) {
|
||||
return 'chromedebugger';
|
||||
}
|
||||
}
|
||||
|
||||
if (typeof global !== 'undefined') {
|
||||
if (global.__debug__) {
|
||||
return 'vscodedebugger';
|
||||
}
|
||||
}
|
||||
|
||||
if (typeof Realm !== 'undefined') {
|
||||
return 'jscore';
|
||||
}
|
||||
|
||||
throw Error("Unknown execution context");
|
||||
}
|
||||
|
||||
var realmConstructor;
|
||||
if (isNode()) {
|
||||
node_require('./submit-analytics')('Run');
|
||||
|
||||
// Prevent React Native packager from seeing this module.
|
||||
var binary = node_require('node-pre-gyp');
|
||||
var path = node_require('path');
|
||||
switch(getContext()) {
|
||||
case 'nodejs':
|
||||
case 'electron':
|
||||
nodeRequire('./submit-analytics')('Run');
|
||||
|
||||
var binary = nodeRequire('node-pre-gyp');
|
||||
var path = nodeRequire('path');
|
||||
var pkg = path.resolve(path.join(__dirname,'../package.json'));
|
||||
var binding_path = binary.find(pkg);
|
||||
|
||||
realmConstructor = require(binding_path).Realm;
|
||||
} else {
|
||||
if (typeof Realm != 'undefined') {
|
||||
// The global Realm constructor should be available on device (using JavaScriptCore).
|
||||
break;
|
||||
|
||||
case 'reactnative':
|
||||
case 'jscore':
|
||||
realmConstructor = Realm; // eslint-disable-line no-undef
|
||||
// eslint-disable-next-line
|
||||
} else if (typeof window != 'undefined') {
|
||||
// The userAgent will be defined when running in a browser (such as Chrome debugging mode).
|
||||
break;
|
||||
|
||||
case 'chromedebugger':
|
||||
case 'vscodedebugger':
|
||||
realmConstructor = require('./browser').default; // (exported as ES6 module)
|
||||
// eslint-disable-next-line
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
if (!realmConstructor) {
|
||||
throw new Error('Missing Realm constructor - please ensure RealmReact framework is included!');
|
||||
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');
|
||||
}
|
||||
|
||||
require('./extensions')(realmConstructor);
|
||||
|
|
Loading…
Reference in New Issue