2024-09-30 01:49:30 +00:00
|
|
|
import { EventEmitter } from "events";
|
|
|
|
|
|
|
|
export type Chatter = {
|
|
|
|
name: string,
|
|
|
|
};
|
|
|
|
|
|
|
|
export const chatters = new Array<Chatter>();
|
|
|
|
|
|
|
|
export type Message = {
|
|
|
|
message: string,
|
2024-10-08 12:42:15 +00:00
|
|
|
timestamp: Date,
|
|
|
|
chatter: string,
|
2024-09-30 01:49:30 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export function createMessageString(message: Message): string {
|
|
|
|
return `${message.chatter.name}: ${message.message}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
export class ChatRoom extends EventEmitter {
|
|
|
|
protected messages: Message[];
|
|
|
|
|
|
|
|
public constructor() {
|
2024-10-01 01:58:44 +00:00
|
|
|
super();
|
2024-10-08 12:42:15 +00:00
|
|
|
this.messages = new Array<Message>();
|
2024-09-30 01:49:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public getMessages(): Message[] {
|
|
|
|
return this.messages;
|
|
|
|
}
|
|
|
|
|
|
|
|
public addMessage(message: Message) {
|
|
|
|
this.messages.push(message);
|
|
|
|
this.emit("message-added", { message });
|
|
|
|
}
|
|
|
|
|
|
|
|
public getMessagesAsArray(): string[] {
|
|
|
|
return this.messages.map(m => createMessageString(m));
|
|
|
|
}
|
|
|
|
|
|
|
|
public getNewestMessage(): Message {
|
|
|
|
const message = this.messages[this.messages - 1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-01 01:58:44 +00:00
|
|
|
export const chatRoom = new ChatRoom();
|