跳过内容

事件

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();

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

可用事件

download

下载

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

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

事件数据:

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

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: BundleInfo - 可供下载的更新捆绑包的信息

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

主要可用

当有主要更新可用但被自动更新设置阻止时触发。

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

事件数据:

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

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
});

Section titled “downloadFailed”

当应用程序更新后,应用程序准备就绪时触发。

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 - Bundle 下载成功

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

object

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. 处理失败时保持风度: 下载和更新失败时正确处理错误。

  3. 向用户提供反馈: 使用下载进度事件向用户展示更新进度。

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

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

如果您正在使用 Events 为原生插件工作做出计划,连接它 使用@capgo/capacitor-updater 在使用@capgo/capacitor-updater中 Capgo插件目录 在Capgo插件目录中 Capacitor插件由Capgo 在Capacitor插件由Capgo中 添加或更新插件 在添加或更新插件中 Ionic企业插件替代品 在Ionic企业插件替代品中