Untangle NativeAnimatedNodesManager from UIManager internals.
Reviewed By: AaaChiuuu Differential Revision: D5861523 fbshipit-source-id: 39eb79b91d4d5e2295d03369f1be585ae7836c75
This commit is contained in:
parent
5180995666
commit
4fc9e20fce
|
@ -11,7 +11,6 @@ package com.facebook.react.animated;
|
||||||
|
|
||||||
import android.util.SparseArray;
|
import android.util.SparseArray;
|
||||||
|
|
||||||
import com.facebook.infer.annotation.Assertions;
|
|
||||||
import com.facebook.common.logging.FLog;
|
import com.facebook.common.logging.FLog;
|
||||||
import com.facebook.react.bridge.Arguments;
|
import com.facebook.react.bridge.Arguments;
|
||||||
import com.facebook.react.bridge.Callback;
|
import com.facebook.react.bridge.Callback;
|
||||||
|
@ -59,7 +58,7 @@ import javax.annotation.Nullable;
|
||||||
// Mapping of a view tag and an event name to a list of event animation drivers. 99% of the time
|
// Mapping of a view tag and an event name to a list of event animation drivers. 99% of the time
|
||||||
// there will be only one driver per mapping so all code code should be optimized around that.
|
// there will be only one driver per mapping so all code code should be optimized around that.
|
||||||
private final Map<String, List<EventAnimationDriver>> mEventDrivers = new HashMap<>();
|
private final Map<String, List<EventAnimationDriver>> mEventDrivers = new HashMap<>();
|
||||||
private final Map<String, Map<String, String>> mCustomEventTypes;
|
private final UIManagerModule.CustomEventNamesResolver mCustomEventNamesResolver;
|
||||||
private final UIImplementation mUIImplementation;
|
private final UIImplementation mUIImplementation;
|
||||||
private int mAnimatedGraphBFSColor = 0;
|
private int mAnimatedGraphBFSColor = 0;
|
||||||
// Used to avoid allocating a new array on every frame in `runUpdates` and `onEventDispatch`.
|
// Used to avoid allocating a new array on every frame in `runUpdates` and `onEventDispatch`.
|
||||||
|
@ -68,8 +67,7 @@ import javax.annotation.Nullable;
|
||||||
public NativeAnimatedNodesManager(UIManagerModule uiManager) {
|
public NativeAnimatedNodesManager(UIManagerModule uiManager) {
|
||||||
mUIImplementation = uiManager.getUIImplementation();
|
mUIImplementation = uiManager.getUIImplementation();
|
||||||
uiManager.getEventDispatcher().addListener(this);
|
uiManager.getEventDispatcher().addListener(this);
|
||||||
Object customEventTypes = Assertions.assertNotNull(uiManager.getConstants()).get("customDirectEventTypes");
|
mCustomEventNamesResolver = uiManager.getDirectEventNamesResolver();
|
||||||
mCustomEventTypes = (Map<String, Map<String, String>>) customEventTypes;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*package*/ @Nullable AnimatedNode getNodeById(int id) {
|
/*package*/ @Nullable AnimatedNode getNodeById(int id) {
|
||||||
|
@ -371,12 +369,7 @@ import javax.annotation.Nullable;
|
||||||
|
|
||||||
if (!mEventDrivers.isEmpty()) {
|
if (!mEventDrivers.isEmpty()) {
|
||||||
// If the event has a different name in native convert it to it's JS name.
|
// If the event has a different name in native convert it to it's JS name.
|
||||||
String eventName = event.getEventName();
|
String eventName = mCustomEventNamesResolver.resolveCustomEventName(event.getEventName());
|
||||||
Map<String, String> customEventType = mCustomEventTypes.get(eventName);
|
|
||||||
if (customEventType != null) {
|
|
||||||
eventName = customEventType.get("registrationName");
|
|
||||||
}
|
|
||||||
|
|
||||||
List<EventAnimationDriver> driversForKey = mEventDrivers.get(event.getViewTag() + eventName);
|
List<EventAnimationDriver> driversForKey = mEventDrivers.get(event.getViewTag() + eventName);
|
||||||
if (driversForKey != null) {
|
if (driversForKey != null) {
|
||||||
for (EventAnimationDriver driver : driversForKey) {
|
for (EventAnimationDriver driver : driversForKey) {
|
||||||
|
|
|
@ -70,6 +70,17 @@ import javax.annotation.Nullable;
|
||||||
public class UIManagerModule extends ReactContextBaseJavaModule implements
|
public class UIManagerModule extends ReactContextBaseJavaModule implements
|
||||||
OnBatchCompleteListener, LifecycleEventListener, PerformanceCounter {
|
OnBatchCompleteListener, LifecycleEventListener, PerformanceCounter {
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Resolves a name coming from native side to a name of the event that is exposed to JS.
|
||||||
|
*/
|
||||||
|
public interface CustomEventNamesResolver {
|
||||||
|
/**
|
||||||
|
* Returns custom event name by the provided event name.
|
||||||
|
*/
|
||||||
|
@Nullable String resolveCustomEventName(String eventName);
|
||||||
|
}
|
||||||
|
|
||||||
protected static final String NAME = "UIManager";
|
protected static final String NAME = "UIManager";
|
||||||
|
|
||||||
private static final boolean DEBUG = false;
|
private static final boolean DEBUG = false;
|
||||||
|
@ -164,6 +175,27 @@ public class UIManagerModule extends ReactContextBaseJavaModule implements
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Resolves Direct Event name exposed to JS from the one known to the Native side.
|
||||||
|
*/
|
||||||
|
public CustomEventNamesResolver getDirectEventNamesResolver() {
|
||||||
|
return new CustomEventNamesResolver() {
|
||||||
|
@Override
|
||||||
|
public @Nullable String resolveCustomEventName(String eventName) {
|
||||||
|
Map<String, Map> directEventTypes =
|
||||||
|
(Map<String, Map>) getConstants().get(
|
||||||
|
UIManagerModuleConstantsHelper.CUSTOM_DIRECT_EVENT_TYPES_KEY);
|
||||||
|
if (directEventTypes != null) {
|
||||||
|
Map<String, String> customEventType = (Map<String, String>) directEventTypes.get(eventName);
|
||||||
|
if (customEventType != null) {
|
||||||
|
return customEventType.get("registrationName");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return eventName;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Map<String, Long> getPerformanceCounters() {
|
public Map<String, Long> getPerformanceCounters() {
|
||||||
return mUIImplementation.getProfiledBatchPerfCounters();
|
return mUIImplementation.getProfiledBatchPerfCounters();
|
||||||
|
|
|
@ -12,7 +12,6 @@ package com.facebook.react.uimanager;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import com.facebook.react.bridge.ReactApplicationContext;
|
|
||||||
import com.facebook.react.common.MapBuilder;
|
import com.facebook.react.common.MapBuilder;
|
||||||
import com.facebook.systrace.Systrace;
|
import com.facebook.systrace.Systrace;
|
||||||
import com.facebook.systrace.SystraceMessage;
|
import com.facebook.systrace.SystraceMessage;
|
||||||
|
@ -25,8 +24,8 @@ import static com.facebook.systrace.Systrace.TRACE_TAG_REACT_JAVA_BRIDGE;
|
||||||
*/
|
*/
|
||||||
/* package */ class UIManagerModuleConstantsHelper {
|
/* package */ class UIManagerModuleConstantsHelper {
|
||||||
|
|
||||||
private static final String CUSTOM_BUBBLING_EVENT_TYPES_KEY = "customBubblingEventTypes";
|
/* package */ static final String CUSTOM_BUBBLING_EVENT_TYPES_KEY = "customBubblingEventTypes";
|
||||||
private static final String CUSTOM_DIRECT_EVENT_TYPES_KEY = "customDirectEventTypes";
|
/* package */ static final String CUSTOM_DIRECT_EVENT_TYPES_KEY = "customDirectEventTypes";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Generates map of constants that is then exposed by {@link UIManagerModule}.
|
* Generates map of constants that is then exposed by {@link UIManagerModule}.
|
||||||
|
@ -41,8 +40,7 @@ import static com.facebook.systrace.Systrace.TRACE_TAG_REACT_JAVA_BRIDGE;
|
||||||
* TODO(6845124): Create a test for this
|
* TODO(6845124): Create a test for this
|
||||||
*/
|
*/
|
||||||
/* package */ static Map<String, Object> createConstants(
|
/* package */ static Map<String, Object> createConstants(
|
||||||
List<ViewManager> viewManagers,
|
List<ViewManager> viewManagers, boolean lazyViewManagersEnabled) {
|
||||||
boolean lazyViewManagersEnabled) {
|
|
||||||
Map<String, Object> constants = UIManagerModuleConstants.getConstants();
|
Map<String, Object> constants = UIManagerModuleConstants.getConstants();
|
||||||
|
|
||||||
// Generic/default event types:
|
// Generic/default event types:
|
||||||
|
@ -69,18 +67,20 @@ import static com.facebook.systrace.Systrace.TRACE_TAG_REACT_JAVA_BRIDGE;
|
||||||
if (viewManagerBubblingEvents != null) {
|
if (viewManagerBubblingEvents != null) {
|
||||||
recursiveMerge(allBubblingEventTypes, viewManagerBubblingEvents);
|
recursiveMerge(allBubblingEventTypes, viewManagerBubblingEvents);
|
||||||
recursiveMerge(viewManagerBubblingEvents, genericBubblingEventTypes);
|
recursiveMerge(viewManagerBubblingEvents, genericBubblingEventTypes);
|
||||||
viewManagerConstants.put("bubblingEventTypes", viewManagerBubblingEvents);
|
|
||||||
} else {
|
} else {
|
||||||
viewManagerConstants.put("bubblingEventTypes", genericBubblingEventTypes);
|
viewManagerBubblingEvents = genericBubblingEventTypes;
|
||||||
}
|
}
|
||||||
|
viewManagerConstants.put("bubblingEventTypes", viewManagerBubblingEvents);
|
||||||
|
|
||||||
Map viewManagerDirectEvents = viewManager.getExportedCustomDirectEventTypeConstants();
|
Map viewManagerDirectEvents = viewManager.getExportedCustomDirectEventTypeConstants();
|
||||||
if (viewManagerDirectEvents != null) {
|
if (viewManagerDirectEvents != null) {
|
||||||
recursiveMerge(allDirectEventTypes, viewManagerDirectEvents);
|
recursiveMerge(allDirectEventTypes, viewManagerDirectEvents);
|
||||||
recursiveMerge(viewManagerDirectEvents, genericDirectEventTypes);
|
recursiveMerge(viewManagerDirectEvents, genericDirectEventTypes);
|
||||||
viewManagerConstants.put("directEventTypes", viewManagerDirectEvents);
|
|
||||||
} else {
|
} else {
|
||||||
viewManagerConstants.put("directEventTypes", genericDirectEventTypes);
|
viewManagerDirectEvents = genericDirectEventTypes;
|
||||||
}
|
}
|
||||||
|
viewManagerConstants.put("directEventTypes", viewManagerDirectEvents);
|
||||||
|
|
||||||
Map customViewConstants = viewManager.getExportedViewConstants();
|
Map customViewConstants = viewManager.getExportedViewConstants();
|
||||||
if (customViewConstants != null) {
|
if (customViewConstants != null) {
|
||||||
viewManagerConstants.put("Constants", customViewConstants);
|
viewManagerConstants.put("Constants", customViewConstants);
|
||||||
|
|
|
@ -22,6 +22,8 @@ import com.facebook.react.uimanager.events.Event;
|
||||||
import com.facebook.react.uimanager.events.EventDispatcher;
|
import com.facebook.react.uimanager.events.EventDispatcher;
|
||||||
import com.facebook.react.uimanager.events.RCTEventEmitter;
|
import com.facebook.react.uimanager.events.RCTEventEmitter;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Rule;
|
import org.junit.Rule;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
@ -108,6 +110,27 @@ public class NativeAnimatedNodeTraversalTest {
|
||||||
return MapBuilder.of("customDirectEventTypes", MapBuilder.newHashMap());
|
return MapBuilder.of("customDirectEventTypes", MapBuilder.newHashMap());
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
PowerMockito
|
||||||
|
.when(mUIManagerMock.getDirectEventNamesResolver())
|
||||||
|
.thenAnswer(new Answer<UIManagerModule.CustomEventNamesResolver>() {
|
||||||
|
@Override
|
||||||
|
public UIManagerModule.CustomEventNamesResolver answer(InvocationOnMock invocation) throws Throwable {
|
||||||
|
return new UIManagerModule.CustomEventNamesResolver() {
|
||||||
|
@Override
|
||||||
|
public String resolveCustomEventName(String eventName) {
|
||||||
|
Map<String, Map> directEventTypes =
|
||||||
|
(Map<String, Map>) mUIManagerMock.getConstants().get("customDirectEventTypes");
|
||||||
|
if (directEventTypes != null) {
|
||||||
|
Map<String, String> customEventType = (Map<String, String>) directEventTypes.get(eventName);
|
||||||
|
if (customEventType != null) {
|
||||||
|
return customEventType.get("registrationName");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return eventName;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
});
|
||||||
mNativeAnimatedNodesManager = new NativeAnimatedNodesManager(mUIManagerMock);
|
mNativeAnimatedNodesManager = new NativeAnimatedNodesManager(mUIManagerMock);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue