Skip to content
Merged
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
5 changes: 0 additions & 5 deletions .env-default

This file was deleted.

2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/addon.node
/build
/dist
/examples/filesInfo.json
/examples/tmp
/node_modules
/node-win.log
/test-files
Expand Down
Binary file removed bin/win32-x64-116/node-win.node
Binary file not shown.
3 changes: 0 additions & 3 deletions examples/callbacks/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
export { default as onRenameCallbackWithCallback } from "./notify-rename.callback";
export { default as onDeleteCallbackWithCallback } from "./notify-delete.callback";
export { default as onMessageCallback } from "./notify-message.callback";
export { default as onCancelFetchDataCallback } from "./cancel-fetch-data.callback";
export { default as onFetchDataCallback } from "./notify-fetch-data.callback";
export { default as onFileAddedCallback } from "./notify-added.callback";
30 changes: 0 additions & 30 deletions examples/callbacks/notify-added.callback.ts

This file was deleted.

28 changes: 5 additions & 23 deletions examples/callbacks/notify-delete.callback.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,6 @@
async function onDeleteCallback(fileId: string, callback: (response: boolean) => void) {
console.log("[EXAMPLE] On delete File ID: " + fileId);
const a = await (new Promise<boolean>((resolve, reject) => {
try {
setTimeout(() => {
resolve(true);
}, 10)
} catch (err) {
reject(err);
}
}));
import { logger } from "@/logger";

return a;
}

function onDeleteCallbackWithCallback(fileId: string, callback: (response: boolean) => void) {
onDeleteCallback(fileId, callback).then((response) => {
callback(response);
}).catch((err) => {
callback(false);
});
}

export default onDeleteCallbackWithCallback;
export const onDeleteCallback = (fileId: string, callback: (response: boolean) => void) => {
logger.info({ event: "onDelete", fileId });
callback(true);
};
59 changes: 12 additions & 47 deletions examples/callbacks/notify-fetch-data.callback.ts
Original file line number Diff line number Diff line change
@@ -1,51 +1,16 @@
import { ItemsInfoManager } from "../utils";
import { getInfoItem } from "examples/info-items-manager";

async function onFetchData(fileId: string): Promise<boolean> {
console.log("[EXAMPLE] downloading file: " + fileId);
// simulating a download from a real server
const a = await (new Promise<boolean>((resolve, reject) => {
try {
import { sleep } from "@/utils";

setTimeout(() => {
resolve(true);
}, 1000)
} catch (err) {
reject(err);
}
}));
type CallbackResponse = (data: boolean, path: string, errorHandler?: () => void) => Promise<{ finished: boolean; progress: number }>;

return a;
}
export const onFetchDataCallback = async (id: string, callback: CallbackResponse) => {
const path = await getInfoItem(id);

type CallbackResponse = (data : boolean, path: string, errorHandler?: () => void) => Promise<{ finished: boolean, progress: number }>;

async function onFetchDataCallback(fileId: string, callback: CallbackResponse ) {
console.log("[EXAMPLE] file id: " + fileId);
// simulate a download from a real server and response with the path of the downloaded file of a fake server
let finish = false;
onFetchData(fileId).then(async (response) => {
while (!finish) {
const itemsManager = await ItemsInfoManager.initialize('dist/examples/filesInfo.json')
const itemPath = itemsManager.get(fileId.replace(/\x00/g, ''))

if (!itemPath) {
console.log("[EXAMPLE] error: file not found");
finish = true;
break;
}

const callbackResponse = await callback(response, itemPath);
finish = callbackResponse.finished;
if (finish) {
console.log("[EXAMPLE] finished");
break;
};
};

}).catch((err) => {
//callback(false, "C:\\Users\\gcarl\\Desktop\\fakeserver\\imagen.rar");
console.log('[EXAMPLE] error:' + err);
});
}

export default onFetchDataCallback;
let finish = false;
while (!finish) {
const result = await callback(true, path);
finish = result.finished;
await sleep(1000);
}
};
44 changes: 44 additions & 0 deletions examples/info-items-manager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { existsSync } from "fs";
import { copyFile, mkdir, readFile, writeFile } from "fs/promises";
import { basename, join } from "path";
import { v4 } from "uuid";
import { TMP_PATH } from "./settings";

const infoItemsPath = join(TMP_PATH, "info-items.json");
const serverPath = join(TMP_PATH, "fake-server");

export const initInfoItems = async () => {
if (!existsSync(infoItemsPath)) {
await writeFile(infoItemsPath, JSON.stringify({}));
}

if (!existsSync(serverPath)) {
await mkdir(serverPath);
}
};

export const getInfoItems = async () => {
return JSON.parse(await readFile(infoItemsPath, "utf8"));
};

export const deleteInfoItems = async () => {
await writeFile(infoItemsPath, JSON.stringify({}));
};

export const addInfoItem = async (itemPath: string) => {
const fileName = basename(itemPath);
const serverItemPath = join(serverPath, fileName);
await copyFile(itemPath, serverItemPath);

const id = v4();
const infoItems = await getInfoItems();
infoItems[id] = serverItemPath;

await writeFile(infoItemsPath, JSON.stringify(infoItems, null, 2));
return id;
};

export const getInfoItem = async (id: string) => {
const infoItems = await getInfoItems();
return infoItems[id];
};
24 changes: 13 additions & 11 deletions examples/queueManager.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { HandleAction, HandleActions } from "src/queue/queueManager";

import { logger } from "@/logger";
import { sleep } from "@/utils";

import { IQueueManager, QueueItem } from "../index";

export type QueueHandler = {
Expand All @@ -8,9 +12,7 @@ export type QueueHandler = {
handleChange?: HandleAction;
handleChangeSize: HandleAction;
};
export async function sleep(ms: number): Promise<void> {
return new Promise((resolve) => setTimeout(resolve, ms));
}

export class QueueManager implements IQueueManager {
private _queue: QueueItem[] = [];

Expand All @@ -29,7 +31,7 @@ export class QueueManager implements IQueueManager {
}

public enqueue(task: QueueItem): void {
console.debug(`Task enqueued: ${JSON.stringify(task)}`);
logger.debug({ fn: "enqueue", task });
this._queue.push(task);
this.sortQueue();
if (!this.isProcessing) {
Expand All @@ -53,13 +55,11 @@ export class QueueManager implements IQueueManager {
}

public async processNext(): Promise<void> {
if (this._queue.length === 0) {
console.debug("No tasks in queue.");
return;
}
const task = this._queue.shift();
if (!task) return;
console.debug(`Processing task: ${JSON.stringify(task)}`);

logger.debug({ fn: "processNext", task });

switch (task.type) {
case "add":
return await this.actions.add(task);
Expand All @@ -78,12 +78,14 @@ export class QueueManager implements IQueueManager {
}

public async processAll(): Promise<void> {
logger.debug({ fn: "processAll", queueLength: this._queue.length });

this.isProcessing = true;
while (this._queue.length > 0) {
await sleep(200);
console.debug("Processing all tasks. Queue length:", this._queue.length);
await this.processNext();
await sleep(200);
}

this.isProcessing = false;
}
}
Loading