native-update
Version:
Foundation package for building a comprehensive update system for Capacitor apps. Provides architecture and interfaces but requires backend implementation.
195 lines • 7.87 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() {
// Implementation for checking app updates
// This would normally call the native platform or server API
try {
// Simulate app update check
const currentVersion = '1.0.0';
const availableVersion = '1.1.0';
// In real implementation, this would check with app store or server
const updateAvailable = this.compareVersions(currentVersion, availableVersion) < 0;
return {
updateAvailable,
currentVersion,
availableVersion,
updatePriority: updateAvailable ? 3 : undefined, // 3 = MEDIUM priority
// releaseNotes: updateAvailable
// ? 'Bug fixes and performance improvements'
// : undefined,
};
}
catch (error) {
console.error('Failed to check app update:', error);
return undefined;
}
}
async checkLiveUpdate() {
// Implementation for checking live updates
try {
// In real implementation, this would call the update server
const currentVersion = '1.0.0';
const latestVersion = '1.0.1';
const updateAvailable = this.compareVersions(currentVersion, latestVersion) < 0;
const result = {
available: updateAvailable,
version: latestVersion,
url: updateAvailable ? 'https://updates.example.com/v1.0.1' : undefined,
notes: updateAvailable ? 'Minor bug fixes' : undefined,
size: updateAvailable ? 1024 * 1024 * 5 : undefined, // 5MB
};
// Store checksum separately if needed
result.checksum = updateAvailable ? 'abc123def456' : undefined;
return result;
}
catch (error) {
console.error('Failed to check live update:', error);
return undefined;
}
}
async sendNotification(appUpdate, liveUpdate) {
var _a;
// Implementation for sending notifications
try {
// Check if notifications are enabled in config
const notificationEnabled = (_a = this.config) === null || _a === void 0 ? void 0 : _a.notificationEnabled;
if (!notificationEnabled) {
return false;
}
let title = 'Update Available';
let body = '';
if (appUpdate === null || appUpdate === void 0 ? void 0 : appUpdate.updateAvailable) {
title = 'App Update Available';
body = `Version ${appUpdate.availableVersion} is ready to install.`;
}
else if (liveUpdate === null || liveUpdate === void 0 ? void 0 : liveUpdate.available) {
title = 'New Update Available';
body = `Version ${liveUpdate.version} is ready to download. ${liveUpdate.notes || ''}`;
}
// In real implementation, this would use native notification APIs
console.log('Sending notification:', { title, body });
// Simulate notification sent
return true;
}
catch (error) {
console.error('Failed to send notification:', error);
return false;
}
}
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;
}
compareVersions(version1, version2) {
const v1Parts = version1.split('.').map(Number);
const v2Parts = version2.split('.').map(Number);
for (let i = 0; i < Math.max(v1Parts.length, v2Parts.length); i++) {
const v1Part = v1Parts[i] || 0;
const v2Part = v2Parts[i] || 0;
if (v1Part > v2Part)
return 1;
if (v1Part < v2Part)
return -1;
}
return 0;
}
}
//# sourceMappingURL=background-scheduler.js.map