Files
bitburner-src/src/NetscriptPort.ts
T
omuretsu 7a384d53f4 typefix netscriptFunctions (see desc)
* Types for InternalFunction and ExternalFunction have been modified to actually typecheck ns functions against docs.
* Internal functions are required to use unknown for any params on the inner function.
* Return types for internal function inner-function must match the respective external function.
* Added new typecheck assertion function for asserting dynamic object types, to allow unknownifying params that were previously hardcoded objec structures.
* Because type assertion for parameter types and return types is enforced by InternalAPI, removed all duplicate type declarations on NetscriptFunction params and returns.
2022-10-12 08:49:27 -04:00

67 lines
1.6 KiB
TypeScript

import { Settings } from "./Settings/Settings";
type PortData = string | number;
export interface IPort {
write: (value: unknown) => PortData | null;
tryWrite: (value: unknown) => boolean;
read: () => PortData;
peek: () => PortData;
full: () => boolean;
empty: () => boolean;
clear: () => void;
}
export function NetscriptPort(): IPort {
const data: PortData[] = [];
return {
write: (value) => {
if (typeof value !== "number" && typeof value !== "string") {
throw new Error(
`port.write: Tried to write type ${typeof value}. Only string and number types may be written to ports.`,
);
}
data.push(value);
if (data.length > Settings.MaxPortCapacity) {
return data.shift() as PortData;
}
return null;
},
tryWrite: (value) => {
if (typeof value != "number" && typeof value != "string") {
throw new Error(
`port.write: Tried to write type ${typeof value}. Only string and number types may be written to ports.`,
);
}
if (data.length >= Settings.MaxPortCapacity) {
return false;
}
data.push(value);
return true;
},
read: () => {
if (data.length === 0) return "NULL PORT DATA";
return data.shift() as PortData;
},
peek: () => {
if (data.length === 0) return "NULL PORT DATA";
return data[0];
},
full: () => {
return data.length == Settings.MaxPortCapacity;
},
empty: () => {
return data.length === 0;
},
clear: () => {
data.length = 0;
},
};
}