2021-03-14 07:08:24 +01:00
|
|
|
import { Settings } from "./Settings/Settings";
|
|
|
|
|
|
|
|
export class NetscriptPort {
|
|
|
|
data: any[] = [];
|
|
|
|
|
2021-05-01 09:17:31 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
|
2021-03-14 07:08:24 +01:00
|
|
|
write(data: any): any {
|
|
|
|
this.data.push(data);
|
|
|
|
if (this.data.length > Settings.MaxPortCapacity) {
|
|
|
|
return this.data.shift();
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2021-05-01 09:17:31 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
|
2021-03-14 07:08:24 +01:00
|
|
|
tryWrite(data: any): boolean {
|
|
|
|
if (this.data.length >= Settings.MaxPortCapacity) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
this.data.push(data);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
read(): any {
|
|
|
|
if (this.data.length === 0) {
|
|
|
|
return "NULL PORT DATA";
|
|
|
|
}
|
|
|
|
return this.data.shift();
|
|
|
|
}
|
|
|
|
|
|
|
|
peek(): any {
|
|
|
|
if (this.data.length === 0) {
|
|
|
|
return "NULL PORT DATA";
|
|
|
|
} else {
|
2021-04-30 05:52:56 +02:00
|
|
|
const foo = this.data.slice();
|
2021-03-14 07:08:24 +01:00
|
|
|
return foo[0];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
full(): boolean {
|
|
|
|
return this.data.length == Settings.MaxPortCapacity;
|
|
|
|
}
|
|
|
|
|
|
|
|
empty(): boolean {
|
|
|
|
return this.data.length === 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
clear(): void {
|
|
|
|
this.data.length = 0;
|
|
|
|
}
|
|
|
|
}
|