Add API for propagating cursor locations (#61)

This commit is contained in:
Andras Schmelczer 2025-06-08 20:20:52 +01:00 committed by GitHub
parent f97193e287
commit e8b9bf40c5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
80 changed files with 1930 additions and 2229 deletions

View file

@ -15,9 +15,12 @@ import { FileOperations } from "./file-operations/file-operations";
import { ConnectionStatus } from "./services/connection-status";
import { UnrestrictedSyncer } from "./sync-operations/unrestricted-syncer";
import { rateLimit } from "./utils/rate-limit";
import { v4 as uuidv4 } from "uuid";
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 type { CursorSpan } from "./services/types/CursorSpan";
import type { ClientCursors } from "./services/types/ClientCursors";
export class SyncClient {
private static readonly MINIMUM_SAVE_INTERVAL_MS = 1000;
@ -29,6 +32,7 @@ export class SyncClient {
private readonly database: Database,
private readonly syncer: Syncer,
private readonly syncService: SyncService,
private readonly webSocketManager: WebSocketManager,
private readonly _logger: Logger,
private readonly connectionStatus: ConnectionStatus
) {
@ -68,7 +72,10 @@ export class SyncClient {
nativeLineEndings?: string;
}): Promise<SyncClient> {
const logger = new Logger();
logger.info("Initialising SyncClient");
const deviceId = createClientId();
logger.info(`Initialising SyncClient with client id ${deviceId}`);
const history = new SyncHistory(logger);
@ -104,7 +111,6 @@ export class SyncClient {
await rateLimitedSave(state);
}
);
const deviceId = uuidv4();
const connectionStatus = new ConnectionStatus(settings, logger);
const syncService = new SyncService(
@ -121,6 +127,7 @@ export class SyncClient {
fs,
nativeLineEndings
);
const unrestrictedSyncer = new UnrestrictedSyncer(
logger,
database,
@ -129,6 +136,7 @@ export class SyncClient {
fileOperations,
history
);
const syncer = new Syncer(
deviceId,
logger,
@ -136,7 +144,15 @@ export class SyncClient {
settings,
syncService,
fileOperations,
unrestrictedSyncer,
unrestrictedSyncer
);
const webSocketManager = new WebSocketManager(
deviceId,
logger,
database,
settings,
syncer,
webSocket
);
@ -146,6 +162,7 @@ export class SyncClient {
database,
syncer,
syncService,
webSocketManager,
logger,
connectionStatus
);
@ -160,7 +177,7 @@ export class SyncClient {
return {
isSuccessful: server.isSuccessful,
serverMessage: server.message,
isWebSocketConnected: this.syncer.isWebSocketConnected
isWebSocketConnected: this.webSocketManager.isWebSocketConnected
};
}
@ -179,7 +196,7 @@ export class SyncClient {
}
public stop(): void {
this.syncer.stop();
this.webSocketManager.stop();
}
public async waitAndStop(): Promise<void> {
@ -194,6 +211,7 @@ export class SyncClient {
this.stop();
this.connectionStatus.startReset();
await this.syncer.reset();
await this.webSocketManager.reset();
this.history.reset();
this.database.reset();
this._logger.reset();
@ -229,7 +247,7 @@ export class SyncClient {
}
public addWebSocketStatusChangeListener(listener: () => void): void {
this.syncer.addWebSocketStatusChangeListener(listener);
this.webSocketManager.addWebSocketStatusChangeListener(listener);
}
public async syncLocallyCreatedFile(
@ -257,6 +275,18 @@ export class SyncClient {
});
}
public async updateLocalCursors(
documentToCursors: Record<RelativePath, CursorSpan[]>
): Promise<void> {
this.webSocketManager.updateLocalCursors({ documentToCursors });
}
public addRemoteCursorsUpdateListener(
listener: (cursors: ClientCursors[]) => void
): void {
this.webSocketManager.addRemoteCursorsUpdateListener(listener);
}
public getDocumentSyncingStatus(
relativePath: RelativePath
): DocumentUpdateStatus {