Move more logic into sync-client

This commit is contained in:
Andras Schmelczer 2025-08-30 11:02:04 +01:00
commit 9177984ff6
No known key found for this signature in database
GPG key ID: FC8F2C3D3D1A718C
20 changed files with 68 additions and 143 deletions

View file

@ -6,7 +6,7 @@
"scripts": { "scripts": {
"dev": "webpack watch --mode development", "dev": "webpack watch --mode development",
"build": "webpack --mode production", "build": "webpack --mode production",
"test": "tsx --test src/**/*.test.ts", "test": "echo \"no tests defined\" && exit 0",
"version": "node version-bump.mjs" "version": "node version-bump.mjs"
}, },
"keywords": [], "keywords": [],
@ -35,4 +35,4 @@
"webpack": "^5.99.9", "webpack": "^5.99.9",
"webpack-cli": "^6.0.1" "webpack-cli": "^6.0.1"
} }
} }

View file

@ -1,7 +1,10 @@
import type { Stat, Vault, Workspace } from "obsidian"; import type { Stat, Vault, Workspace } from "obsidian";
import { MarkdownView, normalizePath } from "obsidian"; import { MarkdownView, normalizePath } from "obsidian";
import type { FileSystemOperations, RelativePath } from "sync-client"; import {
import { positionToLineAndColumn } from "./utils/position-to-line-and-column"; utils,
type FileSystemOperations,
type RelativePath
} from "sync-client";
import { getSelectionsFromEditor } from "./views/cursors/get-selections-from-editor"; import { getSelectionsFromEditor } from "./views/cursors/get-selections-from-editor";
import type { TextWithCursors, CursorPosition } from "reconcile-text"; import type { TextWithCursors, CursorPosition } from "reconcile-text";
@ -105,10 +108,10 @@ export class ObsidianFileSystemOperations implements FileSystemOperations {
const from = result.cursors[2 * i]; const from = result.cursors[2 * i];
const to = result.cursors[2 * i + 1]; const to = result.cursors[2 * i + 1];
const { line: fromLine, column: fromColumn } = const { line: fromLine, column: fromColumn } =
positionToLineAndColumn(result.text, from.position); utils.positionToLineAndColumn(result.text, from.position);
const { line: toLine, column: toColumn } = const { line: toLine, column: toColumn } =
positionToLineAndColumn(result.text, to.position); utils.positionToLineAndColumn(result.text, to.position);
selections.push({ selections.push({
anchor: { line: fromLine, ch: fromColumn }, anchor: { line: fromLine, ch: fromColumn },

View file

@ -1,3 +0,0 @@
export async function sleep(ms: number): Promise<void> {
return new Promise((resolve) => setTimeout(resolve, ms));
}

View file

@ -11,10 +11,15 @@ import { HistoryView } from "./views/history/history-view";
import { StatusBar } from "./views/status-bar/status-bar"; import { StatusBar } from "./views/status-bar/status-bar";
import { LogsView } from "./views/logs/logs-view"; import { LogsView } from "./views/logs/logs-view";
import { StatusDescription } from "./views/status-description/status-description"; import { StatusDescription } from "./views/status-description/status-description";
import { SyncClient, rateLimit, DEFAULT_SETTINGS, Logger } from "sync-client"; import {
SyncClient,
rateLimit,
DEFAULT_SETTINGS,
Logger,
debugging
} from "sync-client";
import { ObsidianFileSystemOperations } from "./obsidian-file-system"; import { ObsidianFileSystemOperations } from "./obsidian-file-system";
import { SyncSettingsTab } from "./views/settings/settings-tab"; import { SyncSettingsTab } from "./views/settings/settings-tab";
import { logToConsole } from "./utils/log-to-console";
import { EditorStatusDisplayManager } from "./views/editor-status-display-manager/editor-status-display-manager"; import { EditorStatusDisplayManager } from "./views/editor-status-display-manager/editor-status-display-manager";
import { remoteCursorsTheme } from "./views/cursors/remote-cursor-theme"; import { remoteCursorsTheme } from "./views/cursors/remote-cursor-theme";
import { import {
@ -22,8 +27,6 @@ import {
RemoteCursorsPluginValue RemoteCursorsPluginValue
} from "./views/cursors/remote-cursors-plugin"; } from "./views/cursors/remote-cursors-plugin";
import { LocalCursorUpdateListener } from "./views/cursors/local-cursor-update-listener"; import { LocalCursorUpdateListener } from "./views/cursors/local-cursor-update-listener";
import { slowFetchFactory } from "./debugging/slow-fetch-factory";
import { flakyWebSocketFactory } from "./debugging/flaky-websocket-factory";
import { renderCursorsInFileExplorer } from "./views/cursors/file-explorer"; import { renderCursorsInFileExplorer } from "./views/cursors/file-explorer";
const MIN_WAIT_BETWEEN_UPDATES_IN_MS = 250; const MIN_WAIT_BETWEEN_UPDATES_IN_MS = 250;
@ -49,8 +52,8 @@ export default class VaultLinkPlugin extends Plugin {
const debugOptions = isDebugBuild const debugOptions = isDebugBuild
? { ? {
fetch: slowFetchFactory(1), fetch: debugging.slowFetchFactory(1),
webSocket: flakyWebSocketFactory(1, new Logger()) webSocket: debugging.slowWebSocketFactory(1, new Logger())
} }
: {}; : {};
@ -67,7 +70,9 @@ export default class VaultLinkPlugin extends Plugin {
...debugOptions ...debugOptions
}); });
logToConsole(this.client); if (isDebugBuild) {
debugging.logToConsole(this.client);
}
const statusDescription = new StatusDescription(this.client); const statusDescription = new StatusDescription(this.client);

View file

@ -7,7 +7,7 @@
span { span {
border-radius: var(--radius-l); border-radius: var(--radius-l);
padding: 0 var(--size-4-1); padding: 0 var(--size-4-1);
border-width: 1px; border-width: 1.4px;
border-style: solid; border-style: solid;
font-size: var(--font-smallest); font-size: var(--font-smallest);
font-style: italic; font-style: italic;

View file

@ -1,8 +1,11 @@
import "./file-explorer.scss"; import "./file-explorer.scss";
import type { App, View } from "obsidian"; import type { App, View } from "obsidian";
import { getRandomColor } from "src/utils/get-random-color"; import {
import type { MaybeOutdatedClientCursors, RelativePath } from "sync-client"; utils,
type MaybeOutdatedClientCursors,
type RelativePath
} from "sync-client";
const REMOTE_USER_CONTAINER_CLASS = "remote-users"; const REMOTE_USER_CONTAINER_CLASS = "remote-users";
@ -36,7 +39,7 @@ export function renderCursorsInFileExplorer(
createSpan({ createSpan({
text: cursor.userName, text: cursor.userName,
attr: { attr: {
style: `border-color: ${getRandomColor(cursor.userName)}` style: `border-color: ${utils.getRandomColor(cursor.userName)}`
} }
}) })
); );

View file

@ -1,5 +1,5 @@
import type { Editor } from "obsidian"; import type { Editor } from "obsidian";
import { lineAndColumnToPosition } from "../../utils/line-and-column-to-position"; import { utils } from "sync-client";
export interface Selection { export interface Selection {
id: number; id: number;
@ -11,7 +11,7 @@ export function getSelectionsFromEditor(editor: Editor): Selection[] {
const text = editor.getValue(); const text = editor.getValue();
return editor.listSelections().map(({ anchor, head }, i) => ({ return editor.listSelections().map(({ anchor, head }, i) => ({
id: i, id: i,
start: lineAndColumnToPosition(text, anchor.line, anchor.ch), start: utils.lineAndColumnToPosition(text, anchor.line, anchor.ch),
end: lineAndColumnToPosition(text, head.line, head.ch) end: utils.lineAndColumnToPosition(text, head.line, head.ch)
})); }));
} }

View file

@ -9,12 +9,15 @@ import type {
ViewUpdate ViewUpdate
} from "@codemirror/view"; } from "@codemirror/view";
import { RemoteCursorWidget } from "./remote-cursor-widget"; import { RemoteCursorWidget } from "./remote-cursor-widget";
import type { CursorSpan, MaybeOutdatedClientCursors } from "sync-client"; import {
utils,
type CursorSpan,
type MaybeOutdatedClientCursors
} from "sync-client";
import type { App } from "obsidian"; import type { App } from "obsidian";
import { MarkdownView } from "obsidian"; import { MarkdownView } from "obsidian";
import { StateEffect } from "@codemirror/state"; import { StateEffect } from "@codemirror/state";
import { getRandomColor } from "src/utils/get-random-color";
import type { SpanWithHistory } from "reconcile-text"; import type { SpanWithHistory } from "reconcile-text";
import { reconcileWithHistory } from "reconcile-text"; import { reconcileWithHistory } from "reconcile-text";
@ -155,7 +158,7 @@ export class RemoteCursorsPluginValue implements PluginValue {
RemoteCursorsPluginValue.cursors.forEach( RemoteCursorsPluginValue.cursors.forEach(
({ name, span: { start, end } }) => { ({ name, span: { start, end } }) => {
const color = getRandomColor(name); const color = utils.getRandomColor(name);
const startLine = update.view.state.doc.lineAt(start); const startLine = update.view.state.doc.lineAt(start);
const endLine = update.view.state.doc.lineAt(end); const endLine = update.view.state.doc.lineAt(end);

View file

@ -0,0 +1,24 @@
import type { SyncClient } from "../sync-client";
import type { LogLine } from "../tracing/logger";
import { LogLevel } from "../tracing/logger";
export function logToConsole(client: SyncClient): void {
client.logger.addOnMessageListener((logLine: LogLine) => {
const formatted = `${logLine.timestamp.toISOString()} ${logLine.level} ${logLine.message}`;
switch (logLine.level) {
case LogLevel.ERROR:
console.error(formatted);
break;
case LogLevel.WARNING:
console.warn(formatted);
break;
case LogLevel.INFO:
console.info(formatted);
break;
case LogLevel.DEBUG:
console.debug(formatted);
break;
}
});
}

View file

@ -1,6 +1,6 @@
import { sleep } from "./sleep"; import { sleep } from "../utils/sleep";
export const flakyFetchFactory = export const slowFetchFactory =
(jitterScaleInSeconds: number) => (jitterScaleInSeconds: number) =>
async ( async (
input: string | URL | globalThis.Request, input: string | URL | globalThis.Request,
@ -12,9 +12,5 @@ export const flakyFetchFactory =
const response = await fetch(input, init); const response = await fetch(input, init);
if (jitterScaleInSeconds > 0) {
await sleep(Math.random() * jitterScaleInSeconds * 1000);
}
return response; return response;
}; };

View file

@ -1,8 +1,8 @@
import type { Logger } from "sync-client"; import { sleep } from "../utils/sleep";
import { helpers } from "sync-client"; import { Locks } from "../utils/locks";
import { sleep } from "./sleep"; import type { Logger } from "../tracing/logger";
export function flakyWebSocketFactory( export function slowWebSocketFactory(
jitterScaleInSeconds: number, jitterScaleInSeconds: number,
logger: Logger logger: Logger
): typeof WebSocket { ): typeof WebSocket {
@ -11,7 +11,7 @@ export function flakyWebSocketFactory(
private static readonly RECEIVE_KEY = "websocket-receive"; private static readonly RECEIVE_KEY = "websocket-receive";
private static readonly SEND_KEY = "websocket-send"; private static readonly SEND_KEY = "websocket-send";
private readonly locks = new helpers.Locks(logger); private readonly locks = new Locks(logger);
public set onopen(callback: (event: Event) => void) { public set onopen(callback: (event: Event) => void) {
super.onopen = async (event: Event): Promise<void> => { super.onopen = async (event: Event): Promise<void> => {
@ -25,7 +25,7 @@ export function flakyWebSocketFactory(
public set onmessage(callback: (event: MessageEvent) => void) { public set onmessage(callback: (event: MessageEvent) => void) {
super.onmessage = async (event: MessageEvent): Promise<void> => { super.onmessage = async (event: MessageEvent): Promise<void> => {
return this.locks.withLock( await this.locks.withLock(
FlakyWebSocket.RECEIVE_KEY, FlakyWebSocket.RECEIVE_KEY,
async () => { async () => {
if (jitterScaleInSeconds > 0) { if (jitterScaleInSeconds > 0) {
@ -70,11 +70,10 @@ export function flakyWebSocketFactory(
data: string | ArrayBufferLike | Blob | ArrayBufferView data: string | ArrayBufferLike | Blob | ArrayBufferView
): Promise<void> { ): Promise<void> {
// maintain message order // maintain message order
return this.locks.withLock(FlakyWebSocket.SEND_KEY, async () => { await this.locks.withLock(FlakyWebSocket.SEND_KEY, async () => {
if (jitterScaleInSeconds > 0) { if (jitterScaleInSeconds > 0) {
await sleep(Math.random() * jitterScaleInSeconds * 1000); await sleep(Math.random() * jitterScaleInSeconds * 1000);
} }
super.send(data); super.send(data);
}); });
} }

View file

@ -1,3 +1,10 @@
import { logToConsole } from "./debugging/log-to-console";
import { slowFetchFactory } from "./debugging/slow-fetch-factory";
import { slowWebSocketFactory } from "./debugging/slow-web-socket-factory";
import { getRandomColor } from "./utils/get-random-color";
import { lineAndColumnToPosition } from "./utils/line-and-column-to-position";
import { positionToLineAndColumn } from "./utils/position-to-line-and-column";
export { export {
SyncType, SyncType,
SyncStatus, SyncStatus,
@ -22,7 +29,14 @@ export type { MaybeOutdatedClientCursors } from "./types/maybe-outdated-client-c
export { DocumentSyncStatus } from "./types/document-sync-status"; export { DocumentSyncStatus } from "./types/document-sync-status";
export { SyncClient } from "./sync-client"; export { SyncClient } from "./sync-client";
import { Locks } from "./utils/locks"; export const debugging = {
export const helpers = { slowFetchFactory,
Locks slowWebSocketFactory,
logToConsole
};
export const utils = {
getRandomColor,
positionToLineAndColumn,
lineAndColumnToPosition
}; };

View file

@ -0,0 +1,9 @@
export function getRandomColor(name: string): string {
let hash = 0;
for (let i = 0; i < name.length; i++) {
hash = (hash << 5) - hash + name.charCodeAt(i);
hash |= 0; // Convert to 32bit integer
}
const normalised = hash / 0x7fffffff;
return `oklch(0.58 0.15 ${Math.round(Math.abs(normalised * 360))})`;
}

View file

@ -0,0 +1,44 @@
import { describe, it } from "node:test";
import assert from "node:assert";
import { lineAndColumnToPosition } from "./line-and-column-to-position";
describe("lineAndColumnToPosition", () => {
it("should return the correct position for the first line", () => {
const text = "Hello\nWorld";
const position = lineAndColumnToPosition(text, 0, 3);
assert.strictEqual(position, 3);
});
it("should return the correct position for the second line", () => {
const text = "Hello\nWorld";
const position = lineAndColumnToPosition(text, 1, 2);
assert.strictEqual(position, 8);
});
it("should return the correct position for an empty string", () => {
const text = "";
const position = lineAndColumnToPosition(text, 0, 0);
assert.strictEqual(position, 0);
});
it("with carrige return", () => {
assert.strictEqual(lineAndColumnToPosition("a\nb", 1, 1), 3);
assert.strictEqual(lineAndColumnToPosition("a\r\nb", 1, 1), 3);
});
it("should handle multi-line strings with varying lengths", () => {
const text = "Line1\nLongerLine2\nShort3";
const position = lineAndColumnToPosition(text, 2, 4);
assert.strictEqual(position, 22);
});
it("should throw an error if the line number is out of range", () => {
const text = "Line1\nLine2";
assert.throws(() => lineAndColumnToPosition(text, 3, 0));
});
it("should throw an error if the column number is out of range", () => {
const text = "Line1\nLine2";
assert.throws(() => lineAndColumnToPosition(text, 1, 10));
});
});

View file

@ -0,0 +1,34 @@
/**
* Converts line and column coordinates to an absolute character position in a text string.
*
* @param line - The zero-based line number
* @param column - The zero-based column number
* @param text - The text string to calculate position in
* @returns The absolute character position (zero-based index) in the text string
* @throws Error if line number is out of range
* @throws Error if column number is out of range
*/
export function lineAndColumnToPosition(
text: string,
line: number,
column: number
): number {
const lines = text.replace("\r", "").split("\n");
if (line >= lines.length) {
throw new Error(`Line number ${line} is out of range.`);
}
if (column > lines[line].length) {
throw new Error(`Column number ${column} is out of range.`);
}
let position = 0;
for (let i = 0; i < line; i++) {
position += lines[i].length + 1;
}
position += column;
return position;
}

View file

@ -0,0 +1,66 @@
import { describe, test } from "node:test";
import assert from "node:assert";
import { positionToLineAndColumn } from "./position-to-line-and-column";
describe("positionToLineAndColumn", () => {
test("converts position to line and column in multi-line text", () => {
const text = "ab\ncd\n";
assert.deepStrictEqual(positionToLineAndColumn(text, 0), {
line: 0,
column: 0
});
assert.deepStrictEqual(positionToLineAndColumn(text, 1), {
line: 0,
column: 1
});
assert.deepStrictEqual(positionToLineAndColumn(text, 2), {
line: 0,
column: 2
});
assert.deepStrictEqual(positionToLineAndColumn(text, 3), {
line: 1,
column: 0
});
assert.deepStrictEqual(positionToLineAndColumn(text, 4), {
line: 1,
column: 1
});
assert.deepStrictEqual(positionToLineAndColumn(text, 6), {
line: 2,
column: 0
});
});
test("with carrige returns", () => {
assert.deepStrictEqual(positionToLineAndColumn("a\nb", 3), {
line: 1,
column: 1
});
assert.deepStrictEqual(positionToLineAndColumn("a\r\nb", 3), {
line: 1,
column: 1
});
});
test("handles empty input", () => {
assert.deepStrictEqual(positionToLineAndColumn("", 0), {
line: 0,
column: 0
});
});
test("handles positions at the end of text", () => {
const text = "End";
assert.deepStrictEqual(positionToLineAndColumn(text, 3), {
line: 0,
column: 3
});
});
test("throws error for position out of range", () => {
const text = "Short text";
assert.throws(() => positionToLineAndColumn(text, 15));
assert.throws(() => positionToLineAndColumn(text, -1));
});
});

View file

@ -0,0 +1,36 @@
/**
* Converts a character position in text to line and column numbers.
*
* @param text The text content to analyze
* @param position The character position to convert
* @returns An object containing line and column numbers
* @throws Will throw an error if the position is negative or exceeds the text length
*/
export function positionToLineAndColumn(
text: string,
position: number
): { line: number; column: number } {
if (position < 0) {
throw new Error("Position cannot be negative");
}
text = text.replace("\r", "");
if (
position >
text.length + 1
// +1 to account for the cursor being after last character
) {
throw new Error(
`Position ${position} exceeds text length ${text.length}`
);
}
const textUpToPosition = text.substring(0, position);
const lines = textUpToPosition.split("\n");
const line = lines.length - 1;
const column = lines[lines.length - 1].length;
return { line, column };
}

View file

@ -2,12 +2,10 @@ import { choose } from "../utils/choose";
import { v4 as uuidv4 } from "uuid"; import { v4 as uuidv4 } from "uuid";
import { assert } from "../utils/assert"; import { assert } from "../utils/assert";
import type { RelativePath, SyncSettings } from "sync-client"; import type { RelativePath, SyncSettings } from "sync-client";
import { Logger, LogLevel } from "sync-client"; import { debugging, Logger, LogLevel } from "sync-client";
import { MockClient } from "./mock-client"; import { MockClient } from "./mock-client";
import { sleep } from "../utils/sleep"; import { sleep } from "../utils/sleep";
import type { LogLine } from "sync-client/dist/types/tracing/logger"; import type { LogLine } from "sync-client/dist/types/tracing/logger";
import { flakyFetchFactory } from "../utils/flaky-fetch";
import { flakyWebSocketFactory } from "../utils/flaky-websocket-factory";
export class MockAgent extends MockClient { export class MockAgent extends MockClient {
private readonly writtenContents: string[] = []; private readonly writtenContents: string[] = [];
@ -28,8 +26,8 @@ export class MockAgent extends MockClient {
public async init(): Promise<void> { public async init(): Promise<void> {
await super.init( await super.init(
flakyFetchFactory(this.jitterScaleInSeconds), debugging.slowFetchFactory(this.jitterScaleInSeconds),
flakyWebSocketFactory( debugging.slowWebSocketFactory(
this.jitterScaleInSeconds, this.jitterScaleInSeconds,
new Logger() // this logger isn't wired anywhere, so messages to it will be ignored new Logger() // this logger isn't wired anywhere, so messages to it will be ignored
) )