@sapphire/async-queue
Advanced tools
Comparing version 1.5.3-next.04bc8e22.0 to 1.5.3-next.0645081b
@@ -7,8 +7,5 @@ var SapphireAsyncQueue = (function (exports) { | ||
var __name = (target, value) => __defProp(target, "name", { value, configurable: true }); | ||
var __publicField = (obj, key, value) => { | ||
__defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value); | ||
return value; | ||
}; | ||
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value); | ||
// src/lib/AsyncQueueEntry.ts | ||
// src/lib/_AsyncQueueEntry.ts | ||
var _AsyncQueueEntry = class _AsyncQueueEntry { | ||
@@ -29,9 +26,7 @@ constructor(queue) { | ||
setSignal(signal) { | ||
if (signal.aborted) | ||
return this; | ||
if (signal.aborted) return this; | ||
this.signal = signal; | ||
this.signalListener = () => { | ||
const index = this.queue["promises"].indexOf(this); | ||
if (index !== -1) | ||
this.queue["promises"].splice(index, 1); | ||
if (index !== -1) this.queue["promises"].splice(index, 1); | ||
this.reject(new Error("Request aborted manually")); | ||
@@ -113,4 +108,3 @@ }; | ||
this.promises.push(entry); | ||
if (options?.signal) | ||
entry.setSignal(options.signal); | ||
if (options?.signal) entry.setSignal(options.signal); | ||
return entry.promise; | ||
@@ -122,4 +116,3 @@ } | ||
shift() { | ||
if (this.promises.length === 0) | ||
return; | ||
if (this.promises.length === 0) return; | ||
if (this.promises.length === 1) { | ||
@@ -137,4 +130,3 @@ this.promises.shift(); | ||
abortAll() { | ||
if (this.queued === 0) | ||
return; | ||
if (this.queued === 0) return; | ||
for (let i = 1; i < this.promises.length; ++i) { | ||
@@ -154,3 +146,3 @@ this.promises[i].abort(); | ||
})({}); | ||
//# sourceMappingURL=out.js.map | ||
//# sourceMappingURL=index.global.js.map | ||
//# sourceMappingURL=index.global.js.map |
{ | ||
"name": "@sapphire/async-queue", | ||
"version": "1.5.3-next.04bc8e22.0", | ||
"version": "1.5.3-next.0645081b", | ||
"description": "Sequential asynchronous lock-based queue for promises", | ||
@@ -29,7 +29,8 @@ "author": "@sapphire", | ||
"docs": "typedoc-json-parser", | ||
"build": "tsup && yarn build:rename-cjs-index", | ||
"build:rename-cjs-index": "tsx --tsconfig ../../scripts/tsconfig.json ../../scripts/rename-cjs-index.cts", | ||
"build": "yarn gen-index && tsup && yarn build:rename-cjs-index", | ||
"build:rename-cjs-index": "tsx ../../scripts/rename-cjs-index.cts", | ||
"prepack": "yarn build", | ||
"bump": "cliff-jumper", | ||
"check-update": "cliff-jumper --dry-run" | ||
"check-update": "cliff-jumper --dry-run", | ||
"gen-index": "tsx ../../scripts/gen-index.cts -w async-queue" | ||
}, | ||
@@ -65,11 +66,11 @@ "repository": { | ||
"devDependencies": { | ||
"@favware/cliff-jumper": "^3.0.3", | ||
"@vitest/coverage-v8": "^1.6.0", | ||
"tsup": "^8.0.2", | ||
"tsx": "^4.9.3", | ||
"@favware/cliff-jumper": "^4.0.3", | ||
"@vitest/coverage-v8": "^2.0.3", | ||
"tsup": "^8.2.1", | ||
"tsx": "^4.16.2", | ||
"typedoc": "^0.25.13", | ||
"typedoc-json-parser": "^10.0.0", | ||
"typescript": "^5.4.5", | ||
"vitest": "^1.6.0" | ||
"typescript": "~5.4.5", | ||
"vitest": "^2.0.3" | ||
} | ||
} |
@@ -16,5 +16,9 @@ <div align="center"> | ||
## Description | ||
**Table of Contents** | ||
Ever needed a queue for a set of promises? This is the package for you. | ||
- [Features](#features) | ||
- [Installation](#installation) | ||
- [Usage](#usage) | ||
- [Buy us some doughnuts](#buy-us-some-doughnuts) | ||
- [Contributors](#contributors) | ||
@@ -25,3 +29,3 @@ ## Features | ||
- Bundled with esbuild so it can be used in NodeJS and browsers | ||
- Offers CommonJS, ESM and UMD bundles | ||
- Offers CommonJS, ESM, and UMD bundles | ||
- Fully tested | ||
@@ -37,2 +41,35 @@ | ||
## Usage | ||
**Note**: While this section uses `require`, the imports match 1:1 with ESM imports. For example `const { AsyncQueue } = require('@sapphire/async-queue')` equals `import { AsyncQueue } from '@sapphire/async-queue'`. | ||
```typescript | ||
// Require the AsyncQueue class | ||
const { AsyncQueue } = require('@sapphire/async-queue'); | ||
const queue = new AsyncQueue(); | ||
async function request(url, options) { | ||
// Wait and lock the queue | ||
await queue.wait(); | ||
try { | ||
// Perform the operation sequentially | ||
return await fetch(url, options); | ||
} finally { | ||
// Unlock the next promise in the queue | ||
queue.shift(); | ||
} | ||
} | ||
request(someUrl1, someOptions1); | ||
// Will call fetch() immediately | ||
request(someUrl2, someOptions2); | ||
// Will call fetch() after the first finished | ||
request(someUrl3, someOptions3); | ||
// Will call fetch() after the second finished | ||
``` | ||
--- | ||
@@ -44,3 +81,3 @@ | ||
We accept donations through Open Collective, Ko-fi, PayPal, Patreon and GitHub Sponsorships. You can use the buttons below to donate through your method of choice. | ||
We accept donations through Open Collective, Ko-fi, PayPal, Patreon, and GitHub Sponsorships. You can use the buttons below to donate through your method of choice. | ||
@@ -47,0 +84,0 @@ | Donate With | Address | |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
57862
98
398