Use new Rust bindings

This commit is contained in:
Andras Schmelczer 2025-01-02 11:58:28 +00:00
parent b2a8db14b6
commit b6d94bce0b
No known key found for this signature in database
GPG key ID: FC8F2C3D3D1A718C
3 changed files with 30 additions and 34 deletions

View file

@ -32,6 +32,8 @@ export default class SyncPlugin extends Plugin {
) )
); );
lib.setPanicHook();
const database = new Database( const database = new Database(
await this.loadData(), await this.loadData(),
this.saveData.bind(this) this.saveData.bind(this)
@ -106,7 +108,7 @@ export default class SyncPlugin extends Plugin {
this.registerView( this.registerView(
HistoryView.TYPE, HistoryView.TYPE,
(leaf) => new HistoryView(leaf, this.history) (leaf) => new HistoryView(leaf, database, this.history)
); );
this.registerView(LogsView.TYPE, (leaf) => new LogsView(leaf)); this.registerView(LogsView.TYPE, (leaf) => new LogsView(leaf));

View file

@ -84,7 +84,7 @@ export class SyncService {
}, },
}, },
body: { body: {
contentBase64: lib.bytes_to_base64(contentBytes), contentBase64: lib.bytesToBase64(contentBytes),
createdDate: createdDate.toISOString(), createdDate: createdDate.toISOString(),
relativePath, relativePath,
}, },
@ -137,7 +137,7 @@ export class SyncService {
}, },
body: { body: {
parentVersionId, parentVersionId,
contentBase64: lib.bytes_to_base64(contentBytes), contentBase64: lib.bytesToBase64(contentBytes),
createdDate: createdDate.toISOString(), createdDate: createdDate.toISOString(),
relativePath, relativePath,
}, },

View file

@ -1,25 +1,21 @@
import { Database } from "src/database/database"; import type { Database } from "src/database/database";
import { RelativePath } from "src/database/document-metadata"; import type { RelativePath } from "src/database/document-metadata";
import { FileOperations } from "src/file-operations/file-operations"; import type { FileOperations } from "src/file-operations/file-operations";
import * as lib from "../../../backend/sync_lib/pkg/sync_lib.js"; import * as lib from "../../../backend/sync_lib/pkg/sync_lib.js";
import { SyncService } from "src/services/sync-service"; import type { SyncService } from "src/services/sync-service";
import { Logger } from "src/tracing/logger"; import { Logger } from "src/tracing/logger";
import { import type { SyncHistory } from "src/tracing/sync-history";
SyncHistory, import { SyncSource, SyncStatus, SyncType } from "src/tracing/sync-history";
SyncSource,
SyncStatus,
SyncType,
} from "src/tracing/sync-history";
import { unlockDocument, waitForDocumentLock } from "./document-lock"; import { unlockDocument, waitForDocumentLock } from "./document-lock";
import PQueue from "p-queue"; import PQueue from "p-queue";
import { EMPTY_HASH, hash } from "src/utils/hash"; import { EMPTY_HASH, hash } from "src/utils/hash";
import { components } from "src/services/types.js"; import type { components } from "src/services/types.js";
export class Syncer { export class Syncer {
private database: Database; private readonly database: Database;
private syncServer: SyncService; private readonly syncServer: SyncService;
private operations: FileOperations; private readonly operations: FileOperations;
private history: SyncHistory; private readonly history: SyncHistory;
private isRunningOfflineSync = false; private isRunningOfflineSync = false;
@ -57,16 +53,16 @@ export class Syncer {
this.offlineSyncQueue.concurrency = settings.syncConcurrency; this.offlineSyncQueue.concurrency = settings.syncConcurrency;
}); });
this.fileSyncQueue.on("active", () => this.fileSyncQueue.on("active", () => {
this.emitRemainingOperationsChange( this.emitRemainingOperationsChange(
this.fileSyncQueue.size + this.offlineSyncQueue.size this.fileSyncQueue.size + this.offlineSyncQueue.size
) );
); });
this.offlineSyncQueue.on("active", () => this.offlineSyncQueue.on("active", () => {
this.emitRemainingOperationsChange( this.emitRemainingOperationsChange(
this.fileSyncQueue.size + this.offlineSyncQueue.size this.fileSyncQueue.size + this.offlineSyncQueue.size
) );
); });
} }
public addRemainingOperationsListener( public addRemainingOperationsListener(
@ -105,9 +101,7 @@ export class Syncer {
type: SyncType.CREATE, type: SyncType.CREATE,
}); });
const responseBytes = lib.base64_to_bytes( const responseBytes = lib.base64ToBytes(response.contentBase64);
response.contentBase64
);
const responseHash = hash(responseBytes); const responseHash = hash(responseBytes);
if (contentHash !== responseHash) { if (contentHash !== responseHash) {
@ -270,9 +264,7 @@ export class Syncer {
return; return;
} }
const responseBytes = lib.base64_to_bytes( const responseBytes = lib.base64ToBytes(response.contentBase64);
response.contentBase64
);
const responseHash = hash(responseBytes); const responseHash = hash(responseBytes);
if (response.relativePath != relativePath) { if (response.relativePath != relativePath) {
@ -359,7 +351,7 @@ export class Syncer {
].filter(([path, _]) => !allLocalFiles.includes(path)); ].filter(([path, _]) => !allLocalFiles.includes(path));
await Promise.all( await Promise.all(
allLocalFiles.map((relativePath) => allLocalFiles.map(async (relativePath) =>
this.offlineSyncQueue.add(async () => { this.offlineSyncQueue.add(async () => {
const metadata = const metadata =
this.database.getDocument(relativePath); this.database.getDocument(relativePath);
@ -474,7 +466,7 @@ export class Syncer {
documentId: remoteVersion.documentId, documentId: remoteVersion.documentId,
}) })
).contentBase64; ).contentBase64;
const contentBytes = lib.base64_to_bytes(content); const contentBytes = lib.base64ToBytes(content);
const contentHash = hash(contentBytes); const contentHash = hash(contentBytes);
await this.operations.create( await this.operations.create(
@ -539,7 +531,7 @@ export class Syncer {
documentId: remoteVersion.documentId, documentId: remoteVersion.documentId,
}) })
).contentBase64; ).contentBase64;
const contentBytes = lib.base64_to_bytes(content); const contentBytes = lib.base64ToBytes(content);
const contentHash = hash(contentBytes); const contentHash = hash(contentBytes);
if (relativePath !== remoteVersion.relativePath) { if (relativePath !== remoteVersion.relativePath) {
@ -592,7 +584,9 @@ export class Syncer {
await this.fileSyncQueue.onEmpty(); await this.fileSyncQueue.onEmpty();
await this.database.resetSyncState(); await this.database.resetSyncState();
this.history.reset(); this.history.reset();
this.remainingOperationsListeners.forEach((listener) => listener(0)); this.remainingOperationsListeners.forEach((listener) => {
listener(0);
});
} }
private async safelySync( private async safelySync(