From 9ef121cb9ffdc974206ebe1fb602632d86e6bcc0 Mon Sep 17 00:00:00 2001 From: ArceDanielShok Date: Mon, 31 Mar 2025 11:05:32 -0300 Subject: [PATCH] Implement Dependency Injection for Addon in VirtualDrive --- src/addon-wrapper.ts | 12 ++++++++++++ src/virtual-drive.ts | 8 ++++---- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/addon-wrapper.ts b/src/addon-wrapper.ts index 14a2097b..08438dfa 100644 --- a/src/addon-wrapper.ts +++ b/src/addon-wrapper.ts @@ -138,3 +138,15 @@ export class Addon { return this.parseAddonZod("hydrateFile", result); } } + +export class DependencyInjectionAddonProvider { + private static _addon: Addon; + + static get() { + if (DependencyInjectionAddonProvider._addon) return DependencyInjectionAddonProvider._addon; + + DependencyInjectionAddonProvider._addon = new Addon(); + + return DependencyInjectionAddonProvider._addon; + } +} diff --git a/src/virtual-drive.ts b/src/virtual-drive.ts index b3895585..b5855a89 100644 --- a/src/virtual-drive.ts +++ b/src/virtual-drive.ts @@ -2,7 +2,7 @@ import fs from "fs"; import path, { join, win32 } from "path"; import winston from "winston"; -import { Addon } from "./addon-wrapper"; +import { Addon, DependencyInjectionAddonProvider } from "./addon-wrapper"; import { createLogger } from "./logger"; import { QueueManager } from "./queue/queue-manager"; import { Callbacks } from "./types/callbacks.type"; @@ -25,7 +25,7 @@ class VirtualDrive { addon: Addon; constructor(syncRootPath: string, providerId: string, loggerPath: string) { - this.addon = new Addon(); + this.addon = DependencyInjectionAddonProvider.get(); this.syncRootPath = this.convertToWindowsPath(syncRootPath); loggerPath = this.convertToWindowsPath(loggerPath); this.providerId = providerId; @@ -164,8 +164,8 @@ class VirtualDrive { return this.addon.unregisterSyncRoot({ providerId: this.providerId }); } - unRegisterSyncRootByProviderId({ providerId }: { providerId: string }) { - return this.addon.unregisterSyncRoot({ providerId }); + static unRegisterSyncRootByProviderId({ providerId }: { providerId: string }) { + return DependencyInjectionAddonProvider.get().unregisterSyncRoot({ providerId }); } watchAndWait(path: string, queueManager: QueueManager, loggerPath: string): void {