@parcel/utils 2.11.0 → 2.12.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/lib/index.js +216 -124
- package/lib/index.js.map +1 -1
- package/package.json +7 -7
- package/src/PromiseQueue.js +13 -0
- package/test/PromiseQueue.test.js +28 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@parcel/utils",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.12.0",
|
|
4
4
|
"description": "Blazing fast, zero configuration web application bundler",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"publishConfig": {
|
|
@@ -33,11 +33,11 @@
|
|
|
33
33
|
}
|
|
34
34
|
},
|
|
35
35
|
"dependencies": {
|
|
36
|
-
"@parcel/codeframe": "2.
|
|
37
|
-
"@parcel/diagnostic": "2.
|
|
38
|
-
"@parcel/logger": "2.
|
|
39
|
-
"@parcel/markdown-ansi": "2.
|
|
40
|
-
"@parcel/rust": "2.
|
|
36
|
+
"@parcel/codeframe": "2.12.0",
|
|
37
|
+
"@parcel/diagnostic": "2.12.0",
|
|
38
|
+
"@parcel/logger": "2.12.0",
|
|
39
|
+
"@parcel/markdown-ansi": "2.12.0",
|
|
40
|
+
"@parcel/rust": "2.12.0",
|
|
41
41
|
"@parcel/source-map": "^2.1.1",
|
|
42
42
|
"chalk": "^4.1.0",
|
|
43
43
|
"nullthrows": "^1.1.1"
|
|
@@ -67,5 +67,5 @@
|
|
|
67
67
|
"./src/openInBrowser.js": false,
|
|
68
68
|
"@parcel/markdown-ansi": false
|
|
69
69
|
},
|
|
70
|
-
"gitHead": "
|
|
70
|
+
"gitHead": "2059029ee91e5f03a273b0954d3e629d7375f986"
|
|
71
71
|
}
|
package/src/PromiseQueue.js
CHANGED
|
@@ -13,6 +13,7 @@ export default class PromiseQueue<T> {
|
|
|
13
13
|
_error: mixed;
|
|
14
14
|
_count: number = 0;
|
|
15
15
|
_results: Array<T> = [];
|
|
16
|
+
_addSubscriptions: Set<() => void> = new Set();
|
|
16
17
|
|
|
17
18
|
constructor(opts: PromiseQueueOpts = {maxConcurrent: Infinity}) {
|
|
18
19
|
if (opts.maxConcurrent <= 0) {
|
|
@@ -43,12 +44,24 @@ export default class PromiseQueue<T> {
|
|
|
43
44
|
|
|
44
45
|
this._queue.push(wrapped);
|
|
45
46
|
|
|
47
|
+
for (const addFn of this._addSubscriptions) {
|
|
48
|
+
addFn();
|
|
49
|
+
}
|
|
50
|
+
|
|
46
51
|
if (this._numRunning > 0 && this._numRunning < this._maxConcurrent) {
|
|
47
52
|
this._next();
|
|
48
53
|
}
|
|
49
54
|
});
|
|
50
55
|
}
|
|
51
56
|
|
|
57
|
+
subscribeToAdd(fn: () => void): () => void {
|
|
58
|
+
this._addSubscriptions.add(fn);
|
|
59
|
+
|
|
60
|
+
return () => {
|
|
61
|
+
this._addSubscriptions.delete(fn);
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
|
|
52
65
|
run(): Promise<Array<T>> {
|
|
53
66
|
if (this._runPromise != null) {
|
|
54
67
|
return this._runPromise;
|
|
@@ -3,6 +3,7 @@ import assert from 'assert';
|
|
|
3
3
|
import randomInt from 'random-int';
|
|
4
4
|
|
|
5
5
|
import PromiseQueue from '../src/PromiseQueue';
|
|
6
|
+
import sinon from 'sinon';
|
|
6
7
|
|
|
7
8
|
describe('PromiseQueue', () => {
|
|
8
9
|
it('run() should resolve when all async functions in queue have completed', async () => {
|
|
@@ -72,4 +73,31 @@ describe('PromiseQueue', () => {
|
|
|
72
73
|
|
|
73
74
|
await queue.run();
|
|
74
75
|
});
|
|
76
|
+
|
|
77
|
+
it('.add() should notify subscribers', async () => {
|
|
78
|
+
const queue = new PromiseQueue();
|
|
79
|
+
|
|
80
|
+
const subscribedFn = sinon.spy();
|
|
81
|
+
queue.subscribeToAdd(subscribedFn);
|
|
82
|
+
|
|
83
|
+
const promise = queue.add(() => Promise.resolve());
|
|
84
|
+
await queue.run();
|
|
85
|
+
await promise;
|
|
86
|
+
|
|
87
|
+
assert(subscribedFn.called);
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
it('.subscribeToAdd() should allow unsubscribing', async () => {
|
|
91
|
+
const queue = new PromiseQueue();
|
|
92
|
+
|
|
93
|
+
const subscribedFn = sinon.spy();
|
|
94
|
+
const unsubscribe = queue.subscribeToAdd(subscribedFn);
|
|
95
|
+
unsubscribe();
|
|
96
|
+
|
|
97
|
+
const promise = queue.add(() => Promise.resolve());
|
|
98
|
+
await queue.run();
|
|
99
|
+
await promise;
|
|
100
|
+
|
|
101
|
+
assert(!subscribedFn.called);
|
|
102
|
+
});
|
|
75
103
|
});
|