diff --git a/docs/jsdoc/AsyncQueue.html b/docs/jsdoc/AsyncQueue.html index 97a1379..f1be166 100644 --- a/docs/jsdoc/AsyncQueue.html +++ b/docs/jsdoc/AsyncQueue.html @@ -31,7 +31,7 @@ -
//to define a queue with a specified length of 3
-const queue = new Queue(3)
+const queue = new Queue({maxConcurrency: 3})
@@ -158,6 +158,61 @@ configthe config for asyncrify
+| Name | + + +Type | + + +Attributes | + + + +Description |
|---|
import Queue from 'asyncrify'
const queue = new Queue()
-queue.add(() => new Promise((resolve) => setTimeout(resolve, 200)), (res, err) => {
- //handle err or result
-})
+queue.add(() => new Promise(
+ (resolve) => setTimeout(resolve, 200)),
+ (res) => {
+ //handle result
+ },
+ (err) => {
+ //handle err
+ })
npm run test
@@ -127,7 +132,7 @@ 📝 License
diff --git a/docs/jsdoc/index.js.html b/docs/jsdoc/index.js.html
index c778ed9..e20cb55 100644
--- a/docs/jsdoc/index.js.html
+++ b/docs/jsdoc/index.js.html
@@ -31,7 +31,7 @@
- Home
Github repo
Classes
Namespaces
Global
+ Home
Github repo
Classes
Namespaces
Global
@@ -54,10 +54,18 @@ index.js
* @namespace AsyncQueue
*/
+/**
+ */
+export const QUEUE_ERRORS = {
+ ABORT: "Aborted",
+ INPUT_REQUIRED: "input is required",
+ MAX_RETRIES: "max retries reached",
+};
+
export default class AsyncQueue {
#queue = new Set();
#running = 0;
- #maxConcurrency = 5;
+ #maxConcurrency = 0;
#timeout = 0;
/**
* @type {RetryEngine| null}
@@ -68,9 +76,10 @@ index.js
* Create a Queue
*
* @memberof AsyncQueue
- * @param {number=} [maxConcurrency] - The max amount of promises to run concurrently
- * @param {number=} maxRetries - The max amount of promises to run concurrently
- * @param {number=} timeout - The max amount of time in ms a promise can take to settle
+ * @param {Object} [config] - the config for asyncrify
+ * @param {number=} config.maxConcurrency - The max amount of promises to run concurrently
+ * @param {number=} config.maxRetries - The max amount of promises to run concurrently
+ * @param {number=} config.timeout - The max amount of time in ms a promise can take to settle
* @example
*
* //to define a queue with a default length of 5
@@ -78,17 +87,20 @@ index.js
*
* @example
* //to define a queue with a specified length of 3
- * const queue = new Queue(3)
+ * const queue = new Queue({maxConcurrency: 3})
*/
- constructor(maxConcurrency, maxRetries, timeout) {
- inputValidation(maxConcurrency, "number", false);
- inputValidation(maxRetries, "number", false);
- inputValidation(timeout, "number", false);
- if (maxConcurrency) this.#maxConcurrency = maxConcurrency;
- if (maxRetries) {
- this.#retryEngine = new RetryEngine(maxRetries);
+ constructor(config) {
+ inputValidation(config, "object", false);
+ if (config) {
+ inputValidation(config.maxConcurrency, "number", false);
+ inputValidation(config.maxRetries, "number", false);
+ inputValidation(config.timeout, "number", false);
+ if (config.maxConcurrency) this.#maxConcurrency = config.maxConcurrency;
+ if (config.maxRetries) {
+ this.#retryEngine = new RetryEngine(config.maxRetries);
+ }
+ if (config.timeout) this.#timeout = config.timeout;
}
- if (timeout) this.#timeout = timeout;
}
/**
@@ -281,7 +293,7 @@ index.js
inputValidation(fn, "function", true);
inputValidation(callback, "function", true);
- if (this.#running >= this.#maxConcurrency) {
+ if (this.#maxConcurrency !== 0 && this.#running >= this.#maxConcurrency) {
this.#queue.add(fn);
} else {
this.#running++;
@@ -373,7 +385,7 @@ index.js
diff --git a/docs/package.md b/docs/package.md
index 75cef95..37e13b9 100644
--- a/docs/package.md
+++ b/docs/package.md
@@ -8,6 +8,8 @@
## Constants
+- QUEUE_ERRORS
+
- abortHandler
abort handler for handling aborts in your promise
@@ -31,7 +33,7 @@
* [AsyncQueue](#AsyncQueue) : object
* [.module.exports](#AsyncQueue.module.exports)
- * [new module.exports([maxConcurrency], [maxRetries], [timeout])](#new_AsyncQueue.module.exports_new)
+ * [new module.exports([config])](#new_AsyncQueue.module.exports_new)
* [.setMaxConcurrency(maxConcurrency)](#AsyncQueue.setMaxConcurrency)
* [.setMaxRetries(maxRetries)](#AsyncQueue.setMaxRetries)
* [.setPromiseTimeout(timeout)](#AsyncQueue.setPromiseTimeout)
@@ -43,15 +45,16 @@
**Kind**: static class of [AsyncQueue](#AsyncQueue)
-#### new module.exports([maxConcurrency], [maxRetries], [timeout])
+#### new module.exports([config])
Create a Queue
| Param | Type | Description |
| --- | --- | --- |
-| [maxConcurrency] | number | The max amount of promises to run concurrently |
-| [maxRetries] | number | The max amount of promises to run concurrently |
-| [timeout] | number | The max amount of time in ms a promise can take to settle |
+| [config] | Object | the config for asyncrify |
+| [config.maxConcurrency] | number | The max amount of promises to run concurrently |
+| [config.maxRetries] | number | The max amount of promises to run concurrently |
+| [config.timeout] | number | The max amount of time in ms a promise can take to settle |
**Example**
```js
@@ -61,7 +64,7 @@ const queue = new Queue()
**Example**
```js
//to define a queue with a specified length of 3
-const queue = new Queue(3)
+const queue = new Queue({maxConcurrency: 3})
```
@@ -194,6 +197,10 @@ const error = (err) => {
//Adding the promise to the queue
queue.add(pets, callback, error)
```
+
+
+## QUEUE\_ERRORS
+**Kind**: global constant
## abortHandler
diff --git a/src/index.js b/src/index.js
index 1d783d3..35827e0 100644
--- a/src/index.js
+++ b/src/index.js
@@ -5,10 +5,18 @@ import inputValidation from "./shared/inputValidation";
* @namespace AsyncQueue
*/
+/**
+ */
+export const QUEUE_ERRORS = {
+ ABORT: "Aborted",
+ INPUT_REQUIRED: "input is required",
+ MAX_RETRIES: "max retries reached",
+};
+
export default class AsyncQueue {
#queue = new Set();
#running = 0;
- #maxConcurrency = 5;
+ #maxConcurrency = 0;
#timeout = 0;
/**
* @type {RetryEngine| null}
@@ -19,9 +27,10 @@ export default class AsyncQueue {
* Create a Queue
*
* @memberof AsyncQueue
- * @param {number=} [maxConcurrency] - The max amount of promises to run concurrently
- * @param {number=} maxRetries - The max amount of promises to run concurrently
- * @param {number=} timeout - The max amount of time in ms a promise can take to settle
+ * @param {Object} [config] - the config for asyncrify
+ * @param {number=} config.maxConcurrency - The max amount of promises to run concurrently
+ * @param {number=} config.maxRetries - The max amount of promises to run concurrently
+ * @param {number=} config.timeout - The max amount of time in ms a promise can take to settle
* @example
*
* //to define a queue with a default length of 5
@@ -29,17 +38,20 @@ export default class AsyncQueue {
*
* @example
* //to define a queue with a specified length of 3
- * const queue = new Queue(3)
+ * const queue = new Queue({maxConcurrency: 3})
*/
- constructor(maxConcurrency, maxRetries, timeout) {
- inputValidation(maxConcurrency, "number", false);
- inputValidation(maxRetries, "number", false);
- inputValidation(timeout, "number", false);
- if (maxConcurrency) this.#maxConcurrency = maxConcurrency;
- if (maxRetries) {
- this.#retryEngine = new RetryEngine(maxRetries);
+ constructor(config) {
+ inputValidation(config, "object", false);
+ if (config) {
+ inputValidation(config.maxConcurrency, "number", false);
+ inputValidation(config.maxRetries, "number", false);
+ inputValidation(config.timeout, "number", false);
+ if (config.maxConcurrency) this.#maxConcurrency = config.maxConcurrency;
+ if (config.maxRetries) {
+ this.#retryEngine = new RetryEngine(config.maxRetries);
+ }
+ if (config.timeout) this.#timeout = config.timeout;
}
- if (timeout) this.#timeout = timeout;
}
/**
@@ -232,7 +244,7 @@ export default class AsyncQueue {
inputValidation(fn, "function", true);
inputValidation(callback, "function", true);
- if (this.#running >= this.#maxConcurrency) {
+ if (this.#maxConcurrency !== 0 && this.#running >= this.#maxConcurrency) {
this.#queue.add(fn);
} else {
this.#running++;
diff --git a/src/index.test.js b/src/index.test.js
index ab16a71..f807a76 100644
--- a/src/index.test.js
+++ b/src/index.test.js
@@ -8,10 +8,10 @@ const enums = {
MAXRETRIES: "max retries reached",
};
-const typeErrMessage = (type) => `input must be a ${type}`;
+const typeErrMessage = (type, passedType) =>
+ `input must be a ${type}, but got ${passedType}`;
const maxConcurrentRuns = 3;
-const defaultMaxConcurrentRuns = 5;
let concurrentRuns = 0;
let queue;
@@ -54,12 +54,20 @@ describe("AsyncQueue class", () => {
beforeEach(beforeFunc);
afterEach(afterFunc);
+ test("should throw Type error when wrong type is passed to config", () => {
+ const typeErr = () => {
+ return new AsyncQueue({ maxConcurrency: "dsf" });
+ };
+ expect(typeErr).toThrow(TypeError);
+ expect(typeErr).toThrow(typeErrMessage("number", "string"));
+ });
+
test("should throw Type error when wrong type is passed to constructor", () => {
const typeErr = () => {
return new AsyncQueue("dsf");
};
expect(typeErr).toThrow(TypeError);
- expect(typeErr).toThrow(typeErrMessage("number"));
+ expect(typeErr).toThrow(typeErrMessage("object", "string"));
});
test("should return resolution for successful promise resolution", (done) => {
@@ -105,7 +113,7 @@ describe("retry on error", () => {
queue.setMaxRetries();
};
expect(typeErr).toThrow(TypeError);
- expect(typeErr).toThrow(typeErrMessage("number"));
+ expect(typeErr).toThrow(typeErrMessage("number", "string"));
expect(noInputErr).toThrow(SyntaxError);
expect(noInputErr).toThrow(enums.INPUTREQUIRED);
});
@@ -244,12 +252,12 @@ describe("concurrency", () => {
queue.setMaxConcurrency();
};
expect(typeErr).toThrow(TypeError);
- expect(typeErr).toThrow(typeErrMessage("number"));
+ expect(typeErr).toThrow(typeErrMessage("number", "string"));
expect(noInputErr).toThrow(SyntaxError);
expect(noInputErr).toThrow(enums.INPUTREQUIRED);
});
- test(`should only run ${defaultMaxConcurrentRuns} max concurrent promises by default`, (done) => {
+ test(`should throttle promises by default`, (done) => {
const testruncount = 10;
for (let i = 0; i < testruncount; i++) {
@@ -258,7 +266,7 @@ describe("concurrency", () => {
() => {
concurrentRuns--;
try {
- expect(concurrentRuns).toBeLessThan(defaultMaxConcurrentRuns);
+ expect(concurrentRuns).toBeLessThan(testruncount);
done();
} catch (err) {
done(err);
@@ -273,7 +281,7 @@ describe("concurrency", () => {
test(`should only run ${maxConcurrentRuns} max concurrent promises by when specified from constructor`, (done) => {
const testruncount = 7;
- queue = new AsyncQueue(maxConcurrentRuns);
+ queue = new AsyncQueue({ maxConcurrency: maxConcurrentRuns });
for (let i = 0; i < testruncount; i++) {
queue.add(
@@ -324,7 +332,7 @@ describe("Timeout", () => {
queue.setPromiseTimeout();
};
expect(typeErr).toThrow(TypeError);
- expect(typeErr).toThrow(typeErrMessage("number"));
+ expect(typeErr).toThrow(typeErrMessage("number", "string"));
expect(noInputErr).toThrow(SyntaxError);
expect(noInputErr).toThrow(enums.INPUTREQUIRED);
});
diff --git a/src/shared/inputValidation.js b/src/shared/inputValidation.js
index 68c2ebd..1528829 100644
--- a/src/shared/inputValidation.js
+++ b/src/shared/inputValidation.js
@@ -10,7 +10,7 @@ const inputValidation = (input, type, required) => {
throw new SyntaxError("input is required");
}
if (input && typeof input !== type) {
- throw new TypeError(`input must be a ${type}`);
+ throw new TypeError(`input must be a ${type}, but got ${typeof input}`);
}
};
export default inputValidation;