@appzung/react-native-code-push
Version:
React Native plugin for the CodePush service
59 lines (58 loc) • 2.42 kB
JavaScript
import { LogLevel } from "../enums/LogLevel.enum.js";
import { NativeRNAppZungCodePushModule } from "./NativeRNAppZungCodePushModule.js";
import { log } from "./utils/log.js";
const DEFAULT_ROLLBACK_RETRY_OPTIONS = {
delayInHours: 24,
maxRetryAttempts: 1
};
function validateLatestRollbackInfo(latestRollbackInfo, packageHash) {
return latestRollbackInfo && latestRollbackInfo.time && latestRollbackInfo.count && latestRollbackInfo.packageHash && latestRollbackInfo.packageHash === packageHash;
}
function validateRollbackRetryOptions(rollbackRetryOptions) {
if (typeof rollbackRetryOptions.delayInHours !== 'number') {
log(LogLevel.ERROR, "The 'delayInHours' rollback retry parameter must be a number.");
return false;
}
if (typeof rollbackRetryOptions.maxRetryAttempts !== 'number') {
log(LogLevel.ERROR, "The 'maxRetryAttempts' rollback retry parameter must be a number.");
return false;
}
if (rollbackRetryOptions.maxRetryAttempts < 1) {
log(LogLevel.ERROR, "The 'maxRetryAttempts' rollback retry parameter cannot be less then 1.");
return false;
}
return true;
}
export async function shouldUpdateBeIgnored(remotePackage, syncOptions) {
const isFailedPackage = remotePackage && remotePackage.failedInstall;
if (!isFailedPackage || !syncOptions.ignoreFailedUpdates) {
return false;
}
if (!syncOptions.rollbackRetryOptions) {
return true;
}
const rollbackRetryOptions = typeof syncOptions.rollbackRetryOptions === 'object' ? {
...DEFAULT_ROLLBACK_RETRY_OPTIONS,
...syncOptions.rollbackRetryOptions
} : DEFAULT_ROLLBACK_RETRY_OPTIONS;
if (!validateRollbackRetryOptions(rollbackRetryOptions)) {
return true;
}
const latestRollbackInfo = await NativeRNAppZungCodePushModule.getLatestRollbackInfo();
if (!validateLatestRollbackInfo(latestRollbackInfo, remotePackage.packageHash)) {
log(LogLevel.ERROR, 'The latest rollback info is not valid.');
return true;
}
const {
delayInHours,
maxRetryAttempts
} = rollbackRetryOptions;
const hoursSinceLatestRollback = (Date.now() - latestRollbackInfo.time) / (1000 * 60 * 60);
if (hoursSinceLatestRollback >= delayInHours && maxRetryAttempts >= latestRollbackInfo.count) {
log(LogLevel.INFO, 'Previous rollback should be ignored due to rollback retry options.');
return false;
}
return true;
}
//# sourceMappingURL=shouldUpdateBeIgnored.js.map
;