documentation for all JS files
This commit is contained in:
@@ -1,9 +1,33 @@
|
||||
/**
|
||||
* @fileoverview Manages global chat state and polling functionality.
|
||||
* This file implements a singleton ChatManager that handles:
|
||||
* - Message polling and state management
|
||||
* - New message processing and event triggering
|
||||
* - Connection state tracking
|
||||
* - Resource cleanup
|
||||
*/
|
||||
|
||||
// Global state and polling management
|
||||
if (typeof window.ChatManager === 'undefined') {
|
||||
window.ChatManager = (function() {
|
||||
let instance = null;
|
||||
let pollInterval = null;
|
||||
|
||||
/**
|
||||
* @typedef {Object} ConnectionState
|
||||
* @property {boolean} hasJoined - Whether the user has joined the conversation
|
||||
* @property {boolean} isConnected - Current connection status
|
||||
* @property {number|null} lastMessageId - ID of the last received message
|
||||
* @property {number} pollAttempts - Number of failed polling attempts
|
||||
*/
|
||||
|
||||
/**
|
||||
* @typedef {Object} ChatState
|
||||
* @property {Set<number>} addedMessageIds - Set of message IDs that have been processed
|
||||
* @property {Set<Object>} messageQueue - Queue of messages waiting to be processed
|
||||
* @property {ConnectionState} connectionState - Current connection state
|
||||
*/
|
||||
|
||||
const state = {
|
||||
addedMessageIds: new Set(),
|
||||
messageQueue: new Set(),
|
||||
@@ -15,6 +39,13 @@ if (typeof window.ChatManager === 'undefined') {
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Initializes a new ChatManager instance for a conversation.
|
||||
* Sets up message tracking and starts polling for new messages.
|
||||
* @function
|
||||
* @param {string} conversationId - The ID of the conversation to manage
|
||||
* @returns {Object} The ChatManager instance
|
||||
*/
|
||||
function init(conversationId) {
|
||||
if (instance) {
|
||||
console.log('[ChatManager] Instance already exists, returning existing instance');
|
||||
@@ -47,6 +78,12 @@ if (typeof window.ChatManager === 'undefined') {
|
||||
return instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* Starts polling for new messages in the conversation.
|
||||
* Polls every 3 seconds and performs an initial fetch.
|
||||
* @function
|
||||
* @param {string} conversationId - The ID of the conversation to poll
|
||||
*/
|
||||
function startPolling(conversationId) {
|
||||
console.log('[ChatManager] Starting polling for conversation:', conversationId);
|
||||
|
||||
@@ -67,6 +104,12 @@ if (typeof window.ChatManager === 'undefined') {
|
||||
fetchNewMessages(conversationId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetches new messages from the server.
|
||||
* Uses the last message ID to only fetch new messages.
|
||||
* @function
|
||||
* @param {string} conversationId - The ID of the conversation to fetch messages from
|
||||
*/
|
||||
function fetchNewMessages(conversationId) {
|
||||
const url = `/conversations/${conversationId}/messages`;
|
||||
const params = new URLSearchParams();
|
||||
@@ -107,6 +150,12 @@ if (typeof window.ChatManager === 'undefined') {
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Processes new messages and triggers events for each new message.
|
||||
* Updates the last message ID and tracks processed messages.
|
||||
* @function
|
||||
* @param {Array<Object>} messages - Array of new message objects to process
|
||||
*/
|
||||
function processNewMessages(messages) {
|
||||
console.log('[ChatManager] Processing new messages:', {
|
||||
messageCount: messages.length,
|
||||
@@ -133,6 +182,11 @@ if (typeof window.ChatManager === 'undefined') {
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Cleans up polling resources and resets the instance.
|
||||
* Should be called when the chat is no longer needed.
|
||||
* @function
|
||||
*/
|
||||
function cleanup() {
|
||||
console.log('[ChatManager] Cleaning up polling');
|
||||
if (pollInterval) {
|
||||
@@ -143,6 +197,12 @@ if (typeof window.ChatManager === 'undefined') {
|
||||
}
|
||||
|
||||
return {
|
||||
/**
|
||||
* Gets or creates a ChatManager instance for a conversation.
|
||||
* @function
|
||||
* @param {string} conversationId - The ID of the conversation to manage
|
||||
* @returns {Object} The ChatManager instance
|
||||
*/
|
||||
getInstance: function(conversationId) {
|
||||
if (!instance) {
|
||||
instance = init(conversationId);
|
||||
|
||||
Reference in New Issue
Block a user