From fa1f22ddc23f1459ed1825ee143b57573799645d Mon Sep 17 00:00:00 2001 From: Kristian Dupont Date: Fri, 6 Jan 2017 14:30:32 +0100 Subject: [PATCH] Fix constructor init (#774) * Update test app to RN 0.39.0 * Fix getContext in index.js * Add dependency on invariant * Add some comments to getContext * Make invariant a devdependency rather than a dependency --- lib/index.js | 40 +++++++++++++++++++------------ tests/react-test-app/package.json | 7 ++++-- 2 files changed, 30 insertions(+), 17 deletions(-) diff --git a/lib/index.js b/lib/index.js index 0f80b8aa..353abe3b 100644 --- a/lib/index.js +++ b/lib/index.js @@ -25,30 +25,40 @@ function nodeRequire(module) { function getContext() { + // If process is an object, we're probably running in Node or Electron + // From: http://stackoverflow.com/a/24279593/1417293 if (typeof process === 'object' && process + '' === '[object process]') { return process.type === 'renderer' ? 'electron' : 'nodejs'; - } + } - if (typeof navigator !== 'undefined') { - if (navigator.product === 'ReactNative') { // eslint-disable-line no-undef + // When running via Jest, the jest object is defined. + if (typeof jest === 'object') { + 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 return 'reactnative'; } - if (/Chrome/.test(navigator.userAgent)) { // eslint-disable-line no-undef - return 'chromedebugger'; - } - } - - if (typeof global !== 'undefined') { - if (global.__debug__) { - return 'vscodedebugger'; - } - } - - if (typeof Realm !== 'undefined') { return 'jscore'; } + // Visual Studio Code defines the global.__debug__ object. + if (typeof global !== 'undefined' && global.__debug__) { + return 'vscodedebugger'; + } + + // Finally, if the navigator.userAgent contains the string "Chrome", we're likely + // running via the chrome debugger. + if (typeof navigator !== 'undefined' && + /Chrome/.test(navigator.userAgent)) { // eslint-disable-line no-undef + return 'chromedebugger'; + } + throw Error("Unknown execution context"); } diff --git a/tests/react-test-app/package.json b/tests/react-test-app/package.json index 445c5b79..31355884 100644 --- a/tests/react-test-app/package.json +++ b/tests/react-test-app/package.json @@ -6,11 +6,14 @@ "start": "react-native start" }, "dependencies": { - "react": "15.3.2", - "react-native": "^0.37.0", + "react": "15.4.0", + "react-native": "^0.39.0", "react-native-fs": "^1.1.0", "realm": "file:../..", "realm-tests": "file:../js", "xmlbuilder": "^4.2.1" + }, + "devDependencies": { + "invariant": "^2.2.2" } }