Skip to content

Commit 78dfe37

Browse files
committed
Migrate Instabug module to NativeEventEmitter
1 parent 452b33c commit 78dfe37

File tree

5 files changed

+49
-39
lines changed

5 files changed

+49
-39
lines changed

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() {

src/modules/Instabug.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type React from 'react';
2-
import { Platform, findNodeHandle, processColor } from 'react-native';
2+
import { NativeEventEmitter, Platform, findNodeHandle, processColor } from 'react-native';
33

44
import type { NavigationState as NavigationStateV5 } from '@react-navigation/native';
55
import type { ComponentDidAppearEvent } from 'react-native-navigation';
@@ -31,8 +31,6 @@ import {
3131
StringKey,
3232
WelcomeMessageMode,
3333
} from '../utils/Enums';
34-
import IBGEventEmitter from '../utils/IBGEventEmitter';
35-
import InstabugConstants from '../utils/InstabugConstants';
3634
import InstabugUtils, { stringifyIfNotString } from '../utils/InstabugUtils';
3735
import * as NetworkLogger from './NetworkLogger';
3836

@@ -56,6 +54,20 @@ export {
5654
strings,
5755
};
5856

57+
/**
58+
* @internal You shouldn't use this enum since you never emit or listen
59+
* for native events in your code.
60+
*/
61+
export enum $NativeEvents {
62+
PRESENDING_HANDLER = 'IBGpreSendingHandler',
63+
}
64+
65+
/**
66+
* @internal You shouldn't use this since you never emit or listen for native
67+
* events in your code.
68+
*/
69+
export const $emitter = new NativeEventEmitter(NativeInstabug);
70+
5971
/**
6072
* Enables or disables Instabug functionality.
6173
* @param isEnabled A boolean to enable/disable Instabug.
@@ -533,7 +545,7 @@ export const show = () => {
533545
};
534546

535547
export const onReportSubmitHandler = (handler?: (report: Report) => void) => {
536-
IBGEventEmitter.addListener(NativeInstabug, InstabugConstants.PRESENDING_HANDLER, (report) => {
548+
$emitter.addListener($NativeEvents.PRESENDING_HANDLER, (report) => {
537549
const { tags, consoleLogs, instabugLogs, userAttributes, fileAttachments } = report;
538550
const reportObj = new Report(tags, consoleLogs, instabugLogs, userAttributes, fileAttachments);
539551
handler && handler(reportObj);

src/utils/InstabugConstants.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
enum InstabugConstants {
2-
PRESENDING_HANDLER = 'IBGpreSendingHandler',
32
ON_REPLY_RECEIVED_HANDLER = 'IBGOnNewReplyReceivedCallback',
43
WILL_SHOW_SURVEY_HANDLER = 'IBGWillShowSurvey',
54
DID_DISMISS_SURVEY_HANDLER = 'IBGDidDismissSurvey',

test/mocks/mockInstabug.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ export default {
4444
show: jest.fn(),
4545
setPreSendingHandler: jest.fn(),
4646
callPrivateApi: jest.fn(),
47-
addListener: jest.fn(),
4847
getReport: jest.fn(),
4948
reportScreenChange: jest.fn(),
5049
addExperiments: jest.fn(),
@@ -62,5 +61,8 @@ export default {
6261
addFileAttachmentWithURLToReport: jest.fn(),
6362
addFileAttachmentWithDataToReport: jest.fn(),
6463
setNetworkLoggingEnabled: jest.fn(),
64+
65+
addListener: jest.fn(),
66+
removeListeners: jest.fn(),
6567
},
6668
};

test/modules/Instabug.spec.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,16 @@ import {
1616
StringKey,
1717
WelcomeMessageMode,
1818
} from '../../src/utils/Enums';
19-
import IBGEventEmitter from '../../src/utils/IBGEventEmitter';
20-
import IBGConstants from '../../src/utils/InstabugConstants';
2119
import InstabugUtils from '../../src/utils/InstabugUtils';
2220

2321
describe('Instabug Module', () => {
22+
beforeEach(() => {
23+
const events = Object.values(Instabug.$NativeEvents);
24+
events.forEach((event) => {
25+
Instabug.$emitter.removeAllListeners(event);
26+
});
27+
});
28+
2429
it('should call the native method setEnabled', () => {
2530
Instabug.setEnabled(true);
2631

@@ -636,9 +641,9 @@ describe('Instabug Module', () => {
636641
done();
637642
};
638643
Instabug.onReportSubmitHandler(callback);
639-
IBGEventEmitter.emit(IBGConstants.PRESENDING_HANDLER, report);
644+
Instabug.$emitter.emit(Instabug.$NativeEvents.PRESENDING_HANDLER, report);
640645

641-
expect(IBGEventEmitter.getListeners(IBGConstants.PRESENDING_HANDLER).length).toEqual(1);
646+
expect(Instabug.$emitter.listenerCount(Instabug.$NativeEvents.PRESENDING_HANDLER)).toBe(1);
642647
});
643648

644649
it('should invoke the native method callPrivateApi', () => {

0 commit comments

Comments
 (0)