From c6f462f0d7ebb95bd82e4c5bfcde24e42a3719db Mon Sep 17 00:00:00 2001 From: tresabhi Date: Sat, 20 May 2023 01:22:20 +0000 Subject: [PATCH] feat: use Promise.all --- package.json | 1 + src/core/blitz/listAccountsPanServer.ts | 75 ++++++++++--------------- src/core/blitz/listClansPanServer.ts | 75 ++++++++++--------------- src/reset.d.ts | 1 + yarn.lock | 5 ++ 5 files changed, 69 insertions(+), 88 deletions(-) create mode 100644 src/reset.d.ts diff --git a/package.json b/package.json index 9808157ff..25ddfdec7 100644 --- a/package.json +++ b/package.json @@ -17,6 +17,7 @@ "node-fetch": "^3.3.1" }, "devDependencies": { + "@total-typescript/ts-reset": "^0.4.2", "@types/lodash.startcase": "^4.4.7", "@types/markdown-escape": "^1.1.0", "@types/node": "^20.2.1", diff --git a/src/core/blitz/listAccountsPanServer.ts b/src/core/blitz/listAccountsPanServer.ts index ad4524255..aaf5676bd 100644 --- a/src/core/blitz/listAccountsPanServer.ts +++ b/src/core/blitz/listAccountsPanServer.ts @@ -7,58 +7,45 @@ export type AccountListWithServer = (Account & { server: 'com' | 'eu' | 'asia'; })[]; -export default function listAccountsPanServer(search: string, limit = 9) { - return new Promise(async (resolve) => { - const trimmed = search.trim(); - if (trimmed.length < 3 && trimmed.length > 100) resolve([]); +export default async function listAccountsPanServer(search: string, limit = 9) { + const trimmed = search.trim(); + if (trimmed.length < 3 && trimmed.length > 100) return []; + const normalizedLimit = Math.round(limit / 3); - const normalizedLimit = Math.round(limit / 3); - const accountList: AccountListWithServer = []; - let done = 0; - - function afterResponse() { - done++; - - if (done === 3) { - resolve(accountList); - } - } - - getWargamingResponse( - `https://api.wotblitz.com/wotb/account/list/?application_id=${args['wargaming-application-id']}&search=${search}&limit=${normalizedLimit}`, - ).then((value) => { - if (value) - accountList.push( - ...value.map((account) => ({ + return ( + await Promise.all([ + getWargamingResponse( + `https://api.wotblitz.com/wotb/account/list/?application_id=${args['wargaming-application-id']}&search=${search}&limit=${normalizedLimit}`, + ).then( + (value) => + value && + value.map((account) => ({ ...account, server: 'com' as BlitzServer, })), - ); - afterResponse(); - }); - getWargamingResponse( - `https://api.wotblitz.eu/wotb/account/list/?application_id=${args['wargaming-application-id']}&search=${search}&limit=${normalizedLimit}`, - ).then((value) => { - if (value) - accountList.push( - ...value.map((account) => ({ + ), + getWargamingResponse( + `https://api.wotblitz.eu/wotb/account/list/?application_id=${args['wargaming-application-id']}&search=${search}&limit=${normalizedLimit}`, + ).then( + (value) => + value && + value.map((account) => ({ ...account, server: 'eu' as BlitzServer, })), - ); - afterResponse(); - }); - getWargamingResponse( - `https://api.wotblitz.asia/wotb/account/list/?application_id=${args['wargaming-application-id']}&search=${search}&limit=${normalizedLimit}`, - ).then((value) => { - if (value) - accountList.push( - ...value.map((account) => ({ + ), + getWargamingResponse( + `https://api.wotblitz.asia/wotb/account/list/?application_id=${args['wargaming-application-id']}&search=${search}&limit=${normalizedLimit}`, + ).then( + (value) => + value && + value.map((account) => ({ ...account, server: 'asia' as BlitzServer, })), - ); - afterResponse(); - }); - }); + ), + ]) + ) + .filter(Boolean) + .flat(); } diff --git a/src/core/blitz/listClansPanServer.ts b/src/core/blitz/listClansPanServer.ts index a7d6e0622..8822831e7 100644 --- a/src/core/blitz/listClansPanServer.ts +++ b/src/core/blitz/listClansPanServer.ts @@ -7,58 +7,45 @@ export type ClanListWithServer = (Clan & { server: 'com' | 'eu' | 'asia'; })[]; -export default function listClansPanServer(search: string, limit = 9) { - return new Promise(async (resolve) => { - const trimmed = search.trim(); - if (trimmed.length < 3 && trimmed.length > 100) resolve([]); +export default async function listClansPanServer(search: string, limit = 9) { + const trimmed = search.trim(); + if (trimmed.length < 3 && trimmed.length > 100) return []; + const normalizedLimit = Math.round(limit / 3); - const normalizedLimit = Math.round(limit / 3); - const accountList: ClanListWithServer = []; - let done = 0; - - function afterResponse() { - done++; - - if (done === 3) { - resolve(accountList); - } - } - - getWargamingResponse( - `https://api.wotblitz.com/wotb/clans/list/?application_id=${args['wargaming-application-id']}&search=${search}&limit=${normalizedLimit}`, - ).then((value) => { - if (value) - accountList.push( - ...value.map((account) => ({ + return ( + await Promise.all([ + getWargamingResponse( + `https://api.wotblitz.com/wotb/clans/list/?application_id=${args['wargaming-application-id']}&search=${search}&limit=${normalizedLimit}`, + ).then( + (value) => + value && + value.map((account) => ({ ...account, server: 'com' as BlitzServer, })), - ); - afterResponse(); - }); - getWargamingResponse( - `https://api.wotblitz.eu/wotb/clans/list/?application_id=${args['wargaming-application-id']}&search=${search}&limit=${normalizedLimit}`, - ).then((value) => { - if (value) - accountList.push( - ...value.map((account) => ({ + ), + getWargamingResponse( + `https://api.wotblitz.eu/wotb/clans/list/?application_id=${args['wargaming-application-id']}&search=${search}&limit=${normalizedLimit}`, + ).then( + (value) => + value && + value.map((account) => ({ ...account, server: 'eu' as BlitzServer, })), - ); - afterResponse(); - }); - getWargamingResponse( - `https://api.wotblitz.asia/wotb/clans/list/?application_id=${args['wargaming-application-id']}&search=${search}&limit=${normalizedLimit}`, - ).then((value) => { - if (value) - accountList.push( - ...value.map((account) => ({ + ), + getWargamingResponse( + `https://api.wotblitz.asia/wotb/clans/list/?application_id=${args['wargaming-application-id']}&search=${search}&limit=${normalizedLimit}`, + ).then( + (value) => + value && + value.map((account) => ({ ...account, server: 'asia' as BlitzServer, })), - ); - afterResponse(); - }); - }); + ), + ]) + ) + .filter(Boolean) + .flat(); } diff --git a/src/reset.d.ts b/src/reset.d.ts new file mode 100644 index 000000000..12bd3edc9 --- /dev/null +++ b/src/reset.d.ts @@ -0,0 +1 @@ +import '@total-typescript/ts-reset'; diff --git a/yarn.lock b/yarn.lock index 59f8a61e8..a3081a5a3 100644 --- a/yarn.lock +++ b/yarn.lock @@ -179,6 +179,11 @@ resolved "https://registry.npmjs.org/@tokenizer/token/-/token-0.3.0.tgz" integrity sha512-OvjF+z51L3ov0OyAU0duzsYuvO01PH7x4t6DJx+guahgTnBHkhJdG7soQeTSFLWN3efnHyibZ4Z8l2EuWwJN3A== +"@total-typescript/ts-reset@^0.4.2": + version "0.4.2" + resolved "https://registry.yarnpkg.com/@total-typescript/ts-reset/-/ts-reset-0.4.2.tgz#c564c173ba09973968e1046c93965b7a257878a4" + integrity sha512-vqd7ZUDSrXFVT1n8b2kc3LnklncDQFPvR58yUS1kEP23/nHPAO9l1lMjUfnPrXYYk4Hj54rrLKMW5ipwk7k09A== + "@types/lodash.startcase@^4.4.7": version "4.4.7" resolved "https://registry.yarnpkg.com/@types/lodash.startcase/-/lodash.startcase-4.4.7.tgz#69857f17be0aa5ce74da4fa386e32e22c012258a"