diff --git a/src/Netscript/killWorkerScript.ts b/src/Netscript/killWorkerScript.ts index ed2198de3..2ad5f00bc 100644 --- a/src/Netscript/killWorkerScript.ts +++ b/src/Netscript/killWorkerScript.ts @@ -15,52 +15,43 @@ import { dialogBoxCreate } from "../ui/React/DialogBox"; import { AddRecentScript } from "./RecentScripts"; import { Player } from "../Player"; -export function killWorkerScript(runningScriptObj: RunningScript, hostname: string, rerenderUi?: boolean): boolean; -export function killWorkerScript(workerScript: WorkerScript): boolean; -export function killWorkerScript(pid: number): boolean; -export function killWorkerScript( - script: RunningScript | WorkerScript | number, - hostname?: string, - rerenderUi?: boolean, -): boolean { - if (rerenderUi == null || typeof rerenderUi !== "boolean") { - rerenderUi = true; - } +export type killScriptParams = WorkerScript | number | { runningScript: RunningScript; hostname: string }; - if (script instanceof WorkerScript) { - stopAndCleanUpWorkerScript(script); +export function killWorkerScript(params: killScriptParams): boolean { + if (params instanceof WorkerScript) { + stopAndCleanUpWorkerScript(params); return true; - } else if (script instanceof RunningScript && typeof hostname === "string") { + } else if (typeof params === "number") { + return killWorkerScriptByPid(params); + } else { // Try to kill by PID - const res = killWorkerScriptByPid(script.pid, rerenderUi); + const res = killWorkerScriptByPid(params.runningScript.pid); if (res) { return res; } // If for some reason that doesn't work, we'll try the old way for (const ws of workerScripts.values()) { - if (ws.name == script.filename && ws.hostname == hostname && compareArrays(ws.args, script.args)) { - stopAndCleanUpWorkerScript(ws, rerenderUi); + if ( + ws.name == params.runningScript.filename && + ws.hostname == params.hostname && + compareArrays(ws.args, params.runningScript.args) + ) { + stopAndCleanUpWorkerScript(ws); return true; } } - return false; - } else if (typeof script === "number") { - return killWorkerScriptByPid(script, rerenderUi); - } else { - console.error(`killWorkerScript() called with invalid argument:`); - console.error(script); return false; } } -function killWorkerScriptByPid(pid: number, rerenderUi = true): boolean { +function killWorkerScriptByPid(pid: number): boolean { const ws = workerScripts.get(pid); if (ws instanceof WorkerScript) { - stopAndCleanUpWorkerScript(ws, rerenderUi); + stopAndCleanUpWorkerScript(ws); return true; } @@ -68,7 +59,7 @@ function killWorkerScriptByPid(pid: number, rerenderUi = true): boolean { return false; } -function stopAndCleanUpWorkerScript(workerScript: WorkerScript, rerenderUi = true): void { +function stopAndCleanUpWorkerScript(workerScript: WorkerScript): void { if (typeof workerScript.atExit === "function") { try { workerScript.atExit(); @@ -83,7 +74,7 @@ function stopAndCleanUpWorkerScript(workerScript: WorkerScript, rerenderUi = tru } workerScript.env.stopFlag = true; killNetscriptDelay(workerScript); - removeWorkerScript(workerScript, rerenderUi); + removeWorkerScript(workerScript); } /** @@ -93,7 +84,7 @@ function stopAndCleanUpWorkerScript(workerScript: WorkerScript, rerenderUi = tru * @param {WorkerScript} - Identifier for WorkerScript. Either the object itself, or * its index in the global workerScripts array */ -function removeWorkerScript(workerScript: WorkerScript, rerenderUi = true): void { +function removeWorkerScript(workerScript: WorkerScript): void { const ip = workerScript.hostname; const name = workerScript.name; @@ -127,9 +118,7 @@ function removeWorkerScript(workerScript: WorkerScript, rerenderUi = true): void // } AddRecentScript(workerScript); - if (rerenderUi) { - WorkerScriptStartStopEventEmitter.emit(); - } + WorkerScriptStartStopEventEmitter.emit(); } /** diff --git a/src/NetscriptFunctions.ts b/src/NetscriptFunctions.ts index 711d48714..8f84c0383 100644 --- a/src/NetscriptFunctions.ts +++ b/src/NetscriptFunctions.ts @@ -1315,7 +1315,7 @@ export function NetscriptFunctions(workerScript: WorkerScript): NS { return false; } - res = killWorkerScript(runningScriptObj, server.hostname); + res = killWorkerScript({ runningScript: runningScriptObj, hostname: server.hostname }); } if (res) { @@ -1348,7 +1348,7 @@ export function NetscriptFunctions(workerScript: WorkerScript): NS { for (let i = server.runningScripts.length - 1; i >= 0; --i) { if (safetyguard === true && server.runningScripts[i].pid == workerScript.pid) continue; - killWorkerScript(server.runningScripts[i], server.hostname, false); + killWorkerScript({ runningScript: server.runningScripts[i], hostname: server.hostname }); ++scriptsKilled; } WorkerScriptStartStopEventEmitter.emit(); @@ -2241,7 +2241,7 @@ export function NetscriptFunctions(workerScript: WorkerScript): NS { let suc = false; for (let i = 0; i < server.runningScripts.length; i++) { if (server.runningScripts[i].filename == scriptname) { - killWorkerScript(server.runningScripts[i], server.hostname); + killWorkerScript({ runningScript: server.runningScripts[i], hostname: server.hostname }); suc = true; i--; } diff --git a/src/Terminal/commands/kill.ts b/src/Terminal/commands/kill.ts index be18d733a..652043496 100644 --- a/src/Terminal/commands/kill.ts +++ b/src/Terminal/commands/kill.ts @@ -39,7 +39,7 @@ export function kill( terminal.error("No such script is running. Nothing to kill"); return; } - killWorkerScript(runningScript, server.hostname, false); + killWorkerScript({ runningScript: runningScript, hostname: server.hostname }); terminal.print(`Killing ${scriptName}`); } catch (e) { terminal.error(e + ""); diff --git a/src/Terminal/commands/killall.ts b/src/Terminal/commands/killall.ts index ae8ae9b7f..6c670fe07 100644 --- a/src/Terminal/commands/killall.ts +++ b/src/Terminal/commands/killall.ts @@ -7,7 +7,7 @@ import { WorkerScriptStartStopEventEmitter } from "../../Netscript/WorkerScriptS export function killall(terminal: ITerminal, router: IRouter, player: IPlayer, server: BaseServer): void { for (let i = server.runningScripts.length - 1; i >= 0; --i) { - killWorkerScript(server.runningScripts[i], server.hostname, false); + killWorkerScript({ runningScript: server.runningScripts[i], hostname: server.hostname }); } WorkerScriptStartStopEventEmitter.emit(); terminal.print("Killing all running scripts"); diff --git a/src/ui/ActiveScripts/WorkerScriptAccordion.tsx b/src/ui/ActiveScripts/WorkerScriptAccordion.tsx index 71e2ff1dd..24c6a7575 100644 --- a/src/ui/ActiveScripts/WorkerScriptAccordion.tsx +++ b/src/ui/ActiveScripts/WorkerScriptAccordion.tsx @@ -53,7 +53,7 @@ export function WorkerScriptAccordion(props: IProps): React.ReactElement { function logClickHandler(): void { LogBoxEvents.emit(scriptRef); } - const killScript = killWorkerScript.bind(null, scriptRef as any, scriptRef.server); + const killScript = killWorkerScript.bind(null, { runningScript: scriptRef, hostname: scriptRef.server }); function killScriptClickHandler(): void { killScript(); diff --git a/src/ui/React/LogBoxManager.tsx b/src/ui/React/LogBoxManager.tsx index c28548751..d558ce83f 100644 --- a/src/ui/React/LogBoxManager.tsx +++ b/src/ui/React/LogBoxManager.tsx @@ -151,7 +151,7 @@ function LogWindow(props: IProps): React.ReactElement { }, []); function kill(): void { - killWorkerScript(script, script.server, true); + killWorkerScript({ runningScript: script, hostname: script.server }); } function run(): void {