/
ReactEventEmitter.java
67 lines (54 loc) · 2.18 KB
/
ReactEventEmitter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/**
* Copyright (c) 2014-present, Facebook, Inc.
*
* 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.uimanager.events;
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;
import com.facebook.react.uimanager.common.ViewUtil;
import java.io.Closeable;
import java.io.IOException;
import javax.annotation.Nullable;
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(ReactApplicationContext reactContext) {
mReactContext = reactContext;
}
public void register(@UIManagerType int uiManagerType, RCTEventEmitter eventEmitter) {
mEventEmitters.put(uiManagerType, eventEmitter);
}
public void unregister(@UIManagerType int uiManagerType) {
mEventEmitters.remove(uiManagerType);
}
@Override
public void receiveEvent(int targetReactTag, String eventName, @Nullable WritableMap event) {
getEventEmitter(targetReactTag).receiveEvent(targetReactTag, eventName, event);
}
@Override
public void receiveTouches(
String eventName,
WritableArray touches,
WritableArray changedIndices) {
Assertions.assertCondition(touches.size() > 0);
int reactTag = touches.getMap(0).getInt(TARGET_KEY);
getEventEmitter(reactTag).receiveTouches(eventName, touches, changedIndices);
}
private RCTEventEmitter getEventEmitter(int reactTag) {
int type = ViewUtil.getUIManagerType(reactTag);
RCTEventEmitter eventEmitter = mEventEmitters.get(type);
if (eventEmitter == null) {
eventEmitter = mReactContext.getJSModule(RCTEventEmitter.class);
}
return eventEmitter;
}
}