Skip to content

Commit

Permalink
allow calling NativeViewHierarchyManager.addRootView() off the UI thread
Browse files Browse the repository at this point in the history
Reviewed By: yungsters, shergin

Differential Revision: D4998706

fbshipit-source-id: f95cdcb0d193ffff2dfe39f9523a222d958ba5c6
  • Loading branch information
aaronechiu authored and facebook-github-bot committed May 8, 2017
1 parent 7932b93 commit 20c2ae8
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public NativeViewHierarchyManager(ViewManagerRegistry viewManagers, RootViewMana
mRootViewManager = manager;
}

public final View resolveView(int tag) {
public synchronized final View resolveView(int tag) {
View view = mTagsToViews.get(tag);
if (view == null) {
throw new IllegalViewOperationException("Trying to resolve view with tag " + tag
Expand All @@ -100,7 +100,7 @@ public final View resolveView(int tag) {
return view;
}

public final ViewManager resolveViewManager(int tag) {
public synchronized final ViewManager resolveViewManager(int tag) {
ViewManager viewManager = mTagsToViewManagers.get(tag);
if (viewManager == null) {
throw new IllegalViewOperationException("ViewManager for tag " + tag + " could not be found");
Expand All @@ -116,7 +116,7 @@ public void setLayoutAnimationEnabled(boolean enabled) {
mLayoutAnimationEnabled = enabled;
}

public void updateProperties(int tag, ReactStylesDiffMap props) {
public synchronized void updateProperties(int tag, ReactStylesDiffMap props) {
UiThreadUtil.assertOnUiThread();

try {
Expand All @@ -128,15 +128,15 @@ public void updateProperties(int tag, ReactStylesDiffMap props) {
}
}

public void updateViewExtraData(int tag, Object extraData) {
public synchronized void updateViewExtraData(int tag, Object extraData) {
UiThreadUtil.assertOnUiThread();

ViewManager viewManager = resolveViewManager(tag);
View viewToUpdate = resolveView(tag);
viewManager.updateExtraData(viewToUpdate, extraData);
}

public void updateLayout(
public synchronized void updateLayout(
int parentTag,
int tag,
int x,
Expand Down Expand Up @@ -200,7 +200,7 @@ private void updateLayout(View viewToUpdate, int x, int y, int width, int height
}
}

public void createView(
public synchronized void createView(
ThemedReactContext themedContext,
int tag,
String className,
Expand Down Expand Up @@ -305,11 +305,12 @@ private static String constructManageChildrenErrorMessage(
* a view which should be added at the specified index
* @param tagsToDelete list of tags corresponding to views that should be removed
*/
public void manageChildren(
public synchronized void manageChildren(
int tag,
@Nullable int[] indicesToRemove,
@Nullable ViewAtIndex[] viewsToAdd,
@Nullable int[] tagsToDelete) {
UiThreadUtil.assertOnUiThread();
final ViewGroup viewToManage = (ViewGroup) mTagsToViews.get(tag);
final ViewGroupManager viewManager = (ViewGroupManager) resolveViewManager(tag);
if (viewToManage == null) {
Expand Down Expand Up @@ -463,9 +464,10 @@ private static String constructSetChildrenErrorMessage(
/**
* Simplified version of manageChildren that only deals with adding children views
*/
public void setChildren(
public synchronized void setChildren(
int tag,
ReadableArray childrenTags) {
UiThreadUtil.assertOnUiThread();
ViewGroup viewToManage = (ViewGroup) mTagsToViews.get(tag);
ViewGroupManager viewManager = (ViewGroupManager) resolveViewManager(tag);

Expand All @@ -486,21 +488,18 @@ public void setChildren(

/**
* See {@link UIManagerModule#addMeasuredRootView}.
*
* Must be called from the UI thread.
*/
public void addRootView(
public synchronized void addRootView(
int tag,
SizeMonitoringFrameLayout view,
ThemedReactContext themedContext) {
addRootViewGroup(tag, view, themedContext);
}

protected final void addRootViewGroup(
protected synchronized final void addRootViewGroup(
int tag,
ViewGroup view,
ThemedReactContext themedContext) {
UiThreadUtil.assertOnUiThread();
if (view.getId() != View.NO_ID) {
throw new IllegalViewOperationException(
"Trying to add a root view with an explicit id already set. React Native uses " +
Expand All @@ -517,7 +516,7 @@ protected final void addRootViewGroup(
/**
* Releases all references to given native View.
*/
protected void dropView(View view) {
protected synchronized void dropView(View view) {
UiThreadUtil.assertOnUiThread();
if (!mRootTags.get(view.getId())) {
// For non-root views we notify viewmanager with {@link ViewManager#onDropInstance}
Expand All @@ -539,7 +538,7 @@ protected void dropView(View view) {
mTagsToViewManagers.remove(view.getId());
}

public void removeRootView(int rootViewTag) {
public synchronized void removeRootView(int rootViewTag) {
UiThreadUtil.assertOnUiThread();
if (!mRootTags.get(rootViewTag)) {
SoftAssertions.assertUnreachable(
Expand All @@ -554,7 +553,7 @@ public void removeRootView(int rootViewTag) {
* Returns true on success, false on failure. If successful, after calling, output buffer will be
* {x, y, width, height}.
*/
public void measure(int tag, int[] outputBuffer) {
public synchronized void measure(int tag, int[] outputBuffer) {
UiThreadUtil.assertOnUiThread();
View v = mTagsToViews.get(tag);
if (v == null) {
Expand Down Expand Up @@ -587,7 +586,7 @@ public void measure(int tag, int[] outputBuffer) {
* @param outputBuffer - output buffer that contains [x,y,width,height] of the view in coordinates
* relative to the device window
*/
public void measureInWindow(int tag, int[] outputBuffer) {
public synchronized void measureInWindow(int tag, int[] outputBuffer) {
UiThreadUtil.assertOnUiThread();
View v = mTagsToViews.get(tag);
if (v == null) {
Expand All @@ -610,15 +609,19 @@ public void measureInWindow(int tag, int[] outputBuffer) {
outputBuffer[3] = v.getHeight();
}

public int findTargetTagForTouch(int reactTag, float touchX, float touchY) {
public synchronized int findTargetTagForTouch(int reactTag, float touchX, float touchY) {
UiThreadUtil.assertOnUiThread();
View view = mTagsToViews.get(reactTag);
if (view == null) {
throw new JSApplicationIllegalArgumentException("Could not find view with tag " + reactTag);
}
return TouchTargetHelper.findTargetTagForTouch(touchX, touchY, (ViewGroup) view);
}

public void setJSResponder(int reactTag, int initialReactTag, boolean blockNativeResponder) {
public synchronized void setJSResponder(
int reactTag,
int initialReactTag,
boolean blockNativeResponder) {
if (!blockNativeResponder) {
mJSResponderHandler.setJSResponder(initialReactTag, null);
return;
Expand Down Expand Up @@ -652,7 +655,7 @@ void clearLayoutAnimation() {
mLayoutAnimator.reset();
}

/* package */ void startAnimationForNativeView(
/* package */ synchronized void startAnimationForNativeView(
int reactTag,
Animation animation,
@Nullable final Callback animationCallback) {
Expand Down Expand Up @@ -691,7 +694,10 @@ public void onCancel() {
}
}

public void dispatchCommand(int reactTag, int commandId, @Nullable ReadableArray args) {
public synchronized void dispatchCommand(
int reactTag,
int commandId,
@Nullable ReadableArray args) {
UiThreadUtil.assertOnUiThread();
View view = mTagsToViews.get(reactTag);
if (view == null) {
Expand All @@ -712,7 +718,7 @@ public void dispatchCommand(int reactTag, int commandId, @Nullable ReadableArray
* @param success will be called with the position of the selected item as the first argument, or
* no arguments if the menu is dismissed
*/
public void showPopupMenu(int reactTag, ReadableArray items, Callback success) {
public synchronized void showPopupMenu(int reactTag, ReadableArray items, Callback success) {
UiThreadUtil.assertOnUiThread();
View anchor = mTagsToViews.get(reactTag);
if (anchor == null) {
Expand Down Expand Up @@ -780,5 +786,4 @@ public void sendAccessibilityEvent(int tag, int eventType) {
}
AccessibilityHelper.sendAccessibilityEvent(view, eventType);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -564,29 +564,10 @@ public boolean isEmpty() {
}

public void addRootView(
final int tag,
final SizeMonitoringFrameLayout rootView,
final ThemedReactContext themedRootContext) {
if (UiThreadUtil.isOnUiThread()) {
mNativeViewHierarchyManager.addRootView(tag, rootView, themedRootContext);
} else {
final Semaphore semaphore = new Semaphore(0);
mReactApplicationContext.runOnUiQueueThread(
new Runnable() {
@Override
public void run() {
mNativeViewHierarchyManager.addRootView(tag, rootView, themedRootContext);
semaphore.release();
}
});
try {
SoftAssertions.assertCondition(
semaphore.tryAcquire(5000, TimeUnit.MILLISECONDS),
"Timed out adding root view");
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
final int tag,
final SizeMonitoringFrameLayout rootView,
final ThemedReactContext themedRootContext) {
mNativeViewHierarchyManager.addRootView(tag, rootView, themedRootContext);
}

/**
Expand Down

0 comments on commit 20c2ae8

Please sign in to comment.