45 lines
924 B
TypeScript
45 lines
924 B
TypeScript
|
import { EventEmitter } from "events";
|
||
|
|
||
|
export type Chatter = {
|
||
|
name: string,
|
||
|
};
|
||
|
|
||
|
export const chatters = new Array<Chatter>();
|
||
|
|
||
|
export type Message = {
|
||
|
message: string,
|
||
|
timestamp: Date
|
||
|
chatter: Chatter
|
||
|
};
|
||
|
|
||
|
export function createMessageString(message: Message): string {
|
||
|
return `${message.chatter.name}: ${message.message}`;
|
||
|
}
|
||
|
|
||
|
export class ChatRoom extends EventEmitter {
|
||
|
protected messages: Message[];
|
||
|
|
||
|
public constructor() {
|
||
|
this.message = new Array<Message>();
|
||
|
}
|
||
|
|
||
|
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];
|
||
|
}
|
||
|
}
|
||
|
|
||
|
export const chatRomm = new ChatRoom();
|