Skip to content

Commit

Permalink
Remove bridge access from JavaTimerManager
Browse files Browse the repository at this point in the history
Summary:
The next step to making our existing timers logic usable outside of the context of the native module. This diff removes the bridge access through ReactContext in JavaTimerManager, and instead relies on a delegate that implements the JSTimers interface. I'm reusing the JSTimers interface for convenience even though it extends JavaScriptModule, which I don't plan on using for bridgeless RN.

Also changing from ReadableArray to ReadableNativeArray so that it can be directly accessed from C++.

Reviewed By: PeteTheHeat

Differential Revision: D17282188

fbshipit-source-id: 5c5e0b12a2250334e96885c220feb52146e57c83
  • Loading branch information
Emily Janzer authored and facebook-github-bot committed Sep 19, 2019
1 parent 4d774bd commit cc36b89
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public void doFrame(long frameTimeNanos) {
}

if (mTimersToCall != null) {
mReactApplicationContext.getJSModule(JSTimers.class).callTimers(mTimersToCall);
mJSTimers.callTimers(mTimersToCall);
mTimersToCall = null;
}

Expand Down Expand Up @@ -131,9 +131,7 @@ public void run() {
}

if (sendIdleEvents) {
mReactApplicationContext
.getJSModule(JSTimers.class)
.callIdleCallbacks(absoluteFrameStartTime);
mJSTimers.callIdleCallbacks(absoluteFrameStartTime);
}

mCurrentIdleCallbackRunnable = null;
Expand All @@ -145,6 +143,7 @@ public void cancel() {
}

private final ReactApplicationContext mReactApplicationContext;
private final JSTimers mJSTimers;
private final ReactChoreographer mReactChoreographer;
private final DevSupportManager mDevSupportManager;
private final Object mTimerGuard = new Object();
Expand All @@ -162,9 +161,11 @@ public void cancel() {

public JavaTimerManager(
ReactApplicationContext reactContext,
JSTimers jsTimers,
ReactChoreographer reactChoreographer,
DevSupportManager devSupportManager) {
mReactApplicationContext = reactContext;
mJSTimers = jsTimers;
mReactChoreographer = reactChoreographer;
mDevSupportManager = devSupportManager;

Expand Down Expand Up @@ -291,11 +292,9 @@ public void createTimer(
if (mDevSupportManager.getDevSupportEnabled()) {
long driftTime = Math.abs(remoteTime - deviceTime);
if (driftTime > 60000) {
mReactApplicationContext
.getJSModule(JSTimers.class)
.emitTimeDriftWarning(
"Debugger and device times have drifted by more than 60s. Please correct this by "
+ "running adb shell \"date `date +%m%d%H%M%Y.%S`\" on your debugger machine.");
mJSTimers.emitTimeDriftWarning(
"Debugger and device times have drifted by more than 60s. Please correct this by "
+ "running adb shell \"date `date +%m%d%H%M%Y.%S`\" on your debugger machine.");
}
}

Expand All @@ -304,7 +303,7 @@ public void createTimer(
if (duration == 0 && !repeat) {
WritableArray timerToCall = Arguments.createArray();
timerToCall.pushInt(callbackID);
mReactApplicationContext.getJSModule(JSTimers.class).callTimers(timerToCall);
mJSTimers.callTimers(timerToCall);
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.WritableArray;
import com.facebook.react.devsupport.interfaces.DevSupportManager;
import com.facebook.react.jstasks.HeadlessJsTaskContext;
import com.facebook.react.jstasks.HeadlessJsTaskEventListener;
Expand All @@ -26,8 +27,30 @@ public final class TimingModule extends ReactContextBaseJavaModule

public TimingModule(ReactApplicationContext reactContext, DevSupportManager devSupportManager) {
super(reactContext);

JSTimers jsTimersProxy =
new JSTimers() {
@Override
public void callTimers(WritableArray timerIDs) {
getReactApplicationContext().getJSModule(JSTimers.class).callTimers(timerIDs);
}

@Override
public void callIdleCallbacks(double frameTime) {
getReactApplicationContext().getJSModule(JSTimers.class).callIdleCallbacks(frameTime);
}

@Override
public void emitTimeDriftWarning(String warningMessage) {
getReactApplicationContext()
.getJSModule(JSTimers.class)
.emitTimeDriftWarning(warningMessage);
}
};

mJavaTimerManager =
new JavaTimerManager(reactContext, ReactChoreographer.getInstance(), devSupportManager);
new JavaTimerManager(
reactContext, jsTimersProxy, ReactChoreographer.getInstance(), devSupportManager);
}

@Override
Expand Down

0 comments on commit cc36b89

Please sign in to comment.