Skip to content

Commit db53915

Browse files
committed
Migrate Surveys to NativeEventEmitter
1 parent 2e378ca commit db53915

File tree

5 files changed

+47
-28
lines changed

5 files changed

+47
-28
lines changed

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
}

src/modules/Surveys.ts

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,26 @@
1-
import { Platform } from 'react-native';
1+
import { NativeEventEmitter, Platform } from 'react-native';
22

33
import { NativeSurveys } from '../native';
4-
import IBGEventEmitter from '../utils/IBGEventEmitter';
5-
import InstabugConstants from '../utils/InstabugConstants';
64

75
export interface Survey {
86
title: string;
97
}
108

9+
/**
10+
* @internal You shouldn't use this enum since you never emit or listen
11+
* for native events in your code.
12+
*/
13+
export enum $NativeEvents {
14+
WILL_SHOW_SURVEY_HANDLER = 'IBGWillShowSurvey',
15+
DID_DISMISS_SURVEY_HANDLER = 'IBGDidDismissSurvey',
16+
}
17+
18+
/**
19+
* @internal You shouldn't use this since you never emit or listen for native
20+
* events in your code.
21+
*/
22+
export const $emitter = new NativeEventEmitter(NativeSurveys);
23+
1124
/**
1225
* Sets whether surveys are enabled or not.
1326
* If you disable surveys on the SDK but still have active surveys on your Instabug dashboard,
@@ -56,11 +69,7 @@ export const setAutoShowingEnabled = (autoShowingSurveysEnabled: boolean) => {
5669
* presenting the survey's UI.
5770
*/
5871
export const setOnShowHandler = (onShowHandler: () => void) => {
59-
IBGEventEmitter.addListener(
60-
NativeSurveys,
61-
InstabugConstants.WILL_SHOW_SURVEY_HANDLER,
62-
onShowHandler,
63-
);
72+
$emitter.addListener($NativeEvents.WILL_SHOW_SURVEY_HANDLER, onShowHandler);
6473
NativeSurveys.setOnShowHandler(onShowHandler);
6574
};
6675

@@ -72,11 +81,7 @@ export const setOnShowHandler = (onShowHandler: () => void) => {
7281
* the survey's UI is dismissed.
7382
*/
7483
export const setOnDismissHandler = (onDismissHandler: () => void) => {
75-
IBGEventEmitter.addListener(
76-
NativeSurveys,
77-
InstabugConstants.DID_DISMISS_SURVEY_HANDLER,
78-
onDismissHandler,
79-
);
84+
$emitter.addListener($NativeEvents.DID_DISMISS_SURVEY_HANDLER, onDismissHandler);
8085
NativeSurveys.setOnDismissHandler(onDismissHandler);
8186
};
8287

src/utils/InstabugConstants.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
enum InstabugConstants {
22
ON_REPLY_RECEIVED_HANDLER = 'IBGOnNewReplyReceivedCallback',
3-
WILL_SHOW_SURVEY_HANDLER = 'IBGWillShowSurvey',
4-
DID_DISMISS_SURVEY_HANDLER = 'IBGDidDismissSurvey',
53
GRAPHQL_HEADER = 'ibg-graphql-header',
64
}
75

test/mocks/mockSurveys.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ export default {
1010
showSurvey: jest.fn(),
1111
hasRespondedToSurvey: jest.fn((_, cb) => cb(true)),
1212
setShouldShowWelcomeScreen: jest.fn(),
13+
1314
addListener: jest.fn(),
15+
removeListeners: jest.fn(),
1416
},
1517
};

test/modules/Surveys.spec.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,15 @@ import { Platform } from 'react-native';
22

33
import * as Surveys from '../../src/modules/Surveys';
44
import { NativeSurveys } from '../../src/native';
5-
import IBGEventEmitter from '../../src/utils/IBGEventEmitter';
6-
import IBGConstants from '../../src/utils/InstabugConstants';
75

86
describe('Surveys Module', () => {
7+
beforeEach(() => {
8+
const events = Object.values(Surveys.$NativeEvents);
9+
events.forEach((event) => {
10+
Surveys.$emitter.removeAllListeners(event);
11+
});
12+
});
13+
914
it('should call the native method setSurveysEnabled', () => {
1015
Surveys.setEnabled(true);
1116

@@ -60,9 +65,9 @@ describe('Surveys Module', () => {
6065
it('should invoke callback on emitting the event IBGWillShowSurvey', () => {
6166
const callback = jest.fn();
6267
Surveys.setOnShowHandler(callback);
63-
IBGEventEmitter.emit(IBGConstants.WILL_SHOW_SURVEY_HANDLER);
68+
Surveys.$emitter.emit(Surveys.$NativeEvents.WILL_SHOW_SURVEY_HANDLER);
6469

65-
expect(IBGEventEmitter.getListeners(IBGConstants.WILL_SHOW_SURVEY_HANDLER).length).toEqual(1);
70+
expect(Surveys.$emitter.listenerCount(Surveys.$NativeEvents.WILL_SHOW_SURVEY_HANDLER)).toBe(1);
6671
expect(callback).toHaveBeenCalled();
6772
});
6873

@@ -77,9 +82,11 @@ describe('Surveys Module', () => {
7782
it('should invoke callback on emitting the event IBGDidDismissSurvey', () => {
7883
const callback = jest.fn();
7984
Surveys.setOnDismissHandler(callback);
80-
IBGEventEmitter.emit(IBGConstants.DID_DISMISS_SURVEY_HANDLER);
85+
Surveys.$emitter.emit(Surveys.$NativeEvents.DID_DISMISS_SURVEY_HANDLER);
8186

82-
expect(IBGEventEmitter.getListeners(IBGConstants.DID_DISMISS_SURVEY_HANDLER).length).toEqual(1);
87+
expect(Surveys.$emitter.listenerCount(Surveys.$NativeEvents.DID_DISMISS_SURVEY_HANDLER)).toBe(
88+
1,
89+
);
8390
expect(callback).toHaveBeenCalled();
8491
});
8592

0 commit comments

Comments
 (0)