Skip to content

Commit

Permalink
Merge pull request #490 from numbersprotocol/fix-migrating-set-has-pr…
Browse files Browse the repository at this point in the history
…efetch-corner-case

Fix migrating set has prefetch corner case
  • Loading branch information
shc261392 authored Jan 27, 2021
2 parents 1616e12 + 30108aa commit dbe5820
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,14 @@ export class PrefetchingDialogComponent {
}

private async prefetch() {
await this.diaBackendAssetPrefetchingService.prefetch(
(currentCount, totalCount) => (this.progress = currentCount / totalCount)
);
await this.onboardingService.setHasPrefetchedDiaBackendAssets(true);
this.dialogRef.close();
try {
await this.diaBackendAssetPrefetchingService.prefetch(
(currentCount, totalCount) =>
(this.progress = currentCount / totalCount)
);
await this.onboardingService.setHasPrefetchedDiaBackendAssets(true);
} finally {
this.dialogRef.close();
}
}
}
50 changes: 31 additions & 19 deletions src/app/shared/services/migration/migration.service.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
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, 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';
Expand All @@ -20,49 +21,61 @@ 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
);

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.runMigrateWithProgressDialog(skip)
).pipe(
concatMap(() => this.preferences.setBoolean(PrefKeys.TO_0_15_0, true)),
concatMap(() => this.updatePreviousVersion())
const runMigrate$ = defer(() => this.preMigrate(skip)).pipe(
concatMap(() => this.runMigrateWithProgressDialog(skip)),
concatMap(() => this.postMigrate())
);
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(skip?: boolean) {
if (
!skip &&
!(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;
}
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.onboardingService.setHasPrefetchedDiaBackendAssets(true);
dialogRef.close();
try {
await this.to0_15_0();
await this.onboardingService.setHasPrefetchedDiaBackendAssets(true);
} finally {
dialogRef.close();
}
}

async updatePreviousVersion() {
Expand Down Expand Up @@ -119,7 +132,6 @@ export class MigrationService {
} = await this.diaBackendAssetRepository
.fetchAllOriginallyOwned$(currentOffset, limit)
.toPromise();

if (diaBackendAssets.length === 0) {
break;
}
Expand Down

0 comments on commit dbe5820

Please sign in to comment.