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