capacitor-native-update
Version:
Native Update Plugin for Capacitor
115 lines • 4.43 kB
JavaScript
import { BackgroundUpdateType, UpdateErrorCode } from '../definitions';
export class BackgroundScheduler {
constructor() {
this.config = null;
this.status = {
enabled: false,
isRunning: false,
checkCount: 0,
failureCount: 0,
};
}
configure(config) {
this.config = config;
this.status.enabled = config.enabled;
}
getStatus() {
return Object.assign({}, this.status);
}
async performCheck() {
var _a;
if (!((_a = this.config) === null || _a === void 0 ? void 0 : _a.enabled)) {
return {
success: false,
updatesFound: false,
notificationSent: false,
error: {
code: UpdateErrorCode.INVALID_CONFIG,
message: 'Background updates not enabled',
},
};
}
this.status.isRunning = true;
this.status.checkCount++;
try {
const result = await this.checkForUpdates();
this.status.lastCheckTime = Date.now();
this.status.isRunning = false;
this.status.lastError = undefined;
return result;
}
catch (error) {
this.status.failureCount++;
this.status.isRunning = false;
this.status.lastError = {
code: UpdateErrorCode.UNKNOWN_ERROR,
message: error instanceof Error ? error.message : 'Unknown error',
};
return {
success: false,
updatesFound: false,
notificationSent: false,
error: this.status.lastError,
};
}
}
async checkForUpdates() {
var _a, _b, _c, _d, _e, _f;
const promises = [];
let appUpdate;
let liveUpdate;
if (((_a = this.config) === null || _a === void 0 ? void 0 : _a.updateTypes.includes(BackgroundUpdateType.APP_UPDATE)) ||
((_b = this.config) === null || _b === void 0 ? void 0 : _b.updateTypes.includes(BackgroundUpdateType.BOTH))) {
promises.push(this.checkAppUpdate());
}
if (((_c = this.config) === null || _c === void 0 ? void 0 : _c.updateTypes.includes(BackgroundUpdateType.LIVE_UPDATE)) ||
((_d = this.config) === null || _d === void 0 ? void 0 : _d.updateTypes.includes(BackgroundUpdateType.BOTH))) {
promises.push(this.checkLiveUpdate());
}
const results = await Promise.allSettled(promises);
if (((_e = results[0]) === null || _e === void 0 ? void 0 : _e.status) === 'fulfilled') {
appUpdate = results[0].value;
}
if (((_f = results[1]) === null || _f === void 0 ? void 0 : _f.status) === 'fulfilled') {
liveUpdate = results[1].value;
}
const updatesFound = (appUpdate === null || appUpdate === void 0 ? void 0 : appUpdate.updateAvailable) || (liveUpdate === null || liveUpdate === void 0 ? void 0 : liveUpdate.available) || false;
let notificationSent = false;
if (updatesFound) {
notificationSent = await this.sendNotification(appUpdate, liveUpdate);
}
return {
success: true,
updatesFound,
appUpdate,
liveUpdate,
notificationSent,
};
}
async checkAppUpdate() {
throw new Error('App update checking not implemented in base class');
}
async checkLiveUpdate() {
throw new Error('Live update checking not implemented in base class');
}
async sendNotification(_appUpdate, _liveUpdate) {
throw new Error('Notification sending not implemented in base class');
}
calculateNextCheckTime() {
var _a;
const now = Date.now();
const interval = ((_a = this.config) === null || _a === void 0 ? void 0 : _a.checkInterval) || 24 * 60 * 60 * 1000; // Default 24 hours
return now + interval;
}
shouldRespectBatteryOptimization() {
var _a, _b;
return (_b = (_a = this.config) === null || _a === void 0 ? void 0 : _a.respectBatteryOptimization) !== null && _b !== void 0 ? _b : true;
}
isNetworkConditionMet() {
return true;
}
isBatteryLevelSufficient() {
return true;
}
}
//# sourceMappingURL=background-scheduler.js.map