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 { 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(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) {
stopAndCleanUpWorkerScript(script);
return true;
} else if (script instanceof RunningScript && typeof serverIp === "string") {
// Try to kill by PID
const res = killWorkerScriptByPid(script.pid);
const res = killWorkerScriptByPid(script.pid, rerenderUi);
if (res) { return res; }
// 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 &&
compareArrays(ws.args, script.args)) {
stopAndCleanUpWorkerScript(ws);
stopAndCleanUpWorkerScript(ws, rerenderUi);
return true;
}
@ -38,7 +42,7 @@ export function killWorkerScript(script: RunningScript | WorkerScript | number,
return false;
} else if (typeof script === "number") {
return killWorkerScriptByPid(script);
return killWorkerScriptByPid(script, rerenderUi);
} else {
console.error(`killWorkerScript() called with invalid argument:`);
console.error(script);
@ -46,23 +50,23 @@ export function killWorkerScript(script: RunningScript | WorkerScript | number,
}
}
function stopAndCleanUpWorkerScript(workerScript: WorkerScript): void {
workerScript.env.stopFlag = true;
killNetscriptDelay(workerScript);
removeWorkerScript(workerScript);
}
function killWorkerScriptByPid(pid: number): boolean {
function killWorkerScriptByPid(pid: number, rerenderUi: boolean=true): boolean {
const ws = workerScripts.get(pid);
if (ws instanceof WorkerScript) {
stopAndCleanUpWorkerScript(ws);
stopAndCleanUpWorkerScript(ws, rerenderUi);
return true;
}
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.
* 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
* its index in the global workerScripts array
*/
function removeWorkerScript(workerScript: WorkerScript): void {
function removeWorkerScript(workerScript: WorkerScript, rerenderUi: boolean=true): void {
if (workerScript instanceof WorkerScript) {
const ip = workerScript.serverIp;
const name = workerScript.name;
@ -105,7 +109,9 @@ function removeWorkerScript(workerScript: WorkerScript): void {
console.warn(workerScript);
}
WorkerScriptStartStopEventEmitter.emitEvent();
if (rerenderUi) {
WorkerScriptStartStopEventEmitter.emitEvent();
}
} else {
console.error(`Invalid argument passed into removeWorkerScript():`);
console.error(workerScript);

@ -2,6 +2,7 @@ const sprintf = require("sprintf-js").sprintf;
const vsprintf = require("sprintf-js").vsprintf;
import { getRamCost } from "./Netscript/RamCostGenerator";
import { WorkerScriptStartStopEventEmitter } from "./Netscript/WorkerScriptStartStopEventEmitter";
import { Augmentation } from "./Augmentation/Augmentation";
import { Augmentations } from "./Augmentation/Augmentations";
@ -983,18 +984,20 @@ function NetscriptFunctions(workerScript) {
if (ip === undefined) {
throw makeRuntimeRejectMsg(workerScript, "killall() call has incorrect number of arguments. Takes 1 argument");
}
var server = getServer(ip);
const server = getServer(ip);
if (server == null) {
workerScript.scriptRef.log("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);
for (var i = server.runningScripts.length-1; i >= 0; --i) {
killWorkerScript(server.runningScripts[i], server.ip);
const scriptsRunning = (server.runningScripts.length > 0);
for (let i = server.runningScripts.length-1; i >= 0; --i) {
killWorkerScript(server.runningScripts[i], server.ip, false);
}
WorkerScriptStartStopEventEmitter.emitEvent();
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");
}
return scriptsRunning;
},
exit : function() {

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