Skip to content

Commit

Permalink
feat: add event when fail install
Browse files Browse the repository at this point in the history
  • Loading branch information
riderx committed May 20, 2022
1 parent ce23cad commit e68a4d3
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 1 deletion.
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ Capacitor Updator works by unzipping a compiled app bundle to the native device
* [`addListener('download', ...)`](#addlistenerdownload)
* [`addListener('majorAvailable', ...)`](#addlistenermajoravailable)
* [`addListener('updateAvailable', ...)`](#addlistenerupdateavailable)
* [`addListener('updateFailed', ...)`](#addlistenerupdatefailed)
* [`getId()`](#getid)
* [`getPluginVersion()`](#getpluginversion)
* [`isAutoUpdateEnabled()`](#isautoupdateenabled)
Expand Down Expand Up @@ -355,6 +356,26 @@ Listen for update event in the App, let you know when update is ready to install
--------------------


### addListener('updateFailed', ...)

```typescript
addListener(eventName: 'updateFailed', listenerFunc: UpdateFailedListener) => Promise<PluginListenerHandle> & PluginListenerHandle
```

Listen for update event in the App, let you know when update is ready to install at next app start

| Param | Type |
| ------------------ | --------------------------------------------------------------------- |
| **`eventName`** | <code>'updateFailed'</code> |
| **`listenerFunc`** | <code><a href="#updatefailedlistener">UpdateFailedListener</a></code> |

**Returns:** <code>Promise&lt;<a href="#pluginlistenerhandle">PluginListenerHandle</a>&gt; & <a href="#pluginlistenerhandle">PluginListenerHandle</a></code>

**Since:** 2.3.0

--------------------


### getId()

```typescript
Expand Down Expand Up @@ -460,6 +481,13 @@ removeAllListeners() => Promise<void>
| **`version`** | <code><a href="#versioninfo">VersionInfo</a></code> | Emit when a new update is available. | 3.0.0 |


#### UpdateFailedEvent

| Prop | Type | Description | Since |
| ------------- | --------------------------------------------------- | ------------------------------------- | ----- |
| **`version`** | <code><a href="#versioninfo">VersionInfo</a></code> | Emit when a update failed to install. | 4.0.0 |


### Type Aliases


Expand All @@ -482,6 +510,11 @@ removeAllListeners() => Promise<void>

<code>(state: <a href="#updateavailableevent">UpdateAvailableEvent</a>): void</code>


#### UpdateFailedListener

<code>(state: <a href="#updatefailedevent">UpdateFailedEvent</a>): void</code>

</docgen-api>

### Listen to download events
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,9 @@ public void onActivityStopped(@NonNull final Activity activity) {
}

if (this.autoDeleteFailed) {

This comment has been minimized.

Copy link
@lincolnthree

lincolnthree May 20, 2022

Collaborator

Not sure the updateFailed event should be contained within the if(autoDeleteFailed) block. WDYT?

final JSObject ret = new JSObject();
ret.put("version", current);
this.notifyListeners("updateFailed", ret);

This comment has been minimized.

Copy link
@lincolnthree

lincolnthree May 20, 2022

Collaborator

This should happen as soon as update fails right? Though TBH I'm not sure this will matter if a rollback occurs. This will only really ever happen if the app is currently running the builtin version.

Log.i(CapacitorUpdater.TAG, "Deleting failing version: " + current);
try {
final Boolean res = this.implementation.delete(current.getVersion());
Expand Down
1 change: 1 addition & 0 deletions ios/Plugin/CapacitorUpdaterPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,7 @@ public class CapacitorUpdaterPlugin: CAPPlugin {
UserDefaults.standard.set(curVersionName, forKey: "failingVersion")
let res = implementation.delete(version: curVersion, versionName: curVersionName)
if (res) {
self.notifyListeners("updateFailed", data: ["version": curVersionName])
print("\(self.implementation.TAG) Delete failing version: \(curVersionName)")
}
} else if (pastVersion != "") {
Expand Down
20 changes: 20 additions & 0 deletions src/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,15 @@ export interface UpdateAvailableEvent {
version: VersionInfo;
}

export interface UpdateFailedEvent {
/**
* Emit when a update failed to install.
*
* @since 4.0.0
*/
version: VersionInfo;
}

export interface VersionInfo {
version: string;
downloaded: string;
Expand All @@ -118,6 +127,7 @@ export type VersionStatus = 'success' | 'error' | 'pending';
export type DownloadChangeListener = (state: DownloadEvent) => void;
export type MajorAvailableListener = (state: MajorAvailableEvent) => void;
export type UpdateAvailableListener = (state: UpdateAvailableEvent) => void;
export type UpdateFailedListener = (state: UpdateFailedEvent) => void;



Expand Down Expand Up @@ -250,6 +260,16 @@ export interface CapacitorUpdaterPlugin {
listenerFunc: UpdateAvailableListener,
): Promise<PluginListenerHandle> & PluginListenerHandle;

/**
* Listen for update event in the App, let you know when update is ready to install at next app start
*
* @since 2.3.0
*/
addListener(
eventName: 'updateFailed',
listenerFunc: UpdateFailedListener,
): Promise<PluginListenerHandle> & PluginListenerHandle;

/**
* Get unique ID used to identify device (sent to auto update server)
*
Expand Down
2 changes: 1 addition & 1 deletion src/web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export class CapacitorUpdaterWeb
return { id: 'default' };
}
async getPluginVersion(): Promise<{ version: string }> {
console.log('Cannot get version in web');
console.warn('Cannot get version in web');
return { version: 'default'};
}
async delete(options: { version: string }): Promise<void> {
Expand Down

0 comments on commit e68a4d3

Please sign in to comment.