diff --git a/frontend/sync-client/src/index.ts b/frontend/sync-client/src/index.ts index 7079f707..0cd94277 100644 --- a/frontend/sync-client/src/index.ts +++ b/frontend/sync-client/src/index.ts @@ -19,7 +19,8 @@ export type { Cursor } from "./file-operations/filesystem-operations"; export type { PersistenceProvider } from "./persistence/persistence"; - +export type { CursorSpan } from "./services/types/CursorSpan"; +export type { ClientCursors } from "./services/types/ClientCursors"; export type { NetworkConnectionStatus } from "./types/network-connection-status"; export { DocumentUpdateStatus } from "./types/document-update-status"; export { SyncClient } from "./sync-client"; diff --git a/frontend/sync-client/src/services/websocket-manager.ts b/frontend/sync-client/src/services/websocket-manager.ts index 8a37f9ff..0006e344 100644 --- a/frontend/sync-client/src/services/websocket-manager.ts +++ b/frontend/sync-client/src/services/websocket-manager.ts @@ -5,10 +5,13 @@ import { WebSocketServerMessage } from "./types/WebSocketServerMessage"; import { Syncer } from "../sync-operations/syncer"; import { WebSocketClientMessage } from "./types/WebSocketClientMessage"; import { CursorPositionFromClient } from "./types/CursorPositionFromClient"; +import { ClientCursors } from "./types/ClientCursors"; export class WebSocketManager { private readonly webSocketStatusChangeListeners: (() => unknown)[] = []; - // private readonly cur: (() => unknown)[] = []; + private readonly remoteCursorsUpdateListeners: (( + cursors: ClientCursors[] + ) => unknown)[] = []; private refreshWebSocketInterval: NodeJS.Timeout | undefined; @@ -66,6 +69,12 @@ export class WebSocketManager { this.webSocketStatusChangeListeners.push(listener); } + public addRemoteCursorsUpdateListener( + listener: (cursors: ClientCursors[]) => void + ): void { + this.remoteCursorsUpdateListeners.push(listener); + } + public async reset(): Promise { this.setWebSocketRefreshInterval(); this.updateWebSocket(this.settings.getSettings()); @@ -129,7 +138,13 @@ export class WebSocketManager { this.logger.info( `Received cursor positions for ${JSON.stringify(message.clients)}` ); - // Handle cursor positions if needed + this.remoteCursorsUpdateListeners.forEach((listener) => { + listener( + message.clients.filter( + (client) => client.deviceId !== this.deviceId + ) + ); + }); } else { this.logger.warn( `Received unknown message type: ${JSON.stringify(message)}` @@ -163,9 +178,7 @@ export class WebSocketManager { }; } - public sendCursorPositions( - cursorPositions: CursorPositionFromClient - ): void { + public updateLocalCursors(cursorPositions: CursorPositionFromClient): void { if (!this.isWebSocketConnected) { this.logger.warn( "WebSocket is not connected, cannot send cursor positions" diff --git a/frontend/sync-client/src/sync-client.ts b/frontend/sync-client/src/sync-client.ts index b9fbbcc2..1bb92f3c 100644 --- a/frontend/sync-client/src/sync-client.ts +++ b/frontend/sync-client/src/sync-client.ts @@ -19,6 +19,8 @@ import type { NetworkConnectionStatus } from "./types/network-connection-status" import { DocumentUpdateStatus } from "./types/document-update-status"; import { WebSocketManager } from "./services/websocket-manager"; import { createClientId } from "./utils/create-client-id"; +import { CursorSpan } from "./services/types/CursorSpan"; +import { ClientCursors } from "./services/types/ClientCursors"; export class SyncClient { private static readonly MINIMUM_SAVE_INTERVAL_MS = 1000; @@ -273,6 +275,18 @@ export class SyncClient { }); } + public async updateLocalCursors(documentToCursors: { + [path: RelativePath]: CursorSpan[]; + }): Promise { + return this.webSocketManager.updateLocalCursors({ documentToCursors }); + } + + public addRemoteCursorsUpdateListener( + listener: (cursors: ClientCursors[]) => void + ): void { + this.webSocketManager.addRemoteCursorsUpdateListener(listener); + } + public getDocumentSyncingStatus( relativePath: RelativePath ): DocumentUpdateStatus {