clean up key backup vm using flatMap to avoid subscription handling

This commit is contained in:
Bruno Windels 2022-01-28 16:36:13 +01:00
parent e0df003aba
commit c9b5ce6508

View File

@ -28,9 +28,10 @@ export class KeyBackupViewModel extends ViewModel {
this._isBusy = false; this._isBusy = false;
this._dehydratedDeviceId = undefined; this._dehydratedDeviceId = undefined;
this._status = undefined; this._status = undefined;
this._needsNewKeySubscription = undefined; this._backupOperation = this._session.keyBackup.flatMap(keyBackup => keyBackup.operationInProgress);
this._operationSubscription = undefined; this._progress = this._backupOperation.flatMap(op => op.progress);
this._operationProgressSubscription = undefined; this.track(this._backupOperation.subscribe(() => this.emitChange("isBackingUp")));
this.track(this._progress.subscribe(() => this.emitChange("backupPercentage")));
this._reevaluateStatus(); this._reevaluateStatus();
this.track(this._session.keyBackup.subscribe(() => { this.track(this._session.keyBackup.subscribe(() => {
if (this._reevaluateStatus()) { if (this._reevaluateStatus()) {
@ -46,24 +47,8 @@ export class KeyBackupViewModel extends ViewModel {
let status; let status;
const keyBackup = this._session.keyBackup.get(); const keyBackup = this._session.keyBackup.get();
if (keyBackup) { if (keyBackup) {
if (!this._needsNewKeySubscription) {
this._needsNewKeySubscription = this.track(keyBackup.needsNewKey.subscribe(() => this._reevaluateStatus()));
}
if (!this._operationSubscription) {
this._operationSubscription = this.track(keyBackup.operationInProgress.subscribe(op => {
if (op && !this._operationProgressSubscription) {
this._operationProgressSubscription = this.track(op.progress.subscribe(() => this.emitChange("backupPercentage")));
} else if (!op && this._operationProgressSubscription) {
this._operationProgressSubscription = this.disposeTracked(this._operationProgressSubscription);
}
this.emitChange("isBackingUp");
}));
}
status = keyBackup.needsNewKey.get() ? Status.NewVersionAvailable : Status.Enabled; status = keyBackup.needsNewKey.get() ? Status.NewVersionAvailable : Status.Enabled;
} else { } else {
this._needsNewKeySubscription = this.disposeTracked(this._needsNewKeySubscription);
this._operationSubscription = this.disposeTracked(this._operationSubscription);
this._operationProgressSubscription = this.disposeTracked(this._operationProgressSubscription);
status = this.showPhraseSetup() ? Status.SetupPhrase : Status.SetupKey; status = this.showPhraseSetup() ? Status.SetupPhrase : Status.SetupKey;
} /* TODO: bring back "waiting to get online" } /* TODO: bring back "waiting to get online"
else { else {
@ -166,40 +151,24 @@ export class KeyBackupViewModel extends ViewModel {
} }
get isBackingUp() { get isBackingUp() {
const keyBackup = this._session.keyBackup.get(); return !!this._backupOperation.get();
if (keyBackup) {
return !!keyBackup.operationInProgress.get();
}
return undefined;
} }
get backupPercentage() { get backupPercentage() {
const keyBackup = this._session.keyBackup.get(); const progress = this._progress.get();
if (keyBackup) {
const op = keyBackup.operationInProgress.get();
const progress = op.progress.get();
if (progress) { if (progress) {
return Math.round(progress.finished / progress.total) * 100; return Math.round(progress.finished / progress.total) * 100;
} }
}
return 0; return 0;
} }
get backupInProgressLabel() { get backupInProgressLabel() {
const keyBackup = this._session.keyBackup.get(); const progress = this._progress.get();
if (keyBackup) {
const op = keyBackup.operationInProgress.get();
if (op) {
const progress = op.progress.get();
if (progress) { if (progress) {
return this.i18n`${progress.finished} of ${progress.total}`; return this.i18n`${progress.finished} of ${progress.total}`;
} else { }
return this.i18n``; return this.i18n``;
} }
}
}
return undefined;
}
cancelBackup() { cancelBackup() {
this._session.keyBackup.get()?.operationInProgress.get()?.abort(); this._session.keyBackup.get()?.operationInProgress.get()?.abort();