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 {
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();
}
}
}

View file

@ -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;
}
}