From c5e2f65cb0dab44260bf0294ff4fd35fe312d152 Mon Sep 17 00:00:00 2001 From: muesli4brekkies <110121045+muesli4brekkies@users.noreply.github.com> Date: Wed, 27 Sep 2023 06:31:47 +0100 Subject: [PATCH] API: Added spawnDelay parameter to ns.spawn options, allowing user defined delay (#807) --- markdown/bitburner.md | 1 + markdown/bitburner.ns.md | 2 +- markdown/bitburner.ns.spawn.md | 12 +++++------ markdown/bitburner.spawnoptions.md | 20 +++++++++++++++++++ markdown/bitburner.spawnoptions.spawndelay.md | 13 ++++++++++++ package-lock.json | 12 +++++------ package.json | 2 +- src/Netscript/NetscriptHelpers.tsx | 16 ++++++++++++++- src/NetscriptFunctions.ts | 7 +++---- src/ScriptEditor/NetscriptDefinitions.d.ts | 19 +++++++++++------- 10 files changed, 78 insertions(+), 26 deletions(-) create mode 100644 markdown/bitburner.spawnoptions.md create mode 100644 markdown/bitburner.spawnoptions.spawndelay.md diff --git a/markdown/bitburner.md b/markdown/bitburner.md index c761e983d..8cb319a2c 100644 --- a/markdown/bitburner.md +++ b/markdown/bitburner.md @@ -98,6 +98,7 @@ | [Sleeve](./bitburner.sleeve.md) | Sleeve API | | [SleevePerson](./bitburner.sleeveperson.md) | | | [SourceFileLvl](./bitburner.sourcefilelvl.md) | | +| [SpawnOptions](./bitburner.spawnoptions.md) | | | [Stanek](./bitburner.stanek.md) | Stanek's Gift API. | | [StockMarketConstants](./bitburner.stockmarketconstants.md) | Constants used for the stockmarket game mechanic. | | [StockOrder](./bitburner.stockorder.md) |

Return value of [getOrders](./bitburner.tix.getorders.md)

Keys are stock symbols, properties are arrays of [StockOrderObject](./bitburner.stockorderobject.md)

| diff --git a/markdown/bitburner.ns.md b/markdown/bitburner.ns.md index ae4a00ed1..6157c1c6e 100644 --- a/markdown/bitburner.ns.md +++ b/markdown/bitburner.ns.md @@ -155,7 +155,7 @@ export async function main(ns) { | [setTitle(title, pid)](./bitburner.ns.settitle.md) | Set the title of the tail window of a script. | | [share()](./bitburner.ns.share.md) | Share the server's ram with your factions. | | [sleep(millis)](./bitburner.ns.sleep.md) | Suspends the script for n milliseconds. | -| [spawn(script, threadOrOptions, args)](./bitburner.ns.spawn.md) | Terminate current script and start another in 10 seconds. | +| [spawn(script, threadOrOptions, args)](./bitburner.ns.spawn.md) | Terminate current script and start another in a defined number of milliseconds. | | [sprintf(format, args)](./bitburner.ns.sprintf.md) | Format a string. | | [sqlinject(host)](./bitburner.ns.sqlinject.md) | Runs SQLInject.exe on a server. | | [tail(fn, host, args)](./bitburner.ns.tail.md) | Open the tail window of a script. | diff --git a/markdown/bitburner.ns.spawn.md b/markdown/bitburner.ns.spawn.md index 4566c2833..90d92a9e7 100644 --- a/markdown/bitburner.ns.spawn.md +++ b/markdown/bitburner.ns.spawn.md @@ -4,12 +4,12 @@ ## NS.spawn() method -Terminate current script and start another in 10 seconds. +Terminate current script and start another in a defined number of milliseconds. **Signature:** ```typescript -spawn(script: string, threadOrOptions?: number | RunOptions, ...args: (string | number | boolean)[]): void; +spawn(script: string, threadOrOptions?: number | SpawnOptions, ...args: (string | number | boolean)[]): void; ``` ## Parameters @@ -17,7 +17,7 @@ spawn(script: string, threadOrOptions?: number | RunOptions, ...args: (string | | Parameter | Type | Description | | --- | --- | --- | | script | string | Filename of script to execute. | -| threadOrOptions | number \| [RunOptions](./bitburner.runoptions.md) | _(Optional)_ Either an integer number of threads for new script, or a [RunOptions](./bitburner.runoptions.md) object. Threads defaults to 1. | +| threadOrOptions | number \| [SpawnOptions](./bitburner.spawnoptions.md) | _(Optional)_ Either an integer number of threads for new script, or a [SpawnOptions](./bitburner.spawnoptions.md) object. Threads defaults to 1. | | args | (string \| number \| boolean)\[\] | Additional arguments to pass into the new script that is being run. | **Returns:** @@ -28,7 +28,7 @@ void RAM cost: 2 GB -Terminates the current script, and then after a delay of about 10 seconds it will execute the newly-specified script. The purpose of this function is to execute a new script without being constrained by the RAM usage of the current one. This function can only be used to run scripts on the local server. +Terminates the current script, and then after a defined delay it will execute the newly-specified script. The purpose of this function is to execute a new script without being constrained by the RAM usage of the current one. This function can only be used to run scripts on the local server. Because this function immediately terminates the script, it does not have a return value. @@ -38,7 +38,7 @@ Running this function with 0 or fewer threads will cause a runtime error. ```js -//The following example will execute the script ‘foo.js’ with 10 threads and the arguments ‘foodnstuff’ and 90: -ns.spawn('foo.js', 10, 'foodnstuff', 90); +//The following example will execute the script ‘foo.js’ with 10 threads, in 500 milliseconds and the arguments ‘foodnstuff’ and 90: +ns.spawn('foo.js', 10, 500, 'foodnstuff', 90); ``` diff --git a/markdown/bitburner.spawnoptions.md b/markdown/bitburner.spawnoptions.md new file mode 100644 index 000000000..4eded037d --- /dev/null +++ b/markdown/bitburner.spawnoptions.md @@ -0,0 +1,20 @@ + + +[Home](./index.md) > [bitburner](./bitburner.md) > [SpawnOptions](./bitburner.spawnoptions.md) + +## SpawnOptions interface + + +**Signature:** + +```typescript +interface SpawnOptions extends RunOptions +``` +**Extends:** [RunOptions](./bitburner.runoptions.md) + +## Properties + +| Property | Modifiers | Type | Description | +| --- | --- | --- | --- | +| [spawnDelay?](./bitburner.spawnoptions.spawndelay.md) | | number | _(Optional)_ Number of milliseconds to delay before spawning script, defaults to 10000 (10s). Must be a positive integer. | + diff --git a/markdown/bitburner.spawnoptions.spawndelay.md b/markdown/bitburner.spawnoptions.spawndelay.md new file mode 100644 index 000000000..a3b86bc46 --- /dev/null +++ b/markdown/bitburner.spawnoptions.spawndelay.md @@ -0,0 +1,13 @@ + + +[Home](./index.md) > [bitburner](./bitburner.md) > [SpawnOptions](./bitburner.spawnoptions.md) > [spawnDelay](./bitburner.spawnoptions.spawndelay.md) + +## SpawnOptions.spawnDelay property + +Number of milliseconds to delay before spawning script, defaults to 10000 (10s). Must be a positive integer. + +**Signature:** + +```typescript +spawnDelay?: number; +``` diff --git a/package-lock.json b/package-lock.json index 67524b3f4..b9ff8d57d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -6233,9 +6233,9 @@ } }, "node_modules/caniuse-lite": { - "version": "1.0.30001473", - "resolved": "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001473.tgz", - "integrity": "sha512-ewDad7+D2vlyy+E4UJuVfiBsU69IL+8oVmTuZnH5Q6CIUbxNfI50uVpRHbUPDD6SUaN2o0Lh4DhTrvLG/Tn1yg==", + "version": "1.0.30001539", + "resolved": "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001539.tgz", + "integrity": "sha512-hfS5tE8bnNiNvEOEkm8HElUHroYwlqMMENEzELymy77+tJ6m+gA2krtHl5hxJaj71OlpC2cHZbdSMX1/YEqEkA==", "funding": [ { "type": "opencollective", @@ -22616,9 +22616,9 @@ "dev": true }, "caniuse-lite": { - "version": "1.0.30001473", - "resolved": "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001473.tgz", - "integrity": "sha512-ewDad7+D2vlyy+E4UJuVfiBsU69IL+8oVmTuZnH5Q6CIUbxNfI50uVpRHbUPDD6SUaN2o0Lh4DhTrvLG/Tn1yg==" + "version": "1.0.30001539", + "resolved": "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001539.tgz", + "integrity": "sha512-hfS5tE8bnNiNvEOEkm8HElUHroYwlqMMENEzELymy77+tJ6m+gA2krtHl5hxJaj71OlpC2cHZbdSMX1/YEqEkA==" }, "ccount": { "version": "2.0.1", diff --git a/package.json b/package.json index dd0c8dce4..d95fd5bf8 100644 --- a/package.json +++ b/package.json @@ -111,7 +111,7 @@ "doc": "bash ./tools/doc.sh", "format": "prettier -c --write .", "format:report": "prettier -c .", - "start": "cd .app && http-server -p 8000", + "start": "electron .app/index.html", "start:dev": "webpack serve --progress --env devServer --mode development", "build": "bash ./tools/build.sh production", "build:dev": "bash ./tools/build.sh development", diff --git a/src/Netscript/NetscriptHelpers.tsx b/src/Netscript/NetscriptHelpers.tsx index 636712974..9358b34cf 100644 --- a/src/Netscript/NetscriptHelpers.tsx +++ b/src/Netscript/NetscriptHelpers.tsx @@ -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; + 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 diff --git a/src/NetscriptFunctions.ts b/src/NetscriptFunctions.ts index 2fe712dc6..7cefda6ca 100644 --- a/src/NetscriptFunctions.ts +++ b/src/NetscriptFunctions.ts @@ -746,9 +746,8 @@ export const ns: InternalAPI = { (ctx) => (_scriptname, _thread_or_opt = 1, ..._args) => { const path = helpers.scriptPath(ctx, "scriptname", _scriptname); - const runOpts = helpers.runOptions(ctx, _thread_or_opt); + const runOpts = helpers.spawnOptions(ctx, _thread_or_opt); const args = helpers.scriptArgs(ctx, _args); - const spawnDelay = 10; setTimeout(() => { const scriptServer = GetServer(ctx.workerScript.hostname); if (scriptServer == null) { @@ -756,9 +755,9 @@ export const ns: InternalAPI = { } return runScriptFromScript("spawn", scriptServer, path, args, ctx.workerScript, runOpts); - }, spawnDelay * 1e3); + }, runOpts.spawnDelay); - helpers.log(ctx, () => `Will execute '${path}' in ${spawnDelay} seconds`); + helpers.log(ctx, () => `Will execute '${path}' in ${runOpts.spawnDelay} seconds`); if (killWorkerScript(ctx.workerScript)) { helpers.log(ctx, () => "Exiting..."); diff --git a/src/ScriptEditor/NetscriptDefinitions.d.ts b/src/ScriptEditor/NetscriptDefinitions.d.ts index 2365912ab..9fae6d3dd 100644 --- a/src/ScriptEditor/NetscriptDefinitions.d.ts +++ b/src/ScriptEditor/NetscriptDefinitions.d.ts @@ -281,6 +281,12 @@ interface RunOptions { preventDuplicates?: boolean; } +/** @public */ +interface SpawnOptions extends RunOptions { + /** Number of milliseconds to delay before spawning script, defaults to 10000 (10s). Must be a positive integer. */ + spawnDelay?: number; +} + /** @public */ interface RecentScript extends RunningScript { /** Timestamp of when the script was killed */ @@ -5494,11 +5500,11 @@ export interface NS { ): number; /** - * Terminate current script and start another in 10 seconds. + * Terminate current script and start another in a defined number of milliseconds. * @remarks * RAM cost: 2 GB * - * Terminates the current script, and then after a delay of about 10 seconds it will execute the + * Terminates the current script, and then after a defined delay it will execute the * newly-specified script. The purpose of this function is to execute a new script without being * constrained by the RAM usage of the current one. This function can only be used to run scripts * on the local server. @@ -5509,15 +5515,14 @@ export interface NS { * * @example * ```js - * //The following example will execute the script ‘foo.js’ with 10 threads and the arguments ‘foodnstuff’ and 90: - * ns.spawn('foo.js', 10, 'foodnstuff', 90); + * //The following example will execute the script ‘foo.js’ with 10 threads, in 500 milliseconds and the arguments ‘foodnstuff’ and 90: + * ns.spawn('foo.js', 10, 500, 'foodnstuff', 90); * ``` * @param script - Filename of script to execute. - * @param threadOrOptions - Either an integer number of threads for new script, or a {@link RunOptions} object. Threads defaults to 1. + * @param threadOrOptions - Either an integer number of threads for new script, or a {@link SpawnOptions} object. Threads defaults to 1. * @param args - Additional arguments to pass into the new script that is being run. */ - spawn(script: string, threadOrOptions?: number | RunOptions, ...args: (string | number | boolean)[]): void; - + spawn(script: string, threadOrOptions?: number | SpawnOptions, ...args: (string | number | boolean)[]): void; /** * Terminate the script with the provided PID. * @remarks