Skip to content

Commit fad603c

Browse files
committed
Move emitters to native directory
1 parent 7a31551 commit fad603c

12 files changed

+74
-123
lines changed

src/modules/BugReporting.ts

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

3-
import { NativeBugReporting } from '../native/NativeBugReporting';
3+
import { NativeBugReporting, NativeEvents, emitter } from '../native/NativeBugReporting';
44
import {
55
dismissType,
66
extendedBugReportMode,
@@ -22,22 +22,6 @@ import type {
2222

2323
export { invocationEvent, extendedBugReportMode, reportType, option, position };
2424

25-
/**
26-
* @internal You shouldn't use this enum since you never emit or listen
27-
* for native events in your code.
28-
*/
29-
export enum $NativeEvents {
30-
ON_INVOKE_HANDLER = 'IBGpreInvocationHandler',
31-
ON_DISMISS_HANDLER = 'IBGpostInvocationHandler',
32-
DID_SELECT_PROMPT_OPTION_HANDLER = 'IBGDidSelectPromptOptionHandler',
33-
}
34-
35-
/**
36-
* @internal You shouldn't use this since you never emit or listen for native
37-
* events in your code.
38-
*/
39-
export const $emitter = new NativeEventEmitter(NativeBugReporting);
40-
4125
/**
4226
* Enables and disables manual invocation and prompt options for bug and feedback.
4327
* @param isEnabled
@@ -71,7 +55,7 @@ export const setOptions = (options: option[] | InvocationOption[]) => {
7155
* @param handler A callback that gets executed before invoking the SDK
7256
*/
7357
export const onInvokeHandler = (handler: () => void) => {
74-
$emitter.addListener($NativeEvents.ON_INVOKE_HANDLER, handler);
58+
emitter.addListener(NativeEvents.ON_INVOKE_HANDLER, handler);
7559
NativeBugReporting.setOnInvokeHandler(handler);
7660
};
7761

@@ -84,7 +68,7 @@ export const onInvokeHandler = (handler: () => void) => {
8468
export const onSDKDismissedHandler = (
8569
handler: (dismissType: dismissType | DismissType, reportType: reportType | ReportType) => void,
8670
) => {
87-
$emitter.addListener($NativeEvents.ON_DISMISS_HANDLER, (payload) => {
71+
emitter.addListener(NativeEvents.ON_DISMISS_HANDLER, (payload) => {
8872
handler(payload.dismissType, payload.reportType);
8973
});
9074
NativeBugReporting.setOnSDKDismissedHandler(handler);
@@ -203,7 +187,7 @@ export const setDidSelectPromptOptionHandler = (handler: (promptOption: string)
203187
if (Platform.OS === 'android') {
204188
return;
205189
}
206-
$emitter.addListener($NativeEvents.DID_SELECT_PROMPT_OPTION_HANDLER, (payload) => {
190+
emitter.addListener(NativeEvents.DID_SELECT_PROMPT_OPTION_HANDLER, (payload) => {
207191
handler(payload.promptOption);
208192
});
209193
NativeBugReporting.setDidSelectPromptOptionHandler(handler);

src/modules/Instabug.ts

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import type React from 'react';
2-
import { NativeEventEmitter, Platform, findNodeHandle, processColor } from 'react-native';
2+
import { 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';
66
import type { NavigationAction, NavigationState as NavigationStateV4 } from 'react-navigation';
77

88
import type { InstabugConfig } from '../models/InstabugConfig';
99
import Report from '../models/Report';
10-
import { NativeInstabug } from '../native/NativeInstabug';
10+
import { NativeEvents, NativeInstabug, emitter } from '../native/NativeInstabug';
1111
import {
1212
IBGPosition,
1313
actionTypes,
@@ -54,20 +54,6 @@ export {
5454
strings,
5555
};
5656

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-
7157
/**
7258
* Enables or disables Instabug functionality.
7359
* @param isEnabled A boolean to enable/disable Instabug.
@@ -545,7 +531,7 @@ export const show = () => {
545531
};
546532

547533
export const onReportSubmitHandler = (handler?: (report: Report) => void) => {
548-
$emitter.addListener($NativeEvents.PRESENDING_HANDLER, (report) => {
534+
emitter.addListener(NativeEvents.PRESENDING_HANDLER, (report) => {
549535
const { tags, consoleLogs, instabugLogs, userAttributes, fileAttachments } = report;
550536
const reportObj = new Report(tags, consoleLogs, instabugLogs, userAttributes, fileAttachments);
551537
handler && handler(reportObj);

src/modules/Replies.ts

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,6 @@
1-
import { NativeEventEmitter, Platform } from 'react-native';
1+
import { Platform } from 'react-native';
22

3-
import { NativeReplies } from '../native/NativeReplies';
4-
5-
/**
6-
* @internal You shouldn't use this enum since you never emit or listen
7-
* for native events in your code.
8-
*/
9-
export enum $NativeEvents {
10-
ON_REPLY_RECEIVED_HANDLER = 'IBGOnNewReplyReceivedCallback',
11-
}
12-
13-
/**
14-
* @internal You shouldn't use this since you never emit or listen for native
15-
* events in your code.
16-
*/
17-
export const $emitter = new NativeEventEmitter(NativeReplies);
3+
import { NativeEvents, NativeReplies, emitter } from '../native/NativeReplies';
184

195
/**
206
* Enables and disables everything related to receiving replies.
@@ -44,7 +30,7 @@ export const show = () => {
4430
* @param handler A callback that gets executed when a new message is received.
4531
*/
4632
export const setOnNewReplyReceivedHandler = (handler: () => void) => {
47-
$emitter.addListener($NativeEvents.ON_REPLY_RECEIVED_HANDLER, handler);
33+
emitter.addListener(NativeEvents.ON_REPLY_RECEIVED_HANDLER, handler);
4834
NativeReplies.setOnNewReplyReceivedHandler(handler);
4935
};
5036

src/modules/Surveys.ts

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,10 @@
1-
import { NativeEventEmitter, Platform } from 'react-native';
1+
import { Platform } from 'react-native';
22

3-
import { NativeSurveys } from '../native/NativeSurveys';
3+
import { NativeEvents, NativeSurveys, emitter } from '../native/NativeSurveys';
44
import type { Survey } from '../native/NativeSurveys';
55

66
export type { Survey };
77

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-
238
/**
249
* Sets whether surveys are enabled or not.
2510
* If you disable surveys on the SDK but still have active surveys on your Instabug dashboard,
@@ -68,7 +53,7 @@ export const setAutoShowingEnabled = (autoShowingSurveysEnabled: boolean) => {
6853
* presenting the survey's UI.
6954
*/
7055
export const setOnShowHandler = (onShowHandler: () => void) => {
71-
$emitter.addListener($NativeEvents.WILL_SHOW_SURVEY_HANDLER, onShowHandler);
56+
emitter.addListener(NativeEvents.WILL_SHOW_SURVEY_HANDLER, onShowHandler);
7257
NativeSurveys.setOnShowHandler(onShowHandler);
7358
};
7459

@@ -80,7 +65,7 @@ export const setOnShowHandler = (onShowHandler: () => void) => {
8065
* the survey's UI is dismissed.
8166
*/
8267
export const setOnDismissHandler = (onDismissHandler: () => void) => {
83-
$emitter.addListener($NativeEvents.DID_DISMISS_SURVEY_HANDLER, onDismissHandler);
68+
emitter.addListener(NativeEvents.DID_DISMISS_SURVEY_HANDLER, onDismissHandler);
8469
NativeSurveys.setOnDismissHandler(onDismissHandler);
8570
};
8671

src/native/NativeBugReporting.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { NativeModule } from 'react-native';
1+
import { NativeEventEmitter, NativeModule } from 'react-native';
22

33
import type {
44
dismissType,
@@ -60,3 +60,11 @@ export interface BugReportingNativeModule extends NativeModule {
6060
}
6161

6262
export const NativeBugReporting = NativeModules.IBGBugReporting;
63+
64+
export enum NativeEvents {
65+
ON_INVOKE_HANDLER = 'IBGpreInvocationHandler',
66+
ON_DISMISS_HANDLER = 'IBGpostInvocationHandler',
67+
DID_SELECT_PROMPT_OPTION_HANDLER = 'IBGDidSelectPromptOptionHandler',
68+
}
69+
70+
export const emitter = new NativeEventEmitter(NativeBugReporting);

src/native/NativeInstabug.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { NativeModule, ProcessedColorValue } from 'react-native';
1+
import { NativeEventEmitter, NativeModule, ProcessedColorValue } from 'react-native';
22

33
import type Report from '../models/Report';
44
import type {
@@ -123,3 +123,9 @@ export interface InstabugNativeModule extends NativeModule {
123123
}
124124

125125
export const NativeInstabug = NativeModules.Instabug;
126+
127+
export enum NativeEvents {
128+
PRESENDING_HANDLER = 'IBGpreSendingHandler',
129+
}
130+
131+
export const emitter = new NativeEventEmitter(NativeInstabug);

src/native/NativeReplies.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { NativeModule } from 'react-native';
1+
import { NativeEventEmitter, NativeModule } from 'react-native';
22

33
import { NativeModules } from './NativePackage';
44

@@ -26,3 +26,9 @@ export interface RepliesNativeModule extends NativeModule {
2626
}
2727

2828
export const NativeReplies = NativeModules.IBGReplies;
29+
30+
export enum NativeEvents {
31+
ON_REPLY_RECEIVED_HANDLER = 'IBGOnNewReplyReceivedCallback',
32+
}
33+
34+
export const emitter = new NativeEventEmitter(NativeReplies);

src/native/NativeSurveys.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { NativeModule } from 'react-native';
1+
import { NativeEventEmitter, NativeModule } from 'react-native';
22

33
import { NativeModules } from './NativePackage';
44

@@ -28,3 +28,10 @@ export interface SurveysNativeModule extends NativeModule {
2828
}
2929

3030
export const NativeSurveys = NativeModules.IBGSurveys;
31+
32+
export enum NativeEvents {
33+
WILL_SHOW_SURVEY_HANDLER = 'IBGWillShowSurvey',
34+
DID_DISMISS_SURVEY_HANDLER = 'IBGDidDismissSurvey',
35+
}
36+
37+
export const emitter = new NativeEventEmitter(NativeSurveys);

test/modules/BugReporting.spec.ts

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

33
import * as BugReporting from '../../src/modules/BugReporting';
4-
import { NativeBugReporting } from '../../src/native/NativeBugReporting';
4+
import { NativeBugReporting, NativeEvents, emitter } from '../../src/native/NativeBugReporting';
55
import {
66
ExtendedBugReportMode,
77
FloatingButtonPosition,
@@ -13,9 +13,9 @@ import {
1313

1414
describe('Testing BugReporting Module', () => {
1515
beforeEach(() => {
16-
const events = Object.values(BugReporting.$NativeEvents);
16+
const events = Object.values(NativeEvents);
1717
events.forEach((event) => {
18-
BugReporting.$emitter.removeAllListeners(event);
18+
emitter.removeAllListeners(event);
1919
});
2020
});
2121

@@ -136,11 +136,9 @@ describe('Testing BugReporting Module', () => {
136136
it('should invoke callback on emitting the event IBGpreInvocationHandler', () => {
137137
const callback = jest.fn();
138138
BugReporting.onInvokeHandler(callback);
139-
BugReporting.$emitter.emit(BugReporting.$NativeEvents.ON_INVOKE_HANDLER);
139+
emitter.emit(NativeEvents.ON_INVOKE_HANDLER);
140140

141-
expect(BugReporting.$emitter.listenerCount(BugReporting.$NativeEvents.ON_INVOKE_HANDLER)).toBe(
142-
1,
143-
);
141+
expect(emitter.listenerCount(NativeEvents.ON_INVOKE_HANDLER)).toBe(1);
144142
expect(callback).toHaveBeenCalled();
145143
});
146144

@@ -158,14 +156,12 @@ describe('Testing BugReporting Module', () => {
158156
const callback = jest.fn();
159157

160158
BugReporting.onSDKDismissedHandler(callback);
161-
BugReporting.$emitter.emit(BugReporting.$NativeEvents.ON_DISMISS_HANDLER, {
159+
emitter.emit(NativeEvents.ON_DISMISS_HANDLER, {
162160
dismissType: dismissType,
163161
reportType: reportType,
164162
});
165163

166-
expect(BugReporting.$emitter.listenerCount(BugReporting.$NativeEvents.ON_DISMISS_HANDLER)).toBe(
167-
1,
168-
);
164+
expect(emitter.listenerCount(NativeEvents.ON_DISMISS_HANDLER)).toBe(1);
169165
expect(callback).toBeCalledTimes(1);
170166
expect(callback).toBeCalledWith(dismissType, reportType);
171167
});
@@ -201,14 +197,10 @@ describe('Testing BugReporting Module', () => {
201197
Platform.OS = 'android';
202198

203199
BugReporting.setDidSelectPromptOptionHandler(jest.fn());
204-
BugReporting.$emitter.emit(BugReporting.$NativeEvents.DID_SELECT_PROMPT_OPTION_HANDLER, {});
200+
emitter.emit(NativeEvents.DID_SELECT_PROMPT_OPTION_HANDLER, {});
205201

206202
expect(NativeBugReporting.setDidSelectPromptOptionHandler).not.toBeCalled();
207-
expect(
208-
BugReporting.$emitter.listenerCount(
209-
BugReporting.$NativeEvents.DID_SELECT_PROMPT_OPTION_HANDLER,
210-
),
211-
).toBe(0);
203+
expect(emitter.listenerCount(NativeEvents.DID_SELECT_PROMPT_OPTION_HANDLER)).toBe(0);
212204
});
213205

214206
it('should call setDidSelectPromptOptionHandler event listener when platform is iOS', () => {
@@ -217,16 +209,9 @@ describe('Testing BugReporting Module', () => {
217209
const payload = { promptOption: 'bug' };
218210

219211
BugReporting.setDidSelectPromptOptionHandler(callback);
220-
BugReporting.$emitter.emit(
221-
BugReporting.$NativeEvents.DID_SELECT_PROMPT_OPTION_HANDLER,
222-
payload,
223-
);
212+
emitter.emit(NativeEvents.DID_SELECT_PROMPT_OPTION_HANDLER, payload);
224213

225-
expect(
226-
BugReporting.$emitter.listenerCount(
227-
BugReporting.$NativeEvents.DID_SELECT_PROMPT_OPTION_HANDLER,
228-
),
229-
).toBe(1);
214+
expect(emitter.listenerCount(NativeEvents.DID_SELECT_PROMPT_OPTION_HANDLER)).toBe(1);
230215
expect(callback).toBeCalledTimes(1);
231216
expect(callback).toBeCalledWith(payload.promptOption);
232217
});

test/modules/Instabug.spec.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import waitForExpect from 'wait-for-expect';
66

77
import Report from '../../src/models/Report';
88
import * as Instabug from '../../src/modules/Instabug';
9-
import { NativeInstabug } from '../../src/native/NativeInstabug';
9+
import { NativeEvents, NativeInstabug, emitter } from '../../src/native/NativeInstabug';
1010
import {
1111
ColorTheme,
1212
InvocationEvent,
@@ -20,9 +20,9 @@ import InstabugUtils from '../../src/utils/InstabugUtils';
2020

2121
describe('Instabug Module', () => {
2222
beforeEach(() => {
23-
const events = Object.values(Instabug.$NativeEvents);
23+
const events = Object.values(NativeEvents);
2424
events.forEach((event) => {
25-
Instabug.$emitter.removeAllListeners(event);
25+
emitter.removeAllListeners(event);
2626
});
2727
});
2828

@@ -641,9 +641,9 @@ describe('Instabug Module', () => {
641641
done();
642642
};
643643
Instabug.onReportSubmitHandler(callback);
644-
Instabug.$emitter.emit(Instabug.$NativeEvents.PRESENDING_HANDLER, report);
644+
emitter.emit(NativeEvents.PRESENDING_HANDLER, report);
645645

646-
expect(Instabug.$emitter.listenerCount(Instabug.$NativeEvents.PRESENDING_HANDLER)).toBe(1);
646+
expect(emitter.listenerCount(NativeEvents.PRESENDING_HANDLER)).toBe(1);
647647
});
648648

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

test/modules/Replies.spec.ts

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

33
import * as Replies from '../../src/modules/Replies';
4-
import { NativeReplies } from '../../src/native/NativeReplies';
4+
import { NativeEvents, NativeReplies, emitter } from '../../src/native/NativeReplies';
55

66
describe('Replies Module', () => {
77
beforeEach(() => {
8-
const events = Object.values(Replies.$NativeEvents);
8+
const events = Object.values(NativeEvents);
99
events.forEach((event) => {
10-
Replies.$emitter.removeAllListeners(event);
10+
emitter.removeAllListeners(event);
1111
});
1212
});
1313

@@ -44,9 +44,9 @@ describe('Replies Module', () => {
4444
it('should invoke callback on emitting the event IBGOnNewReplyReceivedCallback', () => {
4545
const callback = jest.fn();
4646
Replies.setOnNewReplyReceivedHandler(callback);
47-
Replies.$emitter.emit(Replies.$NativeEvents.ON_REPLY_RECEIVED_HANDLER);
47+
emitter.emit(NativeEvents.ON_REPLY_RECEIVED_HANDLER);
4848

49-
expect(Replies.$emitter.listenerCount(Replies.$NativeEvents.ON_REPLY_RECEIVED_HANDLER)).toBe(1);
49+
expect(emitter.listenerCount(NativeEvents.ON_REPLY_RECEIVED_HANDLER)).toBe(1);
5050
expect(callback).toHaveBeenCalled();
5151
});
5252

0 commit comments

Comments
 (0)