下面列出了怎么用com.facebook.react.bridge.ReadableNativeMap的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* @return a clone of the {@link ReactShadowNode} received by parameter. The cloned
* ReactShadowNode will contain a copy of all the internal data of the original node, but its
* props will be overridden with the {@link ReadableMap} received by parameter.
*/
@Nullable
@DoNotStrip
public ReactShadowNode cloneNodeWithNewProps(
ReactShadowNode node, @Nullable ReadableNativeMap newProps) {
if (DEBUG) {
FLog.d(TAG, "cloneNodeWithNewProps \n\tnode: " + node + "\n\tprops: " + newProps);
}
SystraceMessage.beginSection(
Systrace.TRACE_TAG_REACT_JAVA_BRIDGE,
"FabricUIManager.cloneNodeWithNewProps")
.flush();
try {
ReactShadowNode clone = node.mutableCopyWithNewProps(node.getInstanceHandle(),
newProps == null ? null : new ReactStylesDiffMap(newProps));
assertReactShadowNodeCopy(node, clone);
return clone;
} catch (Throwable t) {
handleException(node, t);
return null;
} finally{
Systrace.endSection(Systrace.TRACE_TAG_REACT_JAVA_BRIDGE);
}
}
/**
* @return a clone of the {@link ReactShadowNode} received by parameter. The cloned
* ReactShadowNode will contain a copy of all the internal data of the original node, but its
* props will be overridden with the {@link ReadableMap} received by parameter and its
* children set will be empty.
*/
@Nullable
@DoNotStrip
public ReactShadowNode cloneNodeWithNewChildrenAndProps(
ReactShadowNode node, ReadableNativeMap newProps) {
if (DEBUG) {
FLog.d(TAG, "cloneNodeWithNewChildrenAndProps \n\tnode: " + node + "\n\tnewProps: " + newProps);
}
SystraceMessage.beginSection(
Systrace.TRACE_TAG_REACT_JAVA_BRIDGE,
"FabricUIManager.cloneNodeWithNewChildrenAndProps")
.flush();
try {
ReactShadowNode clone =
node.mutableCopyWithNewChildrenAndProps(node.getInstanceHandle(),
newProps == null ? null : new ReactStylesDiffMap(newProps));
assertReactShadowNodeCopy(node, clone);
return clone;
} catch (Throwable t) {
handleException(node, t);
return null;
} finally{
Systrace.endSection(Systrace.TRACE_TAG_REACT_JAVA_BRIDGE);
}
}
public void testMapIterateOverMapWithBasicTypes() {
mCatalystInstance.getJSModule(TestJSToJavaParametersModule.class).returnMapWithBasicTypes();
waitForBridgeAndUIIdle();
List<ReadableMap> calls = mRecordingTestModule.getMapCalls();
assertEquals(1, calls.size());
ReadableNativeMap map = (ReadableNativeMap) calls.get(0);
assertNotNull(map);
ReadableMapKeySetIterator mapIterator = map.keySetIterator();
Set<String> keys = new HashSet<String>();
while (mapIterator.hasNextKey()) {
keys.add(mapIterator.nextKey());
}
Set<String> expectedKeys = new HashSet<String>(
Arrays.asList("stringKey", "doubleKey", "intKey", "booleanKey", "nullKey"));
assertEquals(keys, expectedKeys);
}
public void testMapIterateOverNestedMaps() {
mCatalystInstance.getJSModule(TestJSToJavaParametersModule.class).returnNestedMap();
waitForBridgeAndUIIdle();
List<ReadableMap> calls = mRecordingTestModule.getMapCalls();
assertEquals(1, calls.size());
ReadableNativeMap map = (ReadableNativeMap) calls.get(0);
assertNotNull(map);
ReadableMapKeySetIterator firstLevelIterator = map.keySetIterator();
String firstLevelKey = firstLevelIterator.nextKey();
assertEquals(firstLevelKey, "weHaveToGoDeeper");
ReadableNativeMap secondMap = map.getMap("weHaveToGoDeeper");
ReadableMapKeySetIterator secondLevelIterator = secondMap.keySetIterator();
String secondLevelKey = secondLevelIterator.nextKey();
assertEquals(secondLevelKey, "inception");
assertTrue(secondMap.getBoolean(secondLevelKey));
}
@ReactMethod
public void delete(String path, ReadableMap data, final Promise promise) {
Log.d(TAG, "delete: " + path);
HashMap<String,Object> map = ((ReadableNativeMap) data).toHashMap();
mApiMagnet.delete(path, map, new Api.Callback() {
@Override
public void resolve(Object result) {
promise.resolve(toReactArgument(result));
}
@Override
public void reject(String error) {
promise.reject(error, error);
}
});
}
/** Creates a new {@link ReactShadowNode} */
@Nullable
@DoNotStrip
public ReactShadowNode createNode(
int reactTag, String viewName, int rootTag, ReadableNativeMap props, long eventTarget) {
if (DEBUG) {
FLog.d(TAG, "createNode \n\ttag: " + reactTag +
"\n\tviewName: " + viewName +
"\n\trootTag: " + rootTag +
"\n\tprops: " + props);
}
try {
ViewManager viewManager = mViewManagerRegistry.get(viewName);
ReactShadowNode node = viewManager.createShadowNodeInstance(mReactApplicationContext);
ReactShadowNode rootNode = getRootNode(rootTag);
node.setRootTag(rootNode.getReactTag());
node.setViewClassName(viewName);
node.setInstanceHandle(eventTarget);
node.setReactTag(reactTag);
node.setThemedContext(rootNode.getThemedContext());
ReactStylesDiffMap styles = updateProps(node, props);
if (!node.isVirtual()) {
mUIViewOperationQueue.enqueueCreateView(
rootNode.getThemedContext(), reactTag, viewName, styles);
}
return node;
} catch (Throwable t) {
handleException(getRootNode(rootTag), t);
return null;
}
}
private ReactStylesDiffMap updateProps(ReactShadowNode node, @Nullable ReadableNativeMap props) {
ReactStylesDiffMap styles = null;
if (props != null) {
styles = new ReactStylesDiffMap(props);
node.updateProperties(styles);
}
return styles;
}
public void testInvalidIteratorExceptionThrown() {
mCatalystInstance.getJSModule(TestJSToJavaParametersModule.class).returnMapWithBasicTypes();
waitForBridgeAndUIIdle();
List<ReadableMap> calls = mRecordingTestModule.getMapCalls();
assertEquals(1, calls.size());
ReadableNativeMap map = (ReadableNativeMap) calls.get(0);
assertNotNull(map);
ReadableMapKeySetIterator mapIterator = map.keySetIterator();
while (mapIterator.hasNextKey()) {
mapIterator.nextKey();
}
assertInvalidIteratorExceptionThrown(mapIterator);
}
@Test
public void testCloneNodeWithNewProps() {
ReactShadowNode node = createViewNode();
ReadableNativeMap props = null; // TODO(ayc): Figure out how to create a Native map from tests.
ReactShadowNode clonedNode = mFabricUIManager.cloneNodeWithNewProps(node, props);
}
@Test
public void testCloneNodeWithNewChildrenAndProps() {
ReactShadowNode node = createViewNode();
ReadableNativeMap props = null;
ReactShadowNode clonedNode = mFabricUIManager.cloneNodeWithNewChildrenAndProps(node, props);
assertThat(clonedNode.getChildCount()).isZero();
}
static private Object fromReactArgument(Object object) {
if (object instanceof ReadableNativeArray) return ((ReadableNativeArray) object).toArrayList();
else if (object instanceof ReadableNativeMap) return ((ReadableNativeMap) object).toHashMap();
else return null;
}