@anmiles/queue 3.0.2 → 4.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/.eslintignore +2 -0
- package/.eslintrc.js +5 -7
- package/CHANGELOG.md +6 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/lib/queue.d.ts +7 -7
- package/dist/lib/queue.d.ts.map +1 -1
- package/dist/lib/queue.js +5 -3
- package/dist/lib/queue.js.map +1 -1
- package/jest.config.js +3 -11
- package/package.json +53 -48
- package/src/index.ts +2 -0
- package/src/lib/__tests__/queue.test.ts +37 -37
- package/src/lib/queue.ts +16 -12
- package/tsconfig.build.json +2 -2
- package/tsconfig.json +5 -5
- package/.github/workflows/ci.yml +0 -94
package/.eslintignore
ADDED
package/.eslintrc.js
CHANGED
|
@@ -1,11 +1,9 @@
|
|
|
1
1
|
module.exports = {
|
|
2
|
-
root
|
|
2
|
+
root : true,
|
|
3
|
+
|
|
3
4
|
extends : [
|
|
4
|
-
'./node_modules/@anmiles/eslint-config
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
'**/node_modules/',
|
|
8
|
-
'coverage/',
|
|
9
|
-
'dist/',
|
|
5
|
+
'./node_modules/@anmiles/eslint-config/src/base.preset.js',
|
|
6
|
+
'./node_modules/@anmiles/eslint-config/src/ts.preset.js',
|
|
7
|
+
'./node_modules/@anmiles/eslint-config/src/jest.preset.js',
|
|
10
8
|
],
|
|
11
9
|
};
|
package/CHANGELOG.md
CHANGED
|
@@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
|
|
|
5
5
|
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
|
6
6
|
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
7
|
|
|
8
|
+
## [4.0.0](../../tags/v4.0.0) - 2024-03-16
|
|
9
|
+
### Changed
|
|
10
|
+
- Update eslint config and raise minimum supported NodeJS version to match one in typescript-eslint plugin
|
|
11
|
+
- Update .npmignore
|
|
12
|
+
- Unify jest.config.js by removing redundant patterns and providing support for both ts and tsx
|
|
13
|
+
|
|
8
14
|
## [3.0.2](../../tags/v3.0.2) - 2024-01-31
|
|
9
15
|
### Changed
|
|
10
16
|
- Migrate to GitHub
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,OAAO,EAAE,MAAM,aAAa,CAAC"}
|
package/dist/index.js
CHANGED
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAAA,0BAA0B;;;;;;AAE1B,qCAAsC;AAA7B,iHAAA,OAAO,OAAA"}
|
package/dist/lib/queue.d.ts
CHANGED
|
@@ -1,20 +1,20 @@
|
|
|
1
1
|
import Emittery from 'emittery';
|
|
2
|
-
|
|
2
|
+
interface Listeners<T> {
|
|
3
3
|
item: (item: T) => Promise<void>;
|
|
4
4
|
done: () => void;
|
|
5
|
-
}
|
|
6
|
-
|
|
5
|
+
}
|
|
6
|
+
declare class Queue<TItem> extends Emittery<{
|
|
7
7
|
[TEvent in keyof Listeners<TItem>]: Parameters<Listeners<TItem>[TEvent]>[0];
|
|
8
8
|
}> {
|
|
9
9
|
private done;
|
|
10
|
-
private data;
|
|
11
|
-
private interval;
|
|
10
|
+
private readonly data;
|
|
11
|
+
private readonly interval;
|
|
12
12
|
constructor(data?: TItem[], { interval }?: {
|
|
13
13
|
interval?: number;
|
|
14
14
|
});
|
|
15
|
-
enqueue(...items: TItem[]):
|
|
15
|
+
enqueue(...items: TItem[]): this;
|
|
16
16
|
dequeue(): Promise<void>;
|
|
17
17
|
count(): number;
|
|
18
18
|
}
|
|
19
|
-
export
|
|
19
|
+
export default Queue;
|
|
20
20
|
//# sourceMappingURL=queue.d.ts.map
|
package/dist/lib/queue.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"queue.d.ts","sourceRoot":"","sources":["../../src/lib/queue.ts"],"names":[],"mappings":"AAAA,OAAO,QAAQ,MAAM,UAAU,CAAC;AAEhC,
|
|
1
|
+
{"version":3,"file":"queue.d.ts","sourceRoot":"","sources":["../../src/lib/queue.ts"],"names":[],"mappings":"AAAA,OAAO,QAAQ,MAAM,UAAU,CAAC;AAEhC,UAAU,SAAS,CAAC,CAAC;IACpB,IAAI,EAAG,CAAC,IAAI,EAAE,CAAC,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;IAClC,IAAI,EAAG,MAAM,IAAI,CAAC;CAClB;AAED,cAAM,KAAK,CAAC,KAAK,CAAE,SAAQ,QAAQ,CAAC;KAAG,MAAM,IAAI,MAAM,SAAS,CAAC,KAAK,CAAC,GAAG,UAAU,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC;CAAE,CAAC;IACnH,OAAO,CAAC,IAAI,CAAwB;IACpC,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAe;IACpC,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAU;gBAEvB,IAAI,GAAE,KAAK,EAAO,EAAE,EAAE,QAAY,EAAE,GAAE;QAAE,QAAQ,CAAC,EAAG,MAAM,CAAA;KAAO;IAO7E,OAAO,CAAC,GAAG,KAAK,EAAE,KAAK,EAAE,GAAG,IAAI;IAU1B,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC;IAe9B,KAAK,IAAI,MAAM;CAGf;AAED,eAAe,KAAK,CAAC"}
|
package/dist/lib/queue.js
CHANGED
|
@@ -17,7 +17,7 @@ class Queue extends emittery_1.default {
|
|
|
17
17
|
enqueue(...items) {
|
|
18
18
|
this.data.push(...items);
|
|
19
19
|
if (this.done) {
|
|
20
|
-
this.dequeue();
|
|
20
|
+
void this.dequeue();
|
|
21
21
|
}
|
|
22
22
|
return this;
|
|
23
23
|
}
|
|
@@ -26,11 +26,13 @@ class Queue extends emittery_1.default {
|
|
|
26
26
|
if (this.data.length > 0) {
|
|
27
27
|
const now = performance.now();
|
|
28
28
|
await this.emit('item', this.data.shift());
|
|
29
|
-
setTimeout(() =>
|
|
29
|
+
setTimeout(() => {
|
|
30
|
+
void this.dequeue();
|
|
31
|
+
}, Math.max(0, this.interval - (performance.now() - now)));
|
|
30
32
|
}
|
|
31
33
|
else {
|
|
32
34
|
this.done = true;
|
|
33
|
-
this.emit('done');
|
|
35
|
+
void this.emit('done');
|
|
34
36
|
}
|
|
35
37
|
}
|
|
36
38
|
count() {
|
package/dist/lib/queue.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"queue.js","sourceRoot":"","sources":["../../src/lib/queue.ts"],"names":[],"mappings":";;;;;AAAA,wDAAgC;AAOhC,
|
|
1
|
+
{"version":3,"file":"queue.js","sourceRoot":"","sources":["../../src/lib/queue.ts"],"names":[],"mappings":";;;;;AAAA,wDAAgC;AAOhC,MAAM,KAAa,SAAQ,kBAAyF;IAC3G,IAAI,CAAwB;IACnB,IAAI,CAAe;IACnB,QAAQ,CAAU;IAEnC,YAAY,OAAgB,EAAE,EAAE,EAAE,QAAQ,GAAG,CAAC,KAA6B,EAAE;QAC5E,KAAK,EAAE,CAAC;QACR,IAAI,CAAC,IAAI,GAAO,KAAK,CAAC;QACtB,IAAI,CAAC,IAAI,GAAO,IAAI,CAAC;QACrB,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC;IAC1B,CAAC;IAED,OAAO,CAAC,GAAG,KAAc;QACxB,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,KAAK,CAAC,CAAC;QAEzB,IAAI,IAAI,CAAC,IAAI,EAAE,CAAC;YACf,KAAK,IAAI,CAAC,OAAO,EAAE,CAAC;QACrB,CAAC;QAED,OAAO,IAAI,CAAC;IACb,CAAC;IAED,KAAK,CAAC,OAAO;QACZ,IAAI,CAAC,IAAI,GAAG,KAAK,CAAC;QAElB,IAAI,IAAI,CAAC,IAAI,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;YAC1B,MAAM,GAAG,GAAG,WAAW,CAAC,GAAG,EAAE,CAAC;YAC9B,MAAM,IAAI,CAAC,IAAI,CAAC,MAAM,EAAE,IAAI,CAAC,IAAI,CAAC,KAAK,EAAW,CAAC,CAAC;YACpD,UAAU,CAAC,GAAG,EAAE;gBACf,KAAK,IAAI,CAAC,OAAO,EAAE,CAAC;YACrB,CAAC,EAAE,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,IAAI,CAAC,QAAQ,GAAG,CAAC,WAAW,CAAC,GAAG,EAAE,GAAG,GAAG,CAAC,CAAC,CAAC,CAAC;QAC5D,CAAC;aAAM,CAAC;YACP,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;YACjB,KAAK,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;QACxB,CAAC;IACF,CAAC;IAED,KAAK;QACJ,OAAO,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC;IACzB,CAAC;CACD;AAED,kBAAe,KAAK,CAAC"}
|
package/jest.config.js
CHANGED
|
@@ -7,18 +7,10 @@ module.exports = {
|
|
|
7
7
|
clearMocks : true,
|
|
8
8
|
|
|
9
9
|
roots : [ '<rootDir>/src' ],
|
|
10
|
-
testMatch : [ '<rootDir>/src/**/__tests__/*.test.ts' ],
|
|
10
|
+
testMatch : [ '<rootDir>/src/**/__tests__/*.test.{ts,tsx}' ],
|
|
11
11
|
|
|
12
12
|
collectCoverageFrom : [
|
|
13
|
-
'<rootDir>/src/**/*.ts',
|
|
14
|
-
'!<rootDir>/src
|
|
15
|
-
'!<rootDir>/src/*.ts',
|
|
16
|
-
'!<rootDir>/src/types/*.ts',
|
|
17
|
-
|
|
18
|
-
'!**/node_modules/**',
|
|
19
|
-
'!**/__tests__/**',
|
|
20
|
-
|
|
21
|
-
'!<rootDir>/coverage/**',
|
|
22
|
-
'!<rootDir>/dist/**',
|
|
13
|
+
'<rootDir>/src/**/*.{ts,tsx}',
|
|
14
|
+
'!<rootDir>/src/**/__tests__/**',
|
|
23
15
|
],
|
|
24
16
|
};
|
package/package.json
CHANGED
|
@@ -1,50 +1,55 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
2
|
+
"name": "@anmiles/queue",
|
|
3
|
+
"version": "4.0.0",
|
|
4
|
+
"description": "Queue dispatcher that sequentially processes incoming items",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"queue",
|
|
7
|
+
"enqueue",
|
|
8
|
+
"dispatcher"
|
|
9
|
+
],
|
|
10
|
+
"author": "Anatoliy Oblaukhov",
|
|
11
|
+
"homepage": "https://github.com/anmiles/queue",
|
|
12
|
+
"repository": "github:anmiles/queue",
|
|
13
|
+
"license": "MIT",
|
|
14
|
+
"engines": {
|
|
15
|
+
"node": ">=18.18.0"
|
|
16
|
+
},
|
|
17
|
+
"main": "dist/index.js",
|
|
18
|
+
"scripts": {
|
|
19
|
+
"build": "rimraf dist && tsc -p ./tsconfig.build.json",
|
|
20
|
+
"build:ci": "tsc -noEmit -p ./tsconfig.build.json",
|
|
21
|
+
"lint": "eslint .",
|
|
22
|
+
"lint:fix": "npm run lint -- --fix",
|
|
23
|
+
"test": "jest --verbose",
|
|
24
|
+
"test:coverage": "npm test -- --coverage",
|
|
25
|
+
"test:ci": "npm test -- --ci --coverage",
|
|
26
|
+
"test:watch": "npm test -- --watch",
|
|
27
|
+
"test:watch:coverage": "npm test -- --watch --coverage",
|
|
28
|
+
"test:report:coverage": "nyc report --nycrc-path ./coverage.config.js -t ./coverage --report-dir ./coverage"
|
|
29
|
+
},
|
|
30
|
+
"dependencies": {
|
|
31
|
+
"emittery": "^0.13.1"
|
|
32
|
+
},
|
|
33
|
+
"devDependencies": {
|
|
34
|
+
"@anmiles/eslint-config": "^7.0.1",
|
|
35
|
+
"@anmiles/sleep": "^4.0.0",
|
|
36
|
+
"@anmiles/tsconfig": "^2.0.1",
|
|
37
|
+
"@stylistic/eslint-plugin": "^1.7.0",
|
|
38
|
+
"@types/jest": "^29.5.12",
|
|
39
|
+
"@typescript-eslint/eslint-plugin": "^7.2.0",
|
|
40
|
+
"@typescript-eslint/parser": "^7.2.0",
|
|
41
|
+
"eslint": "^8.57.0",
|
|
42
|
+
"eslint-import-resolver-typescript": "^3.6.1",
|
|
43
|
+
"eslint-plugin-align-assignments": "^1.1.2",
|
|
44
|
+
"eslint-plugin-import": "^2.29.1",
|
|
45
|
+
"eslint-plugin-jest": "^27.9.0",
|
|
46
|
+
"eslint-plugin-jsonc": "^2.13.0",
|
|
47
|
+
"eslint-plugin-n": "^16.6.2",
|
|
48
|
+
"eslint-plugin-promise": "^6.1.1",
|
|
49
|
+
"jest": "^29.7.0",
|
|
50
|
+
"nyc": "^15.1.0",
|
|
51
|
+
"rimraf": "^5.0.5",
|
|
52
|
+
"ts-jest": "^29.1.2",
|
|
53
|
+
"typescript": "^5.4.2"
|
|
54
|
+
}
|
|
50
55
|
}
|
package/src/index.ts
CHANGED
|
@@ -14,7 +14,7 @@ describe('src/lib/queue', () => {
|
|
|
14
14
|
|
|
15
15
|
beforeEach(() => {
|
|
16
16
|
setTimeoutSpy.mockImplementation((func, timeout) => {
|
|
17
|
-
log.push(`timeout(${Math.floor(timeout
|
|
17
|
+
log.push(`timeout(${Math.floor(timeout ?? 0)})`);
|
|
18
18
|
func();
|
|
19
19
|
return 0 as unknown as ReturnType<typeof setTimeout>;
|
|
20
20
|
});
|
|
@@ -53,8 +53,8 @@ describe('src/lib/queue', () => {
|
|
|
53
53
|
});
|
|
54
54
|
});
|
|
55
55
|
|
|
56
|
-
it('should process empty queue', () => new Promise<void>((resolve) => {
|
|
57
|
-
queue
|
|
56
|
+
it('should process empty queue', async () => new Promise<void>((resolve) => {
|
|
57
|
+
Reflect.set(queue, 'data', []);
|
|
58
58
|
|
|
59
59
|
queue.on('done', () => {
|
|
60
60
|
log.push('done');
|
|
@@ -66,10 +66,10 @@ describe('src/lib/queue', () => {
|
|
|
66
66
|
resolve();
|
|
67
67
|
});
|
|
68
68
|
|
|
69
|
-
queue.dequeue();
|
|
69
|
+
void queue.dequeue();
|
|
70
70
|
}));
|
|
71
71
|
|
|
72
|
-
it('should process queue in sequential order', () => new Promise<void>((resolve) => {
|
|
72
|
+
it('should process queue in sequential order', async () => new Promise<void>((resolve) => {
|
|
73
73
|
queue.on('done', () => {
|
|
74
74
|
log.push('done');
|
|
75
75
|
|
|
@@ -84,11 +84,11 @@ describe('src/lib/queue', () => {
|
|
|
84
84
|
resolve();
|
|
85
85
|
});
|
|
86
86
|
|
|
87
|
-
queue.dequeue();
|
|
87
|
+
void queue.dequeue();
|
|
88
88
|
}));
|
|
89
89
|
|
|
90
|
-
it('should process queue with intervals in sequential order', () => new Promise<void>((resolve) => {
|
|
91
|
-
queue
|
|
90
|
+
it('should process queue with intervals in sequential order', async () => new Promise<void>((resolve) => {
|
|
91
|
+
Reflect.set(queue, 'interval', 1000);
|
|
92
92
|
|
|
93
93
|
queue.on('done', () => {
|
|
94
94
|
log.push('done');
|
|
@@ -104,10 +104,10 @@ describe('src/lib/queue', () => {
|
|
|
104
104
|
resolve();
|
|
105
105
|
});
|
|
106
106
|
|
|
107
|
-
queue.dequeue();
|
|
107
|
+
void queue.dequeue();
|
|
108
108
|
}));
|
|
109
109
|
|
|
110
|
-
it('should process queue in sequential order when enqueuing items before dequeueing', () => new Promise<void>((resolve) => {
|
|
110
|
+
it('should process queue in sequential order when enqueuing items before dequeueing', async () => new Promise<void>((resolve) => {
|
|
111
111
|
queue.on('done', () => {
|
|
112
112
|
log.push('done');
|
|
113
113
|
|
|
@@ -127,10 +127,10 @@ describe('src/lib/queue', () => {
|
|
|
127
127
|
});
|
|
128
128
|
|
|
129
129
|
queue.enqueue(300, 200);
|
|
130
|
-
queue.dequeue();
|
|
130
|
+
void queue.dequeue();
|
|
131
131
|
}));
|
|
132
132
|
|
|
133
|
-
it('should process queue in sequential order when enqueuing items after dequeueing', () => new Promise<void>((resolve) => {
|
|
133
|
+
it('should process queue in sequential order when enqueuing items after dequeueing', async () => new Promise<void>((resolve) => {
|
|
134
134
|
let doneCalledOnce = false;
|
|
135
135
|
|
|
136
136
|
queue.on('done', () => {
|
|
@@ -158,11 +158,11 @@ describe('src/lib/queue', () => {
|
|
|
158
158
|
resolve();
|
|
159
159
|
});
|
|
160
160
|
|
|
161
|
-
queue.dequeue();
|
|
161
|
+
void queue.dequeue();
|
|
162
162
|
}));
|
|
163
163
|
|
|
164
|
-
it('should process queue with intervals in sequential order when enqueuing items before dequeueing', () => new Promise<void>((resolve) => {
|
|
165
|
-
queue
|
|
164
|
+
it('should process queue with intervals in sequential order when enqueuing items before dequeueing', async () => new Promise<void>((resolve) => {
|
|
165
|
+
Reflect.set(queue, 'interval', 1000);
|
|
166
166
|
|
|
167
167
|
queue.on('done', () => {
|
|
168
168
|
log.push('done');
|
|
@@ -183,11 +183,11 @@ describe('src/lib/queue', () => {
|
|
|
183
183
|
});
|
|
184
184
|
|
|
185
185
|
queue.enqueue(300, 200);
|
|
186
|
-
queue.dequeue();
|
|
186
|
+
void queue.dequeue();
|
|
187
187
|
}));
|
|
188
188
|
|
|
189
|
-
it('should process queue with intervals in sequential order when enqueuing items after dequeueing', () => new Promise<void>((resolve) => {
|
|
190
|
-
queue
|
|
189
|
+
it('should process queue with intervals in sequential order when enqueuing items after dequeueing', async () => new Promise<void>((resolve) => {
|
|
190
|
+
Reflect.set(queue, 'interval', 1000);
|
|
191
191
|
|
|
192
192
|
let doneCalledOnce = false;
|
|
193
193
|
|
|
@@ -216,7 +216,7 @@ describe('src/lib/queue', () => {
|
|
|
216
216
|
resolve();
|
|
217
217
|
});
|
|
218
218
|
|
|
219
|
-
queue.dequeue();
|
|
219
|
+
void queue.dequeue();
|
|
220
220
|
}));
|
|
221
221
|
});
|
|
222
222
|
|
|
@@ -229,8 +229,8 @@ describe('src/lib/queue', () => {
|
|
|
229
229
|
});
|
|
230
230
|
});
|
|
231
231
|
|
|
232
|
-
it('should process empty queue', () => new Promise<void>((resolve) => {
|
|
233
|
-
queue
|
|
232
|
+
it('should process empty queue', async () => new Promise<void>((resolve) => {
|
|
233
|
+
Reflect.set(queue, 'data', []);
|
|
234
234
|
|
|
235
235
|
queue.on('done', () => {
|
|
236
236
|
log.push('done');
|
|
@@ -242,10 +242,10 @@ describe('src/lib/queue', () => {
|
|
|
242
242
|
resolve();
|
|
243
243
|
});
|
|
244
244
|
|
|
245
|
-
queue.dequeue();
|
|
245
|
+
void queue.dequeue();
|
|
246
246
|
}));
|
|
247
247
|
|
|
248
|
-
it('should process queue in sequential order', () => new Promise<void>((resolve) => {
|
|
248
|
+
it('should process queue in sequential order', async () => new Promise<void>((resolve) => {
|
|
249
249
|
queue.on('done', () => {
|
|
250
250
|
log.push('done');
|
|
251
251
|
|
|
@@ -264,11 +264,11 @@ describe('src/lib/queue', () => {
|
|
|
264
264
|
resolve();
|
|
265
265
|
});
|
|
266
266
|
|
|
267
|
-
queue.dequeue();
|
|
267
|
+
void queue.dequeue();
|
|
268
268
|
}));
|
|
269
269
|
|
|
270
|
-
it('should process queue with intervals in sequential order', () => new Promise<void>((resolve) => {
|
|
271
|
-
queue
|
|
270
|
+
it('should process queue with intervals in sequential order', async () => new Promise<void>((resolve) => {
|
|
271
|
+
Reflect.set(queue, 'interval', 1000);
|
|
272
272
|
|
|
273
273
|
queue.on('done', () => {
|
|
274
274
|
log.push('done');
|
|
@@ -288,10 +288,10 @@ describe('src/lib/queue', () => {
|
|
|
288
288
|
resolve();
|
|
289
289
|
});
|
|
290
290
|
|
|
291
|
-
queue.dequeue();
|
|
291
|
+
void queue.dequeue();
|
|
292
292
|
}));
|
|
293
293
|
|
|
294
|
-
it('should process queue in sequential order when enqueuing items before dequeueing', () => new Promise<void>((resolve) => {
|
|
294
|
+
it('should process queue in sequential order when enqueuing items before dequeueing', async () => new Promise<void>((resolve) => {
|
|
295
295
|
queue.on('done', () => {
|
|
296
296
|
log.push('done');
|
|
297
297
|
|
|
@@ -319,10 +319,10 @@ describe('src/lib/queue', () => {
|
|
|
319
319
|
});
|
|
320
320
|
|
|
321
321
|
queue.enqueue(300, 200);
|
|
322
|
-
queue.dequeue();
|
|
322
|
+
void queue.dequeue();
|
|
323
323
|
}));
|
|
324
324
|
|
|
325
|
-
it('should process queue in sequential order when enqueuing items after dequeueing', () => new Promise<void>((resolve) => {
|
|
325
|
+
it('should process queue in sequential order when enqueuing items after dequeueing', async () => new Promise<void>((resolve) => {
|
|
326
326
|
let doneCalledOnce = false;
|
|
327
327
|
|
|
328
328
|
queue.on('done', () => {
|
|
@@ -358,11 +358,11 @@ describe('src/lib/queue', () => {
|
|
|
358
358
|
resolve();
|
|
359
359
|
});
|
|
360
360
|
|
|
361
|
-
queue.dequeue();
|
|
361
|
+
void queue.dequeue();
|
|
362
362
|
}));
|
|
363
363
|
|
|
364
|
-
it('should process queue with intervals in sequential order when enqueuing items before dequeueing', () => new Promise<void>((resolve) => {
|
|
365
|
-
queue
|
|
364
|
+
it('should process queue with intervals in sequential order when enqueuing items before dequeueing', async () => new Promise<void>((resolve) => {
|
|
365
|
+
Reflect.set(queue, 'interval', 1000);
|
|
366
366
|
|
|
367
367
|
queue.on('done', () => {
|
|
368
368
|
log.push('done');
|
|
@@ -391,11 +391,11 @@ describe('src/lib/queue', () => {
|
|
|
391
391
|
});
|
|
392
392
|
|
|
393
393
|
queue.enqueue(300, 200);
|
|
394
|
-
queue.dequeue();
|
|
394
|
+
void queue.dequeue();
|
|
395
395
|
}));
|
|
396
396
|
|
|
397
|
-
it('should process queue with intervals in sequential order when enqueuing items after dequeueing', () => new Promise<void>((resolve) => {
|
|
398
|
-
queue
|
|
397
|
+
it('should process queue with intervals in sequential order when enqueuing items after dequeueing', async () => new Promise<void>((resolve) => {
|
|
398
|
+
Reflect.set(queue, 'interval', 1000);
|
|
399
399
|
|
|
400
400
|
let doneCalledOnce = false;
|
|
401
401
|
|
|
@@ -432,7 +432,7 @@ describe('src/lib/queue', () => {
|
|
|
432
432
|
resolve();
|
|
433
433
|
});
|
|
434
434
|
|
|
435
|
-
queue.dequeue();
|
|
435
|
+
void queue.dequeue();
|
|
436
436
|
}));
|
|
437
437
|
});
|
|
438
438
|
|
package/src/lib/queue.ts
CHANGED
|
@@ -1,27 +1,27 @@
|
|
|
1
1
|
import Emittery from 'emittery';
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
item: (item: T) => Promise<void>;
|
|
5
|
-
done: () => void;
|
|
3
|
+
interface Listeners<T> {
|
|
4
|
+
item : (item: T) => Promise<void>;
|
|
5
|
+
done : () => void;
|
|
6
6
|
}
|
|
7
7
|
|
|
8
|
-
|
|
9
|
-
private done: boolean;
|
|
10
|
-
private data: TItem[];
|
|
11
|
-
private interval: number;
|
|
8
|
+
class Queue<TItem> extends Emittery<{ [TEvent in keyof Listeners<TItem>]: Parameters<Listeners<TItem>[TEvent]>[0] }> {
|
|
9
|
+
private done : boolean;
|
|
10
|
+
private readonly data : TItem[];
|
|
11
|
+
private readonly interval : number;
|
|
12
12
|
|
|
13
|
-
constructor(data: TItem[] = [], { interval = 0 }: { interval
|
|
13
|
+
constructor(data: TItem[] = [], { interval = 0 }: { interval? : number } = {}) {
|
|
14
14
|
super();
|
|
15
15
|
this.done = false;
|
|
16
16
|
this.data = data;
|
|
17
17
|
this.interval = interval;
|
|
18
18
|
}
|
|
19
19
|
|
|
20
|
-
enqueue(...items: TItem[]):
|
|
20
|
+
enqueue(...items: TItem[]): this {
|
|
21
21
|
this.data.push(...items);
|
|
22
22
|
|
|
23
23
|
if (this.done) {
|
|
24
|
-
this.dequeue();
|
|
24
|
+
void this.dequeue();
|
|
25
25
|
}
|
|
26
26
|
|
|
27
27
|
return this;
|
|
@@ -33,10 +33,12 @@ export default class Queue<TItem> extends Emittery<{[TEvent in keyof Listeners<T
|
|
|
33
33
|
if (this.data.length > 0) {
|
|
34
34
|
const now = performance.now();
|
|
35
35
|
await this.emit('item', this.data.shift() as TItem);
|
|
36
|
-
setTimeout(() =>
|
|
36
|
+
setTimeout(() => {
|
|
37
|
+
void this.dequeue();
|
|
38
|
+
}, Math.max(0, this.interval - (performance.now() - now)));
|
|
37
39
|
} else {
|
|
38
40
|
this.done = true;
|
|
39
|
-
this.emit('done');
|
|
41
|
+
void this.emit('done');
|
|
40
42
|
}
|
|
41
43
|
}
|
|
42
44
|
|
|
@@ -44,3 +46,5 @@ export default class Queue<TItem> extends Emittery<{[TEvent in keyof Listeners<T
|
|
|
44
46
|
return this.data.length;
|
|
45
47
|
}
|
|
46
48
|
}
|
|
49
|
+
|
|
50
|
+
export default Queue;
|
package/tsconfig.build.json
CHANGED
package/tsconfig.json
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
{
|
|
2
|
-
"extends": "./node_modules/@anmiles/tsconfig/tsconfig.json",
|
|
2
|
+
"extends" : "./node_modules/@anmiles/tsconfig/tsconfig.json",
|
|
3
3
|
|
|
4
|
-
"compilerOptions": {
|
|
5
|
-
"rootDir": "./src",
|
|
6
|
-
"outDir": "./dist",
|
|
4
|
+
"compilerOptions" : {
|
|
5
|
+
"rootDir" : "./src",
|
|
6
|
+
"outDir" : "./dist",
|
|
7
7
|
},
|
|
8
8
|
|
|
9
|
-
"include": [
|
|
9
|
+
"include" : [
|
|
10
10
|
"src",
|
|
11
11
|
],
|
|
12
12
|
}
|
package/.github/workflows/ci.yml
DELETED
|
@@ -1,94 +0,0 @@
|
|
|
1
|
-
name: CI
|
|
2
|
-
|
|
3
|
-
on:
|
|
4
|
-
push:
|
|
5
|
-
branches:
|
|
6
|
-
- main
|
|
7
|
-
pull_request:
|
|
8
|
-
workflow_dispatch:
|
|
9
|
-
|
|
10
|
-
concurrency:
|
|
11
|
-
group: "${{ github.workflow }}-${{ github.ref }}"
|
|
12
|
-
cancel-in-progress: true
|
|
13
|
-
|
|
14
|
-
jobs:
|
|
15
|
-
install:
|
|
16
|
-
runs-on: ubuntu-latest
|
|
17
|
-
container:
|
|
18
|
-
image: node:18.14
|
|
19
|
-
timeout-minutes: 30
|
|
20
|
-
steps:
|
|
21
|
-
- uses: actions/checkout@v4
|
|
22
|
-
- uses: actions/cache@v4
|
|
23
|
-
with:
|
|
24
|
-
path: node_modules
|
|
25
|
-
key: "node_modules-${{ hashFiles('package-lock.json') }}"
|
|
26
|
-
lookup-only: true
|
|
27
|
-
- run: npm ci
|
|
28
|
-
|
|
29
|
-
build:
|
|
30
|
-
needs: install
|
|
31
|
-
runs-on: ubuntu-latest
|
|
32
|
-
container:
|
|
33
|
-
image: node:18.14
|
|
34
|
-
timeout-minutes: 30
|
|
35
|
-
steps:
|
|
36
|
-
- uses: actions/checkout@v4
|
|
37
|
-
- uses: actions/cache@v4
|
|
38
|
-
with:
|
|
39
|
-
path: node_modules
|
|
40
|
-
key: "node_modules-${{ hashFiles('package-lock.json') }}"
|
|
41
|
-
- run: npm run build:ci
|
|
42
|
-
|
|
43
|
-
lint:
|
|
44
|
-
needs: build
|
|
45
|
-
runs-on: ubuntu-latest
|
|
46
|
-
container:
|
|
47
|
-
image: node:18.14
|
|
48
|
-
timeout-minutes: 30
|
|
49
|
-
steps:
|
|
50
|
-
- uses: actions/checkout@v4
|
|
51
|
-
- uses: actions/cache@v4
|
|
52
|
-
with:
|
|
53
|
-
path: node_modules
|
|
54
|
-
key: "node_modules-${{ hashFiles('package-lock.json') }}"
|
|
55
|
-
- run: npm run lint
|
|
56
|
-
|
|
57
|
-
test:
|
|
58
|
-
needs: build
|
|
59
|
-
runs-on: ubuntu-latest
|
|
60
|
-
container:
|
|
61
|
-
image: node:18.14
|
|
62
|
-
timeout-minutes: 30
|
|
63
|
-
steps:
|
|
64
|
-
- uses: actions/checkout@v4
|
|
65
|
-
- uses: actions/cache@v4
|
|
66
|
-
with:
|
|
67
|
-
path: node_modules
|
|
68
|
-
key: "node_modules-${{ hashFiles('package-lock.json') }}"
|
|
69
|
-
- uses: actions/cache@v4
|
|
70
|
-
with:
|
|
71
|
-
path: coverage
|
|
72
|
-
key: "coverage-${{ hashFiles('package-lock.json') }}"
|
|
73
|
-
lookup-only: true
|
|
74
|
-
- run: npm run test:ci
|
|
75
|
-
|
|
76
|
-
coverage:
|
|
77
|
-
needs:
|
|
78
|
-
- lint
|
|
79
|
-
- test
|
|
80
|
-
runs-on: ubuntu-latest
|
|
81
|
-
container:
|
|
82
|
-
image: node:18.14
|
|
83
|
-
timeout-minutes: 30
|
|
84
|
-
steps:
|
|
85
|
-
- uses: actions/checkout@v4
|
|
86
|
-
- uses: actions/cache@v4
|
|
87
|
-
with:
|
|
88
|
-
path: node_modules
|
|
89
|
-
key: "node_modules-${{ hashFiles('package-lock.json') }}"
|
|
90
|
-
- uses: actions/cache@v4
|
|
91
|
-
with:
|
|
92
|
-
path: coverage
|
|
93
|
-
key: "coverage-${{ hashFiles('package-lock.json') }}"
|
|
94
|
-
- run: npm run test:report:coverage
|