UNPKG

@appzung/react-native-code-push

Version:

React Native plugin for the CodePush service

94 lines (93 loc) 4.86 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.notifyAppReady = void 0; var _reactNative = require("react-native"); var _LogLevelEnum = require("./enums/LogLevel.enum.js"); var _CodePushApiSdk = require("./internals/CodePushApiSdk.js"); var _NativeRNAppZungCodePushModule = require("./internals/NativeRNAppZungCodePushModule.js"); var _getConfiguration = require("./internals/getConfiguration.js"); var _log = require("./internals/utils/log.js"); var _requestFetchAdapter = require("./internals/utils/requestFetchAdapter.js"); /** * @function * * Notifies the CodePush runtime that an installed update is considered successful. * * If you are manually checking for and installing updates (i.e. not using the `sync` method to handle it all for you), then this method **MUST** be called; otherwise CodePush will treat the update as failed and rollback to the previous version when the app next restarts. */ const notifyAppReady = exports.notifyAppReady = (() => { // This ensures that notifyApplicationReadyInternal is only called once // in the lifetime of this module instance. let notifyApplicationReadyPromise; return () => { if (!notifyApplicationReadyPromise) { notifyApplicationReadyPromise = notifyApplicationReadyInternal(); } return notifyApplicationReadyPromise; }; })(); async function notifyApplicationReadyInternal() { (0, _log.log)(_LogLevelEnum.LogLevel.DEBUG, 'notifyApplicationReady'); await _NativeRNAppZungCodePushModule.NativeRNAppZungCodePushModule.notifyApplicationReady(); const statusReport = await _NativeRNAppZungCodePushModule.NativeRNAppZungCodePushModule.getNewStatusReport(); if (statusReport) { (0, _log.log)(_LogLevelEnum.LogLevel.DEBUG, `tryReportStatus ${statusReport.status || '(no update)'}`); tryReportStatus(statusReport); // Don't wait for this to complete. } else { (0, _log.log)(_LogLevelEnum.LogLevel.DEBUG, `Nothing to report`); } return statusReport; } async function tryReportStatus(statusReport, retryOnAppResume) { const config = await (0, _getConfiguration.getConfiguration)(); const previousLabelOrAppVersion = statusReport.previousLabelOrAppVersion ?? null; const previousReleaseChannelPublicId = statusReport.previousReleaseChannelPublicId || config.releaseChannelPublicId; try { if (statusReport.appVersion) { (0, _log.log)(_LogLevelEnum.LogLevel.INFO, `Reporting binary update (${statusReport.appVersion})`); if (!config.releaseChannelPublicId) { throw new Error('Release channel is missing'); } const sdk = new _CodePushApiSdk.CodePushApiSdk(_requestFetchAdapter.requestFetchAdapter, _log.log, config); await sdk.reportStatusDeploy(null, previousLabelOrAppVersion, previousReleaseChannelPublicId); } else { if (!statusReport.package) { throw new Error('Missing package in status report'); } const label = statusReport.package.label; if (statusReport.status === 'DeploymentSucceeded') { (0, _log.log)(_LogLevelEnum.LogLevel.INFO, `Reporting CodePush update success (${label})`); } else { (0, _log.log)(_LogLevelEnum.LogLevel.INFO, `Reporting CodePush update rollback (${label})`); await _NativeRNAppZungCodePushModule.NativeRNAppZungCodePushModule.setLatestRollbackInfo(statusReport.package.packageHash); } config.releaseChannelPublicId = statusReport.package.releaseChannelPublicId; const sdk = new _CodePushApiSdk.CodePushApiSdk(_requestFetchAdapter.requestFetchAdapter, _log.log, config); await sdk.reportStatusDeploy({ package: statusReport.package, status: statusReport.status }, previousLabelOrAppVersion, previousReleaseChannelPublicId); } _NativeRNAppZungCodePushModule.NativeRNAppZungCodePushModule.recordStatusReported(statusReport); retryOnAppResume && retryOnAppResume.remove(); } catch (e) { (0, _log.log)(_LogLevelEnum.LogLevel.WARN, `Report status failed: ${JSON.stringify(statusReport)}`); _NativeRNAppZungCodePushModule.NativeRNAppZungCodePushModule.saveStatusReportForRetry(statusReport); // Try again when the app resumes if (!retryOnAppResume) { const resumeListener = _reactNative.AppState.addEventListener('change', async newState => { if (newState !== 'active') return; const refreshedStatusReport = await _NativeRNAppZungCodePushModule.NativeRNAppZungCodePushModule.getNewStatusReport(); if (refreshedStatusReport) { (0, _log.log)(_LogLevelEnum.LogLevel.DEBUG, `tryReportStatus on active appState ${statusReport.status || '(no update)'}`); tryReportStatus(refreshedStatusReport, resumeListener); } else { resumeListener && resumeListener.remove(); } }); } } } //# sourceMappingURL=notifyAppReady.js.map