Refactor netscriptDelay and script kill interaction

Store the Promise reject function instead of resolve in WorkerScript, since
it's only used when killing a script that's blocked in delay. And when killing
a script, reject the delay Promise with the WorkerScript as cause.
This commit is contained in:
Heikki Aitakangas 2022-01-06 02:56:10 +02:00
parent 0de588ee17
commit 59da79a427
3 changed files with 12 additions and 7 deletions

@ -33,9 +33,9 @@ export class WorkerScript {
delay: number | null = null;
/**
* Holds the Promise resolve() function for when the script is "blocked" by an async op
* Holds the Promise reject() function while the script is "blocked" by an async op
*/
delayResolve?: () => void;
delayReject?: (reason?: any) => void;
/**
* Stores names of all functions that have logging disabled

@ -138,8 +138,8 @@ function killNetscriptDelay(workerScript: WorkerScript): void {
if (workerScript instanceof WorkerScript) {
if (workerScript.delay) {
clearTimeout(workerScript.delay);
if (workerScript.delayResolve) {
workerScript.delayResolve();
if (workerScript.delayReject) {
workerScript.delayReject(workerScript);
}
}
}

@ -3,12 +3,17 @@ import { GetServer } from "./Server/AllServers";
import { WorkerScript } from "./Netscript/WorkerScript";
export function netscriptDelay(time: number, workerScript: WorkerScript): Promise<void> {
return new Promise(function (resolve) {
return new Promise(function (resolve, reject) {
workerScript.delay = window.setTimeout(() => {
workerScript.delay = null;
resolve();
workerScript.delayReject = undefined;
if (workerScript.env.stopFlag)
reject(workerScript);
else
resolve();
}, time);
workerScript.delayResolve = resolve;
workerScript.delayReject = reject;
});
}