@highoutput/async-group
Advanced tools
Comparing version 0.2.3 to 0.3.0
export default class AsyncGroup { | ||
private funcs; | ||
private promises; | ||
private static instance; | ||
add<T = any>(promise: Promise<T>): Promise<T>; | ||
wait(): Promise<void>; | ||
get size(): number; | ||
static add<T = any>(promise: Promise<T>): Promise<T>; | ||
@@ -7,0 +8,0 @@ static wait(): Promise<void>; |
@@ -7,29 +7,35 @@ "use strict"; | ||
const v4_1 = __importDefault(require("uuid/v4")); | ||
class AsyncGroup { | ||
constructor() { | ||
this.funcs = new Map(); | ||
} | ||
async add(promise) { | ||
const id = v4_1.default(); | ||
this.funcs.set(id, promise.finally(() => { | ||
this.funcs.delete(id); | ||
})); | ||
return promise; | ||
} | ||
async wait() { | ||
while (this.funcs.size > 0) { | ||
const promises = Array.from(this.funcs.values()); | ||
this.funcs.clear(); | ||
await Promise.all(promises); | ||
let AsyncGroup = (() => { | ||
class AsyncGroup { | ||
constructor() { | ||
this.promises = new Map(); | ||
} | ||
async add(promise) { | ||
const id = v4_1.default(); | ||
this.promises.set(id, promise.finally(() => { | ||
this.promises.delete(id); | ||
})); | ||
return promise; | ||
} | ||
async wait() { | ||
while (this.promises.size > 0) { | ||
const promises = Array.from(this.promises.values()); | ||
this.promises.clear(); | ||
await Promise.all(promises); | ||
} | ||
} | ||
get size() { | ||
return this.promises.size; | ||
} | ||
static async add(promise) { | ||
return this.instance.add(promise); | ||
} | ||
static async wait() { | ||
await this.instance.wait(); | ||
} | ||
} | ||
static async add(promise) { | ||
return this.instance.add(promise); | ||
} | ||
static async wait() { | ||
await this.instance.wait(); | ||
} | ||
} | ||
AsyncGroup.instance = new AsyncGroup(); | ||
return AsyncGroup; | ||
})(); | ||
exports.default = AsyncGroup; | ||
AsyncGroup.instance = new AsyncGroup(); | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@highoutput/async-group", | ||
"version": "0.2.3", | ||
"version": "0.3.0", | ||
"description": "Groups together multiple promises and allows to 'wait' for all of them to settle.", | ||
@@ -27,3 +27,4 @@ "author": "High Output Ventures", | ||
"@types/uuid": "^3.4.6", | ||
"rimraf": "^3.0.0" | ||
"rimraf": "^3.0.0", | ||
"typescript": "^3.9.3" | ||
}, | ||
@@ -33,3 +34,3 @@ "publishConfig": { | ||
}, | ||
"gitHead": "dc55257256f2b9b62063db23471612829fd74adc" | ||
"gitHead": "ebdb30739a8f2bbbe48346aaaed438f38d93e831" | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
5667
48
3