async-batch
Advanced tools
Comparing version 1.0.4 to 1.1.0
@@ -42,5 +42,8 @@ import "jest"; | ||
input, | ||
(t) => (new Promise( | ||
(resolve) => setTimeout(() => processingOrder.push(t) && resolve(t*t), t*25) | ||
)), | ||
(t) => new Promise( | ||
(resolve) => setTimeout( | ||
() => processingOrder.push(t) && resolve(t*t), | ||
t*25, | ||
), | ||
), | ||
2, | ||
@@ -53,12 +56,22 @@ ); | ||
test('concurrency validation', async () => { | ||
test('task and worker index', async () => { | ||
const input: number[] = range(0, 2); | ||
const subject = () => asyncBatch( | ||
input, | ||
async (t) => t, | ||
0, | ||
async (t, taskIndex, workerIndex) => { | ||
expect(t).toEqual(taskIndex); | ||
expect(taskIndex).toEqual(workerIndex); | ||
return t; | ||
}, | ||
5, | ||
); | ||
await expect(subject()).rejects.toEqual(new Error("The value of 'workers' must be at least 1")); | ||
await expect(subject()).resolves.toEqual(input); | ||
}); | ||
test('worker count minimum', async () => { | ||
await expect( | ||
asyncBatch([1], (t) => new Promise((r) => r(t)), 0), | ||
).resolves.toEqual([1]); | ||
}); | ||
}); |
@@ -39,4 +39,4 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var asyncBatch = function (tasks, handler, _workers) { return __awaiter(_this, void 0, void 0, function () { | ||
var workersCount, workers, i_1, results, i; | ||
var asyncBatch = function (tasks, handler, desiredWorkers) { return __awaiter(_this, void 0, void 0, function () { | ||
var workersCount, results, i; | ||
var _this = this; | ||
@@ -46,14 +46,7 @@ return __generator(this, function (_a) { | ||
case 0: | ||
workersCount = Math.floor(_workers); | ||
if (workersCount < 1) { | ||
throw new Error("The value of 'workers' must be at least 1"); | ||
} | ||
workers = new Array(workersCount); | ||
for (i_1 = 0; i_1 < workersCount; i_1++) { | ||
workers[i_1] = i_1; | ||
} | ||
workersCount = Math.max(Math.floor(Math.min(desiredWorkers, tasks.length)), 1); | ||
results = []; | ||
i = 0; | ||
return [4 /*yield*/, Promise.all(workers.map(function () { return __awaiter(_this, void 0, void 0, function () { | ||
var taskId, _a, _b; | ||
return [4 /*yield*/, Promise.all(Array.from({ length: workersCount }).map(function (w, workerIndex) { return __awaiter(_this, void 0, void 0, function () { | ||
var taskIndex, _a, _b; | ||
return __generator(this, function (_c) { | ||
@@ -63,7 +56,7 @@ switch (_c.label) { | ||
if (!(i < tasks.length)) return [3 /*break*/, 2]; | ||
taskId = i; | ||
taskIndex = i; | ||
i++; | ||
_a = results; | ||
_b = taskId; | ||
return [4 /*yield*/, handler(tasks[taskId])]; | ||
_b = taskIndex; | ||
return [4 /*yield*/, handler(tasks[taskIndex], taskIndex, workerIndex)]; | ||
case 1: | ||
@@ -70,0 +63,0 @@ _a[_b] = _c.sent(); |
{ | ||
"name": "async-batch", | ||
"version": "1.0.4", | ||
"version": "1.1.0", | ||
"description": "Asynchronously process task batches", | ||
@@ -19,3 +19,3 @@ "main": "lib/index.js", | ||
"jest": "24.8.0", | ||
"lodash": "4.17.14", | ||
"lodash": "4.17.19", | ||
"npm-check": "^5.9.0", | ||
@@ -22,0 +22,0 @@ "ts-jest": "24.0.2", |
@@ -32,7 +32,7 @@ # async-batch | ||
input, | ||
(task: number) => new Promise( | ||
(task: number, taskIndex: number, workerIndex: number) => new Promise( | ||
(resolve) => setTimeout( | ||
() => processingOrder.push(task) && resolve(task * task), | ||
task * 25, | ||
) | ||
), | ||
), | ||
@@ -39,0 +39,0 @@ 2, |
const asyncBatch = async <TaskType, ResultType>( | ||
tasks: TaskType[], | ||
handler: (task: TaskType) => Promise<ResultType>, | ||
_workers: number, | ||
handler: (task: TaskType, taskIndex: number, workerIndex: number) => Promise<ResultType>, | ||
desiredWorkers: number, | ||
): Promise<ResultType[]> => { | ||
const workersCount = Math.floor(_workers); | ||
if (workersCount < 1) { | ||
throw new Error("The value of 'workers' must be at least 1"); | ||
} | ||
// Cap workers count to task list size, with a min of 1 worker | ||
const workersCount = Math.max(Math.floor(Math.min(desiredWorkers, tasks.length)), 1); | ||
const workers = new Array(workersCount); | ||
for (let i = 0; i < workersCount; i++) { | ||
workers[i] = i; | ||
} | ||
const results: ResultType[] = []; | ||
let i = 0; | ||
await Promise.all(workers.map(async () => { | ||
while (i < tasks.length) { | ||
const taskId = i; | ||
i++; | ||
results[taskId] = await handler(tasks[taskId]); | ||
} | ||
})); | ||
await Promise.all( | ||
Array.from({ length: workersCount }).map(async (w, workerIndex) => { | ||
while (i < tasks.length) { | ||
const taskIndex = i; | ||
i++; | ||
results[taskIndex] = await handler(tasks[taskIndex], taskIndex, workerIndex); | ||
} | ||
}), | ||
); | ||
return results; | ||
@@ -26,0 +21,0 @@ }; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
10308
8
205