recoder-code
Version:
Complete AI-powered development platform with ML model training, plugin registry, real-time collaboration, monitoring, infrastructure automation, and enterprise deployment capabilities
149 lines (148 loc) • 3.72 kB
TypeScript
/**
* Session Manager for Collaboration Service
* Manages real-time collaboration sessions and user connections
*/
import { EventEmitter } from 'events';
import { Server as SocketIOServer } from 'socket.io';
import { TextOperation } from '../operational-transform';
export interface User {
id: string;
username: string;
email: string;
avatar_url?: string;
}
export interface SessionUser {
userId: string;
username: string;
socketId: string;
cursor?: {
line: number;
column: number;
};
selection?: {
start: {
line: number;
column: number;
};
end: {
line: number;
column: number;
};
};
color: string;
lastActivity: Date;
}
export interface CollaborationSession {
id: string;
documentId: string;
users: Map<string, SessionUser>;
operations: Array<{
operation: TextOperation;
userId: string;
timestamp: Date;
operationId: string;
}>;
documentVersion: number;
createdAt: Date;
lastActivity: Date;
}
export declare class SessionManager extends EventEmitter {
private sessions;
private userSessions;
private socketSessions;
private io;
constructor(io: SocketIOServer);
/**
* Create a new collaboration session
*/
createSession(documentId: string, initiatorUserId: string): CollaborationSession;
/**
* Join a user to a collaboration session
*/
joinSession(sessionId: string, user: User, socketId: string): boolean;
/**
* Remove a user from a session
*/
leaveSession(sessionId: string, userId: string, socketId?: string): boolean;
/**
* Add an operation to a session
*/
addOperation(sessionId: string, operation: TextOperation, userId: string): {
success: boolean;
operationId?: string;
documentVersion?: number;
};
/**
* Update user cursor position
*/
updateCursor(sessionId: string, userId: string, cursor: {
line: number;
column: number;
}): boolean;
/**
* Update user selection
*/
updateSelection(sessionId: string, userId: string, selection: {
start: {
line: number;
column: number;
};
end: {
line: number;
column: number;
};
}): boolean;
/**
* Get session information
*/
getSession(sessionId: string): CollaborationSession | undefined;
/**
* Get all sessions for a document
*/
getDocumentSessions(documentId: string): CollaborationSession[];
/**
* Get sessions for a user
*/
getUserSessions(userId: string): CollaborationSession[];
/**
* Get session by socket ID
*/
getSessionBySocket(socketId: string): CollaborationSession | undefined;
/**
* Setup Socket.IO event handlers
*/
private setupSocketHandlers;
/**
* Handle socket disconnection
*/
private handleSocketDisconnect;
/**
* Start cleanup interval for inactive sessions
*/
private startCleanupInterval;
/**
* Clean up inactive sessions
*/
private cleanupInactiveSessions;
/**
* Generate unique session ID
*/
private generateSessionId;
/**
* Generate unique operation ID
*/
private generateOperationId;
/**
* Generate a unique color for a user
*/
private generateUserColor;
/**
* Get statistics about active sessions
*/
getStats(): {
totalSessions: number;
totalUsers: number;
averageUsersPerSession: number;
activeSessions: number;
};
}