From 157ff8ea8897ee3286d616d1651c95bfbaa499bf Mon Sep 17 00:00:00 2001 From: LJ <23249107+LJNeon@users.noreply.github.com> Date: Mon, 15 Jan 2024 04:10:39 -0700 Subject: [PATCH] PORTS: Add ns.nextPortWrite() as a safer option (#1036) --- src/Netscript/RamCostGenerator.ts | 1 + src/NetscriptFunctions.ts | 6 +++++- src/NetscriptPort.ts | 7 +++---- src/ScriptEditor/NetscriptDefinitions.d.ts | 11 +++++++++++ 4 files changed, 20 insertions(+), 5 deletions(-) diff --git a/src/Netscript/RamCostGenerator.ts b/src/Netscript/RamCostGenerator.ts index 132b373f3..fdbb9a198 100644 --- a/src/Netscript/RamCostGenerator.ts +++ b/src/Netscript/RamCostGenerator.ts @@ -544,6 +544,7 @@ export const RamCosts: RamCostTree = { peek: 0, clear: 0, writePort: 0, + nextPortWrite: 0, readPort: 0, getPortHandle: 0, rm: RamCostConstants.ReadWrite, diff --git a/src/NetscriptFunctions.ts b/src/NetscriptFunctions.ts index 386d2b6ed..53b62ecf7 100644 --- a/src/NetscriptFunctions.ts +++ b/src/NetscriptFunctions.ts @@ -97,7 +97,7 @@ import { getBitNodeMultipliers } from "./BitNode/BitNode"; import { assert, arrayAssert, stringAssert, objectAssert } from "./utils/helpers/typeAssertion"; import { cloneDeep, escapeRegExp } from "lodash"; 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 { hasScriptExtension } from "./Paths/ScriptFilePath"; import { hasTextExtension } from "./Paths/TextFilePath"; @@ -1373,6 +1373,10 @@ export const ns: InternalAPI = { } return tryWritePort(portNumber, data); }, + nextPortWrite: (ctx) => (_portNumber) => { + const portNumber = helpers.portNumber(ctx, _portNumber); + return nextPortWrite(portNumber); + }, readPort: (ctx) => (_portNumber) => { const portNumber = helpers.portNumber(ctx, _portNumber); return readPort(portNumber); diff --git a/src/NetscriptPort.ts b/src/NetscriptPort.ts index ca35fe225..c75425f3c 100644 --- a/src/NetscriptPort.ts +++ b/src/NetscriptPort.ts @@ -36,7 +36,7 @@ export function portHandle(n: PortNumber): NetscriptPort { tryWrite: (value: unknown) => tryWritePort(n, value), read: () => readPort(n), peek: () => peekPort(n), - nextWrite: () => nextWritePort(n), + nextWrite: () => nextPortWrite(n), full: () => isFullPort(n), empty: () => isEmptyPort(n), clear: () => clearPort(n), @@ -83,10 +83,9 @@ export function peekPort(n: PortNumber): PortData { return port.data[0]; } -function nextWritePort(n: PortNumber) { +export function nextPortWrite(n: PortNumber) { const port = getPort(n); - if (port.promise) return port.promise; - port.promise = new Promise((res) => (port.resolver = res)); + if (!port.promise) port.promise = new Promise((res) => (port.resolver = res)); return port.promise; } diff --git a/src/ScriptEditor/NetscriptDefinitions.d.ts b/src/ScriptEditor/NetscriptDefinitions.d.ts index 6b3740ab2..a719cd720 100644 --- a/src/ScriptEditor/NetscriptDefinitions.d.ts +++ b/src/ScriptEditor/NetscriptDefinitions.d.ts @@ -6644,6 +6644,17 @@ export interface NS { */ 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; + /** * Read content of a file. * @remarks