事件
复制一个包含安装步骤和此插件的完整 Markdown 指南的配置提示。
Capacitor 更新器插件提供了多个事件,您可以监听这些事件来监控更新过程并响应不同状态。
事件监听设置
标题:事件监听设置要监听事件,请使用 addListener 方法在 CapacitorUpdater 对象上:
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
下载下载过程中触发的事件,提供下载进度信息。
CapacitorUpdater.addListener('download', (event) => { console.log(`Download progress: ${event.percent}%`); console.log('Bundle info:', event.bundle);});事件数据:
percent事件数据:bundle: 下载进度百分比(0-100)
noNeedUpdate
下载过程中会触发多次以报告进度。无需更新的通知
CapacitorUpdater.addListener('noNeedUpdate', (event) => { console.log('App is up to date'); console.log('Current bundle:', event.bundle);});事件数据:
bundle: BundleInfo - 当前捆绑包的信息
updateAvailable
更新可用当有新更新可供下载时触发。
CapacitorUpdater.addListener('updateAvailable', (event) => { console.log('Update available'); console.log('New bundle:', event.bundle); // You can trigger a download here if needed});事件数据:
bundle上下文:Capgo营销网站。角色:UI标签。见于:页面代理/[slug].astro。消息键 `agents_page_copy_label` (代理页面复制标签)。
downloadComplete
: BundleInfo - 可供下载的更新捆绑包的信息下载完成
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
主标题:"majorAvailable"当有重大更新可用,但被自动更新设置阻止时触发。
CapacitorUpdater.addListener('majorAvailable', (event) => { console.log('Major update available:', event.version); // Notify user about major update});事件数据:
version事件数据:
updateFailed
主要更新通常需要用户确认或应用商店更新。使用此事件通知用户。主标题:"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
下载失败下载失败时触发
CapacitorUpdater.addListener('downloadFailed', (event) => { console.error('Download failed for version:', event.version); // Handle download retry logic});事件数据:
version: string -
appReloaded
应用重新加载无
CapacitorUpdater.addListener('appReloaded', () => { console.log('App has been reloaded'); // Perform any necessary reinitialization});事件数据: 应用就绪
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 具有以下属性的对象
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}Where BundleStatus 可以是:
'success'- Bundle 下载成功'error'- Bundle 下载/安装失败'pending'- Bundle 等待设置为下一个'downloading'- Bundle 正在下载
示例:完整更新流程
示例:完整更新流程注意:
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 }}更新
流程-
所
notifyAppReady()需 -
调用的方法。 请参阅以下文档以获取更多信息。: 下载和更新失败时,正确处理错误。
-
向用户提供反馈。: 使用下载进度事件向用户展示更新进度。
-
清除监听器。: 当监听器不再需要时,移除它们以防止内存泄漏。
-
测试更新场景。: 测试各种更新场景,包括失败、回滚和重大更新。
继续阅读:事件
标题:继续阅读:事件如果您正在使用 事件 来规划原生插件工作,连接它与 使用@capgo/capacitor-updater 为native能力在使用@capgo/capacitor-updater Capgo插件目录 为产品工作流程在Capgo插件目录 Capacitor插件由Capgo 为实现细节在Capacitor插件由Capgo 添加或更新插件 为实现细节在添加或更新插件,并且 Ionic企业插件替代品 为产品工作流程在Ionic企业插件替代品