コンテンツへスキップ

Events

Capacitor Updater プラグインは、更新プロセスを監視し、さまざまな状態に応答するためにリッスンできるいくつかのイベントを提供します。

イベントリスナーのセットアップ

Section titled “イベントリスナーのセットアップ”

イベントをリッスンするには、CapacitorUpdater オブジェクトの addListener メソッドを使用します。

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

バンドルのダウンロード プロセス中に発生します。ダウンロードの進行状況に関する情報を提供します。

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

イベントデータ:

  • percent: 数値 - ダウンロードの進行状況のパーセンテージ (0 ~ 100)
  • bundle: BundleInfo - ダウンロード中のバンドルに関する情報

更新のチェックで更新が必要ないと判断された場合に発生します。

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

イベントデータ:

  • bundle: BundleInfo - 現在のバンドルに関する情報

新しいアップデートがダウンロード可能になったときに発生します。

CapacitorUpdater.addListener('updateAvailable', (event) => {
console.log('Update available');
console.log('New bundle:', event.bundle);
// You can trigger a download here if needed
});

イベントデータ:

  • bundle: 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 - ダウンロードされたバンドルに関する情報

メジャー アップデートが利用可能であるが、自動アップデート設定によってブロックされている場合に発生します。

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 - インストールに失敗したバンドルに関する情報

バンドルのダウンロードが失敗したときに発生します。

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

イベントデータ:

  • version: string - ダウンロードに失敗したバージョン

アプリがリロードされたときに発生します。

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

イベントデータ: なし

更新後にアプリを使用できる状態になったときに発生します。

CapacitorUpdater.addListener('appReady', (event) => {
console.log('App is ready');
console.log('Current bundle:', event.bundle);
console.log('Status:', event.status);
});

イベントデータ:

  • bundle: BundleInfo - 現在のバンドルに関する情報
  • status: 文字列 - 準備完了ステータス

多くのイベントには、次のプロパティを持つ 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() を呼び出します。自動更新を使用する場合は、ロールバックを防ぐために、アプリの初期化後に必ずこのメソッドを呼び出します。

Section titled “ベストプラクティス1. 常に notifyAppReady() を呼び出します。自動更新を使用する場合は、ロールバックを防ぐために、アプリの初期化後に必ずこのメソッドを呼び出します。”
  1. 失敗を適切に処理します: ダウンロードおよび更新の失敗に対する適切なエラー処理を実装します。

  2. ユーザー フィードバックの提供: ダウンロード進行状況イベントを使用して、更新の進行状況をユーザーに表示します。

  3. リスナーのクリーンアップ: メモリ リークを防ぐために、不要になったイベント リスナーを削除します。

  4. 更新シナリオのテスト: 失敗、ロールバック、メジャー アップデートなどのさまざまな更新シナリオをテストします。