Skip to content

Commit 08189f8

Browse files
authored
feat!: refactor intercom events (#314)
BREAKING CHANGE: - removes addEventListener - adds `bootstrapEventListeners()` to setup Android events. - Consumers must now use `NativeEventEmitter` for handling Intercom events - Update documentation with new event listener patterns.
1 parent a8ebfa2 commit 08189f8

File tree

4 files changed

+64
-54
lines changed

4 files changed

+64
-54
lines changed

README.md

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1022,37 +1022,48 @@ Set the level of the native logger
10221022
`Promise<boolean>`
10231023
___
10241024

1025-
### `Intercom.addEventListener(event,callback)`
1025+
### `Intercom.bootstrapEventListeners()`
10261026

1027-
Sets a listener for listed events:
1027+
Bootstrap event listeners (Only for Android). This handles platform-specific setup and returns a cleanup function.
1028+
This must be called before setting up your own NativeEventEmitter.
10281029

1029-
| Event | Platform |
1030-
| ------- | -------- |
1031-
| IntercomUnreadConversationCountDidChangeNotification| IOS, Android |
1032-
| IntercomHelpCenterDidShowNotification| IOS |
1033-
| IntercomHelpCenterDidHideNotification| IOS |
1034-
| IntercomWindowDidShowNotification| IOS | |
1035-
| IntercomWindowDidHideNotification| IOS |
1030+
### Returns
1031+
1032+
`() => void` - Cleanup function
1033+
1034+
### Usage
10361035

10371036
```javascript
1037+
import { NativeEventEmitter, NativeModules } from 'react-native';
1038+
import Intercom, { IntercomEvents } from '@intercom/intercom-react-native';
1039+
10381040
useEffect(() => {
1039-
const listener = Intercom.addEventListener('IntercomUnreadConversationCountDidChangeNotification', ({count}) => alert(count));
1040-
return () => {
1041-
listener.remove();
1042-
}
1043-
}, [])
1044-
```
1041+
const cleanupIntercomEventListeners = Intercom.bootstrapEventListeners();
10451042

1046-
### Options
1043+
const eventEmitter = new NativeEventEmitter(NativeModules.IntercomEventEmitter);
10471044

1048-
| Type | Type | Required |
1049-
| ------- | -------- | -------- |
1050-
| event| string (`IntercomEvents`) |yes |
1051-
| callback| function `({count?: number, visible?: boolean}) => void` |yes |
1045+
// Listen to unread conversation count changes
1046+
const unreadCountEventName = IntercomEvents.IntercomUnreadCountDidChange;
1047+
const countListener = eventEmitter.addListener(unreadCountEventName, (response) => {
1048+
console.log('Unread count:', response.count);
1049+
});
10521050

1053-
### Returns
1051+
return () => {
1052+
countListener.remove();
1053+
cleanupIntercomEventListeners();
1054+
};
1055+
}, []);
1056+
```
1057+
1058+
### Available Events
10541059

1055-
`EmitterSubscription`
1060+
| Event | Platform |
1061+
| ------- | -------- |
1062+
| IntercomUnreadConversationCountDidChangeNotification| iOS, Android |
1063+
| IntercomHelpCenterDidShowNotification| iOS |
1064+
| IntercomHelpCenterDidHideNotification| iOS |
1065+
| IntercomWindowDidShowNotification| iOS |
1066+
| IntercomWindowDidHideNotification| iOS |
10561067

10571068
___
10581069
### Types

example/src/App.tsx

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import {
55
AppState,
66
Image,
77
Linking,
8+
NativeEventEmitter,
9+
NativeModules,
810
Platform,
911
ScrollView,
1012
StatusBar,
@@ -13,11 +15,11 @@ import {
1315
View,
1416
} from 'react-native';
1517
import Intercom, {
16-
IntercomEvents,
1718
Space,
1819
type UserAttributes,
1920
Visibility,
2021
IntercomContent,
22+
IntercomEvents,
2123
ThemeMode,
2224
} from '@intercom/intercom-react-native';
2325

@@ -156,17 +158,28 @@ export default function App() {
156158
.catch((e) => console.log(e));
157159

158160
/**
159-
* Handle message count changed
161+
* Bootstrap Intercom event listeners
162+
*/
163+
const cleanupIntercomEventListeners = Intercom.bootstrapEventListeners();
164+
165+
const eventEmitter = new NativeEventEmitter(
166+
NativeModules.IntercomEventEmitter
167+
);
168+
169+
/**
170+
* Unread notification count changed listener
160171
*/
161-
const countListener = Intercom.addEventListener(
162-
IntercomEvents.IntercomUnreadCountDidChange,
172+
const unreadCountEventName = IntercomEvents.IntercomUnreadCountDidChange;
173+
const countListener = eventEmitter.addListener(
174+
unreadCountEventName,
163175
(response) => {
164176
setCount(response.count as number);
165177
}
166178
);
167179

168180
return () => {
169181
countListener.remove();
182+
cleanupIntercomEventListeners();
170183

171184
// @ts-ignore - type definitions haven't been updated to 0.65 yet
172185
urlListener.remove(); // <- for RN 0.65+

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@intercom/intercom-react-native",
3-
"version": "8.8.0",
3+
"version": "9.0.0-beta.1",
44
"description": "React Native wrapper to bridge our iOS and Android SDK",
55
"main": "lib/commonjs/index",
66
"module": "lib/module/index",

src/index.tsx

Lines changed: 13 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
1-
import {
2-
NativeModules,
3-
NativeEventEmitter,
4-
Platform,
5-
type EmitterSubscription,
6-
} from 'react-native';
1+
import { NativeModules, Platform } from 'react-native';
72

83
const { IntercomModule, IntercomEventEmitter } = NativeModules;
94

@@ -45,11 +40,6 @@ export const IntercomEvents = {
4540
IntercomEventEmitter.WINDOW_DID_HIDE_NOTIFICATION,
4641
};
4742

48-
type EventType =
49-
| 'IntercomUnreadConversationCountDidChangeNotification'
50-
| 'IntercomWindowDidHideNotification'
51-
| 'IntercomWindowDidShowNotification';
52-
5343
export type CustomAttributes = {
5444
[key: string]: boolean | string | number;
5545
};
@@ -324,12 +314,11 @@ export type IntercomType = {
324314
setUserJwt(JWT: String): Promise<boolean>;
325315

326316
/**
327-
* Add an event listener for the supported event types.
317+
* [Android Only] Bootstrap event listeners for Android. Call this before setting up your own NativeEventEmitter
318+
*
319+
* @returns cleanup function for Android
328320
*/
329-
addEventListener: (
330-
event: EventType,
331-
callback: (response: { count?: number; visible: boolean }) => void
332-
) => EmitterSubscription;
321+
bootstrapEventListeners: () => () => void;
333322
};
334323

335324
const Intercom: IntercomType = {
@@ -379,20 +368,17 @@ const Intercom: IntercomType = {
379368
setLogLevel: (logLevel) => IntercomModule.setLogLevel(logLevel),
380369
setThemeMode: (themeMode) => IntercomModule.setThemeMode(themeMode),
381370
setUserJwt: (jwt) => IntercomModule.setUserJwt(jwt),
382-
addEventListener: (event, callback) => {
383-
event === IntercomEvents.IntercomUnreadCountDidChange &&
384-
Platform.OS === 'android' &&
371+
372+
bootstrapEventListeners: () => {
373+
if (Platform.OS === 'android') {
385374
IntercomEventEmitter.startEventListener();
386-
const eventEmitter = new NativeEventEmitter(IntercomEventEmitter);
387-
const listener = eventEmitter.addListener(event, callback);
388-
const originalRemove = listener.remove;
389-
listener.remove = () => {
390-
event === IntercomEvents.IntercomUnreadCountDidChange &&
391-
Platform.OS === 'android' &&
375+
}
376+
377+
return () => {
378+
if (Platform.OS === 'android') {
392379
IntercomEventEmitter.removeEventListener();
393-
originalRemove();
380+
}
394381
};
395-
return listener;
396382
},
397383
};
398384

0 commit comments

Comments
 (0)