Refactor RootShadowNodeRegistry to make it ThreadSafe

Reviewed By: achen1

Differential Revision: D7417965

fbshipit-source-id: 90fa007242d2f00a315a4db25d2b6a0949e4b0d3
This commit is contained in:
David Vacca 2018-03-27 18:27:13 -07:00 committed by Facebook Github Bot
parent fd4bc72512
commit 9c805062e7
2 changed files with 22 additions and 12 deletions

View File

@ -270,7 +270,7 @@ public class FabricUIManager implements UIManager {
mUIViewOperationQueue.dispatchViewUpdates( mUIViewOperationQueue.dispatchViewUpdates(
mCurrentBatch++, System.currentTimeMillis(), System.currentTimeMillis()); mCurrentBatch++, System.currentTimeMillis(), System.currentTimeMillis());
mRootShadowNodeRegistry.addNode(currentRootShadowNode); mRootShadowNodeRegistry.replaceNode(currentRootShadowNode);
} catch (Exception e) { } catch (Exception e) {
handleException(getRootNode(rootTag), e); handleException(getRootNode(rootTag), e);
} }
@ -320,7 +320,7 @@ public class FabricUIManager implements UIManager {
} }
@Override @Override
public synchronized <T extends SizeMonitoringFrameLayout & MeasureSpecProvider> int addRootView( public <T extends SizeMonitoringFrameLayout & MeasureSpecProvider> int addRootView(
final T rootView) { final T rootView) {
int rootTag = ReactRootViewTagGenerator.getNextRootViewTag(); int rootTag = ReactRootViewTagGenerator.getNextRootViewTag();
ThemedReactContext themedRootContext = ThemedReactContext themedRootContext =
@ -332,7 +332,7 @@ public class FabricUIManager implements UIManager {
int heightMeasureSpec = rootView.getHeightMeasureSpec(); int heightMeasureSpec = rootView.getHeightMeasureSpec();
updateRootView(rootShadowNode, widthMeasureSpec, heightMeasureSpec); updateRootView(rootShadowNode, widthMeasureSpec, heightMeasureSpec);
mRootShadowNodeRegistry.addNode(rootShadowNode); mRootShadowNodeRegistry.registerNode(rootShadowNode);
mUIViewOperationQueue.addRootView(rootTag, rootView, themedRootContext); mUIViewOperationQueue.addRootView(rootTag, rootView, themedRootContext);
return rootTag; return rootTag;
} }

View File

@ -7,30 +7,40 @@
package com.facebook.react.fabric; package com.facebook.react.fabric;
import android.util.SparseArray;
import com.facebook.react.uimanager.ReactShadowNode; import com.facebook.react.uimanager.ReactShadowNode;
import java.util.concurrent.ConcurrentHashMap;
import javax.annotation.concurrent.ThreadSafe;
/** /**
* Simple container class to keep track of {@link ReactShadowNode}s that represents the Root * Simple container class to keep track of {@link ReactShadowNode}s that represents the Root
* Shadow Nodes of a {@link FabricUIManager} * Shadow Nodes of a {@link FabricUIManager}.
*/ */
@ThreadSafe
public class RootShadowNodeRegistry { public class RootShadowNodeRegistry {
private final SparseArray<ReactShadowNode> mTagsToRootNodes; private final ConcurrentHashMap<Integer, ReactShadowNode> mTagsToRootNodes = new ConcurrentHashMap<>();
public RootShadowNodeRegistry() { /**
mTagsToRootNodes = new SparseArray<>(); * Registers the {@link ReactShadowNode} received as a parameter as a RootShadowNode.
} */
public synchronized void registerNode(ReactShadowNode node) {
public void addNode(ReactShadowNode node) {
mTagsToRootNodes.put(node.getReactTag(), node); mTagsToRootNodes.put(node.getReactTag(), node);
} }
public void removeNode(int tag) { /**
* Register the {@link ReactShadowNode} received as a parameter as a RootShadowNode, replacing
* the previous RootShadowNode associated for the {@link ReactShadowNode#getReactTag()}
*/
public void replaceNode(ReactShadowNode node) {
mTagsToRootNodes.replace(node.getReactTag(), node);
}
public void removeNode(Integer tag) {
mTagsToRootNodes.remove(tag); mTagsToRootNodes.remove(tag);
} }
public ReactShadowNode getNode(int tag) { public ReactShadowNode getNode(int tag) {
return mTagsToRootNodes.get(tag); return mTagsToRootNodes.get(tag);
} }
} }