Comparing version 0.4.2 to 0.5.0
@@ -16,53 +16,50 @@ // Generated by dts-bundle v0.7.3 | ||
declare module 'promiso/concat' { | ||
import { AsyncFunction } from 'promiso/types'; | ||
const _default: <A, B>(items: A[], f: AsyncFunction<A, B[]>) => Promise<B[]>; | ||
export default _default; | ||
import { AsyncFunction, Collection } from 'promiso/types'; | ||
export default function <T, U>(items: Collection<T>, f: AsyncFunction<T, Array<U>>): Promise<Array<U>>; | ||
} | ||
declare module 'promiso/concatLimit' { | ||
import { AsyncFunction } from 'promiso/types'; | ||
const _default: <A, B>(items: A[], limit: number, f: AsyncFunction<A, B[]>) => Promise<B[]>; | ||
export default _default; | ||
import { AsyncFunction, Collection } from 'promiso/types'; | ||
export default function <T, U>(items: Collection<T>, limit: number, f: AsyncFunction<T, Array<U>>): Promise<Array<U>>; | ||
} | ||
declare module 'promiso/concatSeries' { | ||
import { AsyncFunction } from 'promiso/types'; | ||
const _default: <A, B>(items: A[], f: AsyncFunction<A, B[]>) => Promise<B[]>; | ||
export default _default; | ||
import { AsyncFunction, Collection } from 'promiso/types'; | ||
export default function <T, U>(items: Collection<T>, f: AsyncFunction<T, Array<U>>): Promise<Array<U>>; | ||
} | ||
declare module 'promiso/map' { | ||
import { AsyncFunction } from 'promiso/types'; | ||
const _default: <A, B>(items: A[], f: AsyncFunction<A, B>) => Promise<B[]>; | ||
export default _default; | ||
import { AsyncFunction, MapObject } from 'promiso/types'; | ||
export default function <T, U>(items: Array<T>, f: AsyncFunction<T, U>): Promise<Array<U>>; | ||
export default function <T, U>(items: MapObject<T>, f: AsyncFunction<T, U>): Promise<MapObject<U>>; | ||
} | ||
declare module 'promiso/mapLimit' { | ||
import { AsyncFunction } from 'promiso/types'; | ||
const _default: <A, B>(items: A[], limit: number, f: AsyncFunction<A, B>) => Promise<B[]>; | ||
export default _default; | ||
import { AsyncFunction, MapObject } from 'promiso/types'; | ||
export default function <T, U>(items: Array<T>, limit: number, f: AsyncFunction<T, U>): Promise<Array<U>>; | ||
export default function <T, U>(items: MapObject<T>, limit: number, f: AsyncFunction<T, U>): Promise<MapObject<U>>; | ||
} | ||
declare module 'promiso/mapSeries' { | ||
import { AsyncFunction } from 'promiso/types'; | ||
const _default: <A, B>(items: A[], f: AsyncFunction<A, B>) => Promise<B[]>; | ||
export default _default; | ||
import { AsyncFunction, MapObject } from 'promiso/types'; | ||
export default function <T, U>(items: Array<T>, f: AsyncFunction<T, U>): Promise<Array<U>>; | ||
export default function <T, U>(items: MapObject<T>, f: AsyncFunction<T, U>): Promise<MapObject<U>>; | ||
} | ||
declare module 'promiso/parallel' { | ||
import { AsyncSupplier } from 'promiso/types'; | ||
const _default: <T>(tasks: AsyncSupplier<T>[]) => Promise<T[]>; | ||
export default _default; | ||
import { AsyncSupplier, MapObject } from 'promiso/types'; | ||
export default function <T>(tasks: Array<AsyncSupplier<T>>): Promise<Array<T>>; | ||
export default function <T>(tasks: MapObject<AsyncSupplier<T>>): Promise<MapObject<T>>; | ||
} | ||
declare module 'promiso/parallelLimit' { | ||
import { AsyncSupplier } from 'promiso/types'; | ||
const _default: <T>(tasks: AsyncSupplier<T>[], limit?: number) => Promise<T[]>; | ||
export default _default; | ||
import { AsyncSupplier, MapObject } from 'promiso/types'; | ||
export default function <T>(tasks: Array<AsyncSupplier<T>>, limit: number): Promise<Array<T>>; | ||
export default function <T>(tasks: MapObject<AsyncSupplier<T>>, limit: number): Promise<MapObject<T>>; | ||
} | ||
declare module 'promiso/series' { | ||
import { AsyncSupplier } from 'promiso/types'; | ||
const _default: <T>(tasks: AsyncSupplier<T>[]) => Promise<T[]>; | ||
export default _default; | ||
import { AsyncSupplier, MapObject } from 'promiso/types'; | ||
export default function <T>(tasks: Array<AsyncSupplier<T>>): Promise<Array<T>>; | ||
export default function <T>(tasks: MapObject<AsyncSupplier<T>>): Promise<MapObject<T>>; | ||
} | ||
@@ -72,3 +69,6 @@ | ||
export type AsyncResult<T> = Promise<T> | T; | ||
export type Collection<T> = Array<T>; | ||
export type Collection<T> = Array<T> | MapObject<T>; | ||
export interface MapObject<T> { | ||
[name: string]: T; | ||
} | ||
export type AsyncFunction<A, B> = (value: A) => AsyncResult<B>; | ||
@@ -75,0 +75,0 @@ export type AsyncSupplier<T> = () => AsyncResult<T>; |
@@ -16,4 +16,36 @@ (function (global, factory) { | ||
}; | ||
var flatten = function (items) { return items.reduce(reduceMerge, []); }; | ||
var mapCollection = function (collection, f) { return collection.map(f); }; | ||
var map = function (collection, f) { | ||
if (Array.isArray(collection)) { | ||
return collection.map(function (value, index, _) { return f(value, index); }); | ||
} | ||
else { | ||
var output = {}; | ||
for (var key in collection) { | ||
if (!Object.prototype.hasOwnProperty.call(collection, key)) { | ||
continue; | ||
} | ||
var value = collection[key]; | ||
output[key] = f(value, key); | ||
} | ||
return output; | ||
} | ||
}; | ||
var getValues = function (collection) { | ||
if (Array.isArray(collection)) { | ||
return collection.slice(); | ||
} | ||
else { | ||
var values = []; | ||
for (var key in collection) { | ||
if (!Object.prototype.hasOwnProperty.call(collection, key)) { | ||
continue; | ||
} | ||
values.push(collection[key]); | ||
} | ||
return values; | ||
} | ||
}; | ||
var flatten = function (items) { | ||
return getValues(items).reduce(reduceMerge, []); | ||
}; | ||
var startThread = function (tasks, handle) { | ||
@@ -32,3 +64,3 @@ var p = Promise.resolve(); | ||
var runParallelTasks = function (originalTasks, limit) { | ||
var tasks = originalTasks.slice(); | ||
var tasks = getValues(originalTasks); | ||
var numThreads = Math.max(1, Math.min(tasks.length, limit)); | ||
@@ -49,38 +81,65 @@ var chunkSize = Math.ceil(tasks.length / numThreads); | ||
}; | ||
var mapParallelTasks = function (tasks, limit) { | ||
var internalTasks; | ||
var output; | ||
if (Array.isArray(tasks)) { | ||
var results_1 = output = []; | ||
internalTasks = map(tasks, function (task, index) { return function () { | ||
return Promise.resolve().then(task).then(function (result) { | ||
results_1[index] = result; | ||
}); | ||
}; }); | ||
} | ||
else { | ||
var results_2 = output = {}; | ||
internalTasks = map(tasks, function (task, key) { return function () { | ||
return Promise.resolve().then(task).then(function (result) { | ||
results_2[key] = result; | ||
}); | ||
}; }); | ||
} | ||
return runParallelTasks(internalTasks, limit).then(function () { return output; }); | ||
}; | ||
function parallelLimit (tasks, limit) { | ||
if (limit === void 0) { limit = Infinity; } | ||
var results = Array(tasks.length); | ||
var threadTasks = tasks.map(function (task, index) { return function () { | ||
return Promise.resolve(task()).then(function (result) { | ||
results[index] = result; | ||
}); | ||
}; }); | ||
return runParallelTasks(threadTasks, limit).then(function () { return results; }); | ||
function concat (items, f) { | ||
var tasks = map(items, function (item) { return function () { return f(item); }; }); | ||
return mapParallelTasks(tasks, Infinity).then(flatten); | ||
} | ||
function mapLimit (items, limit, f) { | ||
var tasks = mapCollection(items, function (item) { return function () { return f(item); }; }); | ||
return parallelLimit(tasks, limit); | ||
function concatLimit (items, limit, f) { | ||
var tasks = map(items, function (item) { return function () { return f(item); }; }); | ||
return mapParallelTasks(tasks, limit).then(flatten); | ||
} | ||
function concatLimit (items, limit, f) { | ||
return mapLimit(items, limit, f).then(flatten); | ||
function concatSeries (items, f) { | ||
var tasks = map(items, function (item) { return function () { return f(item); }; }); | ||
return mapParallelTasks(tasks, 1).then(flatten); | ||
} | ||
function concat (items, f) { | ||
return concatLimit(items, Infinity, f); | ||
function map$1 (items, f) { | ||
var tasks = map(items, function (item) { return function () { return f(item); }; }); | ||
return mapParallelTasks(tasks, Infinity); | ||
} | ||
function concatSeries (items, f) { | ||
return concatLimit(items, 1, f); | ||
function mapLimit (items, limit, f) { | ||
var tasks = map(items, function (item) { return function () { return f(item); }; }); | ||
return mapParallelTasks(tasks, limit); | ||
} | ||
function map (items, f) { return mapLimit(items, Infinity, f); } | ||
function mapSeries (items, f) { | ||
var tasks = map(items, function (item) { return function () { return f(item); }; }); | ||
return mapParallelTasks(tasks, 1); | ||
} | ||
function mapSeries (items, f) { return mapLimit(items, 1, f); } | ||
function parallel (tasks) { | ||
return mapParallelTasks(tasks, Infinity); | ||
} | ||
function parallel (tasks) { return parallelLimit(tasks, Infinity); } | ||
function parallelLimit (tasks, limit) { | ||
return mapParallelTasks(tasks, limit); | ||
} | ||
function series (tasks) { return parallelLimit(tasks, 1); } | ||
function series (tasks) { | ||
return mapParallelTasks(tasks, 1); | ||
} | ||
@@ -90,3 +149,3 @@ exports.concat = concat; | ||
exports.concatSeries = concatSeries; | ||
exports.map = map; | ||
exports.map = map$1; | ||
exports.mapLimit = mapLimit; | ||
@@ -93,0 +152,0 @@ exports.mapSeries = mapSeries; |
@@ -1,2 +0,2 @@ | ||
!function(n,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t(n.promiso={})}(this,function(n){"use strict";function t(n,t){void 0===t&&(t=1/0);var e=Array(n.length),r=n.map(function(n,t){return function(){return Promise.resolve(n()).then(function(n){e[t]=n})}});return m(r,t).then(function(){return e})}function e(n,e,r){return t(p(n,function(n){return function(){return r(n)}}),e)}function r(n,t,r){return e(n,t,r).then(s)}function u(n,t){return r(n,1/0,t)}function i(n,t){return r(n,1,t)}function o(n,t){return e(n,1/0,t)}function c(n,t){return e(n,1,t)}function f(n){return t(n,1/0)}function a(n){return t(n,1)}var l=function(n,t){return Array.isArray(t)?n.push.apply(n,t):n.push(t),n},s=function(n){return n.reduce(l,[])},p=function(n,t){return n.map(t)},h=function(n,t){var e=Promise.resolve();return n.forEach(function(n){e=e.then(n).then(function(){if(t.canceled)throw new Error("canceled")})}),e},m=function(n,t){for(var e=n.slice(),r=Math.max(1,Math.min(e.length,t)),u=Math.ceil(e.length/r),i=[];i.length<r;)i.push(e.splice(0,u));var o={},c=i.map(function(n){return h(n,o)});return Promise.all(c).then(function(){}).catch(function(n){throw o.canceled=!0,n})};n.concat=u,n.concatLimit=r,n.concatSeries=i,n.map=o,n.mapLimit=e,n.mapSeries=c,n.parallel=f,n.parallelLimit=t,n.series=a,Object.defineProperty(n,"__esModule",{value:!0})}); | ||
!function(n,r){"object"==typeof exports&&"undefined"!=typeof module?r(exports):"function"==typeof define&&define.amd?define(["exports"],r):r(n.promiso={})}(this,function(n){"use strict";function r(n,r){var t=l(n,function(n){return function(){return r(n)}});return y(t,1/0).then(p)}function t(n,r,t){var e=l(n,function(n){return function(){return t(n)}});return y(e,r).then(p)}function e(n,r){var t=l(n,function(n){return function(){return r(n)}});return y(t,1).then(p)}function u(n,r){var t=l(n,function(n){return function(){return r(n)}});return y(t,1/0)}function i(n,r,t){var e=l(n,function(n){return function(){return t(n)}});return y(e,r)}function o(n,r){var t=l(n,function(n){return function(){return r(n)}});return y(t,1)}function c(n){return y(n,1/0)}function f(n,r){return y(n,r)}function a(n){return y(n,1)}var s=function(n,r){return Array.isArray(r)?n.push.apply(n,r):n.push(r),n},l=function(n,r){if(Array.isArray(n))return n.map(function(n,t,e){return r(n,t)});var t={};for(var e in n)if(Object.prototype.hasOwnProperty.call(n,e)){var u=n[e];t[e]=r(u,e)}return t},h=function(n){if(Array.isArray(n))return n.slice();var r=[];for(var t in n)Object.prototype.hasOwnProperty.call(n,t)&&r.push(n[t]);return r},p=function(n){return h(n).reduce(s,[])},v=function(n,r){var t=Promise.resolve();return n.forEach(function(n){t=t.then(n).then(function(){if(r.canceled)throw new Error("canceled")})}),t},m=function(n,r){for(var t=h(n),e=Math.max(1,Math.min(t.length,r)),u=Math.ceil(t.length/e),i=[];i.length<e;)i.push(t.splice(0,u));var o={},c=i.map(function(n){return v(n,o)});return Promise.all(c).then(function(){}).catch(function(n){throw o.canceled=!0,n})},y=function(n,r){var t,e;if(Array.isArray(n)){var u=e=[];t=l(n,function(n,r){return function(){return Promise.resolve().then(n).then(function(n){u[r]=n})}})}else{var i=e={};t=l(n,function(n,r){return function(){return Promise.resolve().then(n).then(function(n){i[r]=n})}})}return m(t,r).then(function(){return e})};n.concat=r,n.concatLimit=t,n.concatSeries=e,n.map=u,n.mapLimit=i,n.mapSeries=o,n.parallel=c,n.parallelLimit=f,n.series=a,Object.defineProperty(n,"__esModule",{value:!0})}); | ||
//# sourceMappingURL=promiso.min.js.map |
{ | ||
"name": "promiso", | ||
"version": "0.4.2", | ||
"version": "0.5.0", | ||
"description": "Powerful promise utilities for any JS environment", | ||
@@ -5,0 +5,0 @@ "main": "dist/promiso", |
@@ -1,6 +0,8 @@ | ||
import concatLimit from './concatLimit'; | ||
import { AsyncFunction, Collection } from './types'; | ||
import { AsyncFunction, AsyncSupplier, Collection } from './types'; | ||
import { flatten, map, mapParallelTasks } from './utils'; | ||
export default <A, B> (items: Collection<A>, f: AsyncFunction<A, Array<B>>): Promise<Array<B>> => { | ||
return concatLimit(items, Infinity, f); | ||
}; | ||
export default function<T, U>(items: Collection<T>, | ||
f: AsyncFunction<T, Array<U>>): Promise<Array<U>> { | ||
const tasks: Collection<AsyncSupplier<Array<U>>> = map(items, (item) => () => f(item)); | ||
return mapParallelTasks(tasks, Infinity).then(flatten); | ||
} |
@@ -1,8 +0,8 @@ | ||
import mapLimit from './mapLimit'; | ||
import { AsyncFunction, Collection } from './types'; | ||
import { flatten } from './utils'; | ||
import { AsyncFunction, AsyncSupplier, Collection } from './types'; | ||
import { flatten, map, mapParallelTasks } from './utils'; | ||
export default <A, B> (items: Collection<A>, limit: number, | ||
f: AsyncFunction<A, Array<B>>): Promise<Array<B>> => { | ||
return mapLimit(items, limit, f).then(flatten); | ||
}; | ||
export default function<T, U>(items: Collection<T>, limit: number, | ||
f: AsyncFunction<T, Array<U>>): Promise<Array<U>> { | ||
const tasks: Collection<AsyncSupplier<Array<U>>> = map(items, (item) => () => f(item)); | ||
return mapParallelTasks(tasks, limit).then(flatten); | ||
} |
@@ -1,6 +0,8 @@ | ||
import concatLimit from './concatLimit'; | ||
import { AsyncFunction, Collection } from './types'; | ||
import { AsyncFunction, AsyncSupplier, Collection } from './types'; | ||
import { flatten, map, mapParallelTasks } from './utils'; | ||
export default <A, B> (items: Collection<A>, f: AsyncFunction<A, Array<B>>): Promise<Array<B>> => { | ||
return concatLimit(items, 1, f); | ||
}; | ||
export default function<T, U>(items: Collection<T>, | ||
f: AsyncFunction<T, Array<U>>): Promise<Array<U>> { | ||
const tasks: Collection<AsyncSupplier<Array<U>>> = map(items, (item) => () => f(item)); | ||
return mapParallelTasks(tasks, 1).then(flatten); | ||
} |
@@ -1,4 +0,11 @@ | ||
import mapLimit from './mapLimit'; | ||
import { AsyncFunction, Collection } from './types'; | ||
import { AsyncFunction, AsyncSupplier, Collection, MapObject } from './types'; | ||
import { map, mapParallelTasks } from './utils'; | ||
export default <A, B>(items: Collection<A>, f: AsyncFunction<A, B>) => mapLimit(items, Infinity, f); | ||
export default function<T, U>(items: Array<T>, f: AsyncFunction<T, U>): Promise<Array<U>>; | ||
export default function<T, U>(items: MapObject<T>, | ||
f: AsyncFunction<T, U>): Promise<MapObject<U>>; | ||
export default function<T, U>(items: Collection<T>, | ||
f: AsyncFunction<T, U>): Promise<Collection<U>> { | ||
const tasks: Collection<AsyncSupplier<U>> = map(items, (item) => () => f(item)); | ||
return mapParallelTasks(tasks, Infinity); | ||
} |
@@ -1,9 +0,12 @@ | ||
import parallelLimit from './parallelLimit'; | ||
import { AsyncFunction, AsyncSupplier, Collection } from './types'; | ||
import { mapCollection } from './utils'; | ||
import { AsyncFunction, AsyncSupplier, Collection, MapObject } from './types'; | ||
import { map, mapParallelTasks } from './utils'; | ||
export default <A, B> (items: Collection<A>, limit: number, | ||
f: AsyncFunction<A, B>): Promise<Array<B>> => { | ||
const tasks: Array<AsyncSupplier<B>> = mapCollection(items, (item) => () => f(item)); | ||
return parallelLimit(tasks, limit); | ||
}; | ||
export default function<T, U>(items: Array<T>, limit: number, | ||
f: AsyncFunction<T, U>): Promise<Array<U>>; | ||
export default function<T, U>(items: MapObject<T>, limit: number, | ||
f: AsyncFunction<T, U>): Promise<MapObject<U>>; | ||
export default function<T, U>(items: Collection<T>, limit: number, | ||
f: AsyncFunction<T, U>): Promise<Collection<U>> { | ||
const tasks: Collection<AsyncSupplier<U>> = map(items, (item) => () => f(item)); | ||
return mapParallelTasks(tasks, limit); | ||
} |
@@ -1,4 +0,11 @@ | ||
import mapLimit from './mapLimit'; | ||
import { AsyncFunction, Collection } from './types'; | ||
import { AsyncFunction, AsyncSupplier, Collection, MapObject } from './types'; | ||
import { map, mapParallelTasks } from './utils'; | ||
export default <A, B>(items: Collection<A>, f: AsyncFunction<A, B>) => mapLimit(items, 1, f); | ||
export default function<T, U>(items: Array<T>, f: AsyncFunction<T, U>): Promise<Array<U>>; | ||
export default function<T, U>(items: MapObject<T>, | ||
f: AsyncFunction<T, U>): Promise<MapObject<U>>; | ||
export default function<T, U>(items: Collection<T>, | ||
f: AsyncFunction<T, U>): Promise<Collection<U>> { | ||
const tasks: Collection<AsyncSupplier<U>> = map(items, (item) => () => f(item)); | ||
return mapParallelTasks(tasks, 1); | ||
} |
@@ -1,4 +0,8 @@ | ||
import parallelLimit from './parallelLimit'; | ||
import { AsyncSupplier } from './types'; | ||
import { AsyncSupplier, Collection, MapObject } from './types'; | ||
import { mapParallelTasks } from './utils'; | ||
export default <T>(tasks: Array<AsyncSupplier<T>>) => parallelLimit(tasks, Infinity); | ||
export default function<T>(tasks: Array<AsyncSupplier<T>>): Promise<Array<T>>; | ||
export default function<T>(tasks: MapObject<AsyncSupplier<T>>): Promise<MapObject<T>>; | ||
export default function<T>(tasks: Collection<AsyncSupplier<T>>): Promise<Collection<T>> { | ||
return mapParallelTasks(tasks, Infinity); | ||
} |
@@ -1,12 +0,10 @@ | ||
import { AsyncSupplier } from './types'; | ||
import { runParallelTasks } from './utils'; | ||
import { AsyncSupplier, Collection, MapObject } from './types'; | ||
import { mapParallelTasks } from './utils'; | ||
export default <T>(tasks: Array<AsyncSupplier<T>>, limit = Infinity): Promise<Array<T>> => { | ||
const results: Array<T> = Array(tasks.length); | ||
const threadTasks: Array<AsyncSupplier<void>> = tasks.map((task, index) => () => { | ||
return Promise.resolve(task()).then((result) => { | ||
results[index] = result; | ||
}); | ||
}); | ||
return runParallelTasks(threadTasks, limit).then(() => results); | ||
}; | ||
export default function<T>(tasks: Array<AsyncSupplier<T>>, limit: number): Promise<Array<T>>; | ||
export default function<T>(tasks: MapObject<AsyncSupplier<T>>, | ||
limit: number): Promise<MapObject<T>>; | ||
export default function<T>(tasks: Collection<AsyncSupplier<T>>, | ||
limit: number): Promise<Collection<T>> { | ||
return mapParallelTasks(tasks, limit); | ||
} |
@@ -1,4 +0,8 @@ | ||
import parallelLimit from './parallelLimit'; | ||
import { AsyncSupplier } from './types'; | ||
import { AsyncSupplier, Collection, MapObject } from './types'; | ||
import { mapParallelTasks } from './utils'; | ||
export default <T>(tasks: Array<AsyncSupplier<T>>) => parallelLimit(tasks, 1); | ||
export default function<T>(tasks: Array<AsyncSupplier<T>>): Promise<Array<T>>; | ||
export default function<T>(tasks: MapObject<AsyncSupplier<T>>): Promise<MapObject<T>>; | ||
export default function<T>(tasks: Collection<AsyncSupplier<T>>): Promise<Collection<T>> { | ||
return mapParallelTasks(tasks, 1); | ||
} |
export type AsyncResult<T> = Promise<T> | T; | ||
export type Collection<T> = Array<T>; | ||
export type Collection<T> = Array<T> | MapObject<T>; | ||
export interface MapObject<T> { | ||
[name: string]: T; | ||
} | ||
export type AsyncFunction<A, B> = (value: A) => AsyncResult<B>; | ||
export type AsyncSupplier<T> = () => AsyncResult<T>; |
@@ -1,2 +0,2 @@ | ||
import { AsyncSupplier, Collection } from './types'; | ||
import { AsyncSupplier, Collection, MapObject } from './types'; | ||
@@ -12,7 +12,35 @@ const reduceMerge = <T> (accum: Array<T>, target: Array<T> | T): Array<T> => { | ||
export const flatten = <T> (items: Array<Array<T>>): Array<T> => items.reduce(reduceMerge, []); | ||
export const map = <T, U>(collection: Collection<T>, | ||
// tslint:disable-next-line:no-any | ||
f: (item: T, index: any) => U): Collection<U> => { | ||
if (Array.isArray(collection)) { | ||
return collection.map((value, index, _) => f(value, index)); | ||
} else { | ||
const output: MapObject<U> = {}; | ||
for (const key in collection) { | ||
if (!Object.prototype.hasOwnProperty.call(collection, key)) { continue; } | ||
const value = collection[key]; | ||
output[key] = f(value, key); | ||
} | ||
return output; | ||
} | ||
}; | ||
export const mapCollection = <T, U> (collection: Collection<T>, | ||
f: (item: T) => U): Collection<U> => collection.map(f); | ||
const getValues = <T>(collection: Collection<T>): Array<T> => { | ||
if (Array.isArray(collection)) { | ||
return collection.slice(); | ||
} else { | ||
const values = []; | ||
for (const key in collection) { | ||
if (!Object.prototype.hasOwnProperty.call(collection, key)) { continue; } | ||
values.push(collection[key]); | ||
} | ||
return values; | ||
} | ||
}; | ||
export const flatten = <T> (items: Collection<Array<T> | T>): Array<T> => { | ||
return getValues(items).reduce(reduceMerge, []); | ||
}; | ||
const startThread = (tasks: Array<AsyncSupplier<void>>, | ||
@@ -30,5 +58,5 @@ handle: CancelHandle): Promise<void> => { | ||
export const runParallelTasks = (originalTasks: Array<AsyncSupplier<void>>, | ||
export const runParallelTasks = (originalTasks: Collection<AsyncSupplier<void>>, | ||
limit: number): Promise<void> => { | ||
const tasks = originalTasks.slice(); | ||
const tasks = getValues(originalTasks); | ||
const numThreads = Math.max(1, Math.min(tasks.length, limit)); | ||
@@ -50,4 +78,26 @@ const chunkSize = Math.ceil(tasks.length / numThreads); | ||
export const mapParallelTasks = <T>(tasks: Collection<AsyncSupplier<T>>, | ||
limit: number): Promise<Collection<T>> => { | ||
let internalTasks; | ||
let output: Collection<T>; | ||
if (Array.isArray(tasks)) { | ||
const results: Array<T> = output = []; | ||
internalTasks = map(tasks, (task, index) => () => { | ||
return Promise.resolve().then(task).then((result) => { | ||
results[index] = result; | ||
}); | ||
}); | ||
} else { | ||
const results: MapObject<T> = output = {}; | ||
internalTasks = map(tasks, (task, key) => () => { | ||
return Promise.resolve().then(task).then((result) => { | ||
results[key] = result; | ||
}); | ||
}); | ||
} | ||
return runParallelTasks(internalTasks, limit).then(() => output); | ||
}; | ||
interface CancelHandle { | ||
canceled?: boolean; | ||
} |
@@ -11,8 +11,2 @@ const parallelLimit = require('../src/parallelLimit').default; | ||
it('should default to no limit', async () => { | ||
const { stats, tasks } = createTasksAndStats(25); | ||
await parallelLimit(tasks); | ||
expect(stats.maxActive).toBe(tasks.length); | ||
}); | ||
it('should return results in order', async () => { | ||
@@ -32,1 +26,12 @@ const tasks = createTasks(3); | ||
}); | ||
it('should work on objects', async () => { | ||
const tasks = {}; | ||
createTasks(3).forEach((task, index) => tasks[index] = task); | ||
const result = await parallelLimit(tasks, 2); | ||
expect(result).toEqual({ | ||
0: 0, | ||
1: 1, | ||
2: 2, | ||
}); | ||
}); |
@@ -13,2 +13,3 @@ { | ||
"no-require-imports": false, | ||
"only-arrow-functions": [true, "allow-declarations"], | ||
"promise-function-async": false, | ||
@@ -15,0 +16,0 @@ "quotemark": [true, "single", "avoid-escape"], |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
90162
883