Skip to content

Commit 6300fd6

Browse files
authored
Merge pull request #40 from Bartozzz/development
Bump version 1.3.0
2 parents e753198 + a0646a8 commit 6300fd6

File tree

7 files changed

+4743
-3277
lines changed

7 files changed

+4743
-3277
lines changed

.babelrc

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
{
2-
"presets": ["env", "flow"],
3-
2+
"presets": [
3+
["@babel/preset-env", { "targets": { "node": "8" } }],
4+
["@babel/preset-flow"]
5+
],
46
"plugins": [
57
"add-module-exports",
6-
"transform-runtime",
7-
"transform-class-properties",
8-
"transform-object-rest-spread",
9-
"transform-async-to-generator"
8+
"@babel/plugin-proposal-async-generator-functions",
9+
"@babel/plugin-proposal-class-properties",
10+
"@babel/plugin-proposal-object-rest-spread"
1011
]
1112
}

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ dist: trusty
33
language: node_js
44

55
node_js:
6-
- node
6+
- lts/*
77

88
cache:
99
directories:

README.md

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,12 @@ $ npm install queue-promise
2323
import Queue from "queue-promise";
2424

2525
const queue = new Queue({
26-
concurrent: 1, // resolve 1 task at a time
27-
interval: 2000, // resolve new tasks each 2000ms,
28-
start: true, // automatically resolve new tasks when they are added
26+
// How many tasks should be resolved at a time (defaults to `5`):
27+
concurrent: 1,
28+
// How often should new tasks be resolved (in ms – defaults to `500`):
29+
interval: 2000,
30+
// If should resolve new tasks automatically when they are added (defaults to `true`):
31+
start: true
2932
});
3033

3134
queue.on("resolve", data => console.log(data));
@@ -43,36 +46,33 @@ queue.enqueue(asyncTaskD); // resolved/rejected after 6s
4346

4447
Create a new `Queue` instance.
4548

46-
| Option | Default | Description |
47-
| :----------- | :------ | :--------------------------------------------------------------------------- |
48-
| `concurrent` | `5` | How many tasks can be handled at the same time |
49-
| `interval` | `500` | How often should new tasks be handled (in ms) |
50-
| `start` | `true` | Whether we should automatically resolve new tasks as soon as they are added |
49+
| Option | Default | Description |
50+
| :----------- | :------ | :-------------------------------------------------------------------------- |
51+
| `concurrent` | `5` | How many tasks can be handled at the same time |
52+
| `interval` | `500` | How often should new tasks be handled (in ms) |
53+
| `start` | `true` | Whether we should automatically resolve new tasks as soon as they are added |
5154

52-
#### **public** `.enqueue(task)`/`.add(task)`
55+
#### **public** `.enqueue(tasks)`/`.add(tasks)`
5356

54-
Puts a new task on the stack. Tasks should be an async function or return a promise. Throws an error if the provided `task` is not a valid function.
57+
Puts a new task on the stack. A task should be an async function (ES2017) or return a [Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise). Throws an error if the provided `task` is not a valid function.
5558

5659
**Example:**
5760

5861
```javascript
5962
async function getRepos(user) {
6063
return await github.getRepos(user);
61-
};
64+
}
6265

6366
queue.enqueue(getRepos("userA"));
6467
queue.enqueue(getRepos("userB"));
6568

6669
// …equivalent to:
67-
queue.enqueue([
68-
getRepos("userA"),
69-
getRepos("userB"),
70-
]);
70+
queue.enqueue([getRepos("userA"), getRepos("userB")]);
7171
```
7272

7373
#### **public** `.dequeue()`
7474

75-
Resolves _n_ concurrent promises from the queue. Uses global [Promises](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise).
75+
Manually resolves _n_ concurrent (based od `options.concurrent`) promises from the queue. Uses global [Promises](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise). Is called automatically if `options.start` is set to `true`. Emits `resolve` and `reject` events.
7676

7777
**Example:**
7878

@@ -85,7 +85,7 @@ const userA = await queue.dequeue();
8585
const userB = await queue.dequeue();
8686

8787
// If "concurrent" is set to 2, two promises are resolved concurrently:
88-
const users = await queue.dequeue();
88+
const [userA, userB] = await queue.dequeue();
8989
```
9090

9191
#### **public** `.on(event, callback)`
@@ -97,18 +97,30 @@ Sets a `callback` for an `event`. You can set callback for those events: `start`
9797
```javascript
9898
queue.enqueue([…]);
9999

100-
queue.on("resolve", output => …);
101-
queue.on("reject", output => …);
100+
queue.on("resolve", data => …);
101+
queue.on("reject", error => …);
102102
queue.on("end", () => …);
103103
```
104104

105105
#### **public** `.start()`
106106

107107
Starts the queue – it will automatically dequeue tasks periodically. Emits `start` event.
108108

109+
```javascript
110+
queue.enqueue(getRepos("userA"));
111+
queue.enqueue(getRepos("userB"));
112+
queue.enqueue(getRepos("userC"));
113+
queue.enqueue(getRepos("userD"));
114+
queue.start();
115+
116+
// No need to call `dequeue` – you can just listen for events:
117+
queue.on("resolve", data => …);
118+
queue.on("reject", error => …);
119+
```
120+
109121
#### **public** `.stop()`
110122

111-
Stops the queue. Emits `stop` event.
123+
Forces the queue to stop. New tasks will not be resolved automatically even if `options.start` was set to `true`. Emits `stop` event.
112124

113125
#### **public** `.clear()`
114126

@@ -118,6 +130,10 @@ Removes all tasks from the queue.
118130

119131
Whether the queue has been started or not.
120132

133+
#### **public** `.stopped`
134+
135+
Whether the queue has been forced to stop.
136+
121137
#### **public** `.isEmpty`
122138

123139
Whether the queue is empty, i.e. there's no tasks.

0 commit comments

Comments
 (0)