Events
Capacitor 更新程序插件提供了多个您可以监听的事件,用于监视更新过程并响应不同的状态。
事件监听器设置
Section titled “事件监听器设置”要侦听事件,请在 CapacitorUpdater 对象上使用 addListener 方法:
import { CapacitorUpdater } from '@capgo/capacitor-updater';
// Add a listenerconst listener = await CapacitorUpdater.addListener('eventName', (event) => { // Handle the event});
// Remove the listener when no longer neededlistener.remove();
// Remove all listenersawait CapacitorUpdater.removeAllListeners();download
Section titled “download”在捆绑包下载过程中触发。提供下载进度信息。
CapacitorUpdater.addListener('download', (event) => { console.log(`Download progress: ${event.percent}%`); console.log('Bundle info:', event.bundle);});事件数据:
percent:数字 - 下载进度百分比(0-100)bundle:BundleInfo - 有关正在下载的捆绑包的信息
noNeedUpdate
Section titled “noNeedUpdate”当更新检查确定不需要更新时触发。
CapacitorUpdater.addListener('noNeedUpdate', (event) => { console.log('App is up to date'); console.log('Current bundle:', event.bundle);});事件数据:
bundle:BundleInfo - 有关当前捆绑包的信息
updateAvailable
Section titled “updateAvailable”当有新的更新可供下载时触发。
CapacitorUpdater.addListener('updateAvailable', (event) => { console.log('Update available'); console.log('New bundle:', event.bundle); // You can trigger a download here if needed});事件数据:
bundle:BundleInfo - 有关可用更新包的信息
downloadComplete
Section titled “downloadComplete”当捆绑包下载成功完成时触发。
CapacitorUpdater.addListener('downloadComplete', (event) => { console.log('Download completed'); console.log('Downloaded bundle:', event.bundle); // You might want to set this bundle as next});事件数据:
bundle:BundleInfo - 有关下载的捆绑包的信息
majorAvailable
Section titled “majorAvailable”当主要更新可用但被自动更新设置阻止时触发。
CapacitorUpdater.addListener('majorAvailable', (event) => { console.log('Major update available:', event.version); // Notify user about major update});事件数据:
version: string - 主要更新的版本号
updateFailed
Section titled “updateFailed”当下次应用程序启动时安装更新失败时触发。
CapacitorUpdater.addListener('updateFailed', (event) => { console.error('Update failed to install'); console.log('Failed bundle:', event.bundle); // Handle rollback or retry logic});事件数据:
bundle:BundleInfo - 有关安装失败的捆绑包的信息
downloadFailed
Section titled “downloadFailed”当捆绑包下载失败时触发。
CapacitorUpdater.addListener('downloadFailed', (event) => { console.error('Download failed for version:', event.version); // Handle download retry logic});事件数据:
version: string - 下载失败的版本
appReloaded
Section titled “appReloaded”当应用程序重新加载时触发。
CapacitorUpdater.addListener('appReloaded', () => { console.log('App has been reloaded'); // Perform any necessary reinitialization});事件数据: 无
appReady
Section titled “appReady”当应用程序更新后可供使用时触发。
CapacitorUpdater.addListener('appReady', (event) => { console.log('App is ready'); console.log('Current bundle:', event.bundle); console.log('Status:', event.status);});事件数据:
bundle:BundleInfo - 有关当前捆绑包的信息status: string - 就绪状态
BundleInfo 对象
Section titled “BundleInfo 对象”许多事件都包含具有以下属性的 BundleInfo 对象:
interface BundleInfo { id: string; // Unique bundle identifier version: string; // Bundle version downloaded: string; // Download timestamp checksum?: string; // Bundle checksum (if available) status: BundleStatus; // Bundle status}其中 BundleStatus 可以是:
'success'- 捆绑包下载成功'错误'- 捆绑包下载/安装失败'pending'- 捆绑包正在等待设置为下一个- “正在下载’` - 捆绑包当前正在下载
示例:完整更新流程
Section titled “示例:完整更新流程”以下是使用事件处理完整更新流程的示例:
import { CapacitorUpdater } from '@capgo/capacitor-updater';
export class UpdateManager { private listeners: any[] = [];
async setupListeners() { // Listen for available updates this.listeners.push( await CapacitorUpdater.addListener('updateAvailable', async (event) => { console.log('Update available:', event.bundle.version); // Auto-download the update await CapacitorUpdater.download({ url: event.bundle.url, version: event.bundle.version }); }) );
// Monitor download progress this.listeners.push( await CapacitorUpdater.addListener('download', (event) => { console.log(`Downloading: ${event.percent}%`); // Update UI progress bar this.updateProgressBar(event.percent); }) );
// Handle download completion this.listeners.push( await CapacitorUpdater.addListener('downloadComplete', async (event) => { console.log('Download complete:', event.bundle.version); // Set as next bundle await CapacitorUpdater.next({ id: event.bundle.id }); }) );
// Handle failures this.listeners.push( await CapacitorUpdater.addListener('downloadFailed', (event) => { console.error('Download failed:', event.version); this.showError('Update download failed. Please try again later.'); }) );
this.listeners.push( await CapacitorUpdater.addListener('updateFailed', (event) => { console.error('Update installation failed:', event.bundle.version); this.showError('Update installation failed. The app has been rolled back.'); }) );
// Handle app ready this.listeners.push( await CapacitorUpdater.addListener('appReady', async (event) => { console.log('App ready with bundle:', event.bundle.version); }) ); }
cleanup() { // Remove all listeners when no longer needed this.listeners.forEach(listener => listener.remove()); this.listeners = []; }
private updateProgressBar(percent: number) { // Update your UI progress bar }
private showError(message: string) { // Show error to user }}最佳实践1. 始终调用 notifyAppReady():使用自动更新时,请始终在应用初始化后调用此方法以防止回滚。
Section titled “最佳实践1. 始终调用 notifyAppReady():使用自动更新时,请始终在应用初始化后调用此方法以防止回滚。”-
优雅地处理失败:对下载和更新失败实施正确的错误处理。
-
提供用户反馈:使用下载进度事件向用户展示更新进度。
-
清理监听器:当不再需要事件监听器时将其删除,以防止内存泄漏。
-
测试更新场景:测试各种更新场景,包括失败、回滚和重大更新。