Fix lost callbacks due to time drift between server and emulator

Reviewed By: yungsters

Differential Revision: D3434921

fbshipit-source-id: ec82374a8ed322d99beadac78a415f952ceb3ec8
This commit is contained in:
Krishna Monian
2016-06-15 13:13:09 -07:00
committed by Facebook Github Bot 0
parent f236667a17
commit 12a87b6674
6 changed files with 27 additions and 12 deletions

View File

@@ -80,7 +80,7 @@ import com.facebook.systrace.Systrace;
new AndroidInfoModule(),
new DeviceEventManagerModule(catalystApplicationContext, mHardwareBackBtnHandler),
new ExceptionsManagerModule(mReactInstanceManager.getDevSupportManager()),
new Timing(catalystApplicationContext),
new Timing(catalystApplicationContext, mReactInstanceManager.getDevSupportManager()),
new SourceCodeModule(mReactInstanceManager.getSourceUrl()),
uiManagerModule,
new JSCHeapCapture(catalystApplicationContext),

View File

@@ -30,6 +30,7 @@ import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.WritableArray;
import com.facebook.react.common.SystemClock;
import com.facebook.react.devsupport.DevSupportManager;
import com.facebook.react.uimanager.ReactChoreographer;
/**
@@ -38,6 +39,8 @@ import com.facebook.react.uimanager.ReactChoreographer;
public final class Timing extends ReactContextBaseJavaModule implements LifecycleEventListener,
OnExecutorUnregisteredListener {
private final DevSupportManager mDevSupportManager;
private static class Timer {
private final ExecutorToken mExecutorToken;
@@ -112,8 +115,9 @@ public final class Timing extends ReactContextBaseJavaModule implements Lifecycl
private @Nullable ReactChoreographer mReactChoreographer;
private boolean mFrameCallbackPosted = false;
public Timing(ReactApplicationContext reactContext) {
public Timing(ReactApplicationContext reactContext, DevSupportManager devSupportManager) {
super(reactContext);
mDevSupportManager = devSupportManager;
// We store timers sorted by finish time.
mTimers = new PriorityQueue<Timer>(
11, // Default capacity: for some reason they don't expose a (Comparator) constructor
@@ -213,6 +217,12 @@ public final class Timing extends ReactContextBaseJavaModule implements Lifecycl
final int duration,
final double jsSchedulingTime,
final boolean repeat) {
// If the times on the server and device have drifted throw an exception to warn the developer
// that things might not work or results may not be accurate. This is required only for
// developer builds.
if (mDevSupportManager.getDevSupportEnabled() && Math.abs(jsSchedulingTime - System.currentTimeMillis()) > 1000) {
throw new RuntimeException("System and emulator/device times have drifted. Please correct this by running adb shell \"date `date +%m%d%H%M%Y.%S`\" on your dev machine");
}
// Adjust for the amount of time it took for native to receive the timer registration call
long adjustedDuration = (long) Math.max(
0,