This commit is contained in:
Andras Schmelczer 2025-03-15 15:17:21 +00:00
parent 15a09dff95
commit 826e391f87
No known key found for this signature in database
GPG key ID: FC8F2C3D3D1A718C
10 changed files with 652 additions and 654 deletions

View file

@ -60,7 +60,6 @@ export class HistoryView extends ItemView {
}
element.createEl("span", {
text: entry.relativePath
});

View file

@ -33,4 +33,4 @@
"webpack-merge": "^6.0.1",
"sync_lib": "file:../../backend/sync_lib/pkg"
}
}
}

View file

@ -53,10 +53,14 @@ export class FileOperations {
// All parent directories are created if they don't exist.
public async create(
path: RelativePath,
newContent: Uint8Array,
whatevs?: any
newContent: Uint8Array
): Promise<void> {
this.logger.debug(`Creating file: ${path}`);
await this.fs.write(path, newContent);
}
public async ensureClearPath(path: RelativePath): Promise<void> {
if (await this.fs.exists(path)) {
const deconflictedPath = await this.deconflictPath(path);
this.logger.debug(
@ -68,10 +72,6 @@ export class FileOperations {
} else {
await this.createParentDirectories(path);
}
whatevs?.();
await this.fs.write(path, newContent);
}
// Update the file at the given path.
@ -143,19 +143,7 @@ export class FileOperations {
return;
}
if (await this.fs.exists(newPath)) {
const deconflictedPath = await this.deconflictPath(newPath);
this.logger.debug(
`Conflict when moving '${oldPath}' to '${newPath}', the latter already exists, deconflicting by moving it to '${deconflictedPath}'`
);
// this.database.move(newPath, deconflictedPath);
// this.database.move(oldPath, newPath);
await this.fs.rename(newPath, deconflictedPath);
} else {
// this.database.move(oldPath, newPath);
await this.createParentDirectories(newPath);
}
await this.ensureClearPath(newPath);
await this.fs.rename(oldPath, newPath);
}
@ -198,7 +186,7 @@ export class FileOperations {
);
stem = stem.replace(FileOperations.PARENTHESES_REGEX, "");
let newName;
let newName = path;
do {
currentCount++;
newName = `${directory}${stem} (${currentCount})${extension}`;

View file

@ -130,12 +130,11 @@ export class Database {
},
identity?: symbol
): void {
let entry: DocumentRecord | undefined;
if (identity !== undefined) {
const entry = this.getDocumentByIdentity(identity);
this.documents = this.documents.filter(
({ identity }) => identity !== entry.identity
(doc) => doc.identity !== entry.identity
);
if (entry.relativePath !== relativePath) {
@ -161,7 +160,7 @@ export class Database {
// We find a match based on relative path and we find one with a different document id
// meaning that two documents occupy the same path in terms of in-flight requests so we
// need to create a new parallel version.
entry = this.getLatestDocumentByRelativePath(relativePath);
const entry = this.getLatestDocumentByRelativePath(relativePath);
if (entry && entry.documentId !== documentId) {
this.documents.push({
// `entry` might be undefined if the document is new
@ -238,7 +237,8 @@ export class Database {
relativePath: RelativePath,
promise: Promise<void>
): void {
const previousEntry = this.getLatestDocumentByRelativePath(relativePath);
const previousEntry =
this.getLatestDocumentByRelativePath(relativePath);
const entry = {
relativePath,
@ -300,7 +300,8 @@ export class Database {
({ identity }) => identity !== oldDocument.identity
);
const newDocument = this.getLatestDocumentByRelativePath(newRelativePath);
const newDocument =
this.getLatestDocumentByRelativePath(newRelativePath);
if (newDocument !== undefined && !newDocument.isDeleted) {
throw new Error(
`Document already exists at new location: ${newRelativePath}`
@ -338,11 +339,13 @@ export class Database {
this.ensureConsistency();
void this.saveData({
documents: this.resolvedDocuments.map(
({ relativePath, metadata }) => ({
({ relativePath, documentId, metadata }) => ({
documentId,
relativePath,
...metadata
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
...metadata! // resolvedDocuments only returns docs with metadata set
})
) as StoredDocumentMetadata[],
),
lastSeenUpdateId: this.lastSeenUpdateId
});
}

View file

@ -23,16 +23,6 @@ export class ConnectedState {
});
}
private handleComingOnline() {
this.logger.debug("Sync is enabled");
this.resolveIsSyncEnabled?.();
}
private handleGoingOffline() {
this.logger.debug("Sync is disabled");
[this.syncIsEnabled, this.resolveIsSyncEnabled] = createPromise();
}
public getFetchImplementation(
fetch: typeof globalThis.fetch,
{ doRetries = true }: { doRetries: boolean } = { doRetries: true }
@ -48,4 +38,14 @@ export class ConnectedState {
return retriedFetch(input);
};
}
private handleComingOnline(): void {
this.logger.debug("Sync is enabled");
this.resolveIsSyncEnabled?.();
}
private handleGoingOffline(): void {
this.logger.debug("Sync is disabled");
[this.syncIsEnabled, this.resolveIsSyncEnabled] = createPromise();
}
}

File diff suppressed because it is too large Load diff

View file

@ -44,11 +44,11 @@ export class Syncer {
this.syncQueue.concurrency = newSettings.syncConcurrency;
});
this.syncQueue.on("active", () =>
{ this.remainingOperationsListeners.forEach((listener) =>
{ listener(this.syncQueue.size); }
); }
);
this.syncQueue.on("active", () => {
this.remainingOperationsListeners.forEach((listener) => {
listener(this.syncQueue.size);
});
});
this.internalSyncer = new UnrestrictedSyncer(
logger,
@ -261,7 +261,9 @@ export class Syncer {
public async reset(): Promise<void> {
this.syncQueue.clear();
await this.syncQueue.onEmpty();
this.remainingOperationsListeners.forEach((listener) => { listener(0); });
this.remainingOperationsListeners.forEach((listener) => {
listener(0);
});
this.internalSyncer.reset();
}

View file

@ -288,9 +288,7 @@ export class UnrestrictedSyncer {
async () => {
let localMetadata = getLatestDocument();
if (
localMetadata?.metadata !== undefined
) {
if (localMetadata?.metadata !== undefined) {
// If the file exists locally, let's pretend the user has updated it
// and deal with remote update/deletion within `unrestrictedSyncLocallyUpdatedFile`
if (
@ -306,6 +304,7 @@ export class UnrestrictedSyncer {
return this.unrestrictedSyncLocallyUpdatedFile({
getLatestDocument: () =>
this.database.getDocumentByIdentity(
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
localMetadata!.identity
)
});
@ -334,8 +333,8 @@ export class UnrestrictedSyncer {
}
if (
localMetadata?.metadata?.parentVersionId ??
-1 >= remoteVersion.vaultUpdateId
(localMetadata?.metadata?.parentVersionId ?? -1) >=
remoteVersion.vaultUpdateId
) {
this.logger.debug(
`Document ${remoteVersion.relativePath} is already more up to date than the fetched version`
@ -347,15 +346,19 @@ export class UnrestrictedSyncer {
const [promise, resolve] = createPromise();
await this.operations.ensureClearPath(
remoteVersion.relativePath
);
this.database.getNewResolvedDocumentByRelativePath(
remoteVersion.documentId,
remoteVersion.relativePath,
promise
);
await this.operations.create(
remoteVersion.relativePath,
contentBytes,
() =>
{ this.database.getNewResolvedDocumentByRelativePath(
remoteVersion.documentId,
remoteVersion.relativePath,
promise
); }
contentBytes
);
const document =

View file

@ -6,12 +6,10 @@
"allowSyntheticDefaultImports": true,
"moduleResolution": "bundler",
"lib": [
"DOM", // to get "fetch"
"DOM" // to get "fetch"
],
"declaration": true,
"declarationDir": "./dist/types"
},
"exclude": [
"./dist"
]
}
"exclude": ["./dist"]
}

View file

@ -23,9 +23,10 @@ export class MockClient implements FileSystemOperations {
await Promise.all(
Object.keys(this.initialSettings).map(async (key) => {
const settingKey = key as keyof SyncSettings; // eslint-disable-line @typescript-eslint/no-unsafe-type-assertion
return this.client.settings.setSetting(
key as keyof SyncSettings,
this.initialSettings[key as keyof SyncSettings]!
settingKey,
this.initialSettings[settingKey]! // eslint-disable-line @typescript-eslint/no-non-null-assertion
);
})
);
@ -88,10 +89,12 @@ export class MockClient implements FileSystemOperations {
const newParts = newContent.split(" ").map((part) => part.trim());
existingParts.forEach((part) =>
// all changes should be additive
{ assert(
newParts.includes(part),
`Part ${part} not found in new content`
); }
{
assert(
newParts.includes(part),
`Part ${part} not found in new content`
);
}
);
this.client.logger.info(