native-update
Version:
Foundation package for building a comprehensive update system for Capacitor apps. Provides architecture and interfaces but requires backend implementation.
123 lines • 4.65 kB
JavaScript
import { Logger } from '../core/logger';
import { AppUpdateInstallStatus, } from './types';
export class AppUpdateInstaller {
constructor(_) {
// config parameter is kept for compatibility but not used
this.logger = new Logger('AppUpdateInstaller');
this.currentState = {
installStatus: AppUpdateInstallStatus.UNKNOWN,
packageName: '',
availableVersion: '',
};
}
async startImmediateUpdate() {
this.logger.log('Starting immediate update installation');
// Update state
this.updateState(AppUpdateInstallStatus.PENDING);
// On Android, this would trigger Play Core immediate update
// On iOS, this would open App Store
if (this.isAndroid()) {
// Android implementation would use Play Core Library
this.logger.log('Triggering Android immediate update');
}
else if (this.isIOS()) {
// iOS implementation would open App Store
this.logger.log('Opening iOS App Store for update');
}
else {
// Web fallback
throw new Error('Immediate updates not supported on web platform');
}
}
async startFlexibleUpdate() {
this.logger.log('Starting flexible update download');
// Update state
this.updateState(AppUpdateInstallStatus.DOWNLOADING);
// Start download simulation for web
if (this.isWeb()) {
this.simulateFlexibleUpdate();
}
else if (this.isAndroid()) {
// Android implementation would use Play Core Library
this.logger.log('Starting Android flexible update');
}
else {
// iOS doesn't support flexible updates
throw new Error('Flexible updates not supported on iOS');
}
}
async completeFlexibleUpdate() {
this.logger.log('Completing flexible update installation');
if (this.currentState.installStatus !== AppUpdateInstallStatus.DOWNLOADED) {
throw new Error('Update not ready for installation');
}
// Update state
this.updateState(AppUpdateInstallStatus.INSTALLING);
// Trigger installation
if (this.isAndroid()) {
// Android implementation would complete the update
this.logger.log('Completing Android update installation');
}
else {
// Simulate completion
setTimeout(() => {
this.updateState(AppUpdateInstallStatus.INSTALLED);
}, 1000);
}
}
async cancelUpdate() {
this.logger.log('Cancelling update');
if (this.currentState.installStatus === AppUpdateInstallStatus.DOWNLOADING) {
this.updateState(AppUpdateInstallStatus.CANCELED);
}
}
async getInstallState() {
return Object.assign({}, this.currentState);
}
onProgress(callback) {
this.progressCallback = callback;
}
updateState(status, errorCode) {
this.currentState.installStatus = status;
if (errorCode !== undefined) {
this.currentState.installErrorCode = errorCode;
}
this.logger.log('Update state changed', this.currentState);
}
simulateFlexibleUpdate() {
// Simulate download progress for web platform
let downloaded = 0;
const totalSize = 50 * 1024 * 1024; // 50MB
const chunkSize = 1024 * 1024; // 1MB per tick
const interval = setInterval(() => {
downloaded += chunkSize;
if (downloaded >= totalSize) {
downloaded = totalSize;
clearInterval(interval);
this.updateState(AppUpdateInstallStatus.DOWNLOADED);
}
const progress = {
bytesDownloaded: downloaded,
totalBytesToDownload: totalSize,
percentComplete: Math.round((downloaded / totalSize) * 100),
downloadSpeed: chunkSize, // 1MB/s
estimatedTime: Math.ceil((totalSize - downloaded) / chunkSize),
};
if (this.progressCallback) {
this.progressCallback(progress);
}
}, 1000);
}
isAndroid() {
return (typeof window !== 'undefined' &&
/android/i.test(window.navigator.userAgent));
}
isIOS() {
return (typeof window !== 'undefined' &&
/iPad|iPhone|iPod/.test(window.navigator.userAgent));
}
isWeb() {
return !this.isAndroid() && !this.isIOS();
}
}
//# sourceMappingURL=app-update-installer.js.map