mirror of
https://github.com/bitburner-official/bitburner-src.git
synced 2024-11-18 13:43:49 +01:00
PORTS: One promise, one resolver (test) (#1022)
This commit is contained in:
parent
87c4cbbc57
commit
44f0a77c4e
@ -21,7 +21,14 @@ export function getPort(n: PortNumber) {
|
|||||||
|
|
||||||
export class Port {
|
export class Port {
|
||||||
data: PortData[] = [];
|
data: PortData[] = [];
|
||||||
resolvers: Resolver[] = [];
|
resolver: Resolver | null = null;
|
||||||
|
promise: Promise<void> | null = null;
|
||||||
|
resolve() {
|
||||||
|
if (!this.resolver) return;
|
||||||
|
this.resolver();
|
||||||
|
this.resolver = null;
|
||||||
|
this.promise = null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
export function portHandle(n: PortNumber): NetscriptPort {
|
export function portHandle(n: PortNumber): NetscriptPort {
|
||||||
return {
|
return {
|
||||||
@ -42,9 +49,9 @@ export function writePort(n: PortNumber, value: unknown): PortData | null {
|
|||||||
`port.write: Tried to write type ${typeof value}. Only string and number types may be written to ports.`,
|
`port.write: Tried to write type ${typeof value}. Only string and number types may be written to ports.`,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
const { data, resolvers } = getPort(n);
|
const { data, resolve } = getPort(n);
|
||||||
data.push(value);
|
data.push(value);
|
||||||
for (const res of resolvers.splice(0, resolvers.length)) res();
|
resolve();
|
||||||
if (data.length > Settings.MaxPortCapacity) return data.shift() as PortData;
|
if (data.length > Settings.MaxPortCapacity) return data.shift() as PortData;
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@ -55,10 +62,10 @@ export function tryWritePort(n: PortNumber, value: unknown): boolean {
|
|||||||
`port.write: Tried to write type ${typeof value}. Only string and number types may be written to ports.`,
|
`port.write: Tried to write type ${typeof value}. Only string and number types may be written to ports.`,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
const { data, resolvers } = getPort(n);
|
const { data, resolve } = getPort(n);
|
||||||
if (data.length >= Settings.MaxPortCapacity) return false;
|
if (data.length >= Settings.MaxPortCapacity) return false;
|
||||||
data.push(value);
|
data.push(value);
|
||||||
for (const res of resolvers.splice(0, resolvers.length)) res();
|
resolve();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -66,7 +73,7 @@ export function readPort(n: PortNumber): PortData {
|
|||||||
const port = NetscriptPorts.get(n);
|
const port = NetscriptPorts.get(n);
|
||||||
if (!port || !port.data.length) return emptyPortData;
|
if (!port || !port.data.length) return emptyPortData;
|
||||||
const returnVal = port.data.shift() as PortData;
|
const returnVal = port.data.shift() as PortData;
|
||||||
if (!port.data.length && !port.resolvers.length) NetscriptPorts.delete(n);
|
if (!port.data.length && !port.resolver) NetscriptPorts.delete(n);
|
||||||
return returnVal;
|
return returnVal;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -77,8 +84,10 @@ export function peekPort(n: PortNumber): PortData {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function nextWritePort(n: PortNumber) {
|
function nextWritePort(n: PortNumber) {
|
||||||
const { resolvers } = getPort(n);
|
const port = getPort(n);
|
||||||
return new Promise<void>((res) => resolvers.push(res as Resolver));
|
if (port.promise) return port.promise;
|
||||||
|
port.promise = new Promise<void>((res) => (port.resolver = res));
|
||||||
|
return port.promise;
|
||||||
}
|
}
|
||||||
|
|
||||||
function isFullPort(n: PortNumber) {
|
function isFullPort(n: PortNumber) {
|
||||||
@ -96,6 +105,6 @@ function isEmptyPort(n: PortNumber) {
|
|||||||
export function clearPort(n: PortNumber) {
|
export function clearPort(n: PortNumber) {
|
||||||
const port = NetscriptPorts.get(n);
|
const port = NetscriptPorts.get(n);
|
||||||
if (!port) return;
|
if (!port) return;
|
||||||
if (!port.resolvers.length) NetscriptPorts.delete(n);
|
if (!port.resolver) NetscriptPorts.delete(n);
|
||||||
port.data.length = 0;
|
port.data.length = 0;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user