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
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 78 additions and 26 deletions

@ -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) | <p>Return value of [getOrders](./bitburner.tix.getorders.md)</p><p>Keys are stock symbols, properties are arrays of [StockOrderObject](./bitburner.stockorderobject.md)</p> |

@ -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. |

@ -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);
```

@ -0,0 +1,20 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->
[Home](./index.md) &gt; [bitburner](./bitburner.md) &gt; [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. |

@ -0,0 +1,13 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->
[Home](./index.md) &gt; [bitburner](./bitburner.md) &gt; [SpawnOptions](./bitburner.spawnoptions.md) &gt; [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;
```

12
package-lock.json generated

@ -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",

@ -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",

@ -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

@ -746,9 +746,8 @@ export const ns: InternalAPI<NSFull> = {
(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<NSFull> = {
}
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...");

@ -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