Server/project/src/services/NotificationService.ts

54 lines
1.3 KiB
TypeScript
Raw Normal View History

import { IWsNotificationEvent } from "@spt/models/eft/ws/IWsNotificationEvent";
import { injectable } from "tsyringe";
2023-03-03 15:23:46 +00:00
@injectable()
export class NotificationService {
2023-03-03 15:23:46 +00:00
protected messageQueue: Record<string, any[]> = {};
public getMessageQueue(): Record<string, any[]> {
2023-03-03 15:23:46 +00:00
return this.messageQueue;
}
public getMessageFromQueue(sessionId: string): any[] {
2023-03-03 15:23:46 +00:00
return this.messageQueue[sessionId];
}
public updateMessageOnQueue(sessionId: string, value: any[]): void {
2023-03-03 15:23:46 +00:00
this.messageQueue[sessionId] = value;
}
public has(sessionID: string): boolean {
2023-03-03 15:23:46 +00:00
return this.get(sessionID).length > 0;
}
/**
* Pop first message from queue.
*/
public pop(sessionID: string): any {
2023-03-03 15:23:46 +00:00
return this.get(sessionID).shift();
}
/**
* Add message to queue
*/
public add(sessionID: string, message: IWsNotificationEvent): void {
2023-03-03 15:23:46 +00:00
this.get(sessionID).push(message);
}
/**
* Get message queue for session
* @param sessionID
*/
public get(sessionID: string): any[] {
if (!sessionID) {
2023-03-03 15:23:46 +00:00
throw new Error("sessionID missing");
}
if (!this.messageQueue[sessionID]) {
2023-03-03 15:23:46 +00:00
this.messageQueue[sessionID] = [];
}
return this.messageQueue[sessionID];
}
2023-11-15 20:35:05 -05:00
}