forked from mystiq/hydrogen-web
port LoginViewModel over to SessionContainer
This commit is contained in:
parent
bb7fca0592
commit
a5965ad378
1 changed files with 56 additions and 8 deletions
|
@ -1,11 +1,15 @@
|
||||||
import {EventEmitter} from "../utils/EventEmitter.js";
|
import {EventEmitter} from "../utils/EventEmitter.js";
|
||||||
|
import {LoadStatus} from "../matrix/SessionContainer.js";
|
||||||
|
import {AbortError} from "../utils/error.js";
|
||||||
|
|
||||||
export class LoginViewModel extends EventEmitter {
|
export class LoginViewModel extends EventEmitter {
|
||||||
constructor({loginCallback, defaultHomeServer, createHsApi}) {
|
constructor({sessionCallback, defaultHomeServer, createSessionContainer}) {
|
||||||
super();
|
super();
|
||||||
this._loginCallback = loginCallback;
|
this._createSessionContainer = createSessionContainer;
|
||||||
|
this._sessionCallback = sessionCallback;
|
||||||
this._defaultHomeServer = defaultHomeServer;
|
this._defaultHomeServer = defaultHomeServer;
|
||||||
this._createHsApi = createHsApi;
|
this._sessionContainer = null;
|
||||||
|
this._loadWaitHandle = null;
|
||||||
this._loading = false;
|
this._loading = false;
|
||||||
this._error = null;
|
this._error = null;
|
||||||
}
|
}
|
||||||
|
@ -18,13 +22,25 @@ export class LoginViewModel extends EventEmitter {
|
||||||
get loading() { return this._loading; }
|
get loading() { return this._loading; }
|
||||||
|
|
||||||
async login(username, password, homeserver) {
|
async login(username, password, homeserver) {
|
||||||
const hsApi = this._createHsApi(homeserver);
|
|
||||||
try {
|
try {
|
||||||
this._loading = true;
|
this._loading = true;
|
||||||
this.emit("change", "loading");
|
this.emit("change", "loading");
|
||||||
const loginData = await hsApi.passwordLogin(username, password).response();
|
this._sessionContainer = this._createSessionContainer();
|
||||||
loginData.homeServerUrl = homeserver;
|
this._sessionContainer.startWithLogin(homeserver, username, password);
|
||||||
this._loginCallback(loginData);
|
this._loadWaitHandle = this._sessionContainer.loadStatus.waitFor(s => {
|
||||||
|
this.emit("change", "loadStatus");
|
||||||
|
return s === LoadStatus.Ready;
|
||||||
|
});
|
||||||
|
try {
|
||||||
|
await this._loadWaitHandle.promise;
|
||||||
|
} catch (err) {
|
||||||
|
if (err instanceof AbortError) {
|
||||||
|
// login was cancelled
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
this._loadWaitHandle = null;
|
||||||
|
this._sessionCallback(this._sessionContainer);
|
||||||
// wait for parent view model to switch away here
|
// wait for parent view model to switch away here
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
this._error = err;
|
this._error = err;
|
||||||
|
@ -33,7 +49,39 @@ export class LoginViewModel extends EventEmitter {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
get loadStatus() {
|
||||||
|
return this._sessionContainer && this._sessionContainer.loadStatus;
|
||||||
|
}
|
||||||
|
|
||||||
|
get loadError() {
|
||||||
|
if (this._sessionContainer) {
|
||||||
|
const error = this._sessionContainer.loadError;
|
||||||
|
if (error) {
|
||||||
|
return error.message;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
async cancelLogin() {
|
||||||
|
if (!this._loading) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
this._loading = false;
|
||||||
|
this.emit("change", "loading");
|
||||||
|
if (this._sessionContainer) {
|
||||||
|
this._sessionContainer.stop();
|
||||||
|
await this._sessionContainer.deleteSession();
|
||||||
|
this._sessionContainer = null;
|
||||||
|
}
|
||||||
|
if (this._loadWaitHandle) {
|
||||||
|
// rejects with AbortError
|
||||||
|
this._loadWaitHandle.dispose();
|
||||||
|
this._loadWaitHandle = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
cancel() {
|
cancel() {
|
||||||
this._loginCallback();
|
this._sessionCallback();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue