feat(api): switched to a more light-weight async queue package
This commit is contained in:
parent
421160c0dc
commit
12f73d42ca
3 changed files with 29 additions and 29 deletions
|
@ -35,8 +35,8 @@
|
|||
"release": "semantic-release"
|
||||
},
|
||||
"dependencies": {
|
||||
"@sapphire/async-queue": "^1.1.4",
|
||||
"abort-controller": "^3.0.0",
|
||||
"async": "^3.2.0",
|
||||
"form-data": "^4.0.0",
|
||||
"lodash": "^4.17.21",
|
||||
"node-fetch": "^2.6.1",
|
||||
|
@ -53,7 +53,6 @@
|
|||
"@semantic-release/git": "^9.0.0",
|
||||
"@semantic-release/npm": "^7.1.3",
|
||||
"@semantic-release/release-notes-generator": "^9.0.3",
|
||||
"@types/async": "^3",
|
||||
"@types/chai": "^4.2.19",
|
||||
"@types/eslint": "^7.2.13",
|
||||
"@types/lodash": "^4",
|
||||
|
|
|
@ -1,12 +1,13 @@
|
|||
import { AsyncQueue } from '@sapphire/async-queue';
|
||||
import { AbortController } from 'abort-controller';
|
||||
import { queue, QueueObject } from 'async';
|
||||
import FormData from 'form-data';
|
||||
import fetch from 'node-fetch';
|
||||
|
||||
import { sleep } from '../utils/sleep';
|
||||
|
||||
import type { ApiManager } from './apiManager';
|
||||
import type { Client } from '../client/client';
|
||||
import type { IMakeRequestOptions, IRequestOptions, IRouteIdentifier } from '../utils/types';
|
||||
import { sleep } from '../utils/sleep';
|
||||
import type { IMakeRequestOptions, IRouteIdentifier } from '../utils/types';
|
||||
|
||||
export class ApiHandler {
|
||||
public client: Client;
|
||||
|
@ -15,7 +16,7 @@ export class ApiHandler {
|
|||
public limit: number;
|
||||
public majorParameter: string;
|
||||
public manager: ApiManager;
|
||||
public queue: QueueObject<unknown>;
|
||||
public queue: AsyncQueue;
|
||||
public remaining: number;
|
||||
public reset: number;
|
||||
|
||||
|
@ -30,10 +31,11 @@ export class ApiHandler {
|
|||
this.limit = -1;
|
||||
this.majorParameter = majorParameter;
|
||||
this.manager = manager;
|
||||
this.queue = queue(async (task: { routeId: IRouteIdentifier, requestOptions: IMakeRequestOptions; }, callback) => {
|
||||
if (this.limited) await sleep(this.timeToReset);
|
||||
callback(await this.makeRequest(task.routeId, task.requestOptions));
|
||||
});
|
||||
this.queue = new AsyncQueue();
|
||||
// this.queue = queue(async (task: { routeId: IRouteIdentifier, requestOptions: IMakeRequestOptions; }, callback) => {
|
||||
// if (this.limited) await sleep(this.timeToReset);
|
||||
// callback(await this.makeRequest(task.routeId, task.requestOptions));
|
||||
// });
|
||||
this.remaining = -1;
|
||||
this.reset = -1;
|
||||
}
|
||||
|
@ -113,8 +115,15 @@ export class ApiHandler {
|
|||
}
|
||||
}
|
||||
|
||||
public async push<T>(routeId: IRouteIdentifier, requestOptions: IRequestOptions): Promise<T> {
|
||||
return await this.queue.pushAsync<T>({ routeId, requestOptions });
|
||||
public async push<T>(routeId: IRouteIdentifier, requestOptions: IMakeRequestOptions): Promise<T> {
|
||||
await this.queue.wait();
|
||||
try {
|
||||
if (this.limited) await sleep(this.timeToReset);
|
||||
|
||||
return await this.makeRequest<T>(routeId, requestOptions);
|
||||
} finally {
|
||||
this.queue.shift();
|
||||
}
|
||||
}
|
||||
|
||||
public get baseApiUrl(): string {
|
||||
|
@ -122,7 +131,7 @@ export class ApiHandler {
|
|||
}
|
||||
|
||||
public get inactive(): boolean {
|
||||
return this.queue.length() === 0 && !this.limited;
|
||||
return !this.queue.remaining && !this.limited;
|
||||
}
|
||||
|
||||
public get limited(): boolean {
|
||||
|
|
24
yarn.lock
24
yarn.lock
|
@ -541,12 +541,12 @@ __metadata:
|
|||
"@commitlint/cz-commitlint": ^12.1.4
|
||||
"@istanbuljs/nyc-config-typescript": ^1.0.1
|
||||
"@saithodev/semantic-release-gitea": ^2.1.0
|
||||
"@sapphire/async-queue": ^1.1.4
|
||||
"@semantic-release/changelog": ^5.0.1
|
||||
"@semantic-release/commit-analyzer": ^8.0.1
|
||||
"@semantic-release/git": ^9.0.0
|
||||
"@semantic-release/npm": ^7.1.3
|
||||
"@semantic-release/release-notes-generator": ^9.0.3
|
||||
"@types/async": ^3
|
||||
"@types/chai": ^4.2.19
|
||||
"@types/eslint": ^7.2.13
|
||||
"@types/lodash": ^4
|
||||
|
@ -561,7 +561,6 @@ __metadata:
|
|||
"@typescript-eslint/parser": ^4.28.1
|
||||
"@typescript-eslint/typescript-estree": ^4.28.1
|
||||
abort-controller: ^3.0.0
|
||||
async: ^3.2.0
|
||||
bufferutil: ^4.0.3
|
||||
chai: ^4.3.4
|
||||
commitizen: ^4.2.4
|
||||
|
@ -950,6 +949,13 @@ __metadata:
|
|||
languageName: node
|
||||
linkType: hard
|
||||
|
||||
"@sapphire/async-queue@npm:^1.1.4":
|
||||
version: 1.1.4
|
||||
resolution: "@sapphire/async-queue@npm:1.1.4"
|
||||
checksum: 416a8f3b1de7452b251a650c1b33d5bef829a7b6ef215ad58a0cc04847f247f17ebae1e9424a26f29ec2e36d2f98294e71cdafe20a32612356b4674dd4125ae0
|
||||
languageName: node
|
||||
linkType: hard
|
||||
|
||||
"@semantic-release/changelog@npm:^5.0.1":
|
||||
version: 5.0.1
|
||||
resolution: "@semantic-release/changelog@npm:5.0.1"
|
||||
|
@ -1162,13 +1168,6 @@ __metadata:
|
|||
languageName: node
|
||||
linkType: hard
|
||||
|
||||
"@types/async@npm:^3":
|
||||
version: 3.2.6
|
||||
resolution: "@types/async@npm:3.2.6"
|
||||
checksum: a4d06f473d7167b7537247f629f9bd5df8ad43001ea5bf421e94dd55df1d19685b5900b4943b795febe53d719fd32b21b4792516f7920e35249c6c8de60254f0
|
||||
languageName: node
|
||||
linkType: hard
|
||||
|
||||
"@types/cacheable-request@npm:^6.0.1":
|
||||
version: 6.0.1
|
||||
resolution: "@types/cacheable-request@npm:6.0.1"
|
||||
|
@ -1814,13 +1813,6 @@ __metadata:
|
|||
languageName: node
|
||||
linkType: hard
|
||||
|
||||
"async@npm:^3.2.0":
|
||||
version: 3.2.0
|
||||
resolution: "async@npm:3.2.0"
|
||||
checksum: 6739fae769e6c9f76b272558f118ef041d45c979c573a8fe93f8cfbc32eb9c92da032e9effe6bbcc9b1131292cde6c4a9e61a442894aa06a262addd8dd3adda1
|
||||
languageName: node
|
||||
linkType: hard
|
||||
|
||||
"asynckit@npm:^0.4.0":
|
||||
version: 0.4.0
|
||||
resolution: "asynckit@npm:0.4.0"
|
||||
|
|
Reference in a new issue