batch-tasks
Advanced tools
Comparing version
@@ -7,3 +7,4 @@ interface Batch<T> { | ||
constructor(generator: () => Generator<Batch<T>>); | ||
static fromArray<T>(items: T[], batchSize: number): BatchTasks<T>; | ||
static fromArrayAndSize<T>(items: T[], batchSize: number): BatchTasks<T>; | ||
static fromArrayAndDuration<T>(items: T[], batchDuration: number): BatchTasks<T>; | ||
batches(): Generator<Batch<T>, any, unknown>; | ||
@@ -10,0 +11,0 @@ } |
@@ -24,2 +24,22 @@ "use strict"; | ||
} | ||
class DurationBatch { | ||
constructor(items, start, duration) { | ||
this.items = items; | ||
this.duration = duration; | ||
this._start = start; | ||
this._end = start; | ||
} | ||
run(action) { | ||
let index = this._start; | ||
const start = Date.now(); // TODO: should use performance.now() but in a browser+NodeJS friendly way. | ||
while (Date.now() < start + this.duration && index < this.items.length) { | ||
action(this.items[index], index); | ||
index += 1; | ||
} | ||
this._end = index; | ||
} | ||
get end() { | ||
return this._end; | ||
} | ||
} | ||
class BatchTasks { | ||
@@ -29,3 +49,3 @@ constructor(generator) { | ||
} | ||
static fromArray(items, batchSize) { | ||
static fromArrayAndSize(items, batchSize) { | ||
return new BatchTasks(function* () { | ||
@@ -39,2 +59,11 @@ for (let i = 0; i < items.length; i += batchSize) { | ||
} | ||
static fromArrayAndDuration(items, batchDuration) { | ||
return new BatchTasks(function* () { | ||
let lastBatch = null; | ||
while (lastBatch === null || lastBatch.end < items.length) { | ||
lastBatch = new DurationBatch(items, lastBatch === null ? 0 : lastBatch.end, batchDuration); | ||
yield lastBatch; | ||
} | ||
}); | ||
} | ||
batches() { | ||
@@ -41,0 +70,0 @@ return this._generator(); |
{ | ||
"name": "batch-tasks", | ||
"version": "0.1.1", | ||
"version": "0.1.2", | ||
"description": "Batch CPU bound tasks", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -23,2 +23,30 @@ interface Batch<T> { | ||
class DurationBatch<T> implements Batch<T> { | ||
public readonly items: T[]; | ||
public readonly duration: number; | ||
private _start: number; | ||
private _end: number; | ||
public constructor(items: T[], start: number, duration: number) { | ||
this.items = items; | ||
this.duration = duration; | ||
this._start = start; | ||
this._end = start; | ||
} | ||
public run(action: (item: T, index: number) => void) { | ||
let index = this._start; | ||
const start = Date.now(); // TODO: should use performance.now() but in a browser+NodeJS friendly way. | ||
while (Date.now() < start + this.duration && index < this.items.length) { | ||
action(this.items[index], index); | ||
index += 1; | ||
} | ||
this._end = index; | ||
} | ||
public get end() { | ||
return this._end; | ||
} | ||
} | ||
export class BatchTasks<T> { | ||
@@ -31,3 +59,3 @@ private _generator: () => Generator<Batch<T>>; | ||
public static fromArray<T>(items: T[], batchSize: number) { | ||
public static fromArrayAndSize<T>(items: T[], batchSize: number) { | ||
return new BatchTasks(function* () { | ||
@@ -42,2 +70,12 @@ for (let i = 0; i < items.length; i += batchSize) { | ||
public static fromArrayAndDuration<T>(items: T[], batchDuration: number) { | ||
return new BatchTasks(function* () { | ||
let lastBatch: DurationBatch<T> | null = null; | ||
while (lastBatch === null || lastBatch.end < items.length) { | ||
lastBatch = new DurationBatch(items, lastBatch === null ? 0 : lastBatch.end, batchDuration); | ||
yield lastBatch; | ||
} | ||
}); | ||
} | ||
public batches() { | ||
@@ -44,0 +82,0 @@ return this._generator(); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
84061
4.18%220
40.13%