Skip to content
Source
Sign inSign up
TanStack/ai
Open History to see the last commit

chat-logic.ts

151 lines4.0 KB

1// Core chat business logic and data structures
2import type { ChatMessage, ChatRoomState } from './chat-api.js'
3​
4export type { ChatMessage, ChatRoomState }
5​
6// Core chat business logic class
7export class ChatLogic {
8 private chatState: ChatRoomState = {
9 onlineUsers: [],
10 messages: [],
11 }
12​
13 // Event callbacks
14 private onUserJoined?: (username: string) => Promise<void>
15 private onUserLeft?: (username: string) => Promise<void>
16 private onMessageSent?: (message: ChatMessage) => Promise<void>
17​
18 constructor(callbacks?: {
19 onUserJoined?: (username: string) => Promise<void>
20 onUserLeft?: (username: string) => Promise<void>
21 onMessageSent?: (message: ChatMessage) => Promise<void>
22 }) {
23 this.onUserJoined = callbacks?.onUserJoined
24 this.onUserLeft = callbacks?.onUserLeft
25 this.onMessageSent = callbacks?.onMessageSent
26 }
27​
28 addUserSync(username: string) {
29 if (!this.chatState.onlineUsers.includes(username)) {
30 this.chatState.onlineUsers.push(username)
31 console.log(`✅ ${username} joined the chat`)
32​
33 if (this.onUserJoined) {
34 void this.onUserJoined(username)
35 }
36 }
37 }
38​
39 removeUserSync(username: string) {
40 const index = this.chatState.onlineUsers.indexOf(username)
41 if (index > -1) {
42 this.chatState.onlineUsers.splice(index, 1)
43 console.log(`👋 ${username} left the chat`)
44​
45 if (this.onUserLeft) {
46 void this.onUserLeft(username)
47 }
48 }
49 }
50​
51 sendMessageSync(username: string, messageText: string): ChatMessage {
52 const message: ChatMessage = {
53 id: Math.random().toString(36).substr(2, 9),
54 username,
55 message: messageText,
56 timestamp: new Date().toISOString(),
57 }
58​
59 this.chatState.messages.push(message)
60 if (this.chatState.messages.length > 100) {
61 this.chatState.messages = this.chatState.messages.slice(-100)
62 }
63​
64 console.log(`💬 ${username}: ${messageText}`)
65​
66 if (this.onMessageSent) {
67 void this.onMessageSent(message)
68 }
69​
70 return message
71 }
72​
73 async addUser(username: string) {
74 if (!this.chatState.onlineUsers.includes(username)) {
75 this.chatState.onlineUsers.push(username)
76 console.log(`✅ ${username} joined the chat`)
77​
78 if (this.onUserJoined) {
79 await this.onUserJoined(username)
80 }
81 }
82 }
83​
84 async removeUser(username: string) {
85 const index = this.chatState.onlineUsers.indexOf(username)
86 if (index > -1) {
87 this.chatState.onlineUsers.splice(index, 1)
88 console.log(`👋 ${username} left the chat`)
89​
90 if (this.onUserLeft) {
91 await this.onUserLeft(username)
92 }
93 }
94 }
95​
96 async sendMessage(
97 username: string,
98 messageText: string,
99 ): Promise<ChatMessage> {
100 const message: ChatMessage = {
101 id: Math.random().toString(36).substr(2, 9),
102 username,
103 message: messageText,
104 timestamp: new Date().toISOString(),
105 }
106​
107 console.log(
108 `💬 ChatLogic.sendMessage() - username: ${username}, messageId: ${message.id}`,
109 )
110 console.log(
111 `💬 Message text (first 80 chars): "${messageText.substring(0, 80)}"`,
112 )
113​
114 // Add to messages (keep last 100 messages)
115 this.chatState.messages.push(message)
116 console.log(
117 `💬 Message added to chatState. Total messages: ${this.chatState.messages.length}`,
118 )
119​
120 if (this.chatState.messages.length > 100) {
121 this.chatState.messages = this.chatState.messages.slice(-100)
122 }
123​
124 console.log(`💬 ${username}: ${messageText}`)
125​
126 if (this.onMessageSent) {
127 console.log(`💬 Calling onMessageSent callback...`)
128 await this.onMessageSent(message)
129 console.log(`💬 onMessageSent callback complete`)
130 } else {
131 console.log(`💬 No onMessageSent callback registered`)
132 }
133​
134 return message
135 }
136​
137 getChatState(): ChatRoomState {
138 return {
139 onlineUsers: [...this.chatState.onlineUsers],
140 messages: [...this.chatState.messages],
141 }
142 }
143​
144 getMessages(): Array<ChatMessage> {
145 return [...this.chatState.messages]
146 }
147​
148 getOnlineUsers(): Array<string> {
149 return [...this.chatState.onlineUsers]
150 }