Add API for propagating cursor locations #61
3 changed files with 34 additions and 6 deletions
Expose cursor management
commit
7e3f972531
|
|
@ -19,7 +19,8 @@ export type {
|
||||||
Cursor
|
Cursor
|
||||||
} from "./file-operations/filesystem-operations";
|
} from "./file-operations/filesystem-operations";
|
||||||
export type { PersistenceProvider } from "./persistence/persistence";
|
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 type { NetworkConnectionStatus } from "./types/network-connection-status";
|
||||||
export { DocumentUpdateStatus } from "./types/document-update-status";
|
export { DocumentUpdateStatus } from "./types/document-update-status";
|
||||||
export { SyncClient } from "./sync-client";
|
export { SyncClient } from "./sync-client";
|
||||||
|
|
|
||||||
|
|
@ -5,10 +5,13 @@ import { WebSocketServerMessage } from "./types/WebSocketServerMessage";
|
||||||
import { Syncer } from "../sync-operations/syncer";
|
import { Syncer } from "../sync-operations/syncer";
|
||||||
import { WebSocketClientMessage } from "./types/WebSocketClientMessage";
|
import { WebSocketClientMessage } from "./types/WebSocketClientMessage";
|
||||||
import { CursorPositionFromClient } from "./types/CursorPositionFromClient";
|
import { CursorPositionFromClient } from "./types/CursorPositionFromClient";
|
||||||
|
import { ClientCursors } from "./types/ClientCursors";
|
||||||
|
|
||||||
export class WebSocketManager {
|
export class WebSocketManager {
|
||||||
private readonly webSocketStatusChangeListeners: (() => unknown)[] = [];
|
private readonly webSocketStatusChangeListeners: (() => unknown)[] = [];
|
||||||
// private readonly cur: (() => unknown)[] = [];
|
private readonly remoteCursorsUpdateListeners: ((
|
||||||
|
cursors: ClientCursors[]
|
||||||
|
) => unknown)[] = [];
|
||||||
|
|
||||||
private refreshWebSocketInterval: NodeJS.Timeout | undefined;
|
private refreshWebSocketInterval: NodeJS.Timeout | undefined;
|
||||||
|
|
||||||
|
|
@ -66,6 +69,12 @@ export class WebSocketManager {
|
||||||
this.webSocketStatusChangeListeners.push(listener);
|
this.webSocketStatusChangeListeners.push(listener);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public addRemoteCursorsUpdateListener(
|
||||||
|
listener: (cursors: ClientCursors[]) => void
|
||||||
|
): void {
|
||||||
|
this.remoteCursorsUpdateListeners.push(listener);
|
||||||
|
}
|
||||||
|
|
||||||
public async reset(): Promise<void> {
|
public async reset(): Promise<void> {
|
||||||
this.setWebSocketRefreshInterval();
|
this.setWebSocketRefreshInterval();
|
||||||
this.updateWebSocket(this.settings.getSettings());
|
this.updateWebSocket(this.settings.getSettings());
|
||||||
|
|
@ -129,7 +138,13 @@ export class WebSocketManager {
|
||||||
this.logger.info(
|
this.logger.info(
|
||||||
`Received cursor positions for ${JSON.stringify(message.clients)}`
|
`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 {
|
} else {
|
||||||
this.logger.warn(
|
this.logger.warn(
|
||||||
`Received unknown message type: ${JSON.stringify(message)}`
|
`Received unknown message type: ${JSON.stringify(message)}`
|
||||||
|
|
@ -163,9 +178,7 @@ export class WebSocketManager {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
public sendCursorPositions(
|
public updateLocalCursors(cursorPositions: CursorPositionFromClient): void {
|
||||||
cursorPositions: CursorPositionFromClient
|
|
||||||
): void {
|
|
||||||
if (!this.isWebSocketConnected) {
|
if (!this.isWebSocketConnected) {
|
||||||
this.logger.warn(
|
this.logger.warn(
|
||||||
"WebSocket is not connected, cannot send cursor positions"
|
"WebSocket is not connected, cannot send cursor positions"
|
||||||
|
|
|
||||||
|
|
@ -19,6 +19,8 @@ import type { NetworkConnectionStatus } from "./types/network-connection-status"
|
||||||
import { DocumentUpdateStatus } from "./types/document-update-status";
|
import { DocumentUpdateStatus } from "./types/document-update-status";
|
||||||
import { WebSocketManager } from "./services/websocket-manager";
|
import { WebSocketManager } from "./services/websocket-manager";
|
||||||
import { createClientId } from "./utils/create-client-id";
|
import { createClientId } from "./utils/create-client-id";
|
||||||
|
import { CursorSpan } from "./services/types/CursorSpan";
|
||||||
|
import { ClientCursors } from "./services/types/ClientCursors";
|
||||||
|
|
||||||
export class SyncClient {
|
export class SyncClient {
|
||||||
private static readonly MINIMUM_SAVE_INTERVAL_MS = 1000;
|
private static readonly MINIMUM_SAVE_INTERVAL_MS = 1000;
|
||||||
|
|
@ -273,6 +275,18 @@ export class SyncClient {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async updateLocalCursors(documentToCursors: {
|
||||||
|
[path: RelativePath]: CursorSpan[];
|
||||||
|
}): Promise<void> {
|
||||||
|
return this.webSocketManager.updateLocalCursors({ documentToCursors });
|
||||||
|
}
|
||||||
|
|
||||||
|
public addRemoteCursorsUpdateListener(
|
||||||
|
listener: (cursors: ClientCursors[]) => void
|
||||||
|
): void {
|
||||||
|
this.webSocketManager.addRemoteCursorsUpdateListener(listener);
|
||||||
|
}
|
||||||
|
|
||||||
public getDocumentSyncingStatus(
|
public getDocumentSyncingStatus(
|
||||||
relativePath: RelativePath
|
relativePath: RelativePath
|
||||||
): DocumentUpdateStatus {
|
): DocumentUpdateStatus {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue