refactor: create src dir
This commit is contained in:
85
maxun-core/src/utils/concurrency.ts
Normal file
85
maxun-core/src/utils/concurrency.ts
Normal file
@@ -0,0 +1,85 @@
|
||||
/**
|
||||
* Concurrency class for running concurrent tasks while managing a limited amount of resources.
|
||||
*/
|
||||
export default class Concurrency {
|
||||
/**
|
||||
* Maximum number of workers running in parallel. If set to `null`, there is no limit.
|
||||
*/
|
||||
maxConcurrency: number = 1;
|
||||
|
||||
/**
|
||||
* Number of currently active workers.
|
||||
*/
|
||||
activeWorkers: number = 0;
|
||||
|
||||
/**
|
||||
* Queue of jobs waiting to be completed.
|
||||
*/
|
||||
private jobQueue: Function[] = [];
|
||||
|
||||
/**
|
||||
* "Resolve" callbacks of the waitForCompletion() promises.
|
||||
*/
|
||||
private waiting: Function[] = [];
|
||||
|
||||
/**
|
||||
* Constructs a new instance of concurrency manager.
|
||||
* @param {number} maxConcurrency Maximum number of workers running in parallel.
|
||||
*/
|
||||
constructor(maxConcurrency: number) {
|
||||
this.maxConcurrency = maxConcurrency;
|
||||
}
|
||||
|
||||
/**
|
||||
* Takes a waiting job out of the queue and runs it.
|
||||
*/
|
||||
private runNextJob(): void {
|
||||
const job = this.jobQueue.pop();
|
||||
|
||||
if (job) {
|
||||
// console.debug("Running a job...");
|
||||
job().then(() => {
|
||||
// console.debug("Job finished, running the next waiting job...");
|
||||
this.runNextJob();
|
||||
});
|
||||
} else {
|
||||
// console.debug("No waiting job found!");
|
||||
this.activeWorkers -= 1;
|
||||
if (this.activeWorkers === 0) {
|
||||
// console.debug("This concurrency manager is idle!");
|
||||
this.waiting.forEach((x) => x());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Pass a job (a time-demanding async function) to the concurrency manager. \
|
||||
* The time of the job's execution depends on the concurrency manager itself
|
||||
* (given a generous enough `maxConcurrency` value, it might be immediate,
|
||||
* but this is not guaranteed).
|
||||
* @param worker Async function to be executed (job to be processed).
|
||||
*/
|
||||
addJob(job: () => Promise<any>): void {
|
||||
// console.debug("Adding a worker!");
|
||||
this.jobQueue.push(job);
|
||||
|
||||
if (!this.maxConcurrency || this.activeWorkers < this.maxConcurrency) {
|
||||
this.runNextJob();
|
||||
this.activeWorkers += 1;
|
||||
} else {
|
||||
// console.debug("No capacity to run a worker now, waiting!");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Waits until there is no running nor waiting job. \
|
||||
* If the concurrency manager is idle at the time of calling this function,
|
||||
* it waits until at least one job is compeleted (can be "presubscribed").
|
||||
* @returns Promise, resolved after there is no running/waiting worker.
|
||||
*/
|
||||
waitForCompletion(): Promise<void> {
|
||||
return new Promise((res) => {
|
||||
this.waiting.push(res);
|
||||
});
|
||||
}
|
||||
}
|
||||
30
maxun-core/src/utils/logger.ts
Normal file
30
maxun-core/src/utils/logger.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
/*
|
||||
* Logger class for more detailed and comprehensible logs (with colors and timestamps)
|
||||
*/
|
||||
|
||||
export enum Level {
|
||||
DATE = 36,
|
||||
LOG = 0,
|
||||
WARN = 93,
|
||||
ERROR = 31,
|
||||
DEBUG = 95,
|
||||
RESET = 0,
|
||||
}
|
||||
|
||||
export default function logger(
|
||||
message: string | Error,
|
||||
level: (Level.LOG | Level.WARN | Level.ERROR | Level.DEBUG) = Level.LOG,
|
||||
) {
|
||||
let m = message;
|
||||
if (message.constructor.name.includes('Error') && typeof message !== 'string') {
|
||||
m = <Error><unknown>(message).message;
|
||||
}
|
||||
process.stdout.write(`\x1b[${Level.DATE}m[${(new Date()).toLocaleString()}]\x1b[0m `);
|
||||
process.stdout.write(`\x1b[${level}m`);
|
||||
if (level === Level.ERROR || level === Level.WARN) {
|
||||
process.stderr.write(<string>m);
|
||||
} else {
|
||||
process.stdout.write(<string>m);
|
||||
}
|
||||
process.stdout.write(`\x1b[${Level.RESET}m\n`);
|
||||
}
|
||||
13
maxun-core/src/utils/utils.ts
Normal file
13
maxun-core/src/utils/utils.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
/**
|
||||
* ESLint rule in case there is only one util function
|
||||
* (it still does not represent the "utils" file)
|
||||
*/
|
||||
|
||||
/* eslint-disable import/prefer-default-export */
|
||||
|
||||
/**
|
||||
* Converts an array of scalars to an object with **items** of the array **for keys**.
|
||||
*/
|
||||
export function arrayToObject(array : any[]) {
|
||||
return array.reduce((p, x) => ({ ...p, [x]: [] }), {});
|
||||
}
|
||||
Reference in New Issue
Block a user