killWorkerScript() now takes an optional argument for whether to rerenderUI. This is used to batch UI updates on killall()

This commit is contained in:
danielyxie 2019-06-22 01:12:17 -07:00 committed by danielyxie
parent 4cc6437408
commit 3a374de210
3 changed files with 30 additions and 21 deletions

@ -12,17 +12,21 @@ import { AllServers } from "../Server/AllServers";
import { compareArrays } from "../../utils/helpers/compareArrays"; import { compareArrays } from "../../utils/helpers/compareArrays";
import { roundToTwo } from "../../utils/helpers/roundToTwo"; import { roundToTwo } from "../../utils/helpers/roundToTwo";
export function killWorkerScript(runningScriptObj: RunningScript, serverIp: string): boolean; export function killWorkerScript(runningScriptObj: RunningScript, serverIp: string, rerenderUi: boolean): boolean;
export function killWorkerScript(workerScript: WorkerScript): boolean; export function killWorkerScript(workerScript: WorkerScript): boolean;
export function killWorkerScript(pid: number): boolean; export function killWorkerScript(pid: number): boolean;
export function killWorkerScript(script: RunningScript | WorkerScript | number, serverIp?: string): boolean { export function killWorkerScript(script: RunningScript | WorkerScript | number, serverIp?: string, rerenderUi?: boolean): boolean {
if (rerenderUi == null || typeof rerenderUi !== "boolean") {
rerenderUi = true;
}
if (script instanceof WorkerScript) { if (script instanceof WorkerScript) {
stopAndCleanUpWorkerScript(script); stopAndCleanUpWorkerScript(script);
return true; return true;
} else if (script instanceof RunningScript && typeof serverIp === "string") { } else if (script instanceof RunningScript && typeof serverIp === "string") {
// Try to kill by PID // Try to kill by PID
const res = killWorkerScriptByPid(script.pid); const res = killWorkerScriptByPid(script.pid, rerenderUi);
if (res) { return res; } if (res) { return res; }
// If for some reason that doesn't work, we'll try the old way // If for some reason that doesn't work, we'll try the old way
@ -30,7 +34,7 @@ export function killWorkerScript(script: RunningScript | WorkerScript | number,
if (ws.name == script.filename && ws.serverIp == serverIp && if (ws.name == script.filename && ws.serverIp == serverIp &&
compareArrays(ws.args, script.args)) { compareArrays(ws.args, script.args)) {
stopAndCleanUpWorkerScript(ws); stopAndCleanUpWorkerScript(ws, rerenderUi);
return true; return true;
} }
@ -38,7 +42,7 @@ export function killWorkerScript(script: RunningScript | WorkerScript | number,
return false; return false;
} else if (typeof script === "number") { } else if (typeof script === "number") {
return killWorkerScriptByPid(script); return killWorkerScriptByPid(script, rerenderUi);
} else { } else {
console.error(`killWorkerScript() called with invalid argument:`); console.error(`killWorkerScript() called with invalid argument:`);
console.error(script); console.error(script);
@ -46,23 +50,23 @@ export function killWorkerScript(script: RunningScript | WorkerScript | number,
} }
} }
function stopAndCleanUpWorkerScript(workerScript: WorkerScript): void { function killWorkerScriptByPid(pid: number, rerenderUi: boolean=true): boolean {
workerScript.env.stopFlag = true;
killNetscriptDelay(workerScript);
removeWorkerScript(workerScript);
}
function killWorkerScriptByPid(pid: number): boolean {
const ws = workerScripts.get(pid); const ws = workerScripts.get(pid);
if (ws instanceof WorkerScript) { if (ws instanceof WorkerScript) {
stopAndCleanUpWorkerScript(ws); stopAndCleanUpWorkerScript(ws, rerenderUi);
return true; return true;
} }
return false; return false;
} }
function stopAndCleanUpWorkerScript(workerScript: WorkerScript, rerenderUi: boolean=true): void {
workerScript.env.stopFlag = true;
killNetscriptDelay(workerScript);
removeWorkerScript(workerScript, rerenderUi);
}
/** /**
* Helper function that removes the script being killed from the global pool. * Helper function that removes the script being killed from the global pool.
* Also handles other cleanup-time operations * Also handles other cleanup-time operations
@ -70,7 +74,7 @@ function killWorkerScriptByPid(pid: number): boolean {
* @param {WorkerScript | number} - Identifier for WorkerScript. Either the object itself, or * @param {WorkerScript | number} - Identifier for WorkerScript. Either the object itself, or
* its index in the global workerScripts array * its index in the global workerScripts array
*/ */
function removeWorkerScript(workerScript: WorkerScript): void { function removeWorkerScript(workerScript: WorkerScript, rerenderUi: boolean=true): void {
if (workerScript instanceof WorkerScript) { if (workerScript instanceof WorkerScript) {
const ip = workerScript.serverIp; const ip = workerScript.serverIp;
const name = workerScript.name; const name = workerScript.name;
@ -105,7 +109,9 @@ function removeWorkerScript(workerScript: WorkerScript): void {
console.warn(workerScript); console.warn(workerScript);
} }
WorkerScriptStartStopEventEmitter.emitEvent(); if (rerenderUi) {
WorkerScriptStartStopEventEmitter.emitEvent();
}
} else { } else {
console.error(`Invalid argument passed into removeWorkerScript():`); console.error(`Invalid argument passed into removeWorkerScript():`);
console.error(workerScript); console.error(workerScript);

@ -2,6 +2,7 @@ const sprintf = require("sprintf-js").sprintf;
const vsprintf = require("sprintf-js").vsprintf; const vsprintf = require("sprintf-js").vsprintf;
import { getRamCost } from "./Netscript/RamCostGenerator"; import { getRamCost } from "./Netscript/RamCostGenerator";
import { WorkerScriptStartStopEventEmitter } from "./Netscript/WorkerScriptStartStopEventEmitter";
import { Augmentation } from "./Augmentation/Augmentation"; import { Augmentation } from "./Augmentation/Augmentation";
import { Augmentations } from "./Augmentation/Augmentations"; import { Augmentations } from "./Augmentation/Augmentations";
@ -983,18 +984,20 @@ function NetscriptFunctions(workerScript) {
if (ip === undefined) { if (ip === undefined) {
throw makeRuntimeRejectMsg(workerScript, "killall() call has incorrect number of arguments. Takes 1 argument"); throw makeRuntimeRejectMsg(workerScript, "killall() call has incorrect number of arguments. Takes 1 argument");
} }
var server = getServer(ip); const server = getServer(ip);
if (server == null) { if (server == null) {
workerScript.scriptRef.log("killall() failed. Invalid IP or hostname passed in: " + ip); workerScript.scriptRef.log("killall() failed. Invalid IP or hostname passed in: " + ip);
throw makeRuntimeRejectMsg(workerScript, "killall() failed. Invalid IP or hostname passed in: " + ip); throw makeRuntimeRejectMsg(workerScript, "killall() failed. Invalid IP or hostname passed in: " + ip);
} }
var scriptsRunning = (server.runningScripts.length > 0); const scriptsRunning = (server.runningScripts.length > 0);
for (var i = server.runningScripts.length-1; i >= 0; --i) { for (let i = server.runningScripts.length-1; i >= 0; --i) {
killWorkerScript(server.runningScripts[i], server.ip); killWorkerScript(server.runningScripts[i], server.ip, false);
} }
WorkerScriptStartStopEventEmitter.emitEvent();
if (workerScript.disableLogs.ALL == null && workerScript.disableLogs.killall == null) { if (workerScript.disableLogs.ALL == null && workerScript.disableLogs.killall == null) {
workerScript.scriptRef.log("killall(): Killing all scripts on " + server.hostname + ". May take a few minutes for the scripts to die"); workerScript.scriptRef.log("killall(): Killing all scripts on " + server.hostname + ". May take a few minutes for the scripts to die");
} }
return scriptsRunning; return scriptsRunning;
}, },
exit : function() { exit : function() {

@ -74,7 +74,7 @@ export function logBoxCreate(script: RunningScript) {
} }
killScriptBtn.addEventListener("click", () => { killScriptBtn.addEventListener("click", () => {
killWorkerScript(script, script.server); killWorkerScript(script, script.server, true);
return false; return false;
}); });