Skip to content

Commit fef7209

Browse files
committed
Migrate Surveys to NativeEventEmitter
1 parent c53470d commit fef7209

File tree

4 files changed

+45
-28
lines changed

4 files changed

+45
-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,12 +1,25 @@
1-
import { Platform } from 'react-native';
1+
import { NativeEventEmitter, Platform } from 'react-native';
22

33
import { NativeSurveys } from '../native/NativeSurveys';
44
import type { Survey } from '../native/NativeSurveys';
5-
import IBGEventEmitter from '../utils/IBGEventEmitter';
6-
import InstabugConstants from '../utils/InstabugConstants';
75

86
export type { Survey };
97

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

@@ -71,11 +80,7 @@ export const setOnShowHandler = (onShowHandler: () => void) => {
7180
* the survey's UI is dismissed.
7281
*/
7382
export const setOnDismissHandler = (onDismissHandler: () => void) => {
74-
IBGEventEmitter.addListener(
75-
NativeSurveys,
76-
InstabugConstants.DID_DISMISS_SURVEY_HANDLER,
77-
onDismissHandler,
78-
);
83+
$emitter.addListener($NativeEvents.DID_DISMISS_SURVEY_HANDLER, onDismissHandler);
7984
NativeSurveys.setOnDismissHandler(onDismissHandler);
8085
};
8186

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/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/NativeSurveys';
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)