Server/project/src/utils/AsyncQueue.ts
TheSparta 418d9f2a8f Import path alias on the whole project (!157)
- Ability to use @spt-aki path alias on the whole project.
- Swapped all imports from relative paths, for imports using the path alias.

Reviewed-on: https://dev.sp-tarkov.com/SPT-AKI/Server/pulls/157
Co-authored-by: TheSparta <thesparta@noreply.dev.sp-tarkov.com>
Co-committed-by: TheSparta <thesparta@noreply.dev.sp-tarkov.com>
2023-10-19 17:21:17 +00:00

32 lines
925 B
TypeScript

import { IAsyncQueue } from "@spt-aki/models/spt/utils/IAsyncQueue";
import { ICommand } from "@spt-aki/models/spt/utils/ICommand";
export class AsyncQueue implements IAsyncQueue
{
protected commandsQueue: ICommand[];
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);
});
}
// When the command is ready, execute it
return this.commandsQueue.shift().cmd();
}
}