Server/project/src/utils/AsyncQueue.ts

33 lines
924 B
TypeScript
Raw Normal View History

import { IAsyncQueue } from "@spt-aki/models/spt/utils/IAsyncQueue";
import { ICommand } from "@spt-aki/models/spt/utils/ICommand";
2023-03-03 15:23:46 +00:00
export class AsyncQueue implements IAsyncQueue
{
protected commandsQueue: ICommand[];
2023-03-03 15:23:46 +00: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
2023-11-15 20:35:05 -05:00
public async waitFor(command: ICommand): Promise<any>
2023-03-03 15:23:46 +00:00
{
// Add to the queue
this.commandsQueue.push(command);
// eslint-disable-next-line no-constant-condition
2023-11-15 20:35:05 -05:00
while (this.commandsQueue[0].uuid !== command.uuid)
2023-03-03 15:23:46 +00:00
{
2023-11-15 20:35:05 -05:00
await new Promise<void>((resolve) =>
2023-03-03 15:23:46 +00:00
{
2023-11-15 20:35:05 -05:00
setTimeout(resolve, 100);
});
2023-03-03 15:23:46 +00:00
}
// When the command is ready, execute it
return this.commandsQueue.shift().cmd();
}
2023-11-15 20:35:05 -05:00
}