add actionLock to element power action

This commit is contained in:
Arthur Lu 2022-12-18 16:53:07 -08:00
parent 5971c1ebef
commit 015855d38f

View File

@ -24,6 +24,7 @@ class Instance extends HTMLElement {
shadowRoot.append(instanceLink); shadowRoot.append(instanceLink);
this.shadowElement = shadowRoot; this.shadowElement = shadowRoot;
this.actionLock = false;
} }
set data (data) { set data (data) {
@ -63,27 +64,34 @@ class Instance extends HTMLElement {
powerButton.src = data.status === "running" ? "images/actions/stop.svg" : "images/actions/start.svg"; powerButton.src = data.status === "running" ? "images/actions/stop.svg" : "images/actions/start.svg";
powerButton.alt = data.status === "running" ? "shutdown instance" : "start instance"; powerButton.alt = data.status === "running" ? "shutdown instance" : "start instance";
powerButton.addEventListener("click", async () => { powerButton.addEventListener("click", async () => {
let targetAction = this.status === "running" ? "shutdown" : "start"; if (this.actionLock) {
let targetStatus = this.status === "running" ? "stopped" : "running"; console.log("already doing an action");
await request(`/nodes/${this.node}/${this.type}/${this.vmid}/status/${targetAction}`, "POST", {node: this.node, vmid: this.vmid});
while (true) {
let data = await request(`/nodes/${this.node}/${this.type}/${this.vmid}/status/current`);
if(data.data.status === targetStatus) {
break;
}
await waitFor(1000);
} }
else {
this.actionLock = true;
let targetAction = this.status === "running" ? "shutdown" : "start";
let targetStatus = this.status === "running" ? "stopped" : "running";
this.status = targetStatus; await request(`/nodes/${this.node}/${this.type}/${this.vmid}/status/${targetAction}`, "POST", {node: this.node, vmid: this.vmid});
let typeImg = this.shadowElement.querySelector("#instance-type"); while (true) {
typeImg.src = `images/instances/${this.type}/${this.status}.svg`; let data = await request(`/nodes/${this.node}/${this.type}/${this.vmid}/status/current`);
if(data.data.status === targetStatus) {
break;
}
await waitFor(1000);
}
let powerButton = this.shadowElement.querySelector("#power-btn"); this.status = targetStatus;
powerButton.src = this.status === "running" ? "images/actions/stop.svg" : "images/actions/start.svg";
powerButton.alt = this.status === "running" ? "shutdown instance" : "start instance"; let typeImg = this.shadowElement.querySelector("#instance-type");
typeImg.src = `images/instances/${this.type}/${this.status}.svg`;
let powerButton = this.shadowElement.querySelector("#power-btn");
powerButton.src = this.status === "running" ? "images/actions/stop.svg" : "images/actions/start.svg";
powerButton.alt = this.status === "running" ? "shutdown instance" : "start instance";
this.actionLock = false;
}
}); });
let configButton = this.shadowElement.querySelector("#configure-btn"); let configButton = this.shadowElement.querySelector("#configure-btn");