今天使用的ReactNative版本为0.61.5:
之前我们讲过UIManagerModule,可参考:
ReactNative源码分析之UIManagerModule.
我们提到,UIManagerModule本身是个NativeModule,它通过各种ReactMethod注解方法,提供JS直接调用,大多数方法直接由UIImplementation代理。
我们看下UIImplementation定义:
/**
* A class that is used to receive React commands from JS and translate them into a shadow node
* hierarchy that is then mapped to a native view hierarchy.
*/
public class UIImplementation {
}
整个UIImplementation起着承上启下的作用,对外给UIManagerModule提供入口方法,内部由三个重要的类提供功能:
1.ShadowNodeRegistry;
2.UIViewOperationQueue;
3.NativeViewHierarchyOptimizer;
这三个类分别扮演什么的角色,下面分别介绍。
一、ShadowNodeRegistry
看下该类的具体实现:
/**
* Simple container class to keep track of {@link ReactShadowNode}s associated with a particular
* UIManagerModule instance.
*/
public class ShadowNodeRegistry {
private final SparseArray<ReactShadowNode> mTagsToCSSNodes;
private final SparseBooleanArray mRootTags;
private final SingleThreadAsserter mThreadAsserter;
public ShadowNodeRegistry() {
mTagsToCSSNodes = new SparseArray<>();
mRootTags = new SparseBooleanArray();
mThreadAsserter = new SingleThreadAsserter();
}
public void addRootNode