2024-05-21 17:59:04 +00:00
|
|
|
import { IAsyncQueue } from "@spt/models/spt/utils/IAsyncQueue";
|
|
|
|
import { ICommand } from "@spt/models/spt/utils/ICommand";
|
2023-03-03 15:23:46 +00:00
|
|
|
|
2024-07-23 11:12:53 -04:00
|
|
|
export class AsyncQueue implements IAsyncQueue {
|
2023-03-22 14:31:05 +00:00
|
|
|
protected commandsQueue: ICommand[];
|
2023-03-03 15:23:46 +00:00
|
|
|
|
2024-07-23 11:12:53 -04:00
|
|
|
constructor() {
|
2023-03-03 15:23:46 +00:00
|
|
|
this.commandsQueue = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for the right command to execute
|
|
|
|
// This ensures that the commands execute in the right order, thus no data corruption
|
2024-07-23 11:12:53 -04:00
|
|
|
public async waitFor(command: ICommand): Promise<any> {
|
2023-03-03 15:23:46 +00:00
|
|
|
// Add to the queue
|
|
|
|
this.commandsQueue.push(command);
|
|
|
|
|
2024-07-23 11:12:53 -04:00
|
|
|
while (this.commandsQueue[0].uuid !== command.uuid) {
|
|
|
|
await new Promise<void>((resolve) => {
|
2023-11-15 20:35:05 -05:00
|
|
|
setTimeout(resolve, 100);
|
2023-03-22 14:31:05 +00:00
|
|
|
});
|
2023-03-03 15:23:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// When the command is ready, execute it
|
2024-07-23 17:30:20 +01:00
|
|
|
return this.commandsQueue.shift().cmd();
|
2023-03-03 15:23:46 +00:00
|
|
|
}
|
2023-11-15 20:35:05 -05:00
|
|
|
}
|