Add asynch version of Session initialization

This commit is contained in:
Stephan Porada
2020-08-06 16:08:03 +02:00
parent 301b090fc0
commit 6b1f588f29
4 changed files with 580 additions and 367 deletions

View File

@ -0,0 +1,121 @@
class CorpusAnalysisClient {
constructor(corpusId, socket) {
this.corpusId = corpusId;
this.socket = socket;
this.displays = {};
}
setDisplay(type, display) {
this.displays[type] = display;
}
async initSession() {
let request = await this.requestSession();
let recvieveSession = await this.recvieveSession();
console.log("corpus_analysis_init: Waiting for response"); // this happens inbetween the two functions above
}
requestSession() { // should be private with ES2020
console.log("corpus_analysis_init: Emitting to server");
if (this.displays.init != undefined) {
this.displays.init.element.M_Modal.open();
this.displays.init.setVisibilityByStatus("waiting");
}
return new Promise((resolve, reject) => {
this.socket.emit("corpus_analysis_init", this.corpusId);
resolve();
})
}
recvieveSession() { // should be private with ES2020
this.socket.on("corpus_analysis_init", (response) => {
console.log("corpus_analysis_init: Recieving response from server");
if (response.code === 200) {
console.log("corpus_analysis_init: Initialization succeeded");
if (this.displays.init != undefined) {
this.displays.init.element.M_Modal.close();
this.displays.init.setVisibilityByStatus("success");
}
} else {
let errorText = `Error ${response.code} - ${response.msg}`;
if (this.displays.init.errorContainer != undefined) {
this.displays.init.errorContainer.innerHTML = `<p class="red-text">` +
`<i class="material-icons tiny">error</i> ${errorText}</p>`;
}
if (this.displays.init != undefined) {
this.displays.init.setVisibilityByStatus("error");
}
console.error(`corpus_analysis_init: ${errorText}`);
}
});
}
}
class CorpusAnalysisDisplay {
constructor(element) {
this.element = (() => {if (element instanceof HTMLElement) {
return element;
} else {
element = element["$el"][0];
return element;
}
})(); // with this function modals can also be handeld
this.errorContainer = element.querySelector(".error-container");
this.showOnError = element.querySelectorAll(".show-on-error");
this.showOnSuccess = element.querySelectorAll(".show-on-success");
this.showWhileWaiting = element.querySelectorAll(".show-while-waiting");
this.hideOnComplete = element.querySelectorAll(".hide-on-complete")
}
setVisibilityByStatus(status) {
switch (status) {
case "error":
for (let element of this.showOnError) {
element.classList.remove("hide");
}
for (let element of this.showOnSuccess) {
element.classList.add("hide");
}
for (let element of this.showWhileWaiting) {
element.classList.add("hide");
}
break;
case "success":
for (let element of this.showOnError) {
element.classList.add("hide");
}
for (let element of this.showOnSuccess) {
element.classList.remove("hide");
}
for (let element of this.showWhileWaiting) {
element.classList.add("hide");
}
break;
case "waiting":
for (let element of this.showOnError) {
element.classList.add("hide");
}
for (let element of this.showOnSuccess) {
element.classList.add("hide");
}
for (let element of this.showWhileWaiting) {
element.classList.remove("hide");
}
break;
default:
// Hide all
for (let element of this.showOnError) {
element.classList.add("hide");
}
for (let element of this.showOnSuccess) {
element.classList.add("hide");
}
for (let element of this.showWhileWaiting) {
element.classList.add("hide");
}
}
}
}
export {CorpusAnalysisClient, CorpusAnalysisDisplay};