-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
153 lines (137 loc) · 4.38 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import EventEmitter from "events";
class Threading {
/**
* @param {Object} data - Data to start threading.
* @param {number} data.threads - Number of threads to use for threading.
* @param {("sequential"|"concurrent")} data.type - Type of threading to use.
* - "sequential": Threads are processed one after another in a batched manner.
* - "concurrent": Threads are processed dynamically based on the completion of earlier threads.
*/
constructor(data) {
this.threads = data.threads;
this.running = 0;
this.callbacks = [];
this.emitter = new EventEmitter();
this.type = data?.type ? data?.type : "concurrent";
if (!this.threads || !(this.threads > 0))
throw new Error("Enter number of threads, must be a postive integer.");
this.emitter.on("error", () => {});
this.emitter.on("begin", () => {});
this.emitter.on("end", () => {});
}
/**
* Adds a callback function and its arguments to the queue.
* @param {Function} callback - The callback function to execute
* @param {...any} args - Arguments to pass to the callback function
*/
insert(callback, ...args) {
this.callbacks.push({ callback, arguments: [...args] });
}
/**
* Executes the queued callback functions asynchronously.
* @returns {Promise<void>}
*/
async run() {
if (this.type.toLowerCase() === "sequential") {
this.#regulateSequential();
} else if (this.type.toLowerCase() === "concurrent") {
for (let i = 0; i < this.callbacks.length; i++) {
this.#regulateConcurrent(
this.callbacks[i]?.callback,
this?.callbacks[i].arguments
);
}
}
}
/**
* Executes a callback function and manages the thread pool according to threads sequential.
* @param {Function} callback - The callback function to execute
* @param {Array} args - Arguments to pass to the callback function
* @returns {Promise<any>} - The result of the callback function
*/
async #regulateSequential() {
for (let i = 0; i < this.callbacks.length; i = i + this.threads) {
const chunkTasks = [];
for (let j = 0; j < this.threads; j++) {
if (this.callbacks[i + j]) {
chunkTasks.push({
callback: this.callbacks[i + j].callback,
arguments: [...this.callbacks[i + j].arguments],
});
}
}
for (const task of chunkTasks) {
this.emitter.emit("begin", task.arguments);
}
const results = await Promise.all(
chunkTasks.map(async (task) => {
try {
const result = await task.callback(...task.arguments);
return { result, arguments: task.arguments };
} catch (error) {
this.emitter.emit("error", {
message: error.message,
stack: error.stack,
});
return null;
}
})
);
results.forEach((resultWithArguments) => {
if (resultWithArguments !== null) {
this.emitter.emit("end", {
type: "sequential",
arguments: resultWithArguments.arguments,
result: resultWithArguments.result,
});
}
});
}
}
/**
* Executes a callback function and manages the thread pool in concurrent.
* @param {Function} callback - The callback function to execute
* @param {Array} args - Arguments to pass to the callback function
* @returns {Promise<any>} - The result of the callback function
*/
async #regulateConcurrent(callback, args) {
if (this.running >= this.threads) {
await this.#waitForThread();
}
this.running++;
try {
this.emitter.emit("begin", args);
const result = await callback(...args);
this.emitter.emit("end", {
type: "concurrent",
arguments: args,
result,
});
return result;
} catch (error) {
this.emitter.emit("error", {
message: error.message,
stack: error.stack,
});
} finally {
this.running--;
}
}
/**
* Waits for an available thread before executing a task.
* @returns {Promise<void>}
*/
async #waitForThread() {
return new Promise((resolve) => {
const checkThread = () => {
if (this.running < this.threads) {
resolve(true);
} else {
setTimeout(checkThread, 10);
}
};
checkThread();
});
}
}
export { Threading };