This commit is contained in:
Andras Schmelczer 2025-03-15 15:17:21 +00:00
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", { element.createEl("span", {
text: entry.relativePath text: entry.relativePath
}); });

View file

@ -33,4 +33,4 @@
"webpack-merge": "^6.0.1", "webpack-merge": "^6.0.1",
"sync_lib": "file:../../backend/sync_lib/pkg" "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. // All parent directories are created if they don't exist.
public async create( public async create(
path: RelativePath, path: RelativePath,
newContent: Uint8Array, newContent: Uint8Array
whatevs?: any
): Promise<void> { ): Promise<void> {
this.logger.debug(`Creating file: ${path}`); 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)) { if (await this.fs.exists(path)) {
const deconflictedPath = await this.deconflictPath(path); const deconflictedPath = await this.deconflictPath(path);
this.logger.debug( this.logger.debug(
@ -68,10 +72,6 @@ export class FileOperations {
} else { } else {
await this.createParentDirectories(path); await this.createParentDirectories(path);
} }
whatevs?.();
await this.fs.write(path, newContent);
} }
// Update the file at the given path. // Update the file at the given path.
@ -143,19 +143,7 @@ export class FileOperations {
return; return;
} }
if (await this.fs.exists(newPath)) { await this.ensureClearPath(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.fs.rename(oldPath, newPath); await this.fs.rename(oldPath, newPath);
} }
@ -198,7 +186,7 @@ export class FileOperations {
); );
stem = stem.replace(FileOperations.PARENTHESES_REGEX, ""); stem = stem.replace(FileOperations.PARENTHESES_REGEX, "");
let newName; let newName = path;
do { do {
currentCount++; currentCount++;
newName = `${directory}${stem} (${currentCount})${extension}`; newName = `${directory}${stem} (${currentCount})${extension}`;

View file

@ -130,12 +130,11 @@ export class Database {
}, },
identity?: symbol identity?: symbol
): void { ): void {
let entry: DocumentRecord | undefined;
if (identity !== undefined) { if (identity !== undefined) {
const entry = this.getDocumentByIdentity(identity); const entry = this.getDocumentByIdentity(identity);
this.documents = this.documents.filter( this.documents = this.documents.filter(
({ identity }) => identity !== entry.identity (doc) => doc.identity !== entry.identity
); );
if (entry.relativePath !== relativePath) { 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 // 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 // meaning that two documents occupy the same path in terms of in-flight requests so we
// need to create a new parallel version. // need to create a new parallel version.
entry = this.getLatestDocumentByRelativePath(relativePath); const entry = this.getLatestDocumentByRelativePath(relativePath);
if (entry && entry.documentId !== documentId) { if (entry && entry.documentId !== documentId) {
this.documents.push({ this.documents.push({
// `entry` might be undefined if the document is new // `entry` might be undefined if the document is new
@ -238,7 +237,8 @@ export class Database {
relativePath: RelativePath, relativePath: RelativePath,
promise: Promise<void> promise: Promise<void>
): void { ): void {
const previousEntry = this.getLatestDocumentByRelativePath(relativePath); const previousEntry =
this.getLatestDocumentByRelativePath(relativePath);
const entry = { const entry = {
relativePath, relativePath,
@ -300,7 +300,8 @@ export class Database {
({ identity }) => identity !== oldDocument.identity ({ identity }) => identity !== oldDocument.identity
); );
const newDocument = this.getLatestDocumentByRelativePath(newRelativePath); const newDocument =
this.getLatestDocumentByRelativePath(newRelativePath);
if (newDocument !== undefined && !newDocument.isDeleted) { if (newDocument !== undefined && !newDocument.isDeleted) {
throw new Error( throw new Error(
`Document already exists at new location: ${newRelativePath}` `Document already exists at new location: ${newRelativePath}`
@ -338,11 +339,13 @@ export class Database {
this.ensureConsistency(); this.ensureConsistency();
void this.saveData({ void this.saveData({
documents: this.resolvedDocuments.map( documents: this.resolvedDocuments.map(
({ relativePath, metadata }) => ({ ({ relativePath, documentId, metadata }) => ({
documentId,
relativePath, 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 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( public getFetchImplementation(
fetch: typeof globalThis.fetch, fetch: typeof globalThis.fetch,
{ doRetries = true }: { doRetries: boolean } = { doRetries: true } { doRetries = true }: { doRetries: boolean } = { doRetries: true }
@ -48,4 +38,14 @@ export class ConnectedState {
return retriedFetch(input); 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.concurrency = newSettings.syncConcurrency;
}); });
this.syncQueue.on("active", () => this.syncQueue.on("active", () => {
{ this.remainingOperationsListeners.forEach((listener) => this.remainingOperationsListeners.forEach((listener) => {
{ listener(this.syncQueue.size); } listener(this.syncQueue.size);
); } });
); });
this.internalSyncer = new UnrestrictedSyncer( this.internalSyncer = new UnrestrictedSyncer(
logger, logger,
@ -261,7 +261,9 @@ export class Syncer {
public async reset(): Promise<void> { public async reset(): Promise<void> {
this.syncQueue.clear(); this.syncQueue.clear();
await this.syncQueue.onEmpty(); await this.syncQueue.onEmpty();
this.remainingOperationsListeners.forEach((listener) => { listener(0); }); this.remainingOperationsListeners.forEach((listener) => {
listener(0);
});
this.internalSyncer.reset(); this.internalSyncer.reset();
} }

View file

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

View file

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

View file

@ -23,9 +23,10 @@ export class MockClient implements FileSystemOperations {
await Promise.all( await Promise.all(
Object.keys(this.initialSettings).map(async (key) => { 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( return this.client.settings.setSetting(
key as keyof SyncSettings, settingKey,
this.initialSettings[key as keyof SyncSettings]! 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()); const newParts = newContent.split(" ").map((part) => part.trim());
existingParts.forEach((part) => existingParts.forEach((part) =>
// all changes should be additive // all changes should be additive
{ assert( {
newParts.includes(part), assert(
`Part ${part} not found in new content` newParts.includes(part),
); } `Part ${part} not found in new content`
);
}
); );
this.client.logger.info( this.client.logger.info(