mirror of
https://github.com/status-im/realm-js.git
synced 2025-02-18 09:36:34 +00:00
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
75
lib/index.js
75
lib/index.js
@ -18,43 +18,68 @@
|
|||||||
|
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
function node_require(module) {
|
// Prevent React Native packager from seeing modules required with this
|
||||||
|
function nodeRequire(module) {
|
||||||
return require(module);
|
return require(module);
|
||||||
}
|
}
|
||||||
|
|
||||||
// If process is defined, we're running in node.
|
|
||||||
function isNode() {
|
function getContext() {
|
||||||
return typeof process == 'object' && (('' + process) == '[object process]' || typeof jest == 'object')
|
if (typeof process === 'object' && process + '' === '[object process]') {
|
||||||
|
return process.type === 'renderer' ? 'electron' : 'nodejs';
|
||||||
|
}
|
||||||
|
|
||||||
|
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");
|
||||||
}
|
}
|
||||||
|
|
||||||
// function isElectronRenderer() {
|
|
||||||
// return isNode() && process.type === 'renderer'
|
|
||||||
// }
|
|
||||||
|
|
||||||
var realmConstructor;
|
var realmConstructor;
|
||||||
if (isNode()) {
|
|
||||||
node_require('./submit-analytics')('Run');
|
|
||||||
|
|
||||||
// Prevent React Native packager from seeing this module.
|
switch(getContext()) {
|
||||||
var binary = node_require('node-pre-gyp');
|
case 'nodejs':
|
||||||
var path = node_require('path');
|
case 'electron':
|
||||||
var pkg = path.resolve(path.join(__dirname,'../package.json'));
|
nodeRequire('./submit-analytics')('Run');
|
||||||
var binding_path = binary.find(pkg);
|
|
||||||
realmConstructor = require(binding_path).Realm;
|
var binary = nodeRequire('node-pre-gyp');
|
||||||
} else {
|
var path = nodeRequire('path');
|
||||||
if (typeof Realm != 'undefined') {
|
var pkg = path.resolve(path.join(__dirname,'../package.json'));
|
||||||
// The global Realm constructor should be available on device (using JavaScriptCore).
|
var binding_path = binary.find(pkg);
|
||||||
|
|
||||||
|
realmConstructor = require(binding_path).Realm;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 'reactnative':
|
||||||
|
case 'jscore':
|
||||||
realmConstructor = Realm; // eslint-disable-line no-undef
|
realmConstructor = Realm; // eslint-disable-line no-undef
|
||||||
// eslint-disable-next-line
|
break;
|
||||||
} else if (typeof window != 'undefined') {
|
|
||||||
// The userAgent will be defined when running in a browser (such as Chrome debugging mode).
|
case 'chromedebugger':
|
||||||
|
case 'vscodedebugger':
|
||||||
realmConstructor = require('./browser').default; // (exported as ES6 module)
|
realmConstructor = require('./browser').default; // (exported as ES6 module)
|
||||||
// eslint-disable-next-line
|
break;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!realmConstructor) {
|
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);
|
require('./extensions')(realmConstructor);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user