From 23f4871f957b76fdbe8d1f2fbc85e52f56ff23af Mon Sep 17 00:00:00 2001 From: James Chien Date: Wed, 27 Jan 2021 11:40:22 +0800 Subject: [PATCH 1/5] Set hasPrefetched true before migrating starts Signed-off-by: James Chien --- .../services/migration/migration.service.ts | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/src/app/shared/services/migration/migration.service.ts b/src/app/shared/services/migration/migration.service.ts index e71f1e968..36c768fd7 100644 --- a/src/app/shared/services/migration/migration.service.ts +++ b/src/app/shared/services/migration/migration.service.ts @@ -37,11 +37,9 @@ export class MigrationService { ) {} migrate$(skip?: boolean) { - const runMigrate$ = defer(() => - this.runMigrateWithProgressDialog(skip) - ).pipe( - concatMap(() => this.preferences.setBoolean(PrefKeys.TO_0_15_0, true)), - concatMap(() => this.updatePreviousVersion()) + const runMigrate$ = defer(() => this.preMigrate()).pipe( + concatMap(() => this.runMigrateWithProgressDialog(skip)), + concatMap(() => this.postMigrate()) ); return defer(() => this.preferences.getBoolean(PrefKeys.TO_0_15_0, false) @@ -51,6 +49,17 @@ export class MigrationService { ); } + private async preMigrate() { + if (!(await this.onboardingService.hasPrefetchedDiaBackendAssets())) { + await this.onboardingService.setHasPrefetchedDiaBackendAssets(true); + } + } + + private async postMigrate() { + await this.preferences.setBoolean(PrefKeys.TO_0_15_0, true); + await this.updatePreviousVersion(); + } + private async runMigrateWithProgressDialog(skip?: boolean) { if (skip) { return; From 47080e305c0de4a81c8b32bbb381210b827b06b4 Mon Sep 17 00:00:00 2001 From: James Chien Date: Wed, 27 Jan 2021 11:47:37 +0800 Subject: [PATCH 2/5] Remove unused hasMigrate$ Signed-off-by: James Chien --- .../shared/services/migration/migration.service.ts | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/src/app/shared/services/migration/migration.service.ts b/src/app/shared/services/migration/migration.service.ts index 36c768fd7..b8535c1d6 100644 --- a/src/app/shared/services/migration/migration.service.ts +++ b/src/app/shared/services/migration/migration.service.ts @@ -1,8 +1,8 @@ import { Injectable } from '@angular/core'; import { MatDialog } from '@angular/material/dialog'; import { Plugins } from '@capacitor/core'; -import { BehaviorSubject, defer } from 'rxjs'; -import { concatMap, distinctUntilChanged, tap } from 'rxjs/operators'; +import { defer } from 'rxjs'; +import { concatMap } from 'rxjs/operators'; import { VOID$ } from '../../../utils/rx-operators/rx-operators'; import { MigratingDialogComponent } from '../../core/migrating-dialog/migrating-dialog.component'; import { @@ -20,10 +20,6 @@ const { Device } = Plugins; providedIn: 'root', }) export class MigrationService { - private readonly _hasMigrated$ = new BehaviorSubject(false); - readonly hasMigrated$ = this._hasMigrated$ - .asObservable() - .pipe(distinctUntilChanged()); private readonly preferences = this.preferenceManager.getPreferences( MigrationService.name ); @@ -43,10 +39,7 @@ export class MigrationService { ); return defer(() => this.preferences.getBoolean(PrefKeys.TO_0_15_0, false) - ).pipe( - concatMap(hasMigrated => (hasMigrated ? VOID$ : runMigrate$)), - tap(() => this._hasMigrated$.next(true)) - ); + ).pipe(concatMap(hasMigrated => (hasMigrated ? VOID$ : runMigrate$))); } private async preMigrate() { From b8453f9e017f3640fd6f07abe68bbaeedf03ede8 Mon Sep 17 00:00:00 2001 From: James Chien Date: Wed, 27 Jan 2021 12:33:21 +0800 Subject: [PATCH 3/5] Fix bad error handling during migration - Dialog will be properly closed if migration fails - Migration dialog will not appear everytime navigating to the home page if the network is not connected Signed-off-by: James Chien --- .../services/migration/migration.service.ts | 22 ++++++++++++++----- 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/src/app/shared/services/migration/migration.service.ts b/src/app/shared/services/migration/migration.service.ts index b8535c1d6..072aa6961 100644 --- a/src/app/shared/services/migration/migration.service.ts +++ b/src/app/shared/services/migration/migration.service.ts @@ -2,13 +2,14 @@ import { Injectable } from '@angular/core'; import { MatDialog } from '@angular/material/dialog'; import { Plugins } from '@capacitor/core'; import { defer } from 'rxjs'; -import { concatMap } from 'rxjs/operators'; +import { concatMap, first } from 'rxjs/operators'; import { VOID$ } from '../../../utils/rx-operators/rx-operators'; import { MigratingDialogComponent } from '../../core/migrating-dialog/migrating-dialog.component'; import { DiaBackendAsset, DiaBackendAssetRepository, } from '../dia-backend/asset/dia-backend-asset-repository.service'; +import { NetworkService } from '../network/network.service'; import { OnboardingService } from '../onboarding/onboarding.service'; import { PreferenceManager } from '../preference-manager/preference-manager.service'; import { getOldProof } from '../repositories/proof/old-proof-adapter'; @@ -27,13 +28,14 @@ export class MigrationService { constructor( private readonly dialog: MatDialog, private readonly diaBackendAssetRepository: DiaBackendAssetRepository, + private readonly networkService: NetworkService, private readonly proofRepository: ProofRepository, private readonly preferenceManager: PreferenceManager, private readonly onboardingService: OnboardingService ) {} migrate$(skip?: boolean) { - const runMigrate$ = defer(() => this.preMigrate()).pipe( + const runMigrate$ = defer(() => this.preMigrate(skip)).pipe( concatMap(() => this.runMigrateWithProgressDialog(skip)), concatMap(() => this.postMigrate()) ); @@ -42,8 +44,11 @@ export class MigrationService { ).pipe(concatMap(hasMigrated => (hasMigrated ? VOID$ : runMigrate$))); } - private async preMigrate() { - if (!(await this.onboardingService.hasPrefetchedDiaBackendAssets())) { + private async preMigrate(skip?: boolean) { + if ( + !skip && + !(await this.onboardingService.hasPrefetchedDiaBackendAssets()) + ) { await this.onboardingService.setHasPrefetchedDiaBackendAssets(true); } } @@ -57,12 +62,18 @@ export class MigrationService { if (skip) { return; } + if (!(await this.networkService.connected$.pipe(first()).toPromise())) { + throw new Error('No network connection, aborting migration.'); + } const dialogRef = this.dialog.open(MigratingDialogComponent, { disableClose: true, data: { progress: 0 }, }); - await this.to0_15_0(); + await this.to0_15_0().catch(err => { + dialogRef.close(); + throw err; + }); await this.onboardingService.setHasPrefetchedDiaBackendAssets(true); dialogRef.close(); } @@ -121,7 +132,6 @@ export class MigrationService { } = await this.diaBackendAssetRepository .fetchAllOriginallyOwned$(currentOffset, limit) .toPromise(); - if (diaBackendAssets.length === 0) { break; } From 89dd594568ce92fb0dc575fac3d37927d279f936 Mon Sep 17 00:00:00 2001 From: James Chien Date: Wed, 27 Jan 2021 13:04:41 +0800 Subject: [PATCH 4/5] Close the dialog when prefetching throws error Signed-off-by: James Chien --- .../prefetching-dialog.component.ts | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/app/features/home/onboarding/prefetching-dialog/prefetching-dialog.component.ts b/src/app/features/home/onboarding/prefetching-dialog/prefetching-dialog.component.ts index 015cca160..800dfc287 100644 --- a/src/app/features/home/onboarding/prefetching-dialog/prefetching-dialog.component.ts +++ b/src/app/features/home/onboarding/prefetching-dialog/prefetching-dialog.component.ts @@ -20,9 +20,15 @@ export class PrefetchingDialogComponent { } private async prefetch() { - await this.diaBackendAssetPrefetchingService.prefetch( - (currentCount, totalCount) => (this.progress = currentCount / totalCount) - ); + await this.diaBackendAssetPrefetchingService + .prefetch( + (currentCount, totalCount) => + (this.progress = currentCount / totalCount) + ) + .catch(err => { + this.dialogRef.close(); + throw err; + }); await this.onboardingService.setHasPrefetchedDiaBackendAssets(true); this.dialogRef.close(); } From c21b3b1b63f728aaa0194158cac1cb5a208eeb10 Mon Sep 17 00:00:00 2001 From: James Chien Date: Wed, 27 Jan 2021 14:21:04 +0800 Subject: [PATCH 5/5] Use try-finally block to handle dialog closing Signed-off-by: James Chien --- .../prefetching-dialog.component.ts | 16 +++++++--------- .../services/migration/migration.service.ts | 10 +++++----- 2 files changed, 12 insertions(+), 14 deletions(-) diff --git a/src/app/features/home/onboarding/prefetching-dialog/prefetching-dialog.component.ts b/src/app/features/home/onboarding/prefetching-dialog/prefetching-dialog.component.ts index 800dfc287..c862c3081 100644 --- a/src/app/features/home/onboarding/prefetching-dialog/prefetching-dialog.component.ts +++ b/src/app/features/home/onboarding/prefetching-dialog/prefetching-dialog.component.ts @@ -20,16 +20,14 @@ export class PrefetchingDialogComponent { } private async prefetch() { - await this.diaBackendAssetPrefetchingService - .prefetch( + try { + await this.diaBackendAssetPrefetchingService.prefetch( (currentCount, totalCount) => (this.progress = currentCount / totalCount) - ) - .catch(err => { - this.dialogRef.close(); - throw err; - }); - await this.onboardingService.setHasPrefetchedDiaBackendAssets(true); - this.dialogRef.close(); + ); + await this.onboardingService.setHasPrefetchedDiaBackendAssets(true); + } finally { + this.dialogRef.close(); + } } } diff --git a/src/app/shared/services/migration/migration.service.ts b/src/app/shared/services/migration/migration.service.ts index 072aa6961..76446a39d 100644 --- a/src/app/shared/services/migration/migration.service.ts +++ b/src/app/shared/services/migration/migration.service.ts @@ -70,12 +70,12 @@ export class MigrationService { data: { progress: 0 }, }); - await this.to0_15_0().catch(err => { + try { + await this.to0_15_0(); + await this.onboardingService.setHasPrefetchedDiaBackendAssets(true); + } finally { dialogRef.close(); - throw err; - }); - await this.onboardingService.setHasPrefetchedDiaBackendAssets(true); - dialogRef.close(); + } } async updatePreviousVersion() {