qpass 1.0.0 β 1.2.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/README.md +136 -99
- package/dist/index.cjs +134 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +28 -0
- package/dist/index.d.ts +28 -0
- package/dist/index.js +113 -0
- package/dist/index.js.map +1 -0
- package/dist/qpass.js +136 -0
- package/dist/qpass.js.map +1 -0
- package/package.json +24 -4
- package/index.js +0 -109
- package/index.ts +0 -148
- package/test.js +0 -68
- package/tsconfig.json +0 -47
package/README.md
CHANGED
|
@@ -1,160 +1,197 @@
|
|
|
1
1
|
# Qpass
|
|
2
2
|
|
|
3
|
-
Qpass is a
|
|
4
|
-
It
|
|
3
|
+
Qpass is a lightweight promise-job queue for JavaScript/TypeScript.
|
|
4
|
+
It runs jobs in cycles with configurable parallelism (`batchSize`) and reports progress after each cycle.
|
|
5
5
|
|
|
6
|
-
##
|
|
6
|
+
## Platform support
|
|
7
7
|
|
|
8
|
-
|
|
8
|
+
- Node.js ESM (`import`) via `qpass`
|
|
9
|
+
- Node.js CommonJS (`require`) via `qpass`
|
|
10
|
+
- Browser bundlers (Vite/Webpack/Rollup) via `qpass`
|
|
11
|
+
- Browser script tag/global build via `dist/qpass.js`
|
|
12
|
+
|
|
13
|
+
Version is sourced from `package.json` at build time, so you only update it in one place.
|
|
14
|
+
|
|
15
|
+
## Install
|
|
16
|
+
|
|
17
|
+
```bash
|
|
9
18
|
npm install qpass
|
|
10
19
|
```
|
|
11
20
|
|
|
12
|
-
##
|
|
21
|
+
## Quick start
|
|
22
|
+
|
|
23
|
+
```js
|
|
24
|
+
import Qpass from "qpass";
|
|
25
|
+
|
|
26
|
+
const queue = new Qpass({
|
|
27
|
+
batchSize: 3,
|
|
28
|
+
onProgress: ({ batchToProcess, itemsToProcess, completed }) => {
|
|
29
|
+
console.log({ batchToProcess, itemsToProcess, completed });
|
|
30
|
+
},
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
const jobs = [
|
|
34
|
+
() => Promise.resolve("A"),
|
|
35
|
+
() => Promise.resolve("B"),
|
|
36
|
+
() => Promise.resolve("C"),
|
|
37
|
+
() => Promise.resolve("D"),
|
|
38
|
+
];
|
|
39
|
+
|
|
40
|
+
queue.add(jobs);
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
## Import by platform
|
|
44
|
+
|
|
45
|
+
### Node.js ESM
|
|
46
|
+
|
|
47
|
+
```js
|
|
48
|
+
import Qpass from "qpass";
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
### Node.js CommonJS
|
|
13
52
|
|
|
14
|
-
|
|
53
|
+
```js
|
|
54
|
+
const Qpass = require("qpass").default;
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
### Browser bundler
|
|
15
58
|
|
|
16
59
|
```js
|
|
17
60
|
import Qpass from "qpass";
|
|
61
|
+
```
|
|
18
62
|
|
|
19
|
-
|
|
63
|
+
### Browser script tag (global)
|
|
20
64
|
|
|
21
|
-
|
|
22
|
-
const job2 = () => Promise.resolve("job2 completed");
|
|
65
|
+
Use the global build from your package output or CDN:
|
|
23
66
|
|
|
24
|
-
|
|
67
|
+
```html
|
|
68
|
+
<script src="https://cdn.jsdelivr.net/npm/qpass/dist/qpass.js"></script>
|
|
69
|
+
<script>
|
|
70
|
+
const queue = new Qpass();
|
|
71
|
+
</script>
|
|
25
72
|
```
|
|
26
73
|
|
|
27
|
-
|
|
74
|
+
## API
|
|
28
75
|
|
|
29
|
-
|
|
76
|
+
### `new Qpass(options?)`
|
|
30
77
|
|
|
31
78
|
```ts
|
|
32
|
-
|
|
79
|
+
new Qpass({
|
|
33
80
|
breakWhenError?: boolean;
|
|
81
|
+
batchSize?: number; // >= 1, default: 1
|
|
34
82
|
onProgress?: (progress: {
|
|
35
|
-
batchToProcess: number;
|
|
36
|
-
itemsToProcess: number;
|
|
37
|
-
completed
|
|
83
|
+
batchToProcess: number;
|
|
84
|
+
itemsToProcess: number;
|
|
85
|
+
completed: any[];
|
|
38
86
|
}) => void;
|
|
39
|
-
|
|
40
|
-
});
|
|
87
|
+
})
|
|
41
88
|
```
|
|
42
89
|
|
|
43
|
-
|
|
90
|
+
#### Options
|
|
44
91
|
|
|
45
|
-
|
|
92
|
+
- `batchSize` (default `1`): number of jobs to run in parallel per cycle.
|
|
93
|
+
- `breakWhenError` (default `false`): when `true`, queued jobs are cleared after an error is seen.
|
|
94
|
+
- `onProgress`: called after each cycle finishes.
|
|
46
95
|
|
|
47
|
-
|
|
48
|
-
const queue = new Qpass({
|
|
49
|
-
batchSize: 2,
|
|
50
|
-
onProgress: (progress) => {
|
|
51
|
-
console.log(progress);
|
|
52
|
-
},
|
|
53
|
-
});
|
|
54
|
-
|
|
55
|
-
await queue.add([
|
|
56
|
-
() => Promise.resolve("Job 1"),
|
|
57
|
-
() => Promise.resolve("Job 2"),
|
|
58
|
-
() => Promise.resolve("Job 3"),
|
|
59
|
-
() => Promise.resolve("Job 4"),
|
|
60
|
-
]);
|
|
96
|
+
### `add(jobs)`
|
|
61
97
|
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
// { batchToProcess: 0, itemsToProcess: 0, completed: ["Job 3", "Job 4"] }
|
|
98
|
+
```ts
|
|
99
|
+
add(jobs: (() => Promise<any>)[] | (() => Promise<any>)): string | string[]
|
|
65
100
|
```
|
|
66
101
|
|
|
67
|
-
|
|
102
|
+
- Adds one or many jobs to the queue.
|
|
103
|
+
- Starts processing automatically.
|
|
104
|
+
- Returns the generated job id for a single job, or an array of ids for multiple jobs.
|
|
68
105
|
|
|
69
|
-
|
|
70
|
-
<br>
|
|
71
|
-
This behavior is controlled via the breakWhenError option.
|
|
106
|
+
### `remove(id)`
|
|
72
107
|
|
|
73
|
-
|
|
108
|
+
```ts
|
|
109
|
+
remove(id: string | number): boolean
|
|
110
|
+
```
|
|
74
111
|
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
onProgress: (progress) => {
|
|
79
|
-
console.log("Completed jobs: ", progress.completed);
|
|
80
|
-
},
|
|
81
|
-
});
|
|
112
|
+
- Removes a pending queued job by its id.
|
|
113
|
+
- Returns `true` when a queued job is removed, or `false` if the id was not found.
|
|
114
|
+
- Does not stop jobs already in progress.
|
|
82
115
|
|
|
83
|
-
|
|
84
|
-
() => Promise.resolve("First success"),
|
|
85
|
-
() => Promise.reject("Second failed β"),
|
|
86
|
-
() => Promise.resolve("Third success β
"),
|
|
87
|
-
];
|
|
116
|
+
### `terminate()`
|
|
88
117
|
|
|
89
|
-
|
|
118
|
+
```ts
|
|
119
|
+
terminate(): void
|
|
90
120
|
```
|
|
91
121
|
|
|
92
|
-
|
|
122
|
+
- Clears all queued (not-yet-started) jobs.
|
|
123
|
+
- Already running jobs continue until they settle.
|
|
124
|
+
|
|
125
|
+
## Progress callback behavior
|
|
126
|
+
|
|
127
|
+
`onProgress` receives:
|
|
128
|
+
|
|
129
|
+
- `completed`: results (or errors) from the cycle that just finished.
|
|
130
|
+
- `itemsToProcess`: queued jobs still waiting to start.
|
|
131
|
+
- `batchToProcess`: `Math.ceil(itemsToProcess / batchSize)`.
|
|
132
|
+
|
|
133
|
+
Notes:
|
|
134
|
+
|
|
135
|
+
- Progress is reported per cycle, not per individual job completion.
|
|
136
|
+
- If you call `add` repeatedly (for example, inside a loop), the first cycle may be smaller than `batchSize` because processing starts immediately.
|
|
137
|
+
|
|
138
|
+
## Error handling
|
|
139
|
+
|
|
140
|
+
### Continue on error (`breakWhenError: false`)
|
|
93
141
|
|
|
94
142
|
```js
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
143
|
+
const queue = new Qpass({
|
|
144
|
+
breakWhenError: false,
|
|
145
|
+
batchSize: 2,
|
|
146
|
+
onProgress: ({ completed }) => {
|
|
147
|
+
console.log(
|
|
148
|
+
completed.map((x) => (x instanceof Error ? `Error: ${x.message}` : x))
|
|
149
|
+
);
|
|
150
|
+
},
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
queue.add([
|
|
154
|
+
() => Promise.resolve("ok-1"),
|
|
155
|
+
() => Promise.reject(new Error("failed-2")),
|
|
156
|
+
() => Promise.resolve("ok-3"),
|
|
157
|
+
]);
|
|
98
158
|
```
|
|
99
159
|
|
|
100
|
-
###
|
|
160
|
+
### Stop queueing after error (`breakWhenError: true`)
|
|
101
161
|
|
|
102
162
|
```js
|
|
103
163
|
const queue = new Qpass({
|
|
104
164
|
breakWhenError: true,
|
|
105
|
-
|
|
106
|
-
|
|
165
|
+
batchSize: 3,
|
|
166
|
+
onProgress: ({ itemsToProcess, completed }) => {
|
|
167
|
+
console.log({ itemsToProcess, completed });
|
|
107
168
|
},
|
|
108
169
|
});
|
|
109
170
|
|
|
110
|
-
|
|
111
|
-
() => Promise.resolve("
|
|
112
|
-
() => Promise.reject("
|
|
113
|
-
() => Promise.resolve("
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
try {
|
|
117
|
-
await queue.add(jobs);
|
|
118
|
-
} catch (err) {
|
|
119
|
-
console.error("Execution stopped: ", err);
|
|
120
|
-
}
|
|
171
|
+
queue.add([
|
|
172
|
+
() => Promise.resolve("ok-1"),
|
|
173
|
+
() => Promise.reject(new Error("failed-2")),
|
|
174
|
+
() => Promise.resolve("ok-3"),
|
|
175
|
+
() => Promise.resolve("queued-but-may-be-cleared"),
|
|
176
|
+
]);
|
|
121
177
|
```
|
|
122
178
|
|
|
123
|
-
|
|
179
|
+
With `breakWhenError: true`, Qpass clears jobs still waiting in the queue after an error is observed. Jobs already running in the same cycle still settle.
|
|
124
180
|
|
|
125
|
-
|
|
126
|
-
Completed jobs: ["First success"]
|
|
127
|
-
Execution stopped: Second failed β
|
|
128
|
-
// Stops immediately on error, so the third job is never executed.
|
|
129
|
-
```
|
|
181
|
+
## Practical pattern: queue all jobs at once
|
|
130
182
|
|
|
131
|
-
|
|
183
|
+
If you want predictable `itemsToProcess` values in `onProgress`, build the job array first and call `add` once:
|
|
132
184
|
|
|
133
185
|
```js
|
|
134
|
-
const queue = new Qpass({
|
|
135
|
-
batchSize: 5,
|
|
136
|
-
onProgress: (progress) => {
|
|
137
|
-
console.log(
|
|
138
|
-
`Remaining batches: ${progress.batchToProcess}, Remaining tasks: ${progress.itemsToProcess}`
|
|
139
|
-
);
|
|
140
|
-
},
|
|
141
|
-
});
|
|
142
|
-
|
|
143
186
|
const jobs = [];
|
|
144
187
|
|
|
145
|
-
for (let i =
|
|
146
|
-
jobs.push(() =>
|
|
188
|
+
for (let i = 1; i <= 14; i++) {
|
|
189
|
+
jobs.push(() => Promise.resolve(`Job ${i}`));
|
|
147
190
|
}
|
|
148
191
|
|
|
149
192
|
queue.add(jobs);
|
|
150
193
|
```
|
|
151
194
|
|
|
152
|
-
##
|
|
195
|
+
## License
|
|
153
196
|
|
|
154
|
-
|
|
155
|
-
<br>
|
|
156
|
-
β’ Error control (breakWhenError) β choose whether to stop or continue on failure
|
|
157
|
-
<br>
|
|
158
|
-
β’ Progress tracking (onProgress) β monitor task progress in real time
|
|
159
|
-
<br>
|
|
160
|
-
β’ Execution state (isRunning) β check whether the queue is currently running
|
|
197
|
+
ISC
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
default: () => index_default
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(index_exports);
|
|
26
|
+
var QPASS_VERSION = true ? "1.2.0" : "0.0.0";
|
|
27
|
+
var Qpass = class {
|
|
28
|
+
version = QPASS_VERSION;
|
|
29
|
+
// version info
|
|
30
|
+
items = [];
|
|
31
|
+
// job queue
|
|
32
|
+
nextJobId = 1;
|
|
33
|
+
// auto-generated job id
|
|
34
|
+
breakWhenError = false;
|
|
35
|
+
// whether to stop on error
|
|
36
|
+
batchSize = 1;
|
|
37
|
+
// number of jobs to process at once
|
|
38
|
+
onProgress;
|
|
39
|
+
// progress callback
|
|
40
|
+
activeCount = 0;
|
|
41
|
+
// number of jobs currently running
|
|
42
|
+
currentCycleCompleted = [];
|
|
43
|
+
// completed results for current cycle
|
|
44
|
+
stopProcessing = false;
|
|
45
|
+
// stop scheduling new jobs when true
|
|
46
|
+
get batchToProcess() {
|
|
47
|
+
return Math.ceil(this.items.length / this.batchSize);
|
|
48
|
+
}
|
|
49
|
+
constructor(option) {
|
|
50
|
+
this.breakWhenError = !!option?.breakWhenError;
|
|
51
|
+
this.batchSize = option?.batchSize ?? 1;
|
|
52
|
+
if (typeof this.batchSize !== "number" || this.batchSize < 1) {
|
|
53
|
+
throw new Error("batchSize must be at least 1");
|
|
54
|
+
}
|
|
55
|
+
if (option?.onProgress) {
|
|
56
|
+
if (typeof option.onProgress === "function") {
|
|
57
|
+
this.onProgress = option.onProgress;
|
|
58
|
+
} else {
|
|
59
|
+
throw new Error("onProgress must be a function");
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
// add jobs to the queue at once. now returns job id.
|
|
64
|
+
add(jobs) {
|
|
65
|
+
const jobsArray = Array.isArray(jobs) ? jobs : [jobs];
|
|
66
|
+
const addedIds = [];
|
|
67
|
+
for (const job of jobsArray) {
|
|
68
|
+
if (typeof job !== "function") {
|
|
69
|
+
throw new Error(
|
|
70
|
+
"Each job must be a function that returns a Promise"
|
|
71
|
+
);
|
|
72
|
+
}
|
|
73
|
+
const id = String(this.nextJobId++);
|
|
74
|
+
this.items.push({ id, job });
|
|
75
|
+
addedIds.push(id);
|
|
76
|
+
}
|
|
77
|
+
this.processNext();
|
|
78
|
+
return addedIds;
|
|
79
|
+
}
|
|
80
|
+
// remove a queued job by id
|
|
81
|
+
remove(id) {
|
|
82
|
+
const jobId = String(id);
|
|
83
|
+
const index = this.items.findIndex((item) => item.id === jobId);
|
|
84
|
+
if (index === -1) {
|
|
85
|
+
return false;
|
|
86
|
+
}
|
|
87
|
+
this.items.splice(index, 1);
|
|
88
|
+
return true;
|
|
89
|
+
}
|
|
90
|
+
// process next batch of jobs
|
|
91
|
+
processNext() {
|
|
92
|
+
if (this.stopProcessing || this.items.length === 0 || this.activeCount > 0) {
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
const jobsToRun = this.items.splice(0, this.batchSize);
|
|
96
|
+
this.activeCount = jobsToRun.length;
|
|
97
|
+
this.currentCycleCompleted = new Array(jobsToRun.length);
|
|
98
|
+
jobsToRun.forEach((item, index) => {
|
|
99
|
+
Promise.resolve().then(() => item.job()).then((result) => {
|
|
100
|
+
this.finalizeJob(index, result, false);
|
|
101
|
+
return result;
|
|
102
|
+
}).catch((err) => {
|
|
103
|
+
this.finalizeJob(index, err, true);
|
|
104
|
+
return err;
|
|
105
|
+
});
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
finalizeJob(index, value, isError) {
|
|
109
|
+
this.currentCycleCompleted[index] = value;
|
|
110
|
+
this.activeCount -= 1;
|
|
111
|
+
if (isError && this.breakWhenError) {
|
|
112
|
+
this.stopProcessing = true;
|
|
113
|
+
this.items = [];
|
|
114
|
+
}
|
|
115
|
+
if (this.activeCount !== 0) {
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
const completed = this.currentCycleCompleted.slice();
|
|
119
|
+
this.currentCycleCompleted = [];
|
|
120
|
+
if (this.onProgress) {
|
|
121
|
+
this.onProgress({
|
|
122
|
+
batchToProcess: this.batchToProcess,
|
|
123
|
+
itemsToProcess: this.items.length,
|
|
124
|
+
completed
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
this.processNext();
|
|
128
|
+
}
|
|
129
|
+
terminate() {
|
|
130
|
+
this.items = [];
|
|
131
|
+
}
|
|
132
|
+
};
|
|
133
|
+
var index_default = Qpass;
|
|
134
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../index.ts"],"sourcesContent":["declare const __QPASS_VERSION__: string;\n\nconst QPASS_VERSION =\n typeof __QPASS_VERSION__ === \"string\" ? __QPASS_VERSION__ : \"0.0.0\";\n\nclass Qpass {\n version = QPASS_VERSION; // version info\n\n private items: { id: string; job: () => Promise<any> }[] = []; // job queue\n private nextJobId: number = 1; // auto-generated job id\n private breakWhenError: boolean = false; // whether to stop on error\n private batchSize: number = 1; // number of jobs to process at once\n private onProgress?: (progress: {\n batchToProcess: number; // remaining batch count\n itemsToProcess: number; // remaining job count\n completed: any[]; // completed job results array\n }) => void; // progress callback\n\n private activeCount: number = 0; // number of jobs currently running\n private currentCycleCompleted: any[] = []; // completed results for current cycle\n private stopProcessing: boolean = false; // stop scheduling new jobs when true\n\n private get batchToProcess() {\n // calculate total batch count\n return Math.ceil(this.items.length / this.batchSize);\n }\n\n constructor(option?: {\n breakWhenError?: boolean;\n onProgress?: (progress: {\n batchToProcess: number;\n itemsToProcess: number;\n completed: any[];\n }) => void;\n batchSize?: number; // must be at least 1, default is 1\n }) {\n this.breakWhenError = !!option?.breakWhenError;\n this.batchSize = option?.batchSize ?? 1;\n\n if (typeof this.batchSize !== \"number\" || this.batchSize < 1) {\n throw new Error(\"batchSize must be at least 1\");\n }\n\n if (option?.onProgress) {\n if (typeof option.onProgress === \"function\") {\n this.onProgress = option.onProgress;\n } else {\n throw new Error(\"onProgress must be a function\");\n }\n }\n }\n\n // add jobs to the queue at once. now returns job id.\n add(jobs: (() => Promise<any>)[] | (() => Promise<any>)): string[] {\n // check if jobs is an array, if not convert to array\n const jobsArray = Array.isArray(jobs) ? jobs : [jobs];\n const addedIds: string[] = [];\n\n // check if each job is a function, if so add to queue\n for (const job of jobsArray) {\n if (typeof job !== \"function\") {\n throw new Error(\n \"Each job must be a function that returns a Promise\"\n );\n }\n\n const id = String(this.nextJobId++);\n this.items.push({ id, job });\n addedIds.push(id);\n }\n\n this.processNext();\n return addedIds;\n }\n\n // remove a queued job by id\n remove(id: string | number) {\n const jobId = String(id);\n const index = this.items.findIndex((item) => item.id === jobId);\n\n if (index === -1) {\n return false;\n }\n\n this.items.splice(index, 1);\n return true;\n }\n\n // process next batch of jobs\n private processNext() {\n // exit if queue is empty, processing is stopped, or a cycle is still running\n if (\n this.stopProcessing ||\n this.items.length === 0 ||\n this.activeCount > 0\n ) {\n return;\n }\n\n const jobsToRun = this.items.splice(0, this.batchSize);\n this.activeCount = jobsToRun.length;\n this.currentCycleCompleted = new Array(jobsToRun.length);\n\n jobsToRun.forEach((item, index) => {\n Promise.resolve()\n .then(() => item.job())\n .then((result) => {\n this.finalizeJob(index, result, false);\n return result;\n })\n .catch((err) => {\n this.finalizeJob(index, err, true);\n return err;\n });\n });\n }\n\n private finalizeJob(index: number, value: any, isError: boolean) {\n this.currentCycleCompleted[index] = value;\n this.activeCount -= 1;\n\n if (isError && this.breakWhenError) {\n this.stopProcessing = true;\n this.items = [];\n }\n\n if (this.activeCount !== 0) {\n return;\n }\n\n const completed = this.currentCycleCompleted.slice();\n this.currentCycleCompleted = [];\n\n if (this.onProgress) {\n this.onProgress({\n batchToProcess: this.batchToProcess,\n itemsToProcess: this.items.length,\n completed: completed,\n });\n }\n\n this.processNext();\n }\n\n terminate() {\n // remove all remaining jobs from queue\n this.items = [];\n }\n}\n\nexport default Qpass;\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAEA,IAAM,gBACF,OAAwC,UAAoB;AAEhE,IAAM,QAAN,MAAY;AAAA,EACR,UAAU;AAAA;AAAA,EAEF,QAAmD,CAAC;AAAA;AAAA,EACpD,YAAoB;AAAA;AAAA,EACpB,iBAA0B;AAAA;AAAA,EAC1B,YAAoB;AAAA;AAAA,EACpB;AAAA;AAAA,EAMA,cAAsB;AAAA;AAAA,EACtB,wBAA+B,CAAC;AAAA;AAAA,EAChC,iBAA0B;AAAA;AAAA,EAElC,IAAY,iBAAiB;AAEzB,WAAO,KAAK,KAAK,KAAK,MAAM,SAAS,KAAK,SAAS;AAAA,EACvD;AAAA,EAEA,YAAY,QAQT;AACC,SAAK,iBAAiB,CAAC,CAAC,QAAQ;AAChC,SAAK,YAAY,QAAQ,aAAa;AAEtC,QAAI,OAAO,KAAK,cAAc,YAAY,KAAK,YAAY,GAAG;AAC1D,YAAM,IAAI,MAAM,8BAA8B;AAAA,IAClD;AAEA,QAAI,QAAQ,YAAY;AACpB,UAAI,OAAO,OAAO,eAAe,YAAY;AACzC,aAAK,aAAa,OAAO;AAAA,MAC7B,OAAO;AACH,cAAM,IAAI,MAAM,+BAA+B;AAAA,MACnD;AAAA,IACJ;AAAA,EACJ;AAAA;AAAA,EAGA,IAAI,MAA+D;AAE/D,UAAM,YAAY,MAAM,QAAQ,IAAI,IAAI,OAAO,CAAC,IAAI;AACpD,UAAM,WAAqB,CAAC;AAG5B,eAAW,OAAO,WAAW;AACzB,UAAI,OAAO,QAAQ,YAAY;AAC3B,cAAM,IAAI;AAAA,UACN;AAAA,QACJ;AAAA,MACJ;AAEA,YAAM,KAAK,OAAO,KAAK,WAAW;AAClC,WAAK,MAAM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BAAgB,KAAK;AAAA,QACrB,gBAAgB,KAAK,MAAM;AAAA,QAC3B;AAAA,MACJ,CAAC;AAAA,IACL;AAEA,SAAK,YAAY;AAAA,EACrB;AAAA,EAEA,YAAY;AAER,SAAK,QAAQ,CAAC;AAAA,EAClB;AACJ;AAEA,IAAO,gBAAQ;","names":[]}
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
declare class Qpass {
|
|
2
|
+
version: string;
|
|
3
|
+
private items;
|
|
4
|
+
private nextJobId;
|
|
5
|
+
private breakWhenError;
|
|
6
|
+
private batchSize;
|
|
7
|
+
private onProgress?;
|
|
8
|
+
private activeCount;
|
|
9
|
+
private currentCycleCompleted;
|
|
10
|
+
private stopProcessing;
|
|
11
|
+
private get batchToProcess();
|
|
12
|
+
constructor(option?: {
|
|
13
|
+
breakWhenError?: boolean;
|
|
14
|
+
onProgress?: (progress: {
|
|
15
|
+
batchToProcess: number;
|
|
16
|
+
itemsToProcess: number;
|
|
17
|
+
completed: any[];
|
|
18
|
+
}) => void;
|
|
19
|
+
batchSize?: number;
|
|
20
|
+
});
|
|
21
|
+
add(jobs: (() => Promise<any>)[] | (() => Promise<any>)): string[];
|
|
22
|
+
remove(id: string | number): boolean;
|
|
23
|
+
private processNext;
|
|
24
|
+
private finalizeJob;
|
|
25
|
+
terminate(): void;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export { Qpass as default };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
declare class Qpass {
|
|
2
|
+
version: string;
|
|
3
|
+
private items;
|
|
4
|
+
private nextJobId;
|
|
5
|
+
private breakWhenError;
|
|
6
|
+
private batchSize;
|
|
7
|
+
private onProgress?;
|
|
8
|
+
private activeCount;
|
|
9
|
+
private currentCycleCompleted;
|
|
10
|
+
private stopProcessing;
|
|
11
|
+
private get batchToProcess();
|
|
12
|
+
constructor(option?: {
|
|
13
|
+
breakWhenError?: boolean;
|
|
14
|
+
onProgress?: (progress: {
|
|
15
|
+
batchToProcess: number;
|
|
16
|
+
itemsToProcess: number;
|
|
17
|
+
completed: any[];
|
|
18
|
+
}) => void;
|
|
19
|
+
batchSize?: number;
|
|
20
|
+
});
|
|
21
|
+
add(jobs: (() => Promise<any>)[] | (() => Promise<any>)): string[];
|
|
22
|
+
remove(id: string | number): boolean;
|
|
23
|
+
private processNext;
|
|
24
|
+
private finalizeJob;
|
|
25
|
+
terminate(): void;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export { Qpass as default };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
// index.ts
|
|
2
|
+
var QPASS_VERSION = true ? "1.2.0" : "0.0.0";
|
|
3
|
+
var Qpass = class {
|
|
4
|
+
version = QPASS_VERSION;
|
|
5
|
+
// version info
|
|
6
|
+
items = [];
|
|
7
|
+
// job queue
|
|
8
|
+
nextJobId = 1;
|
|
9
|
+
// auto-generated job id
|
|
10
|
+
breakWhenError = false;
|
|
11
|
+
// whether to stop on error
|
|
12
|
+
batchSize = 1;
|
|
13
|
+
// number of jobs to process at once
|
|
14
|
+
onProgress;
|
|
15
|
+
// progress callback
|
|
16
|
+
activeCount = 0;
|
|
17
|
+
// number of jobs currently running
|
|
18
|
+
currentCycleCompleted = [];
|
|
19
|
+
// completed results for current cycle
|
|
20
|
+
stopProcessing = false;
|
|
21
|
+
// stop scheduling new jobs when true
|
|
22
|
+
get batchToProcess() {
|
|
23
|
+
return Math.ceil(this.items.length / this.batchSize);
|
|
24
|
+
}
|
|
25
|
+
constructor(option) {
|
|
26
|
+
this.breakWhenError = !!option?.breakWhenError;
|
|
27
|
+
this.batchSize = option?.batchSize ?? 1;
|
|
28
|
+
if (typeof this.batchSize !== "number" || this.batchSize < 1) {
|
|
29
|
+
throw new Error("batchSize must be at least 1");
|
|
30
|
+
}
|
|
31
|
+
if (option?.onProgress) {
|
|
32
|
+
if (typeof option.onProgress === "function") {
|
|
33
|
+
this.onProgress = option.onProgress;
|
|
34
|
+
} else {
|
|
35
|
+
throw new Error("onProgress must be a function");
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
// add jobs to the queue at once. now returns job id.
|
|
40
|
+
add(jobs) {
|
|
41
|
+
const jobsArray = Array.isArray(jobs) ? jobs : [jobs];
|
|
42
|
+
const addedIds = [];
|
|
43
|
+
for (const job of jobsArray) {
|
|
44
|
+
if (typeof job !== "function") {
|
|
45
|
+
throw new Error(
|
|
46
|
+
"Each job must be a function that returns a Promise"
|
|
47
|
+
);
|
|
48
|
+
}
|
|
49
|
+
const id = String(this.nextJobId++);
|
|
50
|
+
this.items.push({ id, job });
|
|
51
|
+
addedIds.push(id);
|
|
52
|
+
}
|
|
53
|
+
this.processNext();
|
|
54
|
+
return addedIds;
|
|
55
|
+
}
|
|
56
|
+
// remove a queued job by id
|
|
57
|
+
remove(id) {
|
|
58
|
+
const jobId = String(id);
|
|
59
|
+
const index = this.items.findIndex((item) => item.id === jobId);
|
|
60
|
+
if (index === -1) {
|
|
61
|
+
return false;
|
|
62
|
+
}
|
|
63
|
+
this.items.splice(index, 1);
|
|
64
|
+
return true;
|
|
65
|
+
}
|
|
66
|
+
// process next batch of jobs
|
|
67
|
+
processNext() {
|
|
68
|
+
if (this.stopProcessing || this.items.length === 0 || this.activeCount > 0) {
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
const jobsToRun = this.items.splice(0, this.batchSize);
|
|
72
|
+
this.activeCount = jobsToRun.length;
|
|
73
|
+
this.currentCycleCompleted = new Array(jobsToRun.length);
|
|
74
|
+
jobsToRun.forEach((item, index) => {
|
|
75
|
+
Promise.resolve().then(() => item.job()).then((result) => {
|
|
76
|
+
this.finalizeJob(index, result, false);
|
|
77
|
+
return result;
|
|
78
|
+
}).catch((err) => {
|
|
79
|
+
this.finalizeJob(index, err, true);
|
|
80
|
+
return err;
|
|
81
|
+
});
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
finalizeJob(index, value, isError) {
|
|
85
|
+
this.currentCycleCompleted[index] = value;
|
|
86
|
+
this.activeCount -= 1;
|
|
87
|
+
if (isError && this.breakWhenError) {
|
|
88
|
+
this.stopProcessing = true;
|
|
89
|
+
this.items = [];
|
|
90
|
+
}
|
|
91
|
+
if (this.activeCount !== 0) {
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
const completed = this.currentCycleCompleted.slice();
|
|
95
|
+
this.currentCycleCompleted = [];
|
|
96
|
+
if (this.onProgress) {
|
|
97
|
+
this.onProgress({
|
|
98
|
+
batchToProcess: this.batchToProcess,
|
|
99
|
+
itemsToProcess: this.items.length,
|
|
100
|
+
completed
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
this.processNext();
|
|
104
|
+
}
|
|
105
|
+
terminate() {
|
|
106
|
+
this.items = [];
|
|
107
|
+
}
|
|
108
|
+
};
|
|
109
|
+
var index_default = Qpass;
|
|
110
|
+
export {
|
|
111
|
+
index_default as default
|
|
112
|
+
};
|
|
113
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../index.ts"],"sourcesContent":["declare const __QPASS_VERSION__: string;\n\nconst QPASS_VERSION =\n typeof __QPASS_VERSION__ === \"string\" ? __QPASS_VERSION__ : \"0.0.0\";\n\nclass Qpass {\n version = QPASS_VERSION; // version info\n\n private items: { id: string; job: () => Promise<any> }[] = []; // job queue\n private nextJobId: number = 1; // auto-generated job id\n private breakWhenError: boolean = false; // whether to stop on error\n private batchSize: number = 1; // number of jobs to process at once\n private onProgress?: (progress: {\n batchToProcess: number; // remaining batch count\n itemsToProcess: number; // remaining job count\n completed: any[]; // completed job results array\n }) => void; // progress callback\n\n private activeCount: number = 0; // number of jobs currently running\n private currentCycleCompleted: any[] = []; // completed results for current cycle\n private stopProcessing: boolean = false; // stop scheduling new jobs when true\n\n private get batchToProcess() {\n // calculate total batch count\n return Math.ceil(this.items.length / this.batchSize);\n }\n\n constructor(option?: {\n breakWhenError?: boolean;\n onProgress?: (progress: {\n batchToProcess: number;\n itemsToProcess: number;\n completed: any[];\n }) => void;\n batchSize?: number; // must be at least 1, default is 1\n }) {\n this.breakWhenError = !!option?.breakWhenError;\n this.batchSize = option?.batchSize ?? 1;\n\n if (typeof this.batchSize !== \"number\" || this.batchSize < 1) {\n throw new Error(\"batchSize must be at least 1\");\n }\n\n if (option?.onProgress) {\n if (typeof option.onProgress === \"function\") {\n this.onProgress = option.onProgress;\n } else {\n throw new Error(\"onProgress must be a function\");\n }\n }\n }\n\n // add jobs to the queue at once. now returns job id.\n add(jobs: (() => Promise<any>)[] | (() => Promise<any>)): string[] {\n // check if jobs is an array, if not convert to array\n const jobsArray = Array.isArray(jobs) ? jobs : [jobs];\n const addedIds: string[] = [];\n\n // check if each job is a function, if so add to queue\n for (const job of jobsArray) {\n if (typeof job !== \"function\") {\n throw new Error(\n \"Each job must be a function that returns a Promise\"\n );\n }\n\n const id = String(this.nextJobId++);\n this.items.push({ id, job });\n addedIds.push(id);\n }\n\n this.processNext();\n return addedIds;\n }\n\n // remove a queued job by id\n remove(id: string | number) {\n const jobId = String(id);\n const index = this.items.findIndex((item) => item.id === jobId);\n\n if (index === -1) {\n return false;\n }\n\n this.items.splice(index, 1);\n return true;\n }\n\n // process next batch of jobs\n private processNext() {\n // exit if queue is empty, processing is stopped, or a cycle is still running\n if (\n this.stopProcessing ||\n this.items.length === 0 ||\n this.activeCount > 0\n ) {\n return;\n }\n\n const jobsToRun = this.items.splice(0, this.batchSize);\n this.activeCount = jobsToRun.length;\n this.currentCycleCompleted = new Array(jobsToRun.length);\n\n jobsToRun.forEach((item, index) => {\n Promise.resolve()\n .then(() => item.job())\n .then((result) => {\n this.finalizeJob(index, result, false);\n return result;\n })\n .catch((err) => {\n this.finalizeJob(index, err, true);\n return err;\n });\n });\n }\n\n private finalizeJob(index: number, value: any, isError: boolean) {\n this.currentCycleCompleted[index] = value;\n this.activeCount -= 1;\n\n if (isError && this.breakWhenError) {\n this.stopProcessing = true;\n this.items = [];\n }\n\n if (this.activeCount !== 0) {\n return;\n }\n\n const completed = this.currentCycleCompleted.slice();\n this.currentCycleCompleted = [];\n\n if (this.onProgress) {\n this.onProgress({\n batchToProcess: this.batchToProcess,\n itemsToProcess: this.items.length,\n completed: completed,\n });\n }\n\n this.processNext();\n }\n\n terminate() {\n // remove all remaining jobs from queue\n this.items = [];\n }\n}\n\nexport default Qpass;\n"],"mappings":";AAEA,IAAM,gBACF,OAAwC,UAAoB;AAEhE,IAAM,QAAN,MAAY;AAAA,EACR,UAAU;AAAA;AAAA,EAEF,QAAmD,CAAC;AAAA;AAAA,EACpD,YAAoB;AAAA;AAAA,EACpB,iBAA0B;AAAA;AAAA,EAC1B,YAAoB;AAAA;AAAA,EACpB;AAAA;AAAA,EAMA,cAAsB;AAAA;AAAA,EACtB,wBAA+B,CAAC;AAAA;AAAA,EAChC,iBAA0B;AAAA;AAAA,EAElC,IAAY,iBAAiB;AAEzB,WAAO,KAAK,KAAK,KAAK,MAAM,SAAS,KAAK,SAAS;AAAA,EACvD;AAAA,EAEA,YAAY,QAQT;AACC,SAAK,iBAAiB,CAAC,CAAC,QAAQ;AAChC,SAAK,YAAY,QAAQ,aAAa;AAEtC,QAAI,OAAO,KAAK,cAAc,YAAY,KAAK,YAAY,GAAG;AAC1D,YAAM,IAAI,MAAM,8BAA8B;AAAA,IAClD;AAEA,QAAI,QAAQ,YAAY;AACpB,UAAI,OAAO,OAAO,eAAe,YAAY;AACzC,aAAK,aAAa,OAAO;AAAA,MAC7B,OAAO;AACH,cAAM,IAAI,MAAM,+BAA+B;AAAA,MACnD;AAAA,IACJ;AAAA,EACJ;AAAA;AAAA,EAGA,IAAI,MAA+D;AAE/D,UAAM,YAAY,MAAM,QAAQ,IAAI,IAAI,OAAO,CAAC,IAAI;AACpD,UAAM,WAAqB,CAAC;AAG5B,eAAW,OAAO,WAAW;AACzB,UAAI,OAAO,QAAQ,YAAY;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AY,KAAK,sBAAsB,MAAM;AACnD,SAAK,wBAAwB,CAAC;AAE9B,QAAI,KAAK,YAAY;AACjB,WAAK,WAAW;AAAA,QACZ,gBAAgB,KAAK;AAAA,QACrB,gBAAgB,KAAK,MAAM;AAAA,QAC3B;AAAA,MACJ,CAAC;AAAA,IACL;AAEA,SAAK,YAAY;AAAA,EACrB;AAAA,EAEA,YAAY;AAER,SAAK,QAAQ,CAAC;AAAA,EAClB;AACJ;AAEA,IAAO,gBAAQ;","names":[]}
|
package/dist/qpass.js
ADDED
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var Qpass = (() => {
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __export = (target, all) => {
|
|
8
|
+
for (var name in all)
|
|
9
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
|
+
};
|
|
11
|
+
var __copyProps = (to, from, except, desc) => {
|
|
12
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
+
for (let key of __getOwnPropNames(from))
|
|
14
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
20
|
+
|
|
21
|
+
// index.ts
|
|
22
|
+
var index_exports = {};
|
|
23
|
+
__export(index_exports, {
|
|
24
|
+
default: () => index_default
|
|
25
|
+
});
|
|
26
|
+
var QPASS_VERSION = true ? "1.2.0" : "0.0.0";
|
|
27
|
+
var Qpass = class {
|
|
28
|
+
version = QPASS_VERSION;
|
|
29
|
+
// version info
|
|
30
|
+
items = [];
|
|
31
|
+
// job queue
|
|
32
|
+
nextJobId = 1;
|
|
33
|
+
// auto-generated job id
|
|
34
|
+
breakWhenError = false;
|
|
35
|
+
// whether to stop on error
|
|
36
|
+
batchSize = 1;
|
|
37
|
+
// number of jobs to process at once
|
|
38
|
+
onProgress;
|
|
39
|
+
// progress callback
|
|
40
|
+
activeCount = 0;
|
|
41
|
+
// number of jobs currently running
|
|
42
|
+
currentCycleCompleted = [];
|
|
43
|
+
// completed results for current cycle
|
|
44
|
+
stopProcessing = false;
|
|
45
|
+
// stop scheduling new jobs when true
|
|
46
|
+
get batchToProcess() {
|
|
47
|
+
return Math.ceil(this.items.length / this.batchSize);
|
|
48
|
+
}
|
|
49
|
+
constructor(option) {
|
|
50
|
+
this.breakWhenError = !!option?.breakWhenError;
|
|
51
|
+
this.batchSize = option?.batchSize ?? 1;
|
|
52
|
+
if (typeof this.batchSize !== "number" || this.batchSize < 1) {
|
|
53
|
+
throw new Error("batchSize must be at least 1");
|
|
54
|
+
}
|
|
55
|
+
if (option?.onProgress) {
|
|
56
|
+
if (typeof option.onProgress === "function") {
|
|
57
|
+
this.onProgress = option.onProgress;
|
|
58
|
+
} else {
|
|
59
|
+
throw new Error("onProgress must be a function");
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
// add jobs to the queue at once. now returns job id.
|
|
64
|
+
add(jobs) {
|
|
65
|
+
const jobsArray = Array.isArray(jobs) ? jobs : [jobs];
|
|
66
|
+
const addedIds = [];
|
|
67
|
+
for (const job of jobsArray) {
|
|
68
|
+
if (typeof job !== "function") {
|
|
69
|
+
throw new Error(
|
|
70
|
+
"Each job must be a function that returns a Promise"
|
|
71
|
+
);
|
|
72
|
+
}
|
|
73
|
+
const id = String(this.nextJobId++);
|
|
74
|
+
this.items.push({ id, job });
|
|
75
|
+
addedIds.push(id);
|
|
76
|
+
}
|
|
77
|
+
this.processNext();
|
|
78
|
+
return addedIds;
|
|
79
|
+
}
|
|
80
|
+
// remove a queued job by id
|
|
81
|
+
remove(id) {
|
|
82
|
+
const jobId = String(id);
|
|
83
|
+
const index = this.items.findIndex((item) => item.id === jobId);
|
|
84
|
+
if (index === -1) {
|
|
85
|
+
return false;
|
|
86
|
+
}
|
|
87
|
+
this.items.splice(index, 1);
|
|
88
|
+
return true;
|
|
89
|
+
}
|
|
90
|
+
// process next batch of jobs
|
|
91
|
+
processNext() {
|
|
92
|
+
if (this.stopProcessing || this.items.length === 0 || this.activeCount > 0) {
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
const jobsToRun = this.items.splice(0, this.batchSize);
|
|
96
|
+
this.activeCount = jobsToRun.length;
|
|
97
|
+
this.currentCycleCompleted = new Array(jobsToRun.length);
|
|
98
|
+
jobsToRun.forEach((item, index) => {
|
|
99
|
+
Promise.resolve().then(() => item.job()).then((result) => {
|
|
100
|
+
this.finalizeJob(index, result, false);
|
|
101
|
+
return result;
|
|
102
|
+
}).catch((err) => {
|
|
103
|
+
this.finalizeJob(index, err, true);
|
|
104
|
+
return err;
|
|
105
|
+
});
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
finalizeJob(index, value, isError) {
|
|
109
|
+
this.currentCycleCompleted[index] = value;
|
|
110
|
+
this.activeCount -= 1;
|
|
111
|
+
if (isError && this.breakWhenError) {
|
|
112
|
+
this.stopProcessing = true;
|
|
113
|
+
this.items = [];
|
|
114
|
+
}
|
|
115
|
+
if (this.activeCount !== 0) {
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
const completed = this.currentCycleCompleted.slice();
|
|
119
|
+
this.currentCycleCompleted = [];
|
|
120
|
+
if (this.onProgress) {
|
|
121
|
+
this.onProgress({
|
|
122
|
+
batchToProcess: this.batchToProcess,
|
|
123
|
+
itemsToProcess: this.items.length,
|
|
124
|
+
completed
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
this.processNext();
|
|
128
|
+
}
|
|
129
|
+
terminate() {
|
|
130
|
+
this.items = [];
|
|
131
|
+
}
|
|
132
|
+
};
|
|
133
|
+
var index_default = Qpass;
|
|
134
|
+
return __toCommonJS(index_exports);
|
|
135
|
+
})();
|
|
136
|
+
//# sourceMappingURL=qpass.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../index.ts"],"sourcesContent":["declare const __QPASS_VERSION__: string;\n\nconst QPASS_VERSION =\n typeof __QPASS_VERSION__ === \"string\" ? __QPASS_VERSION__ : \"0.0.0\";\n\nclass Qpass {\n version = QPASS_VERSION; // version info\n\n private items: { id: string; job: () => Promise<any> }[] = []; // job queue\n private nextJobId: number = 1; // auto-generated job id\n private breakWhenError: boolean = false; // whether to stop on error\n private batchSize: number = 1; // number of jobs to process at once\n private onProgress?: (progress: {\n batchToProcess: number; // remaining batch count\n itemsToProcess: number; // remaining job count\n completed: any[]; // completed job results array\n }) => void; // progress callback\n\n private activeCount: number = 0; // number of jobs currently running\n private currentCycleCompleted: any[] = []; // completed results for current cycle\n private stopProcessing: boolean = false; // stop scheduling new jobs when true\n\n private get batchToProcess() {\n // calculate total batch count\n return Math.ceil(this.items.length / this.batchSize);\n }\n\n constructor(option?: {\n breakWhenError?: boolean;\n onProgress?: (progress: {\n batchToProcess: number;\n itemsToProcess: number;\n completed: any[];\n }) => void;\n batchSize?: number; // must be at least 1, default is 1\n }) {\n this.breakWhenError = !!option?.breakWhenError;\n this.batchSize = option?.batchSize ?? 1;\n\n if (typeof this.batchSize !== \"number\" || this.batchSize < 1) {\n throw new Error(\"batchSize must be at least 1\");\n }\n\n if (option?.onProgress) {\n if (typeof option.onProgress === \"function\") {\n this.onProgress = option.onProgress;\n } else {\n throw new Error(\"onProgress must be a function\");\n }\n }\n }\n\n // add jobs to the queue at once. now returns job id.\n add(jobs: (() => Promise<any>)[] | (() => Promise<any>)): string[] {\n // check if jobs is an array, if not convert to array\n const jobsArray = Array.isArray(jobs) ? jobs : [jobs];\n const addedIds: string[] = [];\n\n // check if each job is a function, if so add to queue\n for (const job of jobsArray) {\n if (typeof job !== \"function\") {\n throw new Error(\n \"Each job must be a function that returns a Promise\"\n );\n }\n\n const id = String(this.nextJobId++);\n this.items.push({ id, job });\n addedIds.push(id);\n }\n\n this.processNext();\n return addedIds;\n }\n\n // remove a queued job by id\n remove(id: string | number) {\n const jobId = String(id);\n const index = this.items.findIndex((item) => item.id === jobId);\n\n if (index === -1) {\n return false;\n }\n\n this.items.splice(index, 1);\n return true;\n }\n\n // process next batch of jobs\n private processNext() {\n // exit if queue is empty, processing is stopped, or a cycle is still running\n if (\n this.stopProcessing ||\n this.items.length === 0 ||\n this.activeCount > 0\n ) {\n return;\n }\n\n const jobsToRun = this.items.splice(0, this.batchSize);\n this.activeCount = jobsToRun.length;\n this.currentCycleCompleted = new Array(jobsToRun.length);\n\n jobsToRun.forEach((item, index) => {\n Promise.resolve()\n .then(() => item.job())\n .then((result) => {\n this.finalizeJob(index, result, false);\n return result;\n })\n .catch((err) => {\n this.finalizeJob(index, err, true);\n return err;\n });\n });\n }\n\n private finalizeJob(index: number, value: any, isError: boolean) {\n this.currentCycleCompleted[index] = value;\n this.activeCount -= 1;\n\n if (isError && this.breakWhenError) {\n this.stopProcessing = true;\n this.items = [];\n }\n\n if (this.activeCount !== 0) {\n return;\n }\n\n const completed = this.currentCycleCompleted.slice();\n this.currentCycleCompleted = [];\n\n if (this.onProgress) {\n this.onProgress({\n batchToProcess: this.batchToProcess,\n itemsToProcess: this.items.length,\n completed: completed,\n });\n }\n\n this.processNext();\n }\n\n terminate() {\n // remove all remaining jobs from queue\n this.items = [];\n }\n}\n\nexport default Qpass;\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAEA,MAAM,gBACF,OAAwC,UAAoB;AAEhE,MAAM,QAAN,MAAY;AAAA,IACR,UAAU;AAAA;AAAA,IAEF,QAAmD,CAAC;AAAA;AAAA,IACpD,YAAoB;AAAA;AAAA,IACpB,iBAA0B;AAA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
"description": "Asynchronous function handler with adjustable concurrency",
|
|
5
|
-
"main": "index.
|
|
5
|
+
"main": "./dist/index.cjs",
|
|
6
|
+
"module": "./dist/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"import": "./dist/index.js",
|
|
12
|
+
"require": "./dist/index.cjs",
|
|
13
|
+
"default": "./dist/index.js"
|
|
14
|
+
},
|
|
15
|
+
"./package.json": "./package.json"
|
|
16
|
+
},
|
|
17
|
+
"browser": "./dist/index.js",
|
|
18
|
+
"unpkg": "./dist/qpass.js",
|
|
19
|
+
"jsdelivr": "./dist/qpass.js",
|
|
20
|
+
"files": [
|
|
21
|
+
"dist",
|
|
22
|
+
"README.md"
|
|
23
|
+
],
|
|
6
24
|
"type": "module",
|
|
7
25
|
"scripts": {
|
|
8
|
-
"test": "
|
|
9
|
-
"
|
|
26
|
+
"test": "npm run build && node ./test.js",
|
|
27
|
+
"typecheck": "tsc --noEmit --project tsconfig.json",
|
|
28
|
+
"build": "tsup --config tsup.config.mjs"
|
|
10
29
|
},
|
|
11
30
|
"author": "",
|
|
12
31
|
"license": "ISC",
|
|
13
32
|
"devDependencies": {
|
|
33
|
+
"tsup": "^8.5.0",
|
|
14
34
|
"typescript": "^5.9.3"
|
|
15
35
|
}
|
|
16
36
|
}
|
package/index.js
DELETED
|
@@ -1,109 +0,0 @@
|
|
|
1
|
-
class Qpass {
|
|
2
|
-
version = "1.0.0"; // version info
|
|
3
|
-
items = []; // job queue
|
|
4
|
-
breakWhenError = false; // whether to stop on error
|
|
5
|
-
batchSize = 1; // number of jobs to process at once
|
|
6
|
-
onProgress; // progress callback
|
|
7
|
-
completed = []; // completed job results array
|
|
8
|
-
runningBatches = []; // currently running batch count
|
|
9
|
-
runningSlots = []; // running slot status array
|
|
10
|
-
get batchToProcess() {
|
|
11
|
-
// calculate total batch count
|
|
12
|
-
return Math.ceil(this.items.length / this.batchSize);
|
|
13
|
-
}
|
|
14
|
-
get batchProcessFinished() {
|
|
15
|
-
let completedCount = Object.keys(this.completed).length;
|
|
16
|
-
let isFinished = completedCount === this.runningBatches.length;
|
|
17
|
-
if (isFinished) {
|
|
18
|
-
const completed = this.completed; // copy
|
|
19
|
-
// reset state before calling callback
|
|
20
|
-
this.completed = [];
|
|
21
|
-
this.runningBatches = [];
|
|
22
|
-
this.runningSlots = [];
|
|
23
|
-
if (this.onProgress) {
|
|
24
|
-
this.onProgress({
|
|
25
|
-
batchToProcess: this.batchToProcess,
|
|
26
|
-
itemsToProcess: this.items.length,
|
|
27
|
-
completed: completed,
|
|
28
|
-
});
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
return isFinished;
|
|
32
|
-
}
|
|
33
|
-
constructor(option) {
|
|
34
|
-
this.breakWhenError = !!option?.breakWhenError;
|
|
35
|
-
this.batchSize = option?.batchSize ?? 1;
|
|
36
|
-
if (typeof this.batchSize !== "number" || this.batchSize < 1) {
|
|
37
|
-
throw new Error("batchSize must be at least 1");
|
|
38
|
-
}
|
|
39
|
-
if (option?.onProgress) {
|
|
40
|
-
if (typeof option.onProgress === "function") {
|
|
41
|
-
this.onProgress = option.onProgress;
|
|
42
|
-
}
|
|
43
|
-
else {
|
|
44
|
-
throw new Error("onProgress must be a function");
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
// add jobs to the queue at once
|
|
49
|
-
add(jobs) {
|
|
50
|
-
// check if jobs is an array, if not convert to array
|
|
51
|
-
const jobsArray = Array.isArray(jobs) ? jobs : [jobs];
|
|
52
|
-
// check if each job is a function, if so add to queue
|
|
53
|
-
for (const job of jobsArray) {
|
|
54
|
-
if (typeof job !== "function") {
|
|
55
|
-
throw new Error("Each job must be a function that returns a Promise");
|
|
56
|
-
}
|
|
57
|
-
this.items.push(job);
|
|
58
|
-
}
|
|
59
|
-
this.processNext();
|
|
60
|
-
}
|
|
61
|
-
// process next batch of jobs
|
|
62
|
-
processNext() {
|
|
63
|
-
// exit if queue is empty
|
|
64
|
-
if (this.items.length === 0) {
|
|
65
|
-
return;
|
|
66
|
-
}
|
|
67
|
-
const batchToRun = this.batchSize - this.runningBatches.length;
|
|
68
|
-
if (batchToRun <= 0) {
|
|
69
|
-
// exit if current running batches reached maximum
|
|
70
|
-
return;
|
|
71
|
-
}
|
|
72
|
-
// extract batchToRun amount of jobs
|
|
73
|
-
this.runningBatches.push(...this.items.splice(0, batchToRun));
|
|
74
|
-
for (let i = 0; i < this.runningBatches.length; i++) {
|
|
75
|
-
if (this.runningSlots?.[i]) {
|
|
76
|
-
continue;
|
|
77
|
-
}
|
|
78
|
-
this.runningSlots[i] = true;
|
|
79
|
-
this.runningBatches[i]()
|
|
80
|
-
.then((result) => {
|
|
81
|
-
if (this.completed[i]) {
|
|
82
|
-
return result;
|
|
83
|
-
}
|
|
84
|
-
this.completed[i] = result;
|
|
85
|
-
if (this.batchProcessFinished) {
|
|
86
|
-
return this.processNext();
|
|
87
|
-
}
|
|
88
|
-
return result;
|
|
89
|
-
})
|
|
90
|
-
.catch((err) => {
|
|
91
|
-
// error handling
|
|
92
|
-
if (this.breakWhenError) {
|
|
93
|
-
throw err;
|
|
94
|
-
}
|
|
95
|
-
this.completed[i] = err;
|
|
96
|
-
if (this.batchProcessFinished) {
|
|
97
|
-
return this.processNext();
|
|
98
|
-
}
|
|
99
|
-
return err;
|
|
100
|
-
});
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
terminate() {
|
|
104
|
-
// remove all remaining jobs from queue
|
|
105
|
-
this.items = [];
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
export default Qpass;
|
|
109
|
-
//# sourceMappingURL=index.js.map
|
package/index.ts
DELETED
|
@@ -1,148 +0,0 @@
|
|
|
1
|
-
class Qpass {
|
|
2
|
-
version = "1.0.0"; // version info
|
|
3
|
-
|
|
4
|
-
private items: (() => Promise<any>)[] = []; // job queue
|
|
5
|
-
private breakWhenError: boolean = false; // whether to stop on error
|
|
6
|
-
private batchSize: number = 1; // number of jobs to process at once
|
|
7
|
-
private onProgress?: (progress: {
|
|
8
|
-
batchToProcess: number; // remaining batch count
|
|
9
|
-
itemsToProcess: number; // remaining job count
|
|
10
|
-
completed: any[]; // completed job results array
|
|
11
|
-
}) => void; // progress callback
|
|
12
|
-
|
|
13
|
-
private completed: any[] = []; // completed job results array
|
|
14
|
-
private runningBatches: (() => Promise<any>)[] = []; // currently running batch count
|
|
15
|
-
private runningSlots: boolean[] = []; // running slot status array
|
|
16
|
-
|
|
17
|
-
private get batchToProcess() {
|
|
18
|
-
// calculate total batch count
|
|
19
|
-
return Math.ceil(this.items.length / this.batchSize);
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
private get batchProcessFinished() {
|
|
23
|
-
let completedCount = Object.keys(this.completed).length;
|
|
24
|
-
let isFinished = completedCount === this.runningBatches.length;
|
|
25
|
-
|
|
26
|
-
if (isFinished) {
|
|
27
|
-
const completed = this.completed; // copy
|
|
28
|
-
|
|
29
|
-
// reset state before calling callback
|
|
30
|
-
this.completed = [];
|
|
31
|
-
this.runningBatches = [];
|
|
32
|
-
this.runningSlots = [];
|
|
33
|
-
|
|
34
|
-
if (this.onProgress) {
|
|
35
|
-
this.onProgress({
|
|
36
|
-
batchToProcess: this.batchToProcess,
|
|
37
|
-
itemsToProcess: this.items.length,
|
|
38
|
-
completed: completed,
|
|
39
|
-
});
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
return isFinished;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
constructor(option?: {
|
|
47
|
-
breakWhenError?: boolean;
|
|
48
|
-
onProgress?: (progress: {
|
|
49
|
-
batchToProcess: number;
|
|
50
|
-
itemsToProcess: number;
|
|
51
|
-
completed: any[];
|
|
52
|
-
}) => void;
|
|
53
|
-
batchSize?: number; // must be at least 1, default is 1
|
|
54
|
-
}) {
|
|
55
|
-
this.breakWhenError = !!option?.breakWhenError;
|
|
56
|
-
this.batchSize = option?.batchSize ?? 1;
|
|
57
|
-
|
|
58
|
-
if (typeof this.batchSize !== "number" || this.batchSize < 1) {
|
|
59
|
-
throw new Error("batchSize must be at least 1");
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
if (option?.onProgress) {
|
|
63
|
-
if (typeof option.onProgress === "function") {
|
|
64
|
-
this.onProgress = option.onProgress;
|
|
65
|
-
} else {
|
|
66
|
-
throw new Error("onProgress must be a function");
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
// add jobs to the queue at once
|
|
72
|
-
add(jobs: (() => Promise<any>)[] | (() => Promise<any>)) {
|
|
73
|
-
// check if jobs is an array, if not convert to array
|
|
74
|
-
const jobsArray = Array.isArray(jobs) ? jobs : [jobs];
|
|
75
|
-
|
|
76
|
-
// check if each job is a function, if so add to queue
|
|
77
|
-
for (const job of jobsArray) {
|
|
78
|
-
if (typeof job !== "function") {
|
|
79
|
-
throw new Error(
|
|
80
|
-
"Each job must be a function that returns a Promise"
|
|
81
|
-
);
|
|
82
|
-
}
|
|
83
|
-
this.items.push(job);
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
this.processNext();
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
// process next batch of jobs
|
|
90
|
-
private processNext() {
|
|
91
|
-
// exit if queue is empty
|
|
92
|
-
if (this.items.length === 0) {
|
|
93
|
-
return;
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
const batchToRun = this.batchSize - this.runningBatches.length;
|
|
97
|
-
if (batchToRun <= 0) {
|
|
98
|
-
// exit if current running batches reached maximum
|
|
99
|
-
return;
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
// extract batchToRun amount of jobs
|
|
103
|
-
this.runningBatches.push(...this.items.splice(0, batchToRun));
|
|
104
|
-
|
|
105
|
-
for (let i = 0; i < this.runningBatches.length; i++) {
|
|
106
|
-
if (this.runningSlots?.[i]) {
|
|
107
|
-
continue;
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
this.runningSlots[i] = true;
|
|
111
|
-
(this.runningBatches[i] as () => Promise<any>)()
|
|
112
|
-
.then((result) => {
|
|
113
|
-
if (this.completed[i]) {
|
|
114
|
-
return result;
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
this.completed[i] = result;
|
|
118
|
-
|
|
119
|
-
if (this.batchProcessFinished) {
|
|
120
|
-
return this.processNext();
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
return result;
|
|
124
|
-
})
|
|
125
|
-
.catch((err) => {
|
|
126
|
-
// error handling
|
|
127
|
-
if (this.breakWhenError) {
|
|
128
|
-
throw err;
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
this.completed[i] = err;
|
|
132
|
-
|
|
133
|
-
if (this.batchProcessFinished) {
|
|
134
|
-
return this.processNext();
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
return err;
|
|
138
|
-
});
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
terminate() {
|
|
143
|
-
// remove all remaining jobs from queue
|
|
144
|
-
this.items = [];
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
export default Qpass;
|
package/test.js
DELETED
|
@@ -1,68 +0,0 @@
|
|
|
1
|
-
import Qpass from "./index.js";
|
|
2
|
-
|
|
3
|
-
// job creator
|
|
4
|
-
const createJob =
|
|
5
|
-
(id, duration, shouldFail = false) =>
|
|
6
|
-
() => {
|
|
7
|
-
return new Promise((resolve, reject) => {
|
|
8
|
-
setTimeout(() => {
|
|
9
|
-
if (shouldFail) {
|
|
10
|
-
reject(new Error(`Job ${id} failed`));
|
|
11
|
-
} else {
|
|
12
|
-
resolve(`Job ${id} completed`);
|
|
13
|
-
}
|
|
14
|
-
}, duration);
|
|
15
|
-
});
|
|
16
|
-
};
|
|
17
|
-
|
|
18
|
-
// test code
|
|
19
|
-
console.log("π Qpass test started\n");
|
|
20
|
-
|
|
21
|
-
let did2ndTest = false;
|
|
22
|
-
|
|
23
|
-
const onProgress = ({ batchToProcess, itemsToProcess, completed }) => {
|
|
24
|
-
console.log(`Remaining batches: ${batchToProcess}`);
|
|
25
|
-
console.log(`Remaining jobs: ${itemsToProcess}`);
|
|
26
|
-
if(itemsToProcess === 5) {
|
|
27
|
-
queue.terminate();
|
|
28
|
-
}
|
|
29
|
-
console.log(
|
|
30
|
-
"Completed:",
|
|
31
|
-
completed.map((r) => (r instanceof Error ? `Error: ${r.message}` : r))
|
|
32
|
-
);
|
|
33
|
-
|
|
34
|
-
console.log("---");
|
|
35
|
-
|
|
36
|
-
if (itemsToProcess === 0) {
|
|
37
|
-
console.log("π Qpass test completed");
|
|
38
|
-
|
|
39
|
-
if (!did2ndTest) {
|
|
40
|
-
console.log("\nπ Qpass array addition 2nd test started\n");
|
|
41
|
-
|
|
42
|
-
did2ndTest = true;
|
|
43
|
-
testArray();
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
};
|
|
47
|
-
|
|
48
|
-
const queue = new Qpass({
|
|
49
|
-
breakWhenError: false,
|
|
50
|
-
batchSize: 3,
|
|
51
|
-
onProgress: onProgress,
|
|
52
|
-
});
|
|
53
|
-
|
|
54
|
-
const testLoop = () => {
|
|
55
|
-
for (let i = 1; i <= 14; i++) {
|
|
56
|
-
queue.add(createJob(i, 800, i % 3 === 0)); // multiples of 3 will fail
|
|
57
|
-
}
|
|
58
|
-
};
|
|
59
|
-
|
|
60
|
-
testLoop();
|
|
61
|
-
|
|
62
|
-
const testArray = () => {
|
|
63
|
-
let jobs = [];
|
|
64
|
-
for (let i = 1; i <= 14; i++) {
|
|
65
|
-
jobs.push(createJob(i, 800, i % 3 === 0)); // multiples of 3 will fail
|
|
66
|
-
}
|
|
67
|
-
queue.add(jobs);
|
|
68
|
-
};
|
package/tsconfig.json
DELETED
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
// Visit https://aka.ms/tsconfig to read more about this file
|
|
3
|
-
"compilerOptions": {
|
|
4
|
-
// File Layout
|
|
5
|
-
// "rootDir": "./src",
|
|
6
|
-
// "outDir": "./dist",
|
|
7
|
-
|
|
8
|
-
// Environment Settings
|
|
9
|
-
// See also https://aka.ms/tsconfig/module
|
|
10
|
-
"module": "nodenext",
|
|
11
|
-
"target": "esnext",
|
|
12
|
-
"types": [],
|
|
13
|
-
// For nodejs:
|
|
14
|
-
// "lib": ["esnext"],
|
|
15
|
-
// "types": ["node"],
|
|
16
|
-
// and npm install -D @types/node
|
|
17
|
-
|
|
18
|
-
// Other Outputs
|
|
19
|
-
"sourceMap": true,
|
|
20
|
-
"declaration": true,
|
|
21
|
-
"declarationMap": true,
|
|
22
|
-
|
|
23
|
-
// Stricter Typechecking Options
|
|
24
|
-
"noUncheckedIndexedAccess": true,
|
|
25
|
-
"exactOptionalPropertyTypes": true,
|
|
26
|
-
|
|
27
|
-
// Style Options
|
|
28
|
-
// "noImplicitReturns": true,
|
|
29
|
-
// "noImplicitOverride": true,
|
|
30
|
-
// "noUnusedLocals": true,
|
|
31
|
-
// "noUnusedParameters": true,
|
|
32
|
-
// "noFallthroughCasesInSwitch": true,
|
|
33
|
-
// "noPropertyAccessFromIndexSignature": true,
|
|
34
|
-
|
|
35
|
-
// Recommended Options
|
|
36
|
-
"strict": true,
|
|
37
|
-
"jsx": "react-jsx",
|
|
38
|
-
"verbatimModuleSyntax": true,
|
|
39
|
-
"isolatedModules": true,
|
|
40
|
-
"noUncheckedSideEffectImports": true,
|
|
41
|
-
"moduleDetection": "force",
|
|
42
|
-
"skipLibCheck": true,
|
|
43
|
-
|
|
44
|
-
// "target": "ES2015", // λλ "ES6"
|
|
45
|
-
"lib": ["ES2015", "DOM"] // Promiseμ λΈλΌμ°μ κ΄λ ¨ APIλ₯Ό μ§μ
|
|
46
|
-
}
|
|
47
|
-
}
|