changed to async

This commit is contained in:
2021-05-24 13:33:46 +02:00
parent 0601f52661
commit eea681d435
3 changed files with 130 additions and 73 deletions

View File

@ -40,7 +40,6 @@ export class App {
}
handleHash(hash) {
console.log(hash);
if (hash[0] === '#') hash = hash.substr(1);
let defaultCase = () => {
@ -81,19 +80,30 @@ export class App {
//this.setHash(hash);
}
login(accountName, domainName, password) {
return USIMP.Domain.fromName(domainName).then(domain => {
let session = new USIMP.Session(domain);
session.authenticate(accountName, password)
.catch((event) => {
console.error(`Unable to reach domain server: ${event}`);
})
.then(() => {
this.session = session;
this.defaultLocation = "/";
this.setHash("/");
});
});
async login(accountName, domainName, password) {
let domain;
try {
domain = await USIMP.Domain.fromName(domainName);
} catch (error) {
if (error instanceof DOMException) {
throw Error("Connection timed out");
} else {
throw Error("Invalid USIMP domain");
}
}
let session = new USIMP.Session(domain);
let rtt = await session.chooseDomainServer();
console.log(rtt);
let response = await session.authenticate(accountName, password);
if (response.status === "success") {
this.session = session;
this.defaultLocation = "/";
this.setHash("/");
} else {
throw Error(response.message);
}
}
hideMain() {
@ -167,15 +177,19 @@ export class App {
form.appendChild(div);
}
this.login(form.account.value, form.domain.value, form.password.value)
.catch(reason => {
try {
await this.login(form.account.value, form.domain.value, form.password.value);
} catch (error) {
if (error.toString() === "Error: unable to authenticate") {
document.getElementsByName("account")[0].setAttribute("invalid", "invalid");
} else {
document.getElementsByName("domain")[0].setAttribute("invalid", "invalid");
formError("Invalid USIMP domain");
console.error(reason);
})
.finally(() => {
for (let e of form) e.disabled = false;
});
}
formError(error.toString());
console.error(error);
} finally {
for (let e of form) e.disabled = false;
}
});
this.windows.appendChild(win);
@ -184,6 +198,10 @@ export class App {
event.target.removeAttribute("invalid");
});
document.getElementsByName("account")[0].addEventListener("input", (event) => {
event.target.removeAttribute("invalid");
});
document.getElementsByName("account")[0].focus();
}
@ -192,7 +210,7 @@ export class App {
<div class="chat">
<div class="chat-history"></div>
<div class="chat-input-wrapper">
<input name="message" type="text"/>
<input name="message" type="text" autocomplete="off"/>
</div>
</div>`;
@ -208,7 +226,7 @@ export class App {
});
this.session.subscribe(response => {
this.addMessage(response.event.data.message);
this.addMessage(response.data.event.data.message);
});
}
}