From 35b83b79762a00ed3b6809be951fe282be048f25 Mon Sep 17 00:00:00 2001 From: Bruno Windels Date: Fri, 9 Oct 2020 16:59:34 +0200 Subject: [PATCH] make SessionLoadViewModel transfer ownership of container with callback also, deleting the session when navigating away is now done by LoginViewModel --- src/domain/LoginViewModel.js | 42 +++++++++++++++-------------- src/domain/SessionLoadViewModel.js | 43 ++++++++++++++---------------- 2 files changed, 42 insertions(+), 43 deletions(-) diff --git a/src/domain/LoginViewModel.js b/src/domain/LoginViewModel.js index ed6ed67a..11ba3d73 100644 --- a/src/domain/LoginViewModel.js +++ b/src/domain/LoginViewModel.js @@ -20,10 +20,11 @@ import {SessionLoadViewModel} from "./SessionLoadViewModel.js"; export class LoginViewModel extends ViewModel { constructor(options) { super(options); - const {sessionCallback, defaultHomeServer, createSessionContainer} = options; + const {ready, defaultHomeServer, createSessionContainer} = options; this._createSessionContainer = createSessionContainer; - this._sessionCallback = sessionCallback; + this._ready = ready; this._defaultHomeServer = defaultHomeServer; + this._sessionContainer = null; this._loadViewModel = null; this._loadViewModelSubscription = null; } @@ -45,25 +46,19 @@ export class LoginViewModel extends ViewModel { if (this._loadViewModel) { this._loadViewModel.cancel(); } - this._loadViewModel = new SessionLoadViewModel({ + this._loadViewModel = this.track(new SessionLoadViewModel({ createAndStartSessionContainer: () => { - const sessionContainer = this._createSessionContainer(); - sessionContainer.startWithLogin(homeserver, username, password); - return sessionContainer; + this._sessionContainer = this._createSessionContainer(); + this._sessionContainer.startWithLogin(homeserver, username, password); + return this._sessionContainer; }, - sessionCallback: sessionContainer => { - if (sessionContainer) { - // make parent view model move away - this._sessionCallback(sessionContainer); - } else { - // show list of session again - this._loadViewModel = null; - this.emitChange("loadViewModel"); - } + ready: sessionContainer => { + // make sure we don't delete the session in dispose when navigating away + this._sessionContainer = null; + this._ready(sessionContainer); }, - deleteSessionOnCancel: true, homeserver, - }); + })); this._loadViewModel.start(); this.emitChange("loadViewModel"); this._loadViewModelSubscription = this.track(this._loadViewModel.disposableOn("change", () => { @@ -74,9 +69,16 @@ export class LoginViewModel extends ViewModel { })); } - cancel() { - if (!this.isBusy) { - this._sessionCallback(); + get cancelUrl() { + return this.urlRouter.urlForSegment("session"); + } + + dispose() { + super.dispose(); + if (this._sessionContainer) { + // if we move away before we're done with initial sync + // delete the session + this._sessionContainer.deleteSession(); } } } diff --git a/src/domain/SessionLoadViewModel.js b/src/domain/SessionLoadViewModel.js index e9454a24..d8736077 100644 --- a/src/domain/SessionLoadViewModel.js +++ b/src/domain/SessionLoadViewModel.js @@ -21,9 +21,9 @@ import {ViewModel} from "./ViewModel.js"; export class SessionLoadViewModel extends ViewModel { constructor(options) { super(options); - const {createAndStartSessionContainer, sessionCallback, homeserver, deleteSessionOnCancel} = options; + const {createAndStartSessionContainer, ready, homeserver, deleteSessionOnCancel} = options; this._createAndStartSessionContainer = createAndStartSessionContainer; - this._sessionCallback = sessionCallback; + this._ready = ready; this._homeserver = homeserver; this._deleteSessionOnCancel = deleteSessionOnCancel; this._loading = false; @@ -60,11 +60,17 @@ export class SessionLoadViewModel extends ViewModel { // did it finish or get stuck at LoginFailed or Error? const loadStatus = this._sessionContainer.loadStatus.get(); + const loadError = this._sessionContainer.loadError; if (loadStatus === LoadStatus.FirstSync || loadStatus === LoadStatus.Ready) { - this._sessionCallback(this._sessionContainer); + const sessionContainer = this._sessionContainer; + // session container is ready, + // don't dispose it anymore when + // we get disposed + this._sessionContainer = null; + this._ready(sessionContainer); } - if (this._sessionContainer.loadError) { - console.error("session load error", this._sessionContainer.loadError); + if (loadError) { + console.error("session load error", loadError); } } catch (err) { this._error = err; @@ -77,24 +83,15 @@ export class SessionLoadViewModel extends ViewModel { } - async cancel() { - try { - if (this._sessionContainer) { - this._sessionContainer.dispose(); - if (this._deleteSessionOnCancel) { - await this._sessionContainer.deleteSession(); - } - this._sessionContainer = null; - } - if (this._waitHandle) { - // rejects with AbortError - this._waitHandle.dispose(); - this._waitHandle = null; - } - this._sessionCallback(); - } catch (err) { - this._error = err; - this.emitChange(); + dispose() { + if (this._sessionContainer) { + this._sessionContainer.dispose(); + this._sessionContainer = null; + } + if (this._waitHandle) { + // rejects with AbortError + this._waitHandle.dispose(); + this._waitHandle = null; } }