Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions examples/callbacks/notify-fetch-data.callback.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { logger } from "examples/drive";
import { getInfoItem } from "examples/info-items-manager";

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) => {
logger.info({ fn: "fetchDataCallback", id });
const path = await getInfoItem(id);

let finish = false;
Expand Down
8 changes: 4 additions & 4 deletions examples/handlers/handle-change-size.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { drive, logger } from "examples/drive";

import { QueueItem } from "@/queue/queueManager";
import { v4 } from "uuid";

export const handleChangeSize = async (task: QueueItem) => {
try {
logger.info({ fn: "handleChangeSize", path: task.path });
const result = Math.random().toString(36).substring(2, 7);
drive.convertToPlaceholder(task.path, result);
drive.updateFileIdentity(task.path, result, false);
drive.updateSyncStatus(task.path, task.isFolder, true);
const id = v4();
drive.convertToPlaceholder(task.path, id);
drive.updateFileIdentity(task.path, id, false);
} catch (error) {
logger.error("handleChangeSize", error);
}
Expand Down
5 changes: 4 additions & 1 deletion examples/populate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import { v4 } from "uuid";

import settings from "./settings";

const rootFile1 = join(settings.syncRootPath, v4());
const rootFileName1 = v4();
const rootZipFileName = `${v4()}.zip`;
const rootFile1 = join(settings.syncRootPath, rootFileName1);
const rootFile2ChangeSize = join(settings.syncRootPath, `change-size-${v4()}.txt`);
const rootFile3 = join(settings.syncRootPath, `${v4()}.txt`);
const rootFile3Moved = join(settings.syncRootPath, `moved-${v4()}.txt`);
Expand All @@ -22,6 +24,7 @@ execSync(`echo Hello, world! > ${rootFile2ChangeSize}`);
execSync(`echo Hello, world! >> ${rootFile2ChangeSize}`); // Sync
execSync(`echo Hello, world! > ${rootFile3}`);
execSync(`type nul > ${rootFile4}`); // No sync (0 bytes)
execSync(`cd ${settings.syncRootPath} && tar -cf ${rootZipFileName} ${rootFileName1}`); // Sync
execSync(`mv ${rootFile3} ${rootFile3Moved}`); // Sync
execSync(`mkdir ${rootFolder1}`); // Sync
execSync(`mkdir ${rootFolder2}`); // Cloud (no files inside)
Expand Down
22 changes: 11 additions & 11 deletions examples/register.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ const handlers = { handleAdd, handleHydrate, handleDehydrate, handleChangeSize }
const notify = { onTaskSuccess: async () => undefined, onTaskProcessing: async () => undefined };
const queueManager = new QueueManager(handlers, notify, settings.queuePersistPath);

drive.registerSyncRoot(settings.driveName, settings.driveVersion, settings.providerid, callbacks, settings.iconPath);
drive.connectSyncRoot();

try {
initInfoItems();
drive.watchAndWait(settings.syncRootPath, queueManager, settings.watcherLogPath);
} catch (error) {
logger.error(error);
drive.disconnectSyncRoot();
VirtualDrive.unregisterSyncRoot(settings.syncRootPath);
}
drive.registerSyncRoot(settings.driveName, settings.driveVersion, settings.providerid, callbacks, settings.iconPath).then(() => {
try {
initInfoItems();
drive.connectSyncRoot();
drive.watchAndWait(settings.syncRootPath, queueManager, settings.watcherLogPath);
} catch (error) {
logger.error(error);
drive.disconnectSyncRoot();
VirtualDrive.unregisterSyncRoot(settings.syncRootPath);
}
});
36 changes: 32 additions & 4 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,33 @@
import { QueueManager } from "./src/queue/queue-manager";
import { QueueItem, typeQueue, HandleAction, HandleActions } from "./src/queue/queueManager";
import VirtualDrive from "./src/virtual-drive";
import { QueueManager, QueueManagerCallback, QueueHandler } from "@/queue/queue-manager";
import { QueueItem, typeQueue, HandleAction, HandleActions } from "@/queue/queueManager";
import { Callbacks } from "@/types/callbacks.type";
import { SyncState, PinState } from "@/types/placeholder.type";
import VirtualDrive from "@/virtual-drive";
import { DetectContextMenuActionService } from "@/watcher/detect-context-menu-action.service";
import { OnAddDirService } from "@/watcher/events/on-add-dir.service";
import { OnAddService } from "@/watcher/events/on-add.service";
import { OnAllService } from "@/watcher/events/on-all.service";
import { OnRawService } from "@/watcher/events/on-raw.service";
import { Watcher } from "@/watcher/watcher";
import { IVirtualDriveFunctions } from "@/watcher/watcher.interface";

export { VirtualDrive, QueueItem, typeQueue, HandleAction, HandleActions, QueueManager };
export {
VirtualDrive,
QueueItem,
typeQueue,
HandleAction,
HandleActions,
QueueManager,
Callbacks,
SyncState,
PinState,
Watcher,
DetectContextMenuActionService,
OnAddDirService,
OnAddService,
OnAllService,
OnRawService,
IVirtualDriveFunctions,
QueueHandler,
QueueManagerCallback,
};
19 changes: 0 additions & 19 deletions jest.config.js

This file was deleted.

5 changes: 0 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
"types": "dist/index.d.ts",
"scripts": {
"========== Testing ==========": "",
"test": "jest",
"vitest": "vitest",
"vitest:once": "yarn vitest --run",
"vitest:one": "yarn vitest related x",
Expand All @@ -29,15 +28,11 @@
"gypfile": true,
"devDependencies": {
"@trivago/prettier-plugin-sort-imports": "^5.2.1",
"@types/jest": "^29.5.12",
"@types/lodash.chunk": "^4.2.9",
"@types/node": "^20.5.0",
"@types/yargs": "^17.0.32",
"jest": "^29.7.0",
"jest-mock-extended": "^4.0.0-beta1",
"nodemon": "^3.1.9",
"prettier": "^3.4.2",
"ts-jest": "^29.1.2",
"ts-node": "^10.9.2",
"tsc-alias": "^1.8.10",
"typescript": "^5.1.6",
Expand Down
30 changes: 30 additions & 0 deletions src/get-placeholder-states.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { readdir } from "fs/promises";
import { join } from "path";

import { isFileInDevice } from "./is-file-in-device.service";
import VirtualDrive from "./virtual-drive";

type TProps = {
self: VirtualDrive;
path: string;
};

export const getPlaceholderStates = async ({ self, path }: TProps) => {
const files = await readdir(path, { withFileTypes: true });

const promises = files.map(async (file) => {
const filePath = join(path, file.name);

if (file.isDirectory()) {
return getPlaceholderStates({ self, path: filePath });
} else {
const status = self.getPlaceholderState(filePath);
if (isFileInDevice(status)) {
const id = self.getFileIdentity(filePath);
self.watcher.fileInDevice.add(id);
}
}
});

await Promise.all(promises);
};
12 changes: 12 additions & 0 deletions src/is-file-in-device.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { PinState, SyncState } from "./types/placeholder.type";

type TProps = {
syncState: SyncState;
pinState: PinState;
};

export const isFileInDevice = ({ syncState, pinState }: TProps) => {
const inSync = syncState === SyncState.InSync;
const isHydrated = pinState === PinState.AlwaysLocal || pinState === PinState.Unspecified;
return inSync && isHydrated;
};
2 changes: 1 addition & 1 deletion src/types/callbacks.type.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export type NapiCallbackFunction = (...args: any[]) => any;

export type InputSyncCallbacks = {
fetchDataCallback?: NapiCallbackFunction;
fetchDataCallback: NapiCallbackFunction;
validateDataCallback?: NapiCallbackFunction;
cancelFetchDataCallback?: NapiCallbackFunction;
fetchPlaceholdersCallback?: NapiCallbackFunction;
Expand Down
14 changes: 0 additions & 14 deletions src/types/placeholder.type.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,13 @@
// Enum para SyncState
export enum SyncState {
Undefined = -1,
NotInSync = 0,
InSync = 1,
}

// Enum para PinState
export enum PinState {
Inherited = 0,
AlwaysLocal = 1,
OnlineOnly = 2,
Unspecified = 3,
Excluded = 4,
}

// Tipo para combinar ambos estados en un objeto de estado
export type Status = {
pinState: PinState;
syncState: SyncState;
};

export enum Attributes {
OTHER = 0,
NOT_PINNED = 1,
PINNED = 2,
}
Loading