メニューに進む

イベント

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

利用可能なイベント

利用可能なイベント

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

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

イベントデータ:

  • percent: ダウンロードの進行度(0-100)
  • bundle: ダウンロード中のバンドルの情報

アップデートのチェックがアップデートが必要ないことを決定したときに発生します。

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

Event Data:

  • version: string - メジャーアップデートのバージョン番号

Copy to clipboard

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

イベントデータ: None

更新後、利用可能な状態になったアプリが起動したときに発火します。

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

イベントデータ:

  • bundle: BundleInfo - 現在のバンドルの情報
  • status: string - 起動可能な状態のステータス

Section titled “BundleInfo Object”

Event Data: 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' - バンドルが正常にダウンロードされました。
  • '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. 失敗を柔軟に処理する: ダウンロードとアップデートの失敗に対する適切なエラーハンドリングを実装する

  3. ユーザーにフィードバックを提供する: ダウンロードの進行状況イベントを使用して、ユーザーにアップデートの進行状況を表示する

  4. リスナーのクリーンアップ: リスナーが必要なくなったら削除してメモリリークを防ぐ

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

あなたは イベント ネイティブ プラグインの作業を計画するには、接続する Using @capgo/capacitor-updater ネイティブ機能のためにUsing @capgo/capacitor-updater Capgo プラグイン ディレクトリ Capgo プラグイン ディレクトリの製品フロー Capacitor プラグインのCapgo Capacitor プラグインのCapgoの実装詳細 プラグインの追加または更新 プラグインの追加または更新の実装詳細、 イオニック エンタープライズ プラグインの代替 イオニック エンタープライズ プラグインの代替の製品フロー