API: Added spawnDelay parameter to ns.spawn options, allowing user defined delay (#807)

This commit is contained in:
muesli4brekkies
2023-09-27 06:31:47 +01:00
committed by GitHub
parent 7fad6e0778
commit c5e2f65cb0
10 changed files with 78 additions and 26 deletions
+15 -1
View File
@@ -45,6 +45,7 @@ export const helpers = {
positiveInteger,
scriptArgs,
runOptions,
spawnOptions,
argsToString,
makeBasicErrorMsg,
makeRuntimeErrorMsg,
@@ -72,13 +73,17 @@ export const helpers = {
failOnHacknetServer,
};
// RunOptions with non-optional, type-validated members, for passing between internal functions.
/** RunOptions with non-optional, type-validated members, for passing between internal functions. */
export interface CompleteRunOptions {
threads: PositiveInteger;
temporary: boolean;
ramOverride?: number;
preventDuplicates: boolean;
}
/** SpawnOptions with non-optional, type-validated members, for passing between internal functions. */
export interface CompleteSpawnOptions extends CompleteRunOptions {
spawnDelay: PositiveInteger;
}
export function assertString(ctx: NetscriptContext, argName: string, v: unknown): asserts v is string {
if (typeof v !== "string")
@@ -207,6 +212,15 @@ function runOptions(ctx: NetscriptContext, threadOrOption: unknown): CompleteRun
return result;
}
function spawnOptions(ctx: NetscriptContext, threadOrOption: unknown): CompleteSpawnOptions {
const result: CompleteSpawnOptions = { spawnDelay: 10000 as PositiveInteger, ...runOptions(ctx, threadOrOption) };
if (typeof threadOrOption !== "object" || !threadOrOption) return result;
// Safe assertion since threadOrOption type has been narrowed to a non-null object
const { spawnDelay } = threadOrOption as Unknownify<CompleteSpawnOptions>;
if (spawnDelay !== undefined) result.spawnDelay = positiveInteger(ctx, "spawnDelayMsec", spawnDelay);
return result;
}
/** Convert multiple arguments for tprint or print into a single string. */
function argsToString(args: unknown[]): string {
// Reduce array of args into a single output string