From 7e0863be58f9f9d4bae533a7805c0928562193bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Jim=C3=A9nez=20Rivera?= Date: Thu, 6 Feb 2025 15:12:07 +0100 Subject: [PATCH] Refactor virtual drive --- .../callbacks/notify-fetch-data.callback.ts | 5 +- src/types/callbacks.type.ts | 7 +- src/virtual-drive.ts | 91 ++++++------------- src/virtual-drive.unit.test.ts | 7 +- .../detect-context-menu-action.service.ts | 1 - src/watcher/events/on-raw.service.ts | 3 +- 6 files changed, 43 insertions(+), 71 deletions(-) diff --git a/examples/callbacks/notify-fetch-data.callback.ts b/examples/callbacks/notify-fetch-data.callback.ts index 0ba096da..b315c840 100644 --- a/examples/callbacks/notify-fetch-data.callback.ts +++ b/examples/callbacks/notify-fetch-data.callback.ts @@ -1,11 +1,10 @@ import { logger } from "examples/drive"; import { getInfoItem } from "examples/info-items-manager"; +import { TFetchDataCallback } from "@/types/callbacks.type"; import { sleep } from "@/utils"; -type TCallback = (data: boolean, path: string, errorHandler?: () => void) => Promise<{ finished: boolean; progress: number }>; - -export const fetchDataCallback = async (id: string, callback: TCallback) => { +export const fetchDataCallback = async (id: string, callback: Parameters[1]) => { logger.info({ fn: "fetchDataCallback", id }); const path = await getInfoItem(id); diff --git a/src/types/callbacks.type.ts b/src/types/callbacks.type.ts index 8702ed9b..96b09a5d 100644 --- a/src/types/callbacks.type.ts +++ b/src/types/callbacks.type.ts @@ -1,7 +1,12 @@ export type NapiCallbackFunction = (...args: any[]) => any; +export type TFetchDataCallback = ( + id: string, + callback: (data: boolean, path: string, errorHandler?: () => void) => Promise<{ finished: boolean; progress: number }>, +) => void; + export type InputSyncCallbacks = { - fetchDataCallback?: NapiCallbackFunction; + fetchDataCallback: TFetchDataCallback; validateDataCallback?: NapiCallbackFunction; cancelFetchDataCallback?: NapiCallbackFunction; fetchPlaceholdersCallback?: NapiCallbackFunction; diff --git a/src/virtual-drive.ts b/src/virtual-drive.ts index 1c54e249..02c7f9d3 100644 --- a/src/virtual-drive.ts +++ b/src/virtual-drive.ts @@ -1,41 +1,37 @@ import path, { join, win32 } from "path"; import fs from "fs"; import { Watcher } from "./watcher/watcher"; -import { ExtraCallbacks, InputSyncCallbacks } from "./types/callbacks.type"; +import { Callbacks } from "./types/callbacks.type"; import { IQueueManager } from "./queue/queueManager"; import { createLogger } from "./logger"; import { Addon } from "./addon-wrapper"; +import winston from "winston"; const addon = new Addon(); -type Callbacks = InputSyncCallbacks & ExtraCallbacks; +const PLACEHOLDER_ATTRIBUTES = { + FILE_ATTRIBUTE_READONLY: 0x1, + FILE_ATTRIBUTE_HIDDEN: 0x2, + FOLDER_ATTRIBUTE_READONLY: 0x1, + FILE_ATTRIBUTE_NORMAL: 0x1, +}; + class VirtualDrive { - PLACEHOLDER_ATTRIBUTES: { [key: string]: number }; syncRootPath: string; callbacks?: Callbacks; + watcher = new Watcher(); + logger: winston.Logger; - private watcher: Watcher; - - constructor(syncRootPath: string, loggerPath?: string) { - this.PLACEHOLDER_ATTRIBUTES = { - FILE_ATTRIBUTE_READONLY: 0x1, - FILE_ATTRIBUTE_HIDDEN: 0x2, - FOLDER_ATTRIBUTE_READONLY: 0x1, - FILE_ATTRIBUTE_NORMAL: 0x1, - }; + constructor(syncRootPath: string, loggerPath: string) { + this.syncRootPath = this.convertToWindowsPath(syncRootPath); + loggerPath = this.convertToWindowsPath(loggerPath); - this.watcher = new Watcher(); - addon.syncRootPath = syncRootPath; + addon.syncRootPath = this.syncRootPath; - this.syncRootPath = syncRootPath; this.createSyncRootFolder(); - - let pathElements = this.syncRootPath.split("\\\\"); - pathElements.pop(); - let parentPath = pathElements.join("\\\\"); - - this.addLoggerPath(loggerPath ?? parentPath); + this.addLoggerPath(loggerPath); + this.logger = createLogger(loggerPath); } addLoggerPath(logPath: string) { @@ -63,39 +59,6 @@ class VirtualDrive { return addon.getPlaceholderWithStatePending(); } - getInputSyncCallbacks(): InputSyncCallbacks { - if (this.callbacks === undefined) { - throw new Error("Callbacks are not defined"); - } - - const inputSyncCallbackKeys: (keyof InputSyncCallbacks)[] = [ - "fetchDataCallback", - "validateDataCallback", - "cancelFetchDataCallback", - "fetchPlaceholdersCallback", - "cancelFetchPlaceholdersCallback", - "notifyFileOpenCompletionCallback", - "notifyFileCloseCompletionCallback", - "notifyDehydrateCallback", - "notifyDehydrateCompletionCallback", - "notifyDeleteCallback", - "notifyDeleteCompletionCallback", - "notifyRenameCallback", - "notifyRenameCompletionCallback", - "noneCallback", - ]; - - const result: InputSyncCallbacks = {}; - - for (const key of inputSyncCallbackKeys) { - if (this.callbacks[key] !== undefined) { - result[key] = this.callbacks[key]; - } - } - - return result; - } - createSyncRootFolder() { if (!fs.existsSync(this.syncRootPath)) { fs.mkdirSync(this.syncRootPath, { recursive: true }); @@ -114,8 +77,12 @@ class VirtualDrive { return addon.deleteFileSyncRoot({ path: this.fixPath(relativePath) }); } - async connectSyncRoot(): Promise { - return addon.connectSyncRoot({ callbacks: this.getInputSyncCallbacks() }); + async connectSyncRoot() { + if (this.callbacks === undefined) { + throw new Error("Callbacks are not defined"); + } + + return addon.connectSyncRoot({ callbacks: this.callbacks }); } createPlaceholderFile( @@ -216,7 +183,7 @@ class VirtualDrive { this.watcher.queueManager = queueManager; - this.watcher.logger = createLogger(loggerPath); + this.watcher.logger = this.logger; this.watcher.syncRootPath = path; this.watcher.options = { @@ -260,7 +227,7 @@ class VirtualDrive { path.basename(fullPath), itemId, size, - this.PLACEHOLDER_ATTRIBUTES.FILE_ATTRIBUTE_NORMAL, + PLACEHOLDER_ATTRIBUTES.FILE_ATTRIBUTE_NORMAL, creationTime, lastWriteTime, Date.now(), @@ -293,7 +260,7 @@ class VirtualDrive { itemId, true, size, - this.PLACEHOLDER_ATTRIBUTES.FILE_ATTRIBUTE_NORMAL, + PLACEHOLDER_ATTRIBUTES.FILE_ATTRIBUTE_NORMAL, creationTime, lastWriteTime, Date.now(), @@ -327,7 +294,7 @@ class VirtualDrive { itemId, true, size, - this.PLACEHOLDER_ATTRIBUTES.FILE_ATTRIBUTE_NORMAL, + PLACEHOLDER_ATTRIBUTES.FILE_ATTRIBUTE_NORMAL, creationTime, lastWriteTime, Date.now(), @@ -353,7 +320,7 @@ class VirtualDrive { itemId, true, 0, - this.PLACEHOLDER_ATTRIBUTES.FILE_ATTRIBUTE_NORMAL, + PLACEHOLDER_ATTRIBUTES.FILE_ATTRIBUTE_NORMAL, Date.now(), Date.now(), Date.now(), @@ -367,7 +334,7 @@ class VirtualDrive { path.basename(fullPath), itemId, size, - this.PLACEHOLDER_ATTRIBUTES.FILE_ATTRIBUTE_NORMAL, + PLACEHOLDER_ATTRIBUTES.FILE_ATTRIBUTE_NORMAL, creationTime, lastWriteTime, Date.now(), diff --git a/src/virtual-drive.unit.test.ts b/src/virtual-drive.unit.test.ts index 6496b406..14c9c711 100644 --- a/src/virtual-drive.unit.test.ts +++ b/src/virtual-drive.unit.test.ts @@ -1,9 +1,11 @@ import fs from "fs"; import { v4 } from "uuid"; import { Mock } from "vitest"; +import { mockDeep } from "vitest-mock-extended"; import { addon } from "@/addon"; +import { Callbacks } from "./types/callbacks.type"; import VirtualDrive from "./virtual-drive"; vi.mock("fs"); @@ -138,13 +140,14 @@ describe("VirtualDrive", () => { const providerVersion = "1.0.0"; const providerId = v4(); const logoPath = "C:\\iconPath"; - const callbacks = {}; + const callbacks = mockDeep(); // Act + expect(drive.callbacks).toBe(undefined); await drive.registerSyncRoot(providerName, providerVersion, providerId, callbacks, logoPath); // Assert - expect(drive.callbacks).toBe(callbacks); + expect(drive.callbacks).not.toBe(undefined); expect(addon.registerSyncRoot).toHaveBeenCalledWith(syncRootPath, providerName, providerVersion, providerId, logoPath); }); }); diff --git a/src/watcher/detect-context-menu-action.service.ts b/src/watcher/detect-context-menu-action.service.ts index f4606cb8..5a9c8e63 100644 --- a/src/watcher/detect-context-menu-action.service.ts +++ b/src/watcher/detect-context-menu-action.service.ts @@ -28,7 +28,6 @@ export class DetectContextMenuActionService { size: curr.size, ctimeMs: curr.ctimeMs, mtimeMs: curr.mtimeMs, - blocks: curr.blocks, }, }); diff --git a/src/watcher/events/on-raw.service.ts b/src/watcher/events/on-raw.service.ts index 585c8b51..de8c8d1c 100644 --- a/src/watcher/events/on-raw.service.ts +++ b/src/watcher/events/on-raw.service.ts @@ -1,11 +1,10 @@ import { stat } from "fs/promises"; -import { extname } from "path"; import { DetectContextMenuActionService } from "../detect-context-menu-action.service"; import { Watcher } from "../watcher"; export class OnRawService { - constructor(private readonly detectContextMenuAction: DetectContextMenuActionService = new DetectContextMenuActionService()) {} + constructor(private readonly detectContextMenuAction = new DetectContextMenuActionService()) {} async execute({ self, event, path, details }: TProps) { try {