Small changes
This commit is contained in:
parent
7293c58a71
commit
b52c09fecc
8 changed files with 69 additions and 292 deletions
|
|
@ -324,7 +324,7 @@ export class SyncClient {
|
||||||
await this.pause();
|
await this.pause();
|
||||||
|
|
||||||
this.logger.info("Resetting SyncClient's local state");
|
this.logger.info("Resetting SyncClient's local state");
|
||||||
this.syncEventQueue.resetState();
|
this.syncEventQueue.clearAllState();
|
||||||
await this.syncEventQueue.save();
|
await this.syncEventQueue.save();
|
||||||
this.resetInMemoryState();
|
this.resetInMemoryState();
|
||||||
this.hasFinishedOfflineSync = false;
|
this.hasFinishedOfflineSync = false;
|
||||||
|
|
|
||||||
|
|
@ -8,18 +8,12 @@
|
||||||
export const CONFLICT_PATH_REGEX =
|
export const CONFLICT_PATH_REGEX =
|
||||||
/(?:^|\/)conflict-[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}-[^/]*$/u;
|
/(?:^|\/)conflict-[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}-[^/]*$/u;
|
||||||
|
|
||||||
// Safe segment length for common filesystems (ext4 / NTFS / APFS all cap
|
|
||||||
// at 255 bytes). `conflict-<36-char-uuid>-` adds 46 bytes; reserve a few
|
|
||||||
// extra bytes for a future prefix bump and leave room for multi-byte UTF-8
|
|
||||||
// characters in the original name.
|
|
||||||
const CONFLICT_PREFIX_LEN = "conflict-".length + 36 + 1;
|
const CONFLICT_PREFIX_LEN = "conflict-".length + 36 + 1;
|
||||||
const MAX_SEGMENT_BYTES = 255;
|
const MAX_SEGMENT_BYTES = 255;
|
||||||
const MAX_ORIGINAL_BYTES = MAX_SEGMENT_BYTES - CONFLICT_PREFIX_LEN - 4;
|
const MAX_ORIGINAL_BYTES = MAX_SEGMENT_BYTES - CONFLICT_PREFIX_LEN - 4;
|
||||||
|
|
||||||
export function buildConflictFileName(fileName: string): string {
|
export function buildConflictFileName(fileName: string): string {
|
||||||
// Truncate the original name if keeping it whole would bust the
|
|
||||||
// filesystem's segment-length cap. Preserve the trailing extension
|
|
||||||
// so the file is still recognizable / openable.
|
|
||||||
const safeName = truncateFileNameToByteLimit(fileName, MAX_ORIGINAL_BYTES);
|
const safeName = truncateFileNameToByteLimit(fileName, MAX_ORIGINAL_BYTES);
|
||||||
return `conflict-${crypto.randomUUID()}-${safeName}`;
|
return `conflict-${crypto.randomUUID()}-${safeName}`;
|
||||||
}
|
}
|
||||||
|
|
@ -40,8 +34,6 @@ function truncateFileNameToByteLimit(
|
||||||
const extensionBytes = encoder.encode(extension).byteLength;
|
const extensionBytes = encoder.encode(extension).byteLength;
|
||||||
const stemBudget = Math.max(0, maxBytes - extensionBytes);
|
const stemBudget = Math.max(0, maxBytes - extensionBytes);
|
||||||
|
|
||||||
// Walk the stem by grapheme cluster so we never split an emoji sequence
|
|
||||||
// (e.g. ZWJ families, skin-tone modifiers) or a base+combining-mark pair.
|
|
||||||
const segmenter = new Intl.Segmenter(undefined, { granularity: "grapheme" });
|
const segmenter = new Intl.Segmenter(undefined, { granularity: "grapheme" });
|
||||||
let truncatedStem = "";
|
let truncatedStem = "";
|
||||||
let usedBytes = 0;
|
let usedBytes = 0;
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
import type { DocumentRecord, RelativePath } from "./types";
|
import type { DocumentRecord, DocumentWithPath, RelativePath } from "./types";
|
||||||
import { SyncEventType } from "./types";
|
import { SyncEventType } from "./types";
|
||||||
import type { Logger } from "../tracing/logger";
|
import type { Logger } from "../tracing/logger";
|
||||||
import { hash } from "../utils/hash";
|
import { hash } from "../utils/hash";
|
||||||
|
|
@ -6,23 +6,9 @@ import type { FileOperations } from "../file-operations/file-operations";
|
||||||
import { findMatchingFile } from "../utils/find-matching-file";
|
import { findMatchingFile } from "../utils/find-matching-file";
|
||||||
import { FileNotFoundError } from "../errors/file-not-found-error";
|
import { FileNotFoundError } from "../errors/file-not-found-error";
|
||||||
import type { SyncEventQueue } from "./sync-event-queue";
|
import type { SyncEventQueue } from "./sync-event-queue";
|
||||||
|
import { removeFromArray } from "../utils/remove-from-array";
|
||||||
|
|
||||||
interface DocumentWithPath {
|
|
||||||
path: RelativePath;
|
|
||||||
record: DocumentRecord;
|
|
||||||
}
|
|
||||||
|
|
||||||
interface SyncInstruction {
|
|
||||||
type: "update" | "create";
|
|
||||||
relativePath: string;
|
|
||||||
oldPath?: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
interface OfflineChangeDetectorDeps {
|
|
||||||
logger: Logger;
|
|
||||||
operations: FileOperations;
|
|
||||||
queue: SyncEventQueue;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Scans the local filesystem and the document database to determine
|
* Scans the local filesystem and the document database to determine
|
||||||
|
|
@ -30,218 +16,64 @@ interface OfflineChangeDetectorDeps {
|
||||||
* client was offline, then enqueues the appropriate sync events.
|
* client was offline, then enqueues the appropriate sync events.
|
||||||
*/
|
*/
|
||||||
export async function scheduleOfflineChanges(
|
export async function scheduleOfflineChanges(
|
||||||
deps: OfflineChangeDetectorDeps,
|
logger: Logger,
|
||||||
|
operations: FileOperations,
|
||||||
|
queue: SyncEventQueue,
|
||||||
enqueueCreate: (path: RelativePath) => void,
|
enqueueCreate: (path: RelativePath) => void,
|
||||||
enqueueUpdate: (args: { oldPath?: RelativePath; relativePath: RelativePath }) => void,
|
enqueueUpdate: (args: { oldPath?: RelativePath; relativePath: RelativePath }) => void,
|
||||||
enqueueDelete: (path: RelativePath) => void,
|
enqueueDelete: (path: RelativePath) => void,
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
const { logger, operations, queue } = deps;
|
|
||||||
|
|
||||||
const allLocalFiles = await operations.listFilesRecursively();
|
const allLocalFiles = await operations.listFilesRecursively();
|
||||||
logger.info(`Scheduling sync for ${allLocalFiles.length} local files`);
|
logger.info(`Scheduling sync for ${allLocalFiles.length} local files`);
|
||||||
|
const allDocuments = queue.allSettledDocuments();
|
||||||
|
|
||||||
queue.clear();
|
const locallyPossiblyDeletedFiles: DocumentWithPath[] = [];
|
||||||
|
|
||||||
const allDocuments = new Map(queue.allSettledDocuments());
|
for (const [path, record] of allDocuments.entries()) {
|
||||||
const locallyRenamedPaths = enqueueRenamedDocuments(deps, allDocuments);
|
if (
|
||||||
|
record !== undefined
|
||||||
const deletedCandidates = await findLocallyDeletedFiles(operations, allDocuments);
|
) {
|
||||||
|
locallyPossiblyDeletedFiles.push({ path, record });
|
||||||
const instructions = await buildSyncInstructions(
|
|
||||||
deps,
|
|
||||||
allLocalFiles,
|
|
||||||
locallyRenamedPaths,
|
|
||||||
deletedCandidates,
|
|
||||||
);
|
|
||||||
|
|
||||||
// Enqueue deletes first
|
|
||||||
for (const { path } of deletedCandidates) {
|
|
||||||
logger.debug(`Document ${path} has been deleted locally, scheduling sync to delete it`);
|
|
||||||
enqueueDelete(path);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Then updates/moves
|
|
||||||
for (const instruction of instructions) {
|
|
||||||
if (instruction.type === "update") {
|
|
||||||
enqueueUpdate({
|
|
||||||
oldPath: instruction.oldPath,
|
|
||||||
relativePath: instruction.relativePath,
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Creates last so the server can merge with existing documents
|
const locallyPossibleCreatedFiles: RelativePath[] = [];
|
||||||
for (const instruction of instructions) {
|
const syncedLocalFiles: RelativePath[] = [];
|
||||||
if (instruction.type === "create") {
|
|
||||||
enqueueCreate(instruction.relativePath);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function enqueueRenamedDocuments(
|
for (const localFile of allLocalFiles) {
|
||||||
{ queue, logger }: OfflineChangeDetectorDeps,
|
if (allDocuments.has(localFile)
|
||||||
allDocuments: Map<RelativePath, DocumentRecord>,
|
) {
|
||||||
): Set<RelativePath> {
|
syncedLocalFiles.push(localFile);
|
||||||
const locallyRenamedPaths = new Set<RelativePath>();
|
} else {
|
||||||
|
locallyPossibleCreatedFiles.push(localFile);
|
||||||
for (const [path, record] of allDocuments) {
|
|
||||||
const remoteRelPath = record.remoteRelativePath;
|
|
||||||
const hasLocalRename = remoteRelPath !== undefined && remoteRelPath !== path;
|
|
||||||
|
|
||||||
if (hasLocalRename) {
|
|
||||||
queue.enqueue({ type: SyncEventType.LocalUpdate, path });
|
|
||||||
locallyRenamedPaths.add(path);
|
|
||||||
logger.debug(`Document ${path} was renamed locally (from ${remoteRelPath}), scheduling sync`);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return locallyRenamedPaths;
|
for (const path of locallyPossibleCreatedFiles) {
|
||||||
}
|
const content = await operations.read(path);
|
||||||
|
const contentHash = await hash(content);
|
||||||
async function findLocallyDeletedFiles(
|
|
||||||
operations: FileOperations,
|
|
||||||
allDocuments: Map<RelativePath, DocumentRecord>,
|
|
||||||
): Promise<DocumentWithPath[]> {
|
|
||||||
const result: DocumentWithPath[] = [];
|
|
||||||
|
|
||||||
for (const [path, record] of allDocuments) {
|
|
||||||
if (!(await operations.exists(path))) {
|
|
||||||
result.push({ path, record });
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
async function buildSyncInstructions(
|
|
||||||
deps: OfflineChangeDetectorDeps,
|
|
||||||
allLocalFiles: RelativePath[],
|
|
||||||
locallyRenamedPaths: Set<RelativePath>,
|
|
||||||
deletedCandidates: DocumentWithPath[],
|
|
||||||
): Promise<SyncInstruction[]> {
|
|
||||||
const { logger, operations, queue } = deps;
|
|
||||||
const instructions: SyncInstruction[] = [];
|
|
||||||
|
|
||||||
for (const relativePath of allLocalFiles) {
|
|
||||||
if (locallyRenamedPaths.has(relativePath)) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
const existingRecord = queue.getSettledDocumentByPath(relativePath);
|
|
||||||
|
|
||||||
if (existingRecord !== undefined) {
|
|
||||||
const result = await handleExistingDocument(
|
|
||||||
deps,
|
|
||||||
relativePath,
|
|
||||||
existingRecord,
|
|
||||||
deletedCandidates,
|
|
||||||
);
|
|
||||||
if (result !== undefined) {
|
|
||||||
if (result.updatedDeletedCandidates !== undefined) {
|
|
||||||
deletedCandidates = result.updatedDeletedCandidates;
|
|
||||||
}
|
|
||||||
if (result.instruction !== undefined) {
|
|
||||||
instructions.push(result.instruction);
|
|
||||||
}
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
const matchingDeletedFile = await findMatchingFile(contentHash, locallyPossiblyDeletedFiles);
|
||||||
|
if (matchingDeletedFile !== undefined) {
|
||||||
logger.debug(
|
logger.debug(
|
||||||
`Document ${relativePath} might have been updated locally, scheduling sync to validate and update it`,
|
`File ${path} might have been moved from ${matchingDeletedFile.path} while offline, scheduling sync to move it`,
|
||||||
);
|
);
|
||||||
instructions.push({ type: "update", relativePath });
|
enqueueUpdate({ oldPath: matchingDeletedFile.path, relativePath: path });
|
||||||
continue;
|
removeFromArray(locallyPossiblyDeletedFiles, matchingDeletedFile);
|
||||||
|
removeFromArray(locallyPossibleCreatedFiles, path);
|
||||||
}
|
}
|
||||||
|
|
||||||
const result = await handleNewFile(deps, relativePath, deletedCandidates);
|
|
||||||
if (result.updatedDeletedCandidates !== undefined) {
|
|
||||||
deletedCandidates = result.updatedDeletedCandidates;
|
|
||||||
}
|
|
||||||
instructions.push(result.instruction);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return instructions;
|
for (const path of locallyPossibleCreatedFiles) {
|
||||||
}
|
logger.debug(`File ${path} was created while offline, scheduling sync to create it`);
|
||||||
|
enqueueCreate(path);
|
||||||
async function handleExistingDocument(
|
}
|
||||||
{ logger, operations }: OfflineChangeDetectorDeps,
|
|
||||||
relativePath: RelativePath,
|
for (const item of locallyPossiblyDeletedFiles) {
|
||||||
existingRecord: DocumentRecord,
|
enqueueDelete(item.path);
|
||||||
deletedCandidates: DocumentWithPath[],
|
}
|
||||||
): Promise<
|
|
||||||
| { instruction?: SyncInstruction; updatedDeletedCandidates?: DocumentWithPath[] }
|
for (const path of syncedLocalFiles) {
|
||||||
| undefined
|
enqueueUpdate({ relativePath: path });
|
||||||
> {
|
}
|
||||||
if (deletedCandidates.length === 0) {
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
|
|
||||||
let contentHash: string | undefined;
|
|
||||||
try {
|
|
||||||
const bytes = await operations.read(relativePath);
|
|
||||||
contentHash = await hash(bytes);
|
|
||||||
} catch (e) {
|
|
||||||
if (e instanceof FileNotFoundError) return { instruction: undefined };
|
|
||||||
throw e;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (contentHash === existingRecord.remoteHash) {
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
|
|
||||||
const originalFile = await findMatchingFile(contentHash, deletedCandidates);
|
|
||||||
if (originalFile === undefined) {
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
|
|
||||||
// This file was moved here from a different path, displacing the existing document
|
|
||||||
const updatedDeletedCandidates = [
|
|
||||||
...deletedCandidates.filter((item) => item.path !== originalFile.path),
|
|
||||||
{ path: relativePath, record: existingRecord },
|
|
||||||
];
|
|
||||||
|
|
||||||
logger.debug(
|
|
||||||
`Document '${originalFile.path}' was moved to ${relativePath} (displacing existing document), scheduling sync to move it`,
|
|
||||||
);
|
|
||||||
|
|
||||||
return {
|
|
||||||
instruction: { type: "update", oldPath: originalFile.path, relativePath },
|
|
||||||
updatedDeletedCandidates,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
async function handleNewFile(
|
|
||||||
{ logger, operations }: OfflineChangeDetectorDeps,
|
|
||||||
relativePath: RelativePath,
|
|
||||||
deletedCandidates: DocumentWithPath[],
|
|
||||||
): Promise<{ instruction: SyncInstruction; updatedDeletedCandidates?: DocumentWithPath[] }> {
|
|
||||||
let contentHash: string | undefined;
|
|
||||||
try {
|
|
||||||
const contentBytes = await operations.read(relativePath);
|
|
||||||
contentHash = await hash(contentBytes);
|
|
||||||
} catch (e) {
|
|
||||||
if (e instanceof FileNotFoundError) {
|
|
||||||
return { instruction: { type: "create", relativePath } };
|
|
||||||
}
|
|
||||||
throw e;
|
|
||||||
}
|
|
||||||
|
|
||||||
const originalFile = await findMatchingFile(contentHash, deletedCandidates);
|
|
||||||
if (originalFile !== undefined) {
|
|
||||||
const updatedDeletedCandidates = deletedCandidates.filter(
|
|
||||||
(item) => item.path !== originalFile.path,
|
|
||||||
);
|
|
||||||
|
|
||||||
logger.debug(
|
|
||||||
`Document '${originalFile.path}' was not found under its current path in the database but was found under a different path (${relativePath}), scheduling sync to move it`,
|
|
||||||
);
|
|
||||||
|
|
||||||
return {
|
|
||||||
instruction: { type: "update", oldPath: originalFile.path, relativePath },
|
|
||||||
updatedDeletedCandidates,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
logger.debug(`Document ${relativePath} not found in database, scheduling sync to create it`);
|
|
||||||
return { instruction: { type: SyncEventType.LocalCreate, relativePath } };
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -316,7 +316,7 @@ describe("SyncEventQueue", () => {
|
||||||
assert.ok(promiseA !== undefined);
|
assert.ok(promiseA !== undefined);
|
||||||
assert.ok(promiseB !== undefined);
|
assert.ok(promiseB !== undefined);
|
||||||
|
|
||||||
queue.clear();
|
queue.clearPending();
|
||||||
|
|
||||||
await assert.rejects(promiseA);
|
await assert.rejects(promiseA);
|
||||||
await assert.rejects(promiseB);
|
await assert.rejects(promiseB);
|
||||||
|
|
@ -360,7 +360,7 @@ describe("SyncEventQueue", () => {
|
||||||
|
|
||||||
assert.strictEqual(queue.pendingUpdateCount, 2);
|
assert.strictEqual(queue.pendingUpdateCount, 2);
|
||||||
|
|
||||||
queue.clear();
|
queue.clearPending();
|
||||||
|
|
||||||
assert.strictEqual(queue.pendingUpdateCount, 0);
|
assert.strictEqual(queue.pendingUpdateCount, 0);
|
||||||
assert.strictEqual(queue.syncedDocumentCount, 1);
|
assert.strictEqual(queue.syncedDocumentCount, 1);
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,7 @@ import { globsToRegexes } from "../utils/globs-to-regexes";
|
||||||
import { CONFLICT_PATH_REGEX } from "./conflict-path";
|
import { CONFLICT_PATH_REGEX } from "./conflict-path";
|
||||||
import { removeFromArray } from "../utils/remove-from-array";
|
import { removeFromArray } from "../utils/remove-from-array";
|
||||||
import {
|
import {
|
||||||
|
DocumentWithPath,
|
||||||
SyncEventType,
|
SyncEventType,
|
||||||
type DocumentId,
|
type DocumentId,
|
||||||
type DocumentRecord,
|
type DocumentRecord,
|
||||||
|
|
@ -50,7 +51,7 @@ export class SyncEventQueue {
|
||||||
private readonly saveData: (data: StoredSyncState) => Promise<void>
|
private readonly saveData: (data: StoredSyncState) => Promise<void>
|
||||||
) {
|
) {
|
||||||
this.ignorePatterns = [
|
this.ignorePatterns = [
|
||||||
CONFLICT_PATH_REGEX,
|
CONFLICT_PATH_REGEX, // conflict paths need to be resolved before they can be synced again
|
||||||
...globsToRegexes(
|
...globsToRegexes(
|
||||||
this.settings.getSettings().ignorePatterns,
|
this.settings.getSettings().ignorePatterns,
|
||||||
this.logger
|
this.logger
|
||||||
|
|
@ -85,12 +86,7 @@ export class SyncEventQueue {
|
||||||
}
|
}
|
||||||
|
|
||||||
public enqueue(input: FileSyncEvent): void {
|
public enqueue(input: FileSyncEvent): void {
|
||||||
if (input.type === SyncEventType.RemoteChange) {
|
const path = (input.type === SyncEventType.RemoteChange) ? input.remoteVersion.relativePath : input.path;
|
||||||
this.events.push(input);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const { path } = input;
|
|
||||||
|
|
||||||
if (this.ignorePatterns.some((pattern) => pattern.test(path))) {
|
if (this.ignorePatterns.some((pattern) => pattern.test(path))) {
|
||||||
this.logger.info(
|
this.logger.info(
|
||||||
|
|
@ -99,6 +95,12 @@ export class SyncEventQueue {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (input.type === SyncEventType.RemoteChange) {
|
||||||
|
this.events.push(input);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
if (input.type === SyncEventType.LocalCreate) {
|
if (input.type === SyncEventType.LocalCreate) {
|
||||||
this.events.push({ type: SyncEventType.LocalCreate, path, originalPath: path, resolvers: Promise.withResolvers() });
|
this.events.push({ type: SyncEventType.LocalCreate, path, originalPath: path, resolvers: Promise.withResolvers() });
|
||||||
return;
|
return;
|
||||||
|
|
@ -111,7 +113,6 @@ export class SyncEventQueue {
|
||||||
|
|
||||||
if (documentId === undefined) {
|
if (documentId === undefined) {
|
||||||
// we can get here when deleting a local document after a remote update
|
// we can get here when deleting a local document after a remote update
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -173,7 +174,7 @@ export class SyncEventQueue {
|
||||||
|
|
||||||
public getDocumentByDocumentId(
|
public getDocumentByDocumentId(
|
||||||
target: DocumentId
|
target: DocumentId
|
||||||
): { path: RelativePath; record: DocumentRecord } | undefined {
|
): DocumentWithPath | undefined {
|
||||||
for (const [path, record] of this.documents) {
|
for (const [path, record] of this.documents) {
|
||||||
if (record.documentId === target) {
|
if (record.documentId === target) {
|
||||||
return { path, record };
|
return { path, record };
|
||||||
|
|
@ -186,7 +187,7 @@ export class SyncEventQueue {
|
||||||
|
|
||||||
public getDocumentByDocumentIdOrFail(
|
public getDocumentByDocumentIdOrFail(
|
||||||
target: DocumentId
|
target: DocumentId
|
||||||
): { path: RelativePath; record: DocumentRecord } {
|
): DocumentWithPath {
|
||||||
const result = this.getDocumentByDocumentId(target);
|
const result = this.getDocumentByDocumentId(target);
|
||||||
if (!result) {
|
if (!result) {
|
||||||
throw new Error(`No document found with id ${target}`);
|
throw new Error(`No document found with id ${target}`);
|
||||||
|
|
@ -215,48 +216,10 @@ export class SyncEventQueue {
|
||||||
return this.documents.get(path);
|
return this.documents.get(path);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public allSettledDocuments(): Map<RelativePath, DocumentRecord> {
|
||||||
|
return new Map(this.documents.entries());
|
||||||
|
|
||||||
|
|
||||||
public allSettledDocuments(): [RelativePath, DocumentRecord][] {
|
|
||||||
return Array.from(this.documents.entries());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns the set of paths we expect to exist on disk by replaying
|
|
||||||
* the event queue on top of the settled documents map.
|
|
||||||
*/
|
|
||||||
public trackedPaths(): Set<RelativePath> {
|
|
||||||
const paths = new Set(this.documents.keys());
|
|
||||||
// Track current path for each pending create so moves can be applied
|
|
||||||
const pendingPaths = new Map<Promise<DocumentId>, RelativePath>();
|
|
||||||
|
|
||||||
for (const event of this.events) {
|
|
||||||
if (event.type === SyncEventType.LocalCreate) {
|
|
||||||
paths.add(event.path);
|
|
||||||
if (event.resolvers !== undefined) {
|
|
||||||
pendingPaths.set(event.resolvers.promise, event.path);
|
|
||||||
}
|
|
||||||
} else if (event.type === SyncEventType.LocalDelete) {
|
|
||||||
if (typeof event.documentId === "string") {
|
|
||||||
const path = this.getDocumentByDocumentId(event.documentId)?.path;
|
|
||||||
if (path) {
|
|
||||||
paths.delete(path);
|
|
||||||
} else {
|
|
||||||
throw new Error(`Delete event for unknown documentId ${event.documentId}`);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
const path = pendingPaths.get(event.documentId);
|
|
||||||
if (!path) {
|
|
||||||
throw new Error(`Delete event with unresolved documentId promise`);
|
|
||||||
}
|
|
||||||
paths.delete(path);
|
|
||||||
}
|
|
||||||
} // no need to handle SyncLocal as path updates are applied to this.documents immediately when the event is enqueued
|
|
||||||
}
|
|
||||||
return paths;
|
|
||||||
}
|
|
||||||
|
|
||||||
public hasPendingEventsForPath(path: RelativePath): boolean {
|
public hasPendingEventsForPath(path: RelativePath): boolean {
|
||||||
const record = this.documents.get(path);
|
const record = this.documents.get(path);
|
||||||
|
|
@ -288,36 +251,19 @@ export class SyncEventQueue {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public resetState(): void {
|
public async clearAllState(): Promise<void> {
|
||||||
this.rejectAllPendingCreates();
|
this.clearPending();
|
||||||
this.documents.clear();
|
this.documents.clear();
|
||||||
this.saveInTheBackground();
|
this.lastSeenUpdateId = -1;
|
||||||
|
await this.save();
|
||||||
}
|
}
|
||||||
|
|
||||||
public clear(): void {
|
public clearPending(): void {
|
||||||
this.rejectAllPendingCreates();
|
this.rejectAllPendingCreates();
|
||||||
this.events.length = 0;
|
this.events.length = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public removeAllEventsForDocumentId(documentId: DocumentId): void {
|
|
||||||
for (let i = this.events.length - 1; i >= 0; i--) {
|
|
||||||
const e = this.events[i];
|
|
||||||
if (
|
|
||||||
(e.type === SyncEventType.LocalUpdate &&
|
|
||||||
e.documentId === documentId) ||
|
|
||||||
(e.type === SyncEventType.RemoteChange &&
|
|
||||||
e.remoteVersion.documentId === documentId) ||
|
|
||||||
(e.type === SyncEventType.LocalDelete &&
|
|
||||||
e.documentId === documentId)
|
|
||||||
) {
|
|
||||||
// eslint-disable-next-line no-restricted-syntax -- Bulk removal by predicate, not single-item removal
|
|
||||||
this.events.splice(i, 1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private updatePendingCreatePath(
|
private updatePendingCreatePath(
|
||||||
oldPath: RelativePath,
|
oldPath: RelativePath,
|
||||||
newPath: RelativePath
|
newPath: RelativePath
|
||||||
|
|
|
||||||
|
|
@ -154,7 +154,7 @@ export class Syncer {
|
||||||
|
|
||||||
public reset(): void {
|
public reset(): void {
|
||||||
this._isFirstSyncStarted = false;
|
this._isFirstSyncStarted = false;
|
||||||
this.queue.clear();
|
this.queue.clearPending();
|
||||||
// Don't null the reference synchronously — if the scan is
|
// Don't null the reference synchronously — if the scan is
|
||||||
// still in flight, the next reconnect would spawn a second
|
// still in flight, the next reconnect would spawn a second
|
||||||
// concurrent scan racing on the same queue. Defer the
|
// concurrent scan racing on the same queue. Defer the
|
||||||
|
|
|
||||||
|
|
@ -11,6 +11,11 @@ export interface DocumentRecord {
|
||||||
remoteRelativePath: RelativePath;
|
remoteRelativePath: RelativePath;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface DocumentWithPath {
|
||||||
|
path: RelativePath;
|
||||||
|
record: DocumentRecord;
|
||||||
|
}
|
||||||
|
|
||||||
export interface StoredDocument extends DocumentRecord {
|
export interface StoredDocument extends DocumentRecord {
|
||||||
relativePath: RelativePath;
|
relativePath: RelativePath;
|
||||||
}
|
}
|
||||||
|
|
@ -29,7 +34,9 @@ export enum SyncEventType {
|
||||||
|
|
||||||
export type FileSyncEvent =
|
export type FileSyncEvent =
|
||||||
| { type: SyncEventType.LocalCreate; path: RelativePath }
|
| { type: SyncEventType.LocalCreate; path: RelativePath }
|
||||||
| { type: SyncEventType.LocalUpdate; path: RelativePath; oldPath?: RelativePath }
|
| {
|
||||||
|
type: SyncEventType.LocalUpdate; path: RelativePath; oldPath?: RelativePath // oldPath is undefined for content changes
|
||||||
|
}
|
||||||
| { type: SyncEventType.LocalDelete; path: RelativePath }
|
| { type: SyncEventType.LocalDelete; path: RelativePath }
|
||||||
| { type: SyncEventType.RemoteChange; remoteVersion: DocumentVersionWithoutContent };
|
| { type: SyncEventType.RemoteChange; remoteVersion: DocumentVersionWithoutContent };
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,11 @@
|
||||||
import type { DocumentRecord, RelativePath } from "../sync-operations/types";
|
import type { DocumentRecord, DocumentWithPath, RelativePath } from "../sync-operations/types";
|
||||||
import { EMPTY_HASH } from "./hash";
|
import { EMPTY_HASH } from "./hash";
|
||||||
|
|
||||||
// TODO: make this smarter so that offline files can be renamed & edited at the same time
|
// TODO: make this smarter so that offline files can be renamed & edited at the same time
|
||||||
export async function findMatchingFile(
|
export async function findMatchingFile(
|
||||||
contentHash: string,
|
contentHash: string,
|
||||||
candidates: { path: RelativePath; record: DocumentRecord }[]
|
candidates: { path: RelativePath; record: DocumentRecord }[]
|
||||||
): Promise<{ path: RelativePath; record: DocumentRecord } | undefined> {
|
): Promise<DocumentWithPath | undefined> {
|
||||||
if (contentHash === await EMPTY_HASH) {
|
if (contentHash === await EMPTY_HASH) {
|
||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue