Fix NullPointerException when emiting event using UIManagerModule

Reviewed By: achen1

Differential Revision: D8321766

fbshipit-source-id: ae5052c83f46e08d540b90bf5b71c68f354c566d
This commit is contained in:
David Vacca 2018-06-08 06:15:49 -07:00 committed by Facebook Github Bot
parent e5aa5b7c50
commit 291c01f4ff
2 changed files with 14 additions and 5 deletions

View File

@ -96,13 +96,14 @@ public class EventDispatcher implements LifecycleEventListener {
private Event[] mEventsToDispatch = new Event[16];
private int mEventsToDispatchSize = 0;
private volatile @Nullable ReactEventEmitter mReactEventEmitter = new ReactEventEmitter();
private volatile ReactEventEmitter mReactEventEmitter;
private short mNextEventTypeId = 0;
private volatile boolean mHasDispatchScheduled = false;
public EventDispatcher(ReactApplicationContext reactContext) {
mReactContext = reactContext;
mReactContext.addLifecycleEventListener(this);
mReactEventEmitter = new ReactEventEmitter(mReactContext);
}
/**

View File

@ -12,6 +12,7 @@ import static com.facebook.react.uimanager.events.TouchesHelper.TARGET_KEY;
import android.util.Log;
import android.util.SparseArray;
import com.facebook.infer.annotation.Assertions;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.WritableArray;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.uimanager.common.UIManagerType;
@ -24,8 +25,11 @@ public class ReactEventEmitter implements RCTEventEmitter {
private static final String TAG = ReactEventEmitter.class.getSimpleName();
private final SparseArray<RCTEventEmitter> mEventEmitters = new SparseArray<>();
private final ReactApplicationContext mReactContext;
public ReactEventEmitter() { }
public ReactEventEmitter(ReactApplicationContext reactContext) {
mReactContext = reactContext;
}
public void register(@UIManagerType int uiManagerType, RCTEventEmitter eventEmitter) {
mEventEmitters.put(uiManagerType, eventEmitter);
@ -48,12 +52,16 @@ public class ReactEventEmitter implements RCTEventEmitter {
Assertions.assertCondition(touches.size() > 0);
int targetReactTag = touches.getMap(0).getInt(TARGET_KEY);
getEventEmitter(targetReactTag).receiveTouches(eventName, touches, changedIndices);
int reactTag = touches.getMap(0).getInt(TARGET_KEY);
getEventEmitter(reactTag).receiveTouches(eventName, touches, changedIndices);
}
private RCTEventEmitter getEventEmitter(int reactTag) {
int type = ViewUtil.getUIManagerType(reactTag);
return mEventEmitters.get(type);
RCTEventEmitter eventEmitter = mEventEmitters.get(type);
if (eventEmitter == null) {
eventEmitter = mReactContext.getJSModule(RCTEventEmitter.class);
}
return eventEmitter;
}
}