Files
react-native/ReactAndroid/src/main/java/com/facebook/react/bridge/DynamicFromObject.java
T
Spencer Ahrens a46fba5dd3 use DynamicFromObject to avoid maps
Summary:
Changes our property access pattern to iterate through props once and pass the Object value directly rather than looking the value up in the map with the key.

Note some ViewManagers methods (especially yoga related ones on shadow nodes) expect a `Dyanamic`, so this diff also creates Dynamic's only when needed by the hand-written code, and introduces a new `DynamicWithObject` to create them that simply wraps the underlying object (as opposed to `DynamicWithMap` which wraps the map and does a lookup any time the `Dynamic` is accessed.

Reviewed By: mdvacca

Differential Revision: D14453300

fbshipit-source-id: df98567b6eff1e6b7c611f179eb11e413fb94e5d
2019-03-25 12:12:10 -07:00

89 lines
1.8 KiB
Java

/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
package com.facebook.react.bridge;
import com.facebook.common.logging.FLog;
import com.facebook.react.common.ReactConstants;
import javax.annotation.Nullable;
/**
* Implementation of Dynamic wrapping a ReadableArray.
*/
public class DynamicFromObject implements Dynamic {
private @Nullable Object mObject;
public DynamicFromObject(@Nullable Object obj) {
mObject = obj;
}
@Override
public void recycle() {
// Noop - nothing to recycle since there is no pooling
}
@Override
public boolean isNull() {
return mObject == null;
}
@Override
public boolean asBoolean() {
return (boolean)mObject;
}
@Override
public double asDouble() {
return (double)mObject;
}
@Override
public int asInt() {
// Numbers from JS are always Doubles
return ((Double)mObject).intValue();
}
@Override
public String asString() {
return (String)mObject;
}
@Override
public ReadableArray asArray() {
return (ReadableArray)mObject;
}
@Override
public ReadableMap asMap() {
return (ReadableMap)mObject;
}
@Override
public ReadableType getType() {
if (isNull()) {
return ReadableType.Null;
}
if (mObject instanceof Boolean) {
return ReadableType.Boolean;
}
if (mObject instanceof Number) {
return ReadableType.Number;
}
if (mObject instanceof String) {
return ReadableType.String;
}
if (mObject instanceof ReadableMap) {
return ReadableType.Map;
}
if (mObject instanceof ReadableArray) {
return ReadableType.Array;
}
FLog.e(ReactConstants.TAG, "Unmapped object type " + mObject.getClass().getName());
return ReadableType.Null;
}
}