Skip to content

Commit 70a7e90

Browse files
committed
NotificationListener [nfc]: Have listenIOS take a params object
This will be useful soon when we have listenIOS use a new NativeEventEmitter we've written, instead of relying on the emitter @react-native-community/push-notification-ios gives us, which is buggy. The `addListener` method of our new NativeEventEmitter will be typed such that the passed event name and handler are checked against each other. Now that listenIOS has a single param with both the name and the handler, it'll be easy to write it so it expects one or another kind of name/handler pair, by typing its param as a union of object types. Then the new caller can pass a value that flows through one branch of the union, to the new NativeEventEmitter, without changing any type constraints on the existing callers.
1 parent 367f777 commit 70a7e90

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

src/notification/NotificationListener.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,19 @@ export default class NotificationListener {
4242
}
4343

4444
/** Private. */
45-
listenIOS(name: PushNotificationEventName, handler: (...empty) => void | Promise<void>) {
45+
// prettier-ignore
46+
listenIOS(
47+
args:
48+
{| +name: PushNotificationEventName, +handler: (...empty) => void | Promise<void> |}
49+
) {
4650
// In the native code, the PushNotificationEventName we pass here
4751
// is mapped to something else (see implementation):
4852
//
4953
// 'notification' -> 'remoteNotificationReceived'
5054
// 'localNotification' -> 'localNotificationReceived'
5155
// 'register' -> 'remoteNotificationsRegistered'
5256
// 'registrationError' -> 'remoteNotificationRegistrationError'
57+
const { name, handler } = args;
5358
PushNotificationIOS.addEventListener(name, handler);
5459
this.unsubs.push(() => PushNotificationIOS.removeEventListener(name));
5560
}
@@ -91,8 +96,8 @@ export default class NotificationListener {
9196
this.listenAndroid('notificationOpened', this.handleNotificationOpen);
9297
this.listenAndroid('remoteNotificationsRegistered', this.handleDeviceToken);
9398
} else {
94-
this.listenIOS('register', this.handleDeviceToken);
95-
this.listenIOS('registrationError', this.handleIOSRegistrationFailure);
99+
this.listenIOS({ name: 'register', handler: this.handleDeviceToken });
100+
this.listenIOS({ name: 'registrationError', handler: this.handleIOSRegistrationFailure });
96101
}
97102

98103
if (Platform.OS === 'android') {

0 commit comments

Comments
 (0)