make SessionLoadViewModel transfer ownership of container with callback

also, deleting the session when navigating away is now done by
LoginViewModel
This commit is contained in:
Bruno Windels 2020-10-09 16:59:34 +02:00
parent 4ae622bdd3
commit 35b83b7976
2 changed files with 42 additions and 43 deletions

View file

@ -20,10 +20,11 @@ import {SessionLoadViewModel} from "./SessionLoadViewModel.js";
export class LoginViewModel extends ViewModel { export class LoginViewModel extends ViewModel {
constructor(options) { constructor(options) {
super(options); super(options);
const {sessionCallback, defaultHomeServer, createSessionContainer} = options; const {ready, defaultHomeServer, createSessionContainer} = options;
this._createSessionContainer = createSessionContainer; this._createSessionContainer = createSessionContainer;
this._sessionCallback = sessionCallback; this._ready = ready;
this._defaultHomeServer = defaultHomeServer; this._defaultHomeServer = defaultHomeServer;
this._sessionContainer = null;
this._loadViewModel = null; this._loadViewModel = null;
this._loadViewModelSubscription = null; this._loadViewModelSubscription = null;
} }
@ -45,25 +46,19 @@ export class LoginViewModel extends ViewModel {
if (this._loadViewModel) { if (this._loadViewModel) {
this._loadViewModel.cancel(); this._loadViewModel.cancel();
} }
this._loadViewModel = new SessionLoadViewModel({ this._loadViewModel = this.track(new SessionLoadViewModel({
createAndStartSessionContainer: () => { createAndStartSessionContainer: () => {
const sessionContainer = this._createSessionContainer(); this._sessionContainer = this._createSessionContainer();
sessionContainer.startWithLogin(homeserver, username, password); this._sessionContainer.startWithLogin(homeserver, username, password);
return sessionContainer; return this._sessionContainer;
}, },
sessionCallback: sessionContainer => { ready: sessionContainer => {
if (sessionContainer) { // make sure we don't delete the session in dispose when navigating away
// make parent view model move away this._sessionContainer = null;
this._sessionCallback(sessionContainer); this._ready(sessionContainer);
} else {
// show list of session again
this._loadViewModel = null;
this.emitChange("loadViewModel");
}
}, },
deleteSessionOnCancel: true,
homeserver, homeserver,
}); }));
this._loadViewModel.start(); this._loadViewModel.start();
this.emitChange("loadViewModel"); this.emitChange("loadViewModel");
this._loadViewModelSubscription = this.track(this._loadViewModel.disposableOn("change", () => { this._loadViewModelSubscription = this.track(this._loadViewModel.disposableOn("change", () => {
@ -74,9 +69,16 @@ export class LoginViewModel extends ViewModel {
})); }));
} }
cancel() { get cancelUrl() {
if (!this.isBusy) { return this.urlRouter.urlForSegment("session");
this._sessionCallback(); }
dispose() {
super.dispose();
if (this._sessionContainer) {
// if we move away before we're done with initial sync
// delete the session
this._sessionContainer.deleteSession();
} }
} }
} }

View file

@ -21,9 +21,9 @@ import {ViewModel} from "./ViewModel.js";
export class SessionLoadViewModel extends ViewModel { export class SessionLoadViewModel extends ViewModel {
constructor(options) { constructor(options) {
super(options); super(options);
const {createAndStartSessionContainer, sessionCallback, homeserver, deleteSessionOnCancel} = options; const {createAndStartSessionContainer, ready, homeserver, deleteSessionOnCancel} = options;
this._createAndStartSessionContainer = createAndStartSessionContainer; this._createAndStartSessionContainer = createAndStartSessionContainer;
this._sessionCallback = sessionCallback; this._ready = ready;
this._homeserver = homeserver; this._homeserver = homeserver;
this._deleteSessionOnCancel = deleteSessionOnCancel; this._deleteSessionOnCancel = deleteSessionOnCancel;
this._loading = false; this._loading = false;
@ -60,11 +60,17 @@ export class SessionLoadViewModel extends ViewModel {
// did it finish or get stuck at LoginFailed or Error? // did it finish or get stuck at LoginFailed or Error?
const loadStatus = this._sessionContainer.loadStatus.get(); const loadStatus = this._sessionContainer.loadStatus.get();
const loadError = this._sessionContainer.loadError;
if (loadStatus === LoadStatus.FirstSync || loadStatus === LoadStatus.Ready) { 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) { if (loadError) {
console.error("session load error", this._sessionContainer.loadError); console.error("session load error", loadError);
} }
} catch (err) { } catch (err) {
this._error = err; this._error = err;
@ -77,13 +83,9 @@ export class SessionLoadViewModel extends ViewModel {
} }
async cancel() { dispose() {
try {
if (this._sessionContainer) { if (this._sessionContainer) {
this._sessionContainer.dispose(); this._sessionContainer.dispose();
if (this._deleteSessionOnCancel) {
await this._sessionContainer.deleteSession();
}
this._sessionContainer = null; this._sessionContainer = null;
} }
if (this._waitHandle) { if (this._waitHandle) {
@ -91,11 +93,6 @@ export class SessionLoadViewModel extends ViewModel {
this._waitHandle.dispose(); this._waitHandle.dispose();
this._waitHandle = null; this._waitHandle = null;
} }
this._sessionCallback();
} catch (err) {
this._error = err;
this.emitChange();
}
} }
// to show a spinner or not // to show a spinner or not