Skip to content

Commit 0f48f89

Browse files
authored
[MOB-9962] Migrate to NativeEventEmitter (#924)
* Migrate the current IBGEventEmitter which uses the old NativeAppEventEmitter and DeviceEventEmitter to the new NativeEventEmitter which requires each module to have its own event emitter so the way we use emitter got restructured. * Eliminate NetworkLogger's event emitters usage by using callbacks.
1 parent ec1f130 commit 0f48f89

27 files changed

+219
-250
lines changed

android/src/main/java/com/instabug/reactlibrary/RNInstabugBugReportingModule.java

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import com.facebook.react.bridge.Arguments;
77
import com.facebook.react.bridge.Callback;
88
import com.facebook.react.bridge.ReactApplicationContext;
9-
import com.facebook.react.bridge.ReactContextBaseJavaModule;
109
import com.facebook.react.bridge.ReactMethod;
1110
import com.facebook.react.bridge.ReadableArray;
1211
import com.facebook.react.bridge.WritableMap;
@@ -20,15 +19,14 @@
2019
import com.instabug.library.invocation.util.InstabugFloatingButtonEdge;
2120
import com.instabug.library.invocation.util.InstabugVideoRecordingButtonPosition;
2221
import com.instabug.reactlibrary.utils.ArrayUtil;
23-
import com.instabug.reactlibrary.utils.InstabugUtil;
22+
import com.instabug.reactlibrary.utils.EventEmitterModule;
2423
import com.instabug.reactlibrary.utils.MainThreadHandler;
2524

2625
import java.util.ArrayList;
2726

2827
import javax.annotation.Nonnull;
2928

30-
public class RNInstabugBugReportingModule extends ReactContextBaseJavaModule {
31-
29+
public class RNInstabugBugReportingModule extends EventEmitterModule {
3230
public RNInstabugBugReportingModule(ReactApplicationContext reactContext) {
3331
super(reactContext);
3432
}
@@ -39,6 +37,16 @@ public String getName() {
3937
return "IBGBugReporting";
4038
}
4139

40+
@ReactMethod
41+
public void addListener(String event) {
42+
super.addListener(event);
43+
}
44+
45+
@ReactMethod
46+
public void removeListeners(Integer count) {
47+
super.removeListeners(count);
48+
}
49+
4250
/**
4351
* Enable or disable all BugReporting related features.
4452
* @param isEnabled boolean indicating enabled or disabled.
@@ -235,10 +243,10 @@ public void setOnInvokeHandler(final Callback onInvokeHandler) {
235243
@Override
236244
public void run() {
237245
try {
238-
BugReporting.setOnInvokeCallback(new OnInvokeCallback() {
246+
BugReporting.setOnInvokeCallback(new OnInvokeCallback() {
239247
@Override
240248
public void onInvoke() {
241-
InstabugUtil.sendEvent(getReactApplicationContext(), Constants.IBG_PRE_INVOCATION_HANDLER, null);
249+
sendEvent(Constants.IBG_PRE_INVOCATION_HANDLER, null);
242250
}
243251
});
244252
} catch (java.lang.Exception exception) {
@@ -286,7 +294,7 @@ public void call(DismissType dismissType, ReportType reportType) {
286294
WritableMap params = Arguments.createMap();
287295
params.putString("dismissType", dismissType.toString());
288296
params.putString("reportType", reportType.toString());
289-
InstabugUtil.sendEvent(getReactApplicationContext(), Constants.IBG_POST_INVOCATION_HANDLER, params);
297+
sendEvent(Constants.IBG_POST_INVOCATION_HANDLER, params);
290298
}
291299
});
292300
} catch (java.lang.Exception exception) {

android/src/main/java/com/instabug/reactlibrary/RNInstabugReactnativeModule.java

Lines changed: 21 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,32 @@
11
package com.instabug.reactlibrary;
22

33
import android.annotation.SuppressLint;
4-
import android.app.Application;
54
import android.graphics.Bitmap;
65
import android.net.Uri;
7-
import android.os.Handler;
8-
import android.os.Looper;
96
import android.util.Log;
107
import android.view.View;
118

129
import com.facebook.react.bridge.Arguments;
1310
import com.facebook.react.bridge.Promise;
1411
import com.facebook.react.bridge.ReactApplicationContext;
15-
import com.facebook.react.bridge.ReactContextBaseJavaModule;
1612
import com.facebook.react.bridge.ReactMethod;
1713
import com.facebook.react.bridge.ReadableArray;
18-
import com.facebook.react.bridge.ReadableMap;
1914
import com.facebook.react.bridge.WritableArray;
2015
import com.facebook.react.bridge.WritableNativeArray;
2116
import com.facebook.react.bridge.WritableMap;
2217
import com.facebook.react.bridge.WritableNativeMap;
2318
import com.facebook.react.bridge.Callback;
2419

25-
import com.facebook.react.modules.core.DeviceEventManagerModule;
2620
import com.facebook.react.uimanager.NativeViewHierarchyManager;
2721
import com.facebook.react.uimanager.UIBlock;
2822
import com.facebook.react.uimanager.UIManagerModule;
2923
import com.instabug.apm.APM;
3024
import com.instabug.bug.BugReporting;
3125
import com.instabug.bug.instabugdisclaimer.Internal;
32-
import com.instabug.bug.invocation.Option;
3326
import com.instabug.chat.Replies;
34-
import com.instabug.crash.CrashReporting;
3527
import com.instabug.featuresrequest.FeatureRequests;
36-
import com.instabug.featuresrequest.ActionType;
3728
import com.instabug.library.Feature;
3829
import com.instabug.library.Instabug;
39-
import com.instabug.library.InstabugState;
4030
import com.instabug.library.OnSdkDismissCallback;
4131
import com.instabug.library.Platform;
4232
import com.instabug.library.LogLevel;
@@ -45,8 +35,6 @@
4535
import com.instabug.library.invocation.InstabugInvocationEvent;
4636
import com.instabug.library.InstabugColorTheme;
4737
import com.instabug.library.invocation.OnInvokeCallback;
48-
import com.instabug.library.invocation.util.InstabugFloatingButtonEdge;
49-
import com.instabug.library.invocation.util.InstabugVideoRecordingButtonPosition;
5038
import com.instabug.library.logging.InstabugLog;
5139
import com.instabug.library.ui.onboarding.WelcomeMessage;
5240
import com.instabug.library.InstabugCustomTextPlaceHolder;
@@ -55,6 +43,7 @@
5543
import com.instabug.library.visualusersteps.State;
5644

5745
import com.instabug.reactlibrary.utils.ArrayUtil;
46+
import com.instabug.reactlibrary.utils.EventEmitterModule;
5847
import com.instabug.reactlibrary.utils.InstabugUtil;
5948
import com.instabug.reactlibrary.utils.MainThreadHandler;
6049
import com.instabug.reactlibrary.utils.ReportUtil;
@@ -84,7 +73,7 @@
8473
/**
8574
* The type Rn instabug reactnative module.
8675
*/
87-
public class RNInstabugReactnativeModule extends ReactContextBaseJavaModule {
76+
public class RNInstabugReactnativeModule extends EventEmitterModule {
8877

8978
private static final String TAG = RNInstabugReactnativeModule.class.getSimpleName();
9079

@@ -98,15 +87,26 @@ public class RNInstabugReactnativeModule extends ReactContextBaseJavaModule {
9887
*/
9988
public RNInstabugReactnativeModule(ReactApplicationContext reactContext) {
10089
super(reactContext);
101-
//init placHolders
90+
//init placeHolders
10291
placeHolders = new InstabugCustomTextPlaceHolder();
10392
}
10493

10594
@Override
10695
public String getName() {
10796
return "Instabug";
10897
}
109-
98+
99+
100+
@ReactMethod
101+
public void addListener(String event) {
102+
super.addListener(event);
103+
}
104+
105+
@ReactMethod
106+
public void removeListeners(Integer count) {
107+
super.removeListeners(count);
108+
}
109+
110110
/**
111111
* Enables or disables Instabug functionality.
112112
* @param isEnabled A boolean to enable/disable Instabug.
@@ -953,7 +953,7 @@ public void run() {
953953
BugReporting.setOnInvokeCallback(new OnInvokeCallback() {
954954
@Override
955955
public void onInvoke() {
956-
sendEvent(getReactApplicationContext(), "IBGpreInvocationHandler", null);
956+
sendEvent("IBGpreInvocationHandler", null);
957957
}
958958
});
959959
} catch (java.lang.Exception exception) {
@@ -986,7 +986,7 @@ public void onReportCreated(Report report) {
986986
reportParam.putString("userData", report.getUserData());
987987
reportParam.putMap("userAttributes", convertFromHashMapToWriteableMap(report.getUserAttributes()));
988988
reportParam.putMap("fileAttachments", convertFromHashMapToWriteableMap(report.getFileAttachments()));
989-
sendEvent(getReactApplicationContext(), "IBGpreSendingHandler", reportParam);
989+
sendEvent("IBGpreSendingHandler", reportParam);
990990
currentReport = report;
991991
}
992992
});
@@ -1159,7 +1159,7 @@ public void call(DismissType dismissType, ReportType reportType) {
11591159
WritableMap params = Arguments.createMap();
11601160
params.putString("dismissType", dismissType.toString());
11611161
params.putString("reportType", reportType.toString());
1162-
sendEvent(getReactApplicationContext(), "IBGpostInvocationHandler", params);
1162+
sendEvent("IBGpostInvocationHandler", params);
11631163
}
11641164
});
11651165
} catch (java.lang.Exception exception) {
@@ -1203,7 +1203,7 @@ public void run() {
12031203
Surveys.setOnShowCallback(new OnShowCallback() {
12041204
@Override
12051205
public void onShow() {
1206-
sendEvent(getReactApplicationContext(), "IBGWillShowSurvey", null);
1206+
sendEvent("IBGWillShowSurvey", null);
12071207
}
12081208
});
12091209
}
@@ -1225,7 +1225,7 @@ public void run() {
12251225
Surveys.setOnDismissCallback(new OnDismissCallback() {
12261226
@Override
12271227
public void onDismiss() {
1228-
sendEvent(getReactApplicationContext(), "IBGDidDismissSurvey", null);
1228+
sendEvent("IBGDidDismissSurvey", null);
12291229
}
12301230
});
12311231
}
@@ -1347,7 +1347,7 @@ public void run() {
13471347
Runnable onNewReplyReceivedRunnable = new Runnable() {
13481348
@Override
13491349
public void run() {
1350-
sendEvent(getReactApplicationContext(), "IBGOnNewReplyReceivedCallback", null);
1350+
sendEvent("IBGOnNewReplyReceivedCallback", null);
13511351
}
13521352
};
13531353
Replies.setOnNewReplyReceivedCallback(onNewReplyReceivedRunnable);
@@ -1697,14 +1697,6 @@ public void run() {
16971697
});
16981698
}
16991699

1700-
private void sendEvent(ReactApplicationContext reactContext,
1701-
String eventName,
1702-
WritableMap params) {
1703-
reactContext
1704-
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
1705-
.emit(eventName, params);
1706-
}
1707-
17081700
@ReactMethod
17091701
public void addExperiments(final ReadableArray experiments) {
17101702
MainThreadHandler.runOnMainThread(new Runnable() {

android/src/main/java/com/instabug/reactlibrary/RNInstabugRepliesModule.java

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,21 @@
11
package com.instabug.reactlibrary;
22

3-
import android.os.Handler;
4-
import android.os.Looper;
5-
63
import com.facebook.react.bridge.Callback;
74
import com.facebook.react.bridge.ReactApplicationContext;
8-
import com.facebook.react.bridge.ReactContextBaseJavaModule;
95
import com.facebook.react.bridge.ReactMethod;
106
import com.facebook.react.bridge.ReadableMap;
117
import com.facebook.react.bridge.ReadableType;
128
import com.facebook.react.bridge.ReadableMapKeySetIterator;
139
import com.instabug.chat.Replies;
1410
import com.instabug.library.Feature;
15-
import com.instabug.reactlibrary.utils.InstabugUtil;
11+
import com.instabug.reactlibrary.utils.EventEmitterModule;
1612
import com.instabug.reactlibrary.utils.MainThreadHandler;
1713

1814
import javax.annotation.Nonnull;
1915
import java.util.HashMap;
2016
import java.util.Map;
2117

22-
public class RNInstabugRepliesModule extends ReactContextBaseJavaModule {
18+
public class RNInstabugRepliesModule extends EventEmitterModule {
2319

2420
public RNInstabugRepliesModule(ReactApplicationContext reactApplicationContext) {
2521
super(reactApplicationContext);
@@ -31,6 +27,16 @@ public String getName() {
3127
return "IBGReplies";
3228
}
3329

30+
@ReactMethod
31+
public void addListener(String event) {
32+
super.addListener(event);
33+
}
34+
35+
@ReactMethod
36+
public void removeListeners(Integer count) {
37+
super.removeListeners(count);
38+
}
39+
3440
@ReactMethod
3541
public void setEnabled(final boolean isEnabled) {
3642
MainThreadHandler.runOnMainThread(new Runnable() {
@@ -278,7 +284,7 @@ public void run() {
278284
Runnable onNewReplyReceivedRunnable = new Runnable() {
279285
@Override
280286
public void run() {
281-
InstabugUtil.sendEvent(getReactApplicationContext(), Constants.IBG_ON_NEW_REPLY_RECEIVED_CALLBACK, null);
287+
sendEvent(Constants.IBG_ON_NEW_REPLY_RECEIVED_CALLBACK, null);
282288
}
283289
};
284290
Replies.setOnNewReplyReceivedCallback(onNewReplyReceivedRunnable);

android/src/main/java/com/instabug/reactlibrary/RNInstabugSurveysModule.java

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
package com.instabug.reactlibrary;
22

3-
import android.os.Handler;
4-
import android.os.Looper;
5-
63
import com.facebook.react.bridge.Callback;
74
import com.facebook.react.bridge.ReactApplicationContext;
8-
import com.facebook.react.bridge.ReactContextBaseJavaModule;
95
import com.facebook.react.bridge.ReactMethod;
106
import com.facebook.react.bridge.WritableArray;
117
import com.instabug.library.Feature;
128
import com.instabug.reactlibrary.utils.ArrayUtil;
9+
import com.instabug.reactlibrary.utils.EventEmitterModule;
1310
import com.instabug.reactlibrary.utils.InstabugUtil;
1411
import com.instabug.reactlibrary.utils.MainThreadHandler;
1512
import com.instabug.survey.callbacks.*;
@@ -22,7 +19,7 @@
2219

2320
import javax.annotation.Nonnull;
2421

25-
public class RNInstabugSurveysModule extends ReactContextBaseJavaModule {
22+
public class RNInstabugSurveysModule extends EventEmitterModule {
2623

2724
public RNInstabugSurveysModule(ReactApplicationContext reactContext) {
2825
super(reactContext);
@@ -34,6 +31,16 @@ public String getName() {
3431
return "IBGSurveys";
3532
}
3633

34+
@ReactMethod
35+
public void addListener(String event) {
36+
super.addListener(event);
37+
}
38+
39+
@ReactMethod
40+
public void removeListeners(Integer count) {
41+
super.removeListeners(count);
42+
}
43+
3744
/**
3845
* Returns true if the survey with a specific token was answered before.
3946
* Will return false if the token does not exist or if the survey was not answered before.
@@ -137,7 +144,7 @@ public void run() {
137144
Surveys.setOnShowCallback(new OnShowCallback() {
138145
@Override
139146
public void onShow() {
140-
InstabugUtil.sendEvent(getReactApplicationContext(), Constants.IBG_ON_SHOW_SURVEY_HANDLER, null);
147+
sendEvent(Constants.IBG_ON_SHOW_SURVEY_HANDLER, null);
141148
}
142149
});
143150
}
@@ -159,7 +166,7 @@ public void run() {
159166
Surveys.setOnDismissCallback(new OnDismissCallback() {
160167
@Override
161168
public void onDismiss() {
162-
InstabugUtil.sendEvent(getReactApplicationContext(), Constants.IBG_ON_DISMISS_SURVEY_HANDLER, null);
169+
sendEvent(Constants.IBG_ON_DISMISS_SURVEY_HANDLER, null);
163170
}
164171
});
165172
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.instabug.reactlibrary.utils;
2+
3+
import androidx.annotation.Nullable;
4+
import androidx.annotation.VisibleForTesting;
5+
6+
import com.facebook.react.bridge.ReactApplicationContext;
7+
import com.facebook.react.bridge.ReactContextBaseJavaModule;
8+
import com.facebook.react.bridge.WritableMap;
9+
import com.facebook.react.modules.core.DeviceEventManagerModule;
10+
11+
public abstract class EventEmitterModule extends ReactContextBaseJavaModule {
12+
private int listenerCount = 0;
13+
14+
public EventEmitterModule(ReactApplicationContext context) {
15+
super(context);
16+
}
17+
18+
@VisibleForTesting
19+
public void sendEvent(String event, @Nullable WritableMap params) {
20+
if (listenerCount > 0) {
21+
getReactApplicationContext()
22+
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
23+
.emit(event, params);
24+
}
25+
}
26+
27+
protected void addListener(String ignoredEvent) {
28+
listenerCount++;
29+
}
30+
31+
protected void removeListeners(Integer count) {
32+
listenerCount -= count;
33+
}
34+
}

android/src/main/java/com/instabug/reactlibrary/utils/InstabugUtil.java

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,6 @@ public static Method getMethod(Class clazz, String methodName, Class... paramete
4141
return null;
4242
}
4343

44-
public static void sendEvent(ReactApplicationContext reactContext,
45-
String eventName,
46-
WritableMap params) {
47-
reactContext
48-
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
49-
.emit(eventName, params);
50-
}
51-
5244
/**
5345
* Convenience method to convert from a list of Surveys to a JSON array
5446
*

0 commit comments

Comments
 (0)