Skip to content

Commit b55f594

Browse files
committed
notif: Push dispatch down into code that really uses it.
There isn't any meaningful abstraction boundary being drawn by hiding `dispatch` from this code and passing it these applications of it instead. Simplify by just passing it `dispatch`, as we often do elsewhere. Probably what this is really saying is that this code all belongs on the action-creator side; perhaps another time.
1 parent a7e58f5 commit b55f594

File tree

2 files changed

+12
-23
lines changed

2 files changed

+12
-23
lines changed

src/notification/index.js

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import config from '../config';
88
import { registerPush, unregisterPush } from '../api';
99
import { logErrorRemotely } from '../utils/logging';
1010
import { doNarrow } from '../message/messagesActions';
11+
import { saveTokenPush, deleteTokenPush } from './notificationActions';
1112

1213
const getGroupNarrowFromNotificationData = (data: NotificationGroup, usersById: UserIdMap = {}) => {
1314
const userIds = data.pm_users.split(',');
@@ -98,58 +99,50 @@ export class NotificationListener {
9899
}
99100
}
100101

101-
const getTokenIOS = (auth: Auth, saveTokenPush: (pushToken: string) => void) => {
102+
const getTokenIOS = (auth: Auth, dispatch: Dispatch) => {
102103
NotificationsIOS.addEventListener('remoteNotificationsRegistered', async deviceToken => {
103104
await registerPush(auth, deviceToken);
104-
saveTokenPush(deviceToken);
105+
dispatch(saveTokenPush(deviceToken));
105106
});
106107
NotificationsIOS.addEventListener('remoteNotificationsRegistrationFailed', (error: string) => {
107108
logErrorRemotely(new Error(error), 'Failed to register iOS push token');
108109
});
109110
NotificationsIOS.requestPermissions();
110111
};
111112

112-
const getTokenAndroid = (
113-
auth: Auth,
114-
oldToken: string | null,
115-
saveTokenPush: (pushToken: string) => void,
116-
) => {
113+
const getTokenAndroid = (auth: Auth, oldToken: string | null, dispatch: Dispatch) => {
117114
if (auth.apiKey !== '' && oldToken === null) {
118115
NotificationsAndroid.refreshToken();
119116
}
120117
NotificationsAndroid.setRegistrationTokenUpdateListener(async deviceToken => {
121118
try {
122119
await registerPush(auth, deviceToken);
123-
saveTokenPush(deviceToken);
120+
dispatch(saveTokenPush(deviceToken));
124121
} catch (e) {
125122
logErrorRemotely(e, 'Failed to register GCM');
126123
}
127124
});
128125
};
129126

130-
export const getNotificationToken = (
131-
auth: Auth,
132-
oldToken: string | null,
133-
saveTokenPush: (pushToken: string) => void,
134-
) => {
127+
export const getNotificationToken = (auth: Auth, oldToken: string | null, dispatch: Dispatch) => {
135128
if (Platform.OS === 'ios') {
136-
getTokenIOS(auth, saveTokenPush);
129+
getTokenIOS(auth, dispatch);
137130
} else {
138-
getTokenAndroid(auth, oldToken, saveTokenPush);
131+
getTokenAndroid(auth, oldToken, dispatch);
139132
}
140133
};
141134

142135
export const tryStopNotifications = async (
143136
auth: Auth,
144137
token: string | null,
145-
callback: () => void,
138+
dispatch: Dispatch,
146139
) => {
147140
if (token !== null) {
148141
try {
149142
await unregisterPush(auth, token);
150143
} catch (e) {
151144
logErrorRemotely(e, 'failed to unregister Push token');
152145
}
153-
callback();
146+
dispatch(deleteTokenPush());
154147
}
155148
};

src/notification/notificationActions.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,11 @@ export const saveTokenPush = (pushToken: string): SaveTokenPushAction => ({
1919
export const initNotifications = () => (dispatch: Dispatch, getState: GetState) => {
2020
const auth = getAuth(getState());
2121
const pushToken = getPushToken(getState());
22-
getNotificationToken(auth, pushToken, token => {
23-
dispatch(saveTokenPush(token));
24-
});
22+
getNotificationToken(auth, pushToken, dispatch);
2523
};
2624

2725
export const tryStopNotifications = () => async (dispatch: Dispatch, getState: GetState) => {
2826
const auth = getAuth(getState());
2927
const pushToken = getPushToken(getState());
30-
innerStopNotifications(auth, pushToken, () => {
31-
dispatch(deleteTokenPush());
32-
});
28+
innerStopNotifications(auth, pushToken, dispatch);
3329
};

0 commit comments

Comments
 (0)