2018-03-25 01:23:46 +00:00
|
|
|
/* eslint-disable guard-for-in,no-restricted-syntax */
|
|
|
|
global.bridge.context = null;
|
|
|
|
|
|
|
|
const consoleContext = require('./console');
|
|
|
|
const { createContext } = require('vm');
|
2018-03-25 18:24:18 +00:00
|
|
|
const chalk = require('chalk');
|
2018-03-25 01:23:46 +00:00
|
|
|
|
|
|
|
let customBridgeProps = [];
|
2018-03-25 18:24:18 +00:00
|
|
|
let driftCheckStart = null;
|
2018-03-25 01:23:46 +00:00
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
/**
|
|
|
|
* Cleanup existing context - just some quick iterations over common fb/rn/bridge locations
|
|
|
|
* garbage collection will do the rest. This is probably not needed...
|
|
|
|
*/
|
|
|
|
async cleanup() {
|
|
|
|
if (global.bridge.context) {
|
|
|
|
if (global.bridge.beforeContextReset) {
|
|
|
|
await global.bridge.beforeContextReset();
|
|
|
|
}
|
|
|
|
|
2018-03-25 05:52:30 +00:00
|
|
|
for (const name in global.bridge.context.__fbBatchedBridge) {
|
|
|
|
global.bridge.context.__fbBatchedBridge[name] = undefined;
|
|
|
|
delete global.bridge.context.__fbBatchedBridge[name];
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const name in global.bridge.context.__fbGenNativeModule) {
|
|
|
|
global.bridge.context.__fbGenNativeModule[name] = undefined;
|
|
|
|
delete global.bridge.context.__fbGenNativeModule[name];
|
|
|
|
}
|
2018-03-25 01:23:46 +00:00
|
|
|
|
2018-03-25 05:52:30 +00:00
|
|
|
for (const name in global.bridge.context.__fbBatchedBridgeConfig) {
|
|
|
|
global.bridge.context.__fbBatchedBridgeConfig[name] = undefined;
|
|
|
|
delete global.bridge.context.__fbBatchedBridgeConfig[name];
|
|
|
|
}
|
2018-03-25 01:23:46 +00:00
|
|
|
|
2018-03-25 05:52:30 +00:00
|
|
|
for (const name in global.bridge.context) {
|
|
|
|
global.bridge.context[name] = undefined;
|
|
|
|
delete global.bridge.context[name];
|
2018-03-25 01:23:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
global.bridge.context = undefined;
|
|
|
|
|
|
|
|
// clear custom props and reset props track array
|
|
|
|
for (let i = 0; i < customBridgeProps.length; i++) {
|
|
|
|
global.bridge[customBridgeProps[i]] = undefined;
|
|
|
|
delete global.bridge[customBridgeProps[i]];
|
|
|
|
}
|
|
|
|
|
|
|
|
customBridgeProps = [];
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2018-03-25 02:50:10 +00:00
|
|
|
* Create a new context for a RN app to attach to, we additionally provide __bridgeNode for
|
2018-03-25 01:23:46 +00:00
|
|
|
* the counterpart RN bridge code to attach to and communicate back
|
|
|
|
*/
|
|
|
|
create() {
|
|
|
|
global.bridge.context = createContext({
|
|
|
|
console: consoleContext(),
|
|
|
|
__bridgeNode: {
|
|
|
|
_ready() {
|
2018-03-25 18:24:18 +00:00
|
|
|
if (!driftCheckStart) {
|
|
|
|
driftCheckStart = Date.now();
|
|
|
|
global.bridge.context.__driftCheck(1);
|
|
|
|
} else {
|
|
|
|
setTimeout(() => process.emit('bridge-attached'), 1);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_callbackDriftCheck() {
|
|
|
|
const timeTaken = Date.now() - driftCheckStart;
|
|
|
|
if (timeTaken > 5000) {
|
|
|
|
console.log(
|
|
|
|
`${chalk.blue(
|
|
|
|
'[bridge] ⚠️ '
|
|
|
|
)} It looks like there's an issue with device timer performance...`
|
|
|
|
);
|
|
|
|
console.log(
|
|
|
|
`${chalk.blue(
|
|
|
|
'[bridge] ⚠️ '
|
|
|
|
)} You may experience slow testing times as a result - ensure your device date/time correctly matches your debugger machine.`
|
|
|
|
);
|
|
|
|
// todo android only
|
|
|
|
// fake the RN warning for this
|
|
|
|
global.bridge.context.console.warn(
|
|
|
|
`Debugger and device times have drifted. ` +
|
|
|
|
`Please correct this by running adb shell "date \`date +%m%d%H%M%Y.%S\`" on your debugger machine.`
|
|
|
|
);
|
|
|
|
}
|
2018-03-25 02:41:43 +00:00
|
|
|
setTimeout(() => process.emit('bridge-attached'), 1);
|
2018-03-25 01:23:46 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
setBridgeProperty(key, value) {
|
|
|
|
customBridgeProps.push(key);
|
|
|
|
global.bridge[key] = value;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
},
|
|
|
|
};
|