跳过内容

Events

GitHub

Capacitor 更新器插件提供了几个事件,您可以监听这些事件来监控更新过程并响应不同状态。

事件监听器设置

标题:事件监听器设置

要监听事件,请使用 addListener 方法在 CapacitorUpdater 对象上:

import { CapacitorUpdater } from '@capgo/capacitor-updater';
// Add a listener
const listener = await CapacitorUpdater.addListener('eventName', (event) => {
// Handle the event
});
// Remove the listener when no longer needed
listener.remove();
// Remove all listeners
await CapacitorUpdater.removeAllListeners();

在 bundle 下载过程中触发。提供下载进度信息。

CapacitorUpdater.addListener('download', (event) => {
console.log(`Download progress: ${event.percent}%`);
console.log('Bundle info:', event.bundle);
});

事件数据:

  • percent: number - 下载进度百分比 (0-100)
  • bundle: BundleInfo - 下载的包信息

当检测到无需更新时触发此事件。

CapacitorUpdater.addListener('noNeedUpdate', (event) => {
console.log('App is up to date');
console.log('Current bundle:', event.bundle);
});

事件数据:

  • bundle: BundleInfo - 当前包信息

updateAvailable

《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

《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

《majorAvailable》

当有可用的大版本更新但被自动更新设置阻止时触发。

CapacitorUpdater.addListener('majorAvailable', (event) => {
console.log('Major update available:', event.version);
// Notify user about major update
});

事件数据:

  • version: string - 主要更新的版本号

在下一个应用启动时安装更新失败时触发。

CapacitorUpdater.addListener('updateFailed', (event) => {
console.error('Update failed to install');
console.log('Failed bundle:', event.bundle);
// Handle rollback or retry logic
});

事件数据:

  • bundle: BundleInfo - 安装失败的包信息

downloadFailed

标题:下载失败

当bundle下载失败时触发的事件。

CapacitorUpdater.addListener('downloadFailed', (event) => {
console.error('Download failed for version:', event.version);
// Handle download retry logic
});

事件数据:

  • versionstring - 未能下载的版本号

当app重新加载时触发的事件。

CapacitorUpdater.addListener('appReloaded', () => {
console.log('App has been reloaded');
// Perform any necessary reinitialization
});

事件数据:

当app更新后准备就绪时触发的事件。

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
}

在哪里可以: BundleStatus - 下载成功

  • 'success' - 下载安装失败
  • 'error' - 等待设置为下一个
  • 'pending' - 正在下载
  • 'downloading' 示例:完整更新流程

注意

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()向用户提供反馈

  2. Copy to clipboardAlways call __CAPGO_KEEP_0__ after your app initializes to prevent rollback.

  3. Handle failures gracefully: Implement proper error handling for download and update failures.: 使用下载进度事件来向用户显示更新进度。

  4. 清除监听器: 移除不再需要的事件监听器以防止内存泄漏。

  5. 测试更新场景: 测试各种更新场景,包括失败、回滚和重大更新。

如果您正在使用 事件 来规划原生插件工作,连接它与 使用 @capgo/capacitor-updater 来更新原生能力的 Using @capgo/capacitor-updater, Capgo 插件目录 在 Capgo 插件目录中管理产品流程 Capacitor 由 Capgo 提供 了解 Capacitor 由 Capgo 提供的实现细节 添加或更新插件 了解添加或更新插件的实现细节 Ionic Enterprise 插件替代方案 了解 Ionic Enterprise 插件替代方案的产品流程