Flows between RN Threads

Reviewed By: tadeuzagallo

Differential Revision: D2743733

fb-gh-sync-id: df4ae69a3501a37e08286857a8d4be3cd27c0ac3
This commit is contained in:
Mike Armstrong
2016-01-04 02:15:19 -08:00
committed by facebook-github-bot-3
parent cdad5c047a
commit e42c6d4446
8 changed files with 106 additions and 25 deletions

View File

@@ -56,6 +56,7 @@ public class CatalystInstanceImpl implements CatalystInstance {
private final TraceListener mTraceListener;
private final JavaScriptModuleRegistry mJSModuleRegistry;
private final JSBundleLoader mJSBundleLoader;
private volatile int mTraceID = 0;
// Access from native modules thread
private final NativeModuleRegistry mJavaRegistry;
@@ -176,12 +177,23 @@ public class CatalystInstanceImpl implements CatalystInstance {
incrementPendingJSCalls();
final int traceID = mTraceID++;
Systrace.startAsyncFlow(
Systrace.TRACE_TAG_REACT_JAVA_BRIDGE,
tracingName,
traceID);
mCatalystQueueConfiguration.getJSQueueThread().runOnQueue(
new Runnable() {
@Override
public void run() {
mCatalystQueueConfiguration.getJSQueueThread().assertIsOnThread();
Systrace.endAsyncFlow(
Systrace.TRACE_TAG_REACT_JAVA_BRIDGE,
tracingName,
traceID);
if (mDestroyed) {
return;
}
@@ -208,12 +220,23 @@ public class CatalystInstanceImpl implements CatalystInstance {
incrementPendingJSCalls();
final int traceID = mTraceID++;
Systrace.startAsyncFlow(
Systrace.TRACE_TAG_REACT_JAVA_BRIDGE,
"<callback>",
traceID);
mCatalystQueueConfiguration.getJSQueueThread().runOnQueue(
new Runnable() {
@Override
public void run() {
mCatalystQueueConfiguration.getJSQueueThread().assertIsOnThread();
Systrace.endAsyncFlow(
Systrace.TRACE_TAG_REACT_JAVA_BRIDGE,
"<callback>",
traceID);
if (mDestroyed) {
return;
}

View File

@@ -18,9 +18,12 @@ package com.facebook.react.uimanager.events;
*/
public abstract class Event<T extends Event> {
private static int sUniqueID = 0;
private boolean mInitialized;
private int mViewTag;
private long mTimestampMs;
private int mUniqueID = sUniqueID++;
protected Event() {
}
@@ -80,6 +83,13 @@ public abstract class Event<T extends Event> {
return 0;
}
/**
* @return The unique id of this event.
*/
public int getUniqueID() {
return mUniqueID;
}
/**
* Called when the EventDispatcher is done with an event, either because it was dispatched or
* because it was coalesced with another Event.

View File

@@ -100,6 +100,7 @@ public class EventDispatcher implements LifecycleEventListener {
private volatile @Nullable ScheduleDispatchFrameCallback mCurrentFrameCallback;
private short mNextEventTypeId = 0;
private volatile boolean mHasDispatchScheduled = false;
private volatile int mHasDispatchScheduledCount = 0;
public EventDispatcher(ReactApplicationContext reactContext) {
mReactContext = reactContext;
@@ -113,6 +114,10 @@ public class EventDispatcher implements LifecycleEventListener {
Assertions.assertCondition(event.isInitialized(), "Dispatched event hasn't been initialized");
synchronized (mEventsStagingLock) {
mEventStaging.add(event);
Systrace.startAsyncFlow(
Systrace.TRACE_TAG_REACT_JAVA_BRIDGE,
event.getEventName(),
event.getUniqueID());
}
}
@@ -242,6 +247,10 @@ public class EventDispatcher implements LifecycleEventListener {
if (!mHasDispatchScheduled) {
mHasDispatchScheduled = true;
Systrace.startAsyncFlow(
Systrace.TRACE_TAG_REACT_JAVA_BRIDGE,
"ScheduleDispatchFrameCallback",
mHasDispatchScheduledCount);
mReactContext.runOnJSQueueThread(mDispatchEventsRunnable);
}
@@ -263,7 +272,12 @@ public class EventDispatcher implements LifecycleEventListener {
public void run() {
Systrace.beginSection(Systrace.TRACE_TAG_REACT_JAVA_BRIDGE, "DispatchEventsRunnable");
try {
Systrace.endAsyncFlow(
Systrace.TRACE_TAG_REACT_JAVA_BRIDGE,
"ScheduleDispatchFrameCallback",
mHasDispatchScheduledCount);
mHasDispatchScheduled = false;
mHasDispatchScheduledCount++;
Assertions.assertNotNull(mRCTEventEmitter);
synchronized (mEventsToDispatchLock) {
// We avoid allocating an array and iterator, and "sorting" if we don't need to.
@@ -277,6 +291,10 @@ public class EventDispatcher implements LifecycleEventListener {
if (event == null) {
continue;
}
Systrace.endAsyncFlow(
Systrace.TRACE_TAG_REACT_JAVA_BRIDGE,
event.getEventName(),
event.getUniqueID());
event.dispatch(mRCTEventEmitter);
event.dispose();
}