mirror of
https://github.com/bitburner-official/bitburner-src.git
synced 2024-12-01 20:13:51 +01:00
NETSCRIPT: added portHandle.nextWrite() (#187)
This commit is contained in:
parent
3ce4dcf612
commit
9eb5f7ef23
@ -1,11 +1,13 @@
|
|||||||
import { Settings } from "./Settings/Settings";
|
import { Settings } from "./Settings/Settings";
|
||||||
|
|
||||||
type PortData = string | number;
|
type PortData = string | number;
|
||||||
|
type Resolver = () => void;
|
||||||
export interface IPort {
|
export interface IPort {
|
||||||
write: (value: unknown) => PortData | null;
|
write: (value: unknown) => PortData | null;
|
||||||
tryWrite: (value: unknown) => boolean;
|
tryWrite: (value: unknown) => boolean;
|
||||||
read: () => PortData;
|
read: () => PortData;
|
||||||
peek: () => PortData;
|
peek: () => PortData;
|
||||||
|
nextWrite: () => Promise<void>;
|
||||||
full: () => boolean;
|
full: () => boolean;
|
||||||
empty: () => boolean;
|
empty: () => boolean;
|
||||||
clear: () => void;
|
clear: () => void;
|
||||||
@ -13,6 +15,7 @@ export interface IPort {
|
|||||||
|
|
||||||
export function NetscriptPort(): IPort {
|
export function NetscriptPort(): IPort {
|
||||||
const data: PortData[] = [];
|
const data: PortData[] = [];
|
||||||
|
const resolvers: Resolver[] = [];
|
||||||
|
|
||||||
return {
|
return {
|
||||||
write: (value) => {
|
write: (value) => {
|
||||||
@ -22,6 +25,9 @@ export function NetscriptPort(): IPort {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
data.push(value);
|
data.push(value);
|
||||||
|
while (resolvers.length > 0) {
|
||||||
|
(resolvers.pop() as Resolver)();
|
||||||
|
}
|
||||||
if (data.length > Settings.MaxPortCapacity) {
|
if (data.length > Settings.MaxPortCapacity) {
|
||||||
return data.shift() as PortData;
|
return data.shift() as PortData;
|
||||||
}
|
}
|
||||||
@ -38,6 +44,9 @@ export function NetscriptPort(): IPort {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
data.push(value);
|
data.push(value);
|
||||||
|
while (resolvers.length > 0) {
|
||||||
|
(resolvers.pop() as Resolver)();
|
||||||
|
}
|
||||||
return true;
|
return true;
|
||||||
},
|
},
|
||||||
|
|
||||||
@ -51,6 +60,10 @@ export function NetscriptPort(): IPort {
|
|||||||
return data[0];
|
return data[0];
|
||||||
},
|
},
|
||||||
|
|
||||||
|
nextWrite: () => {
|
||||||
|
return new Promise((res) => resolvers.push(res));
|
||||||
|
},
|
||||||
|
|
||||||
full: () => {
|
full: () => {
|
||||||
return data.length == Settings.MaxPortCapacity;
|
return data.length == Settings.MaxPortCapacity;
|
||||||
},
|
},
|
||||||
|
7
src/ScriptEditor/NetscriptDefinitions.d.ts
vendored
7
src/ScriptEditor/NetscriptDefinitions.d.ts
vendored
@ -998,6 +998,13 @@ export interface NetscriptPort {
|
|||||||
*/
|
*/
|
||||||
tryWrite(value: string | number): boolean;
|
tryWrite(value: string | number): boolean;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sleeps until the port is written to.
|
||||||
|
* @remarks
|
||||||
|
* RAM cost: 0 GB
|
||||||
|
*/
|
||||||
|
nextWrite(): Promise<void>;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Shift an element out of the port.
|
* Shift an element out of the port.
|
||||||
* @remarks
|
* @remarks
|
||||||
|
Loading…
Reference in New Issue
Block a user