Lint
This commit is contained in:
parent
9f46af4a65
commit
cd70f8b426
3 changed files with 76 additions and 75 deletions
|
|
@ -2,7 +2,7 @@ import type { FileSystemOperations } from "dist/types";
|
||||||
import type { RelativePath } from "src/persistence/database";
|
import type { RelativePath } from "src/persistence/database";
|
||||||
|
|
||||||
export class FileNotFoundError extends Error {
|
export class FileNotFoundError extends Error {
|
||||||
constructor(message: string) {
|
public constructor(message: string) {
|
||||||
super(message);
|
super(message);
|
||||||
this.name = "FileNotFoundError";
|
this.name = "FileNotFoundError";
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -95,16 +95,6 @@ export class Syncer {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
private async syncRemotelyUpdatedFile(
|
|
||||||
remoteVersion: components["schemas"]["DocumentVersionWithoutContent"]
|
|
||||||
): Promise<void> {
|
|
||||||
await this.syncQueue.add(async () =>
|
|
||||||
this.internalSyncer.unrestrictedSyncRemotelyUpdatedFile(
|
|
||||||
remoteVersion
|
|
||||||
)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
public async scheduleSyncForOfflineChanges(): Promise<void> {
|
public async scheduleSyncForOfflineChanges(): Promise<void> {
|
||||||
if (!this.settings.getSettings().isSyncEnabled) {
|
if (!this.settings.getSettings().isSyncEnabled) {
|
||||||
this.logger.debug(
|
this.logger.debug(
|
||||||
|
|
@ -133,6 +123,52 @@ export class Syncer {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async applyRemoteChangesLocally(): Promise<void> {
|
||||||
|
if (!this.settings.getSettings().isSyncEnabled) {
|
||||||
|
this.logger.debug(
|
||||||
|
`Syncing is disabled, not fetching remote changes`
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this.runningApplyRemoteChangesLocally != null) {
|
||||||
|
this.logger.debug(
|
||||||
|
"Applying remote changes locally is already in progress"
|
||||||
|
);
|
||||||
|
return this.runningApplyRemoteChangesLocally;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
this.runningApplyRemoteChangesLocally =
|
||||||
|
this.internalApplyRemoteChangesLocally();
|
||||||
|
await this.runningApplyRemoteChangesLocally;
|
||||||
|
this.logger.info("All remote changes have been applied locally");
|
||||||
|
} catch (e) {
|
||||||
|
this.logger.error(`Failed to apply remote changes locally: ${e}`);
|
||||||
|
throw e;
|
||||||
|
} finally {
|
||||||
|
this.runningApplyRemoteChangesLocally = undefined;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public async reset(): Promise<void> {
|
||||||
|
this.syncQueue.clear();
|
||||||
|
await this.syncQueue.onEmpty();
|
||||||
|
this.remainingOperationsListeners.forEach((listener) => {
|
||||||
|
listener(0);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private async syncRemotelyUpdatedFile(
|
||||||
|
remoteVersion: components["schemas"]["DocumentVersionWithoutContent"]
|
||||||
|
): Promise<void> {
|
||||||
|
await this.syncQueue.add(async () =>
|
||||||
|
this.internalSyncer.unrestrictedSyncRemotelyUpdatedFile(
|
||||||
|
remoteVersion
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
private async internalScheduleSyncForOfflineChanges(): Promise<void> {
|
private async internalScheduleSyncForOfflineChanges(): Promise<void> {
|
||||||
const allLocalFiles = await this.operations.listAllFiles();
|
const allLocalFiles = await this.operations.listAllFiles();
|
||||||
|
|
||||||
|
|
@ -226,34 +262,6 @@ export class Syncer {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async applyRemoteChangesLocally(): Promise<void> {
|
|
||||||
if (!this.settings.getSettings().isSyncEnabled) {
|
|
||||||
this.logger.debug(
|
|
||||||
`Syncing is disabled, not fetching remote changes`
|
|
||||||
);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (this.runningApplyRemoteChangesLocally != null) {
|
|
||||||
this.logger.debug(
|
|
||||||
"Applying remote changes locally is already in progress"
|
|
||||||
);
|
|
||||||
return this.runningApplyRemoteChangesLocally;
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
this.runningApplyRemoteChangesLocally =
|
|
||||||
this.internalApplyRemoteChangesLocally();
|
|
||||||
await this.runningApplyRemoteChangesLocally;
|
|
||||||
this.logger.info("All remote changes have been applied locally");
|
|
||||||
} catch (e) {
|
|
||||||
this.logger.error(`Failed to apply remote changes locally: ${e}`);
|
|
||||||
throw e;
|
|
||||||
} finally {
|
|
||||||
this.runningApplyRemoteChangesLocally = undefined;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private async internalApplyRemoteChangesLocally(): Promise<void> {
|
private async internalApplyRemoteChangesLocally(): Promise<void> {
|
||||||
const remote = await this.syncService.getAll(
|
const remote = await this.syncService.getAll(
|
||||||
this.database.getLastSeenUpdateId()
|
this.database.getLastSeenUpdateId()
|
||||||
|
|
@ -281,14 +289,6 @@ export class Syncer {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public async reset(): Promise<void> {
|
|
||||||
this.syncQueue.clear();
|
|
||||||
await this.syncQueue.onEmpty();
|
|
||||||
this.remainingOperationsListeners.forEach((listener) => {
|
|
||||||
listener(0);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
private emitRemainingOperationsChange(remainingOperations: number): void {
|
private emitRemainingOperationsChange(remainingOperations: number): void {
|
||||||
this.remainingOperationsListeners.forEach((listener) => {
|
this.remainingOperationsListeners.forEach((listener) => {
|
||||||
listener(remainingOperations);
|
listener(remainingOperations);
|
||||||
|
|
|
||||||
|
|
@ -7,9 +7,9 @@ import { SyncClient } from "sync-client";
|
||||||
import { assert } from "../utils/assert";
|
import { assert } from "../utils/assert";
|
||||||
|
|
||||||
export class MockClient implements FileSystemOperations {
|
export class MockClient implements FileSystemOperations {
|
||||||
protected readonly localFiles: Record<string, Uint8Array> = {};
|
protected readonly localFiles = new Map<string, Uint8Array>();
|
||||||
protected client!: SyncClient;
|
protected client!: SyncClient;
|
||||||
protected data: unknown = "";
|
protected data: object | undefined = undefined;
|
||||||
|
|
||||||
public constructor(
|
public constructor(
|
||||||
private readonly initialSettings: Partial<SyncSettings>
|
private readonly initialSettings: Partial<SyncSettings>
|
||||||
|
|
@ -18,15 +18,17 @@ export class MockClient implements FileSystemOperations {
|
||||||
public async init(): Promise<void> {
|
public async init(): Promise<void> {
|
||||||
this.client = await SyncClient.create(this, {
|
this.client = await SyncClient.create(this, {
|
||||||
load: async () => this.data,
|
load: async () => this.data,
|
||||||
save: async (data: unknown) => void (this.data = data)
|
save: async (data) => void (this.data = data)
|
||||||
});
|
});
|
||||||
|
|
||||||
await Promise.all(
|
await Promise.all(
|
||||||
Object.keys(this.initialSettings).map(async (key) => {
|
Object.keys(this.initialSettings).map(async (key) => {
|
||||||
return this.client.settings.setSetting(
|
if (key in this.client.settings) {
|
||||||
key as keyof SyncSettings,
|
return this.client.settings.setSetting(
|
||||||
this.initialSettings[key as keyof SyncSettings]
|
key as keyof SyncSettings, // eslint-disable-line @typescript-eslint/no-unsafe-type-assertion
|
||||||
);
|
this.initialSettings[key as keyof SyncSettings] // eslint-disable-line @typescript-eslint/no-unsafe-type-assertion
|
||||||
|
);
|
||||||
|
}
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
@ -37,46 +39,44 @@ export class MockClient implements FileSystemOperations {
|
||||||
}
|
}
|
||||||
|
|
||||||
public async listAllFiles(): Promise<RelativePath[]> {
|
public async listAllFiles(): Promise<RelativePath[]> {
|
||||||
return Object.keys(this.localFiles);
|
return Array.from(this.localFiles.keys());
|
||||||
}
|
}
|
||||||
|
|
||||||
public async read(path: RelativePath): Promise<Uint8Array> {
|
public async read(path: RelativePath): Promise<Uint8Array> {
|
||||||
if (!(path in this.localFiles)) {
|
const file = this.localFiles.get(path);
|
||||||
|
if (!file) {
|
||||||
throw new Error(`File ${path} does not exist`);
|
throw new Error(`File ${path} does not exist`);
|
||||||
}
|
}
|
||||||
return this.localFiles[path];
|
return file;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async getFileSize(path: RelativePath): Promise<number> {
|
public async getFileSize(path: RelativePath): Promise<number> {
|
||||||
if (!(path in this.localFiles)) {
|
return (await this.read(path)).length;
|
||||||
throw new Error(`File ${path} does not exist`);
|
|
||||||
}
|
|
||||||
return this.localFiles[path].length;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public async getModificationTime(path: RelativePath): Promise<Date> {
|
public async getModificationTime(path: RelativePath): Promise<Date> {
|
||||||
if (!(path in this.localFiles)) {
|
if (!this.localFiles.has(path)) {
|
||||||
throw new Error(`File ${path} does not exist`);
|
throw new Error(`File ${path} does not exist`);
|
||||||
}
|
}
|
||||||
return new Date();
|
return new Date();
|
||||||
}
|
}
|
||||||
|
|
||||||
public async exists(path: RelativePath): Promise<boolean> {
|
public async exists(path: RelativePath): Promise<boolean> {
|
||||||
return path in this.localFiles;
|
return this.localFiles.has(path);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async create(
|
public async create(
|
||||||
path: RelativePath,
|
path: RelativePath,
|
||||||
newContent: Uint8Array
|
newContent: Uint8Array
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
if (path in this.localFiles) {
|
if (this.localFiles.has(path)) {
|
||||||
throw new Error(`File ${path} already exists`);
|
throw new Error(`File ${path} already exists`);
|
||||||
}
|
}
|
||||||
this.localFiles[path] = newContent;
|
this.localFiles.set(path, newContent);
|
||||||
void this.client.syncer.syncLocallyCreatedFile(path, new Date());
|
void this.client.syncer.syncLocallyCreatedFile(path, new Date());
|
||||||
}
|
}
|
||||||
|
|
||||||
public async createDirectory(path: RelativePath): Promise<void> {
|
public async createDirectory(_path: RelativePath): Promise<void> {
|
||||||
// This doesn't mean anything in our virtual FS representation
|
// This doesn't mean anything in our virtual FS representation
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -84,13 +84,14 @@ export class MockClient implements FileSystemOperations {
|
||||||
path: RelativePath,
|
path: RelativePath,
|
||||||
updater: (currentContent: string) => string
|
updater: (currentContent: string) => string
|
||||||
): Promise<string> {
|
): Promise<string> {
|
||||||
if (!(path in this.localFiles)) {
|
const file = this.localFiles.get(path);
|
||||||
|
if (!file) {
|
||||||
throw new Error(`File ${path} does not exist`);
|
throw new Error(`File ${path} does not exist`);
|
||||||
}
|
}
|
||||||
const currentContent = new TextDecoder().decode(this.localFiles[path]);
|
const currentContent = new TextDecoder().decode(file);
|
||||||
const newContent = updater(currentContent);
|
const newContent = updater(currentContent);
|
||||||
const newContentUint8Array = new TextEncoder().encode(newContent);
|
const newContentUint8Array = new TextEncoder().encode(newContent);
|
||||||
this.localFiles[path] = newContentUint8Array;
|
this.localFiles.set(path, newContentUint8Array);
|
||||||
|
|
||||||
void this.client.syncer.syncLocallyUpdatedFile({
|
void this.client.syncer.syncLocallyUpdatedFile({
|
||||||
relativePath: path,
|
relativePath: path,
|
||||||
|
|
@ -101,7 +102,7 @@ export class MockClient implements FileSystemOperations {
|
||||||
}
|
}
|
||||||
|
|
||||||
public async write(path: RelativePath, content: Uint8Array): Promise<void> {
|
public async write(path: RelativePath, content: Uint8Array): Promise<void> {
|
||||||
this.localFiles[path] = content;
|
this.localFiles.set(path, content);
|
||||||
|
|
||||||
void this.client.syncer.syncLocallyUpdatedFile({
|
void this.client.syncer.syncLocallyUpdatedFile({
|
||||||
relativePath: path,
|
relativePath: path,
|
||||||
|
|
@ -110,7 +111,7 @@ export class MockClient implements FileSystemOperations {
|
||||||
}
|
}
|
||||||
|
|
||||||
public async delete(path: RelativePath): Promise<void> {
|
public async delete(path: RelativePath): Promise<void> {
|
||||||
delete this.localFiles[path];
|
this.localFiles.delete(path);
|
||||||
void this.client.syncer.syncLocallyDeletedFile(path);
|
void this.client.syncer.syncLocallyDeletedFile(path);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -118,13 +119,13 @@ export class MockClient implements FileSystemOperations {
|
||||||
oldPath: RelativePath,
|
oldPath: RelativePath,
|
||||||
newPath: RelativePath
|
newPath: RelativePath
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
if (!(oldPath in this.localFiles)) {
|
const file = this.localFiles.get(oldPath);
|
||||||
|
if (!file) {
|
||||||
throw new Error(`File ${oldPath} does not exist`);
|
throw new Error(`File ${oldPath} does not exist`);
|
||||||
}
|
}
|
||||||
|
this.localFiles.set(newPath, file);
|
||||||
this.localFiles[newPath] = this.localFiles[oldPath];
|
|
||||||
if (oldPath !== newPath) {
|
if (oldPath !== newPath) {
|
||||||
delete this.localFiles[oldPath];
|
this.localFiles.delete(oldPath);
|
||||||
}
|
}
|
||||||
|
|
||||||
void this.client.syncer.syncLocallyUpdatedFile({
|
void this.client.syncer.syncLocallyUpdatedFile({
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue