Skip to content

Commit 43f49c0

Browse files
trcoffmancc-matthias-m
authored andcommitted
Fix usage of removed method AppState.removeEventListener (microsoft#2385)
AppState.removeEventListener was removed in react-native 0.70, after being deprecated for over a year. Anybody using react-native 0.70 will experience an unhandled TypeError when trying to invoke removeEventListener. Replace the usage of removeEventListener with the `remove()` method on the subscription returned by addEventListener. Stop reassigning the resumeListener argument. It's a bad practice to reassign function arguments, and it is common to have an eslint rule to block this. Rename the argument to retryOnAppResume so that when we create resumeListener, there is no reassignment of a function argument.
1 parent bd883b8 commit 43f49c0

File tree

1 file changed

+6
-7
lines changed

1 file changed

+6
-7
lines changed

CodePush.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ async function notifyApplicationReadyInternal() {
180180
return statusReport;
181181
}
182182

183-
async function tryReportStatus(statusReport, resumeListener) {
183+
async function tryReportStatus(statusReport, retryOnAppResume) {
184184
const config = await getConfiguration();
185185
const previousLabelOrAppVersion = statusReport.previousLabelOrAppVersion;
186186
const previousDeploymentKey = statusReport.previousDeploymentKey || config.deploymentKey;
@@ -209,22 +209,21 @@ async function tryReportStatus(statusReport, resumeListener) {
209209
}
210210

211211
NativeCodePush.recordStatusReported(statusReport);
212-
resumeListener && AppState.removeEventListener("change", resumeListener);
212+
retryOnAppResume && retryOnAppResume.remove();
213213
} catch (e) {
214214
log(`Report status failed: ${JSON.stringify(statusReport)}`);
215215
NativeCodePush.saveStatusReportForRetry(statusReport);
216216
// Try again when the app resumes
217-
if (!resumeListener) {
218-
resumeListener = async (newState) => {
217+
if (!retryOnAppResume) {
218+
const resumeListener = AppState.addEventListener("change", async (newState) => {
219219
if (newState !== "active") return;
220220
const refreshedStatusReport = await NativeCodePush.getNewStatusReport();
221221
if (refreshedStatusReport) {
222222
tryReportStatus(refreshedStatusReport, resumeListener);
223223
} else {
224-
AppState.removeEventListener("change", resumeListener);
224+
resumeListener && resumeListener.remove();
225225
}
226-
};
227-
AppState.addEventListener("change", resumeListener);
226+
});
228227
}
229228
}
230229
}

0 commit comments

Comments
 (0)