paralysis 1.0.3 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -2,7 +2,13 @@
2
2
 
3
3
  var TinyQueue = require('tinyqueue');
4
4
 
5
- function createRun(concurrency) {
5
+ function paralysis(list, fn, concurrency = Infinity) {
6
+ if (typeof fn !== "function") {
7
+ throw new TypeError(`${fn} is not a function`);
8
+ }
9
+ if (!(typeof concurrency === "number" && concurrency >= 1)) {
10
+ throw new TypeError("Expected `concurrency` to be a number from 1 and up");
11
+ }
6
12
  const queue = new TinyQueue();
7
13
  let slots = Math.floor(concurrency);
8
14
  function release() {
@@ -14,12 +20,12 @@ function createRun(concurrency) {
14
20
  slots++;
15
21
  }
16
22
  }
17
- function runRecipe([fn, args]) {
23
+ function runRecipe([fn2, args]) {
18
24
  return new Promise((resolve) => {
19
- resolve(fn(...args));
25
+ resolve(fn2(...args));
20
26
  }).finally(release);
21
27
  }
22
- return function run(recipe) {
28
+ function run(recipe) {
23
29
  if (slots > 0) {
24
30
  slots--;
25
31
  return runRecipe(recipe);
@@ -27,13 +33,7 @@ function createRun(concurrency) {
27
33
  return new Promise((resolve) => {
28
34
  queue.push([resolve, recipe]);
29
35
  }).then(runRecipe);
30
- };
31
- }
32
- function paralysis(list, fn, concurrency = Infinity) {
33
- if (!(typeof concurrency === "number" && concurrency >= 1)) {
34
- throw new TypeError("Expected `concurrency` to be a number from 1 and up");
35
36
  }
36
- const run = createRun(concurrency);
37
37
  return Promise.all(Array.from(list, (...args) => run([fn, args])));
38
38
  }
39
39
 
@@ -1 +1 @@
1
- {"version":3,"file":"index.cjs","sources":["../src/paralysis.js"],"sourcesContent":["import TinyQueue from \"tinyqueue\";\n\nfunction createRun(concurrency) {\n const queue = new TinyQueue();\n let slots = Math.floor(concurrency);\n\n function release() {\n // Check if we have more functions waiting in the queue. If we\n // do, run the next delayed function. Otherwise, free a slot.\n const next = queue.pop();\n if (next) {\n const [resolve, delayed] = next;\n resolve(delayed);\n } else {\n slots++;\n }\n }\n\n function runRecipe([fn, args]) {\n return new Promise((resolve) => {\n resolve(fn(...args));\n }).finally(release);\n }\n\n return function run(recipe) {\n // If we have an available slot, take the slot and return\n // a promise that executes the function immediately.\n if (slots > 0) {\n slots--;\n return runRecipe(recipe);\n }\n\n // If the limit of slots is reached, enqueue the function\n // and return a promise that will be resolved only after\n // the next slot is released and the function is executed.\n return new Promise((resolve) => {\n queue.push([resolve, recipe]);\n }).then(runRecipe);\n };\n}\n\nexport function paralysis(list, fn, concurrency = Infinity) {\n if (!(typeof concurrency === \"number\" && concurrency >= 1)) {\n throw new TypeError(\"Expected `concurrency` to be a number from 1 and up\");\n }\n const run = createRun(concurrency);\n return Promise.all(Array.from(list, (...args) => run([fn, args])));\n}\n"],"names":[],"mappings":";;;;AAEA,SAAS,UAAU,WAAA,EAAa;AAC9B,EAAA,MAAM,KAAA,GAAQ,IAAI,SAAA,EAAU;AAC5B,EAAA,IAAI,KAAA,GAAQ,IAAA,CAAK,KAAA,CAAM,WAAW,CAAA;AAElC,EAAA,SAAS,OAAA,GAAU;AAGjB,IAAA,MAAM,IAAA,GAAO,MAAM,GAAA,EAAI;AACvB,IAAA,IAAI,IAAA,EAAM;AACR,MAAA,MAAM,CAAC,OAAA,EAAS,OAAO,CAAA,GAAI,IAAA;AAC3B,MAAA,OAAA,CAAQ,OAAO,CAAA;AAAA,IACjB,CAAA,MAAO;AACL,MAAA,KAAA,EAAA;AAAA,IACF;AAAA,EACF;AAEA,EAAA,SAAS,SAAA,CAAU,CAAC,EAAA,EAAI,IAAI,CAAA,EAAG;AAC7B,IAAA,OAAO,IAAI,OAAA,CAAQ,CAAC,OAAA,KAAY;AAC9B,MAAA,OAAA,CAAQ,EAAA,CAAG,GAAG,IAAI,CAAC,CAAA;AAAA,IACrB,CAAC,CAAA,CAAE,OAAA,CAAQ,OAAO,CAAA;AAAA,EACpB;AAEA,EAAA,OAAO,SAAS,IAAI,MAAA,EAAQ;AAG1B,IAAA,IAAI,QAAQ,CAAA,EAAG;AACb,MAAA,KAAA,EAAA;AACA,MAAA,OAAO,UAAU,MAAM,CAAA;AAAA,IACzB;AAKA,IAAA,OAAO,IAAI,OAAA,CAAQ,CAAC,OAAA,KAAY;AAC9B,MAAA,KAAA,CAAM,IAAA,CAAK,CAAC,OAAA,EAAS,MAAM,CAAC,CAAA;AAAA,IAC9B,CAAC,CAAA,CAAE,IAAA,CAAK,SAAS,CAAA;AAAA,EACnB,CAAA;AACF;AAEO,SAAS,SAAA,CAAU,IAAA,EAAM,EAAA,EAAI,WAAA,GAAc,QAAA,EAAU;AAC1D,EAAA,IAAI,EAAE,OAAO,WAAA,KAAgB,QAAA,IAAY,eAAe,CAAA,CAAA,EAAI;AAC1D,IAAA,MAAM,IAAI,UAAU,qDAAqD,CAAA;AAAA,EAC3E;AACA,EAAA,MAAM,GAAA,GAAM,UAAU,WAAW,CAAA;AACjC,EAAA,OAAO,OAAA,CAAQ,GAAA,CAAI,KAAA,CAAM,IAAA,CAAK,IAAA,EAAM,CAAA,GAAI,IAAA,KAAS,GAAA,CAAI,CAAC,EAAA,EAAI,IAAI,CAAC,CAAC,CAAC,CAAA;AACnE;;;;"}
1
+ {"version":3,"file":"index.cjs","sources":["../src/paralysis.js"],"sourcesContent":["import TinyQueue from \"tinyqueue\";\n\nexport function paralysis(list, fn, concurrency = Infinity) {\n if (typeof fn !== \"function\") {\n throw new TypeError(`${fn} is not a function`);\n }\n\n if (!(typeof concurrency === \"number\" && concurrency >= 1)) {\n throw new TypeError(\"Expected `concurrency` to be a number from 1 and up\");\n }\n\n const queue = new TinyQueue();\n let slots = Math.floor(concurrency);\n\n function release() {\n // Check if we have more functions waiting in the queue. If we\n // do, run the next delayed function. Otherwise, free a slot.\n const next = queue.pop();\n if (next) {\n const [resolve, delayed] = next;\n resolve(delayed);\n } else {\n slots++;\n }\n }\n\n function runRecipe([fn, args]) {\n // Note that if `fn` throws an synchronous error\n // inside it will reject the promise. This wouldn't\n // happen if we used `Promise.resolve()` directly.\n return new Promise((resolve) => {\n resolve(fn(...args));\n }).finally(release);\n }\n\n function run(recipe) {\n // If we have an available slot, take the slot and return\n // a promise that executes the function immediately.\n if (slots > 0) {\n slots--;\n return runRecipe(recipe);\n }\n\n // If the limit of slots is reached, enqueue the function\n // and return a promise that will be resolved only after\n // the next slot is released and the function is executed.\n return new Promise((resolve) => {\n queue.push([resolve, recipe]);\n }).then(runRecipe);\n }\n\n return Promise.all(Array.from(list, (...args) => run([fn, args])));\n}\n"],"names":["fn"],"mappings":";;;;AAEO,SAAS,SAAA,CAAU,IAAA,EAAM,EAAA,EAAI,WAAA,GAAc,QAAA,EAAU;AAC1D,EAAA,IAAI,OAAO,OAAO,UAAA,EAAY;AAC5B,IAAA,MAAM,IAAI,SAAA,CAAU,CAAA,EAAG,EAAE,CAAA,kBAAA,CAAoB,CAAA;AAAA,EAC/C;AAEA,EAAA,IAAI,EAAE,OAAO,WAAA,KAAgB,QAAA,IAAY,eAAe,CAAA,CAAA,EAAI;AAC1D,IAAA,MAAM,IAAI,UAAU,qDAAqD,CAAA;AAAA,EAC3E;AAEA,EAAA,MAAM,KAAA,GAAQ,IAAI,SAAA,EAAU;AAC5B,EAAA,IAAI,KAAA,GAAQ,IAAA,CAAK,KAAA,CAAM,WAAW,CAAA;AAElC,EAAA,SAAS,OAAA,GAAU;AAGjB,IAAA,MAAM,IAAA,GAAO,MAAM,GAAA,EAAI;AACvB,IAAA,IAAI,IAAA,EAAM;AACR,MAAA,MAAM,CAAC,OAAA,EAAS,OAAO,CAAA,GAAI,IAAA;AAC3B,MAAA,OAAA,CAAQ,OAAO,CAAA;AAAA,IACjB,CAAA,MAAO;AACL,MAAA,KAAA,EAAA;AAAA,IACF;AAAA,EACF;AAEA,EAAA,SAAS,SAAA,CAAU,CAACA,GAAAA,EAAI,IAAI,CAAA,EAAG;AAI7B,IAAA,OAAO,IAAI,OAAA,CAAQ,CAAC,OAAA,KAAY;AAC9B,MAAA,OAAA,CAAQA,GAAAA,CAAG,GAAG,IAAI,CAAC,CAAA;AAAA,IACrB,CAAC,CAAA,CAAE,OAAA,CAAQ,OAAO,CAAA;AAAA,EACpB;AAEA,EAAA,SAAS,IAAI,MAAA,EAAQ;AAGnB,IAAA,IAAI,QAAQ,CAAA,EAAG;AACb,MAAA,KAAA,EAAA;AACA,MAAA,OAAO,UAAU,MAAM,CAAA;AAAA,IACzB;AAKA,IAAA,OAAO,IAAI,OAAA,CAAQ,CAAC,OAAA,KAAY;AAC9B,MAAA,KAAA,CAAM,IAAA,CAAK,CAAC,OAAA,EAAS,MAAM,CAAC,CAAA;AAAA,IAC9B,CAAC,CAAA,CAAE,IAAA,CAAK,SAAS,CAAA;AAAA,EACnB;AAEA,EAAA,OAAO,OAAA,CAAQ,GAAA,CAAI,KAAA,CAAM,IAAA,CAAK,IAAA,EAAM,CAAA,GAAI,IAAA,KAAS,GAAA,CAAI,CAAC,EAAA,EAAI,IAAI,CAAC,CAAC,CAAC,CAAA;AACnE;;;;"}
package/dist/index.mjs CHANGED
@@ -1,6 +1,12 @@
1
1
  import TinyQueue from 'tinyqueue';
2
2
 
3
- function createRun(concurrency) {
3
+ function paralysis(list, fn, concurrency = Infinity) {
4
+ if (typeof fn !== "function") {
5
+ throw new TypeError(`${fn} is not a function`);
6
+ }
7
+ if (!(typeof concurrency === "number" && concurrency >= 1)) {
8
+ throw new TypeError("Expected `concurrency` to be a number from 1 and up");
9
+ }
4
10
  const queue = new TinyQueue();
5
11
  let slots = Math.floor(concurrency);
6
12
  function release() {
@@ -12,12 +18,12 @@ function createRun(concurrency) {
12
18
  slots++;
13
19
  }
14
20
  }
15
- function runRecipe([fn, args]) {
21
+ function runRecipe([fn2, args]) {
16
22
  return new Promise((resolve) => {
17
- resolve(fn(...args));
23
+ resolve(fn2(...args));
18
24
  }).finally(release);
19
25
  }
20
- return function run(recipe) {
26
+ function run(recipe) {
21
27
  if (slots > 0) {
22
28
  slots--;
23
29
  return runRecipe(recipe);
@@ -25,13 +31,7 @@ function createRun(concurrency) {
25
31
  return new Promise((resolve) => {
26
32
  queue.push([resolve, recipe]);
27
33
  }).then(runRecipe);
28
- };
29
- }
30
- function paralysis(list, fn, concurrency = Infinity) {
31
- if (!(typeof concurrency === "number" && concurrency >= 1)) {
32
- throw new TypeError("Expected `concurrency` to be a number from 1 and up");
33
34
  }
34
- const run = createRun(concurrency);
35
35
  return Promise.all(Array.from(list, (...args) => run([fn, args])));
36
36
  }
37
37
 
@@ -1 +1 @@
1
- {"version":3,"file":"index.mjs","sources":["../src/paralysis.js"],"sourcesContent":["import TinyQueue from \"tinyqueue\";\n\nfunction createRun(concurrency) {\n const queue = new TinyQueue();\n let slots = Math.floor(concurrency);\n\n function release() {\n // Check if we have more functions waiting in the queue. If we\n // do, run the next delayed function. Otherwise, free a slot.\n const next = queue.pop();\n if (next) {\n const [resolve, delayed] = next;\n resolve(delayed);\n } else {\n slots++;\n }\n }\n\n function runRecipe([fn, args]) {\n return new Promise((resolve) => {\n resolve(fn(...args));\n }).finally(release);\n }\n\n return function run(recipe) {\n // If we have an available slot, take the slot and return\n // a promise that executes the function immediately.\n if (slots > 0) {\n slots--;\n return runRecipe(recipe);\n }\n\n // If the limit of slots is reached, enqueue the function\n // and return a promise that will be resolved only after\n // the next slot is released and the function is executed.\n return new Promise((resolve) => {\n queue.push([resolve, recipe]);\n }).then(runRecipe);\n };\n}\n\nexport function paralysis(list, fn, concurrency = Infinity) {\n if (!(typeof concurrency === \"number\" && concurrency >= 1)) {\n throw new TypeError(\"Expected `concurrency` to be a number from 1 and up\");\n }\n const run = createRun(concurrency);\n return Promise.all(Array.from(list, (...args) => run([fn, args])));\n}\n"],"names":[],"mappings":";;AAEA,SAAS,UAAU,WAAA,EAAa;AAC9B,EAAA,MAAM,KAAA,GAAQ,IAAI,SAAA,EAAU;AAC5B,EAAA,IAAI,KAAA,GAAQ,IAAA,CAAK,KAAA,CAAM,WAAW,CAAA;A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AI,IAAA,KAAS,GAAA,CAAI,CAAC,EAAA,EAAI,IAAI,CAAC,CAAC,CAAC,CAAA;AACnE;;;;"}
1
+ {"version":3,"file":"index.mjs","sources":["../src/paralysis.js"],"sourcesContent":["import TinyQueue from \"tinyqueue\";\n\nexport function paralysis(list, fn, concurrency = Infinity) {\n if (typeof fn !== \"function\") {\n throw new TypeError(`${fn} is not a function`);\n }\n\n if (!(typeof concurrency === \"number\" && concurrency >= 1)) {\n throw new TypeError(\"Expected `concurrency` to be a number from 1 and up\");\n }\n\n const queue = new TinyQueue();\n let slots = Math.floor(concurrency);\n\n function release() {\n // Check if we have more functions waiting in the queue. If we\n // do, run the next delayed function. Otherwise, free a slot.\n const next = queue.pop();\n if (next) {\n const [resolve, delayed] = next;\n resolve(delayed);\n } else {\n slots++;\n }\n }\n\n function runRecipe([fn, args]) {\n // Note that if `fn` throws an synchronous error\n // inside it will reject the promise. This wouldn't\n // happen if we used `Promise.resolve()` directly.\n return new Promise((resolve) => {\n resolve(fn(...args));\n }).finally(release);\n }\n\n function run(recipe) {\n // If we have an available slot, take the slot and return\n // a promise that executes the function immediately.\n if (slots > 0) {\n slots--;\n return runRecipe(recipe);\n }\n\n // If the limit of slots is reached, enqueue the function\n // and return a promise that will be resolved only after\n // the next slot is released and the function is executed.\n return new Promise((resolve) => {\n queue.push([resolve, recipe]);\n }).then(runRecipe);\n }\n\n return Promise.all(Array.from(list, (...args) => run([fn, args])));\n}\n"],"names":["fn"],"mappings":";;AAEO,SAAS,SAAA,CAAU,IAAA,EAAM,EAAA,EAAI,WAAA,GAAc,QAAA,EAAU;AAC1D,EAAA,IAAI,OAAO,OAAO,UAAA,EAAY;AAC5B,IAAA,MAAM,IAAI,SAAA,CAAU,CAAA,EAAG,EAAE,CAAA,kBAAA,CAAoB,CAAA;AAAA,EAC/C;AAEA,EAAA,IAAI,EAAE,OAAO,WAAA,KAAgB,QAAA,IAAY,eAAe,CAAA,CAAA,EAAI;AAC1D,IAAA,MAAM,IAA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@@
1
1
  {
2
2
  "name": "paralysis",
3
- "version": "1.0.3",
3
+ "version": "2.0.0",
4
4
  "description": "Map over promises concurrently",
5
5
  "repository": "th3rius/paralysis",
6
6
  "keywords": [
package/src/paralysis.js CHANGED
@@ -17,6 +17,9 @@ function createRun(concurrency) {
17
17
  }
18
18
 
19
19
  function runRecipe([fn, args]) {
20
+ // Note that if `fn` throws an synchronous error
21
+ // inside it will reject the promise. This wouldn't
22
+ // happen if we used `Promise.resolve()` directly.
20
23
  return new Promise((resolve) => {
21
24
  resolve(fn(...args));
22
25
  }).finally(release);
@@ -40,9 +43,14 @@ function createRun(concurrency) {
40
43
  }
41
44
 
42
45
  export function paralysis(list, fn, concurrency = Infinity) {
46
+ if (typeof fn !== "function") {
47
+ throw new TypeError(`${fn} is not a function`);
48
+ }
49
+
43
50
  if (!(typeof concurrency === "number" && concurrency >= 1)) {
44
51
  throw new TypeError("Expected `concurrency` to be a number from 1 and up");
45
52
  }
53
+
46
54
  const run = createRun(concurrency);
47
55
  return Promise.all(Array.from(list, (...args) => run([fn, args])));
48
56
  }