0
0
mirror of https://github.com/sp-tarkov/server.git synced 2025-02-13 09:50:43 -05:00
server/project/src/utils/AsyncQueue.ts
CZPZ 8c37bc7837 Lint: ts files (!82)
Co-authored-by: alimoncul <alimoncul@gmail.com>
Reviewed-on: SPT-AKI/Server#82
Co-authored-by: CZPZ <czpz@noreply.dev.sp-tarkov.com>
Co-committed-by: CZPZ <czpz@noreply.dev.sp-tarkov.com>
2023-03-22 14:31:05 +00:00

32 lines
913 B
TypeScript

import { IAsyncQueue } from "../models/spt/utils/IAsyncQueue";
import { ICommand } from "../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();
}
}