PORTS: Add ns.nextPortWrite() as a safer option (#1036)

This commit is contained in:
LJ 2024-01-15 04:10:39 -07:00 committed by GitHub
parent d0e66d5be2
commit 157ff8ea88
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 20 additions and 5 deletions

@ -544,6 +544,7 @@ export const RamCosts: RamCostTree<NSFull> = {
peek: 0, peek: 0,
clear: 0, clear: 0,
writePort: 0, writePort: 0,
nextPortWrite: 0,
readPort: 0, readPort: 0,
getPortHandle: 0, getPortHandle: 0,
rm: RamCostConstants.ReadWrite, rm: RamCostConstants.ReadWrite,

@ -97,7 +97,7 @@ import { getBitNodeMultipliers } from "./BitNode/BitNode";
import { assert, arrayAssert, stringAssert, objectAssert } from "./utils/helpers/typeAssertion"; import { assert, arrayAssert, stringAssert, objectAssert } from "./utils/helpers/typeAssertion";
import { cloneDeep, escapeRegExp } from "lodash"; import { cloneDeep, escapeRegExp } from "lodash";
import numeral from "numeral"; import numeral from "numeral";
import { clearPort, peekPort, portHandle, readPort, tryWritePort, writePort } from "./NetscriptPort"; import { clearPort, peekPort, portHandle, readPort, tryWritePort, writePort, nextPortWrite } from "./NetscriptPort";
import { FilePath, resolveFilePath } from "./Paths/FilePath"; import { FilePath, resolveFilePath } from "./Paths/FilePath";
import { hasScriptExtension } from "./Paths/ScriptFilePath"; import { hasScriptExtension } from "./Paths/ScriptFilePath";
import { hasTextExtension } from "./Paths/TextFilePath"; import { hasTextExtension } from "./Paths/TextFilePath";
@ -1373,6 +1373,10 @@ export const ns: InternalAPI<NSFull> = {
} }
return tryWritePort(portNumber, data); return tryWritePort(portNumber, data);
}, },
nextPortWrite: (ctx) => (_portNumber) => {
const portNumber = helpers.portNumber(ctx, _portNumber);
return nextPortWrite(portNumber);
},
readPort: (ctx) => (_portNumber) => { readPort: (ctx) => (_portNumber) => {
const portNumber = helpers.portNumber(ctx, _portNumber); const portNumber = helpers.portNumber(ctx, _portNumber);
return readPort(portNumber); return readPort(portNumber);

@ -36,7 +36,7 @@ export function portHandle(n: PortNumber): NetscriptPort {
tryWrite: (value: unknown) => tryWritePort(n, value), tryWrite: (value: unknown) => tryWritePort(n, value),
read: () => readPort(n), read: () => readPort(n),
peek: () => peekPort(n), peek: () => peekPort(n),
nextWrite: () => nextWritePort(n), nextWrite: () => nextPortWrite(n),
full: () => isFullPort(n), full: () => isFullPort(n),
empty: () => isEmptyPort(n), empty: () => isEmptyPort(n),
clear: () => clearPort(n), clear: () => clearPort(n),
@ -83,10 +83,9 @@ export function peekPort(n: PortNumber): PortData {
return port.data[0]; return port.data[0];
} }
function nextWritePort(n: PortNumber) { export function nextPortWrite(n: PortNumber) {
const port = getPort(n); const port = getPort(n);
if (port.promise) return port.promise; if (!port.promise) port.promise = new Promise<void>((res) => (port.resolver = res));
port.promise = new Promise<void>((res) => (port.resolver = res));
return port.promise; return port.promise;
} }

@ -6644,6 +6644,17 @@ export interface NS {
*/ */
tryWritePort(portNumber: number, data: string | number): boolean; tryWritePort(portNumber: number, data: string | number): boolean;
/**
* Listen for a port write.
* @remarks
* RAM cost: 0 GB
*
* Sleeps until the port is written to.
*
* @param port - Port to listen for a write on. Must be a positive integer.
*/
nextPortWrite(port: number): Promise<void>;
/** /**
* Read content of a file. * Read content of a file.
* @remarks * @remarks