From 38e769ad2c9f9842499477e747b62663b34951df Mon Sep 17 00:00:00 2001 From: ZeroWave022 <36341766+ZeroWave022@users.noreply.github.com> Date: Sun, 30 Apr 2023 00:49:09 +0200 Subject: [PATCH 1/2] feat: Move currency caches to own classes --- src/structures/CurrencyCache.ts | 87 +++++++++++++++++++++++++++------ src/structures/TipccClient.ts | 51 +++++-------------- 2 files changed, 84 insertions(+), 54 deletions(-) diff --git a/src/structures/CurrencyCache.ts b/src/structures/CurrencyCache.ts index 3cee1a3..bd90d98 100644 --- a/src/structures/CurrencyCache.ts +++ b/src/structures/CurrencyCache.ts @@ -1,3 +1,11 @@ +import { CryptoCurrency, ExchangeRate, FiatCurrency, TipccClient } from '..'; +import { + RESTGetAPICurrenciesCryptoCurrenciesResult, + RESTGetAPICurrenciesFiatsResult, + RESTGetAPICurrenciesRatesResult, + Routes, +} from '@tipccjs/tipcc-api-types/v0'; + /** * A class extending Array holding a cache of objects with type T. * @@ -5,26 +13,15 @@ * @typeParam T - The object type this {@link CurrencyCache} will hold */ export class CurrencyCache extends Array { - private refreshFunction: () => T[] | Promise; + public client: TipccClient; /** * Create a CurrencyCache. - * @param refreshFunction The refresh function which returns new values to insert to this cache + * @param client The client which instantiated this CurrencyCache */ - constructor(refreshFunction: () => T[] | Promise) { + constructor(client: TipccClient) { super(); - - this.refreshFunction = refreshFunction; - } - - /** - * Refresh this CurrencyCache with new values received from refreshFunction. - */ - public async refresh(): Promise> { - const refreshedData = await this.refreshFunction(); - this.splice(0, this.length, refreshedData); - - return this; + this.client = client; } /** @@ -37,3 +34,63 @@ export class CurrencyCache extends Array { return null; } } + +/** + * A class extending {@link CurrencyCache} holding cryptocurrencies. + * + * @category Client utilities + */ +export class CryptocurrencyCache extends CurrencyCache { + public async refresh(): Promise { + const { cryptocurrencies } = (await this.client.REST.get( + Routes.currenciesCryptocurrencies(), + )) as RESTGetAPICurrenciesCryptoCurrenciesResult; + + this.splice( + 0, + this.length, + ...cryptocurrencies.map((c) => new CryptoCurrency(c)), + ); + + return this; + } +} + +/** + * A class extending {@link CurrencyCache} holding fiats. + * + * @category Client utilities + */ +export class FiatCache extends CurrencyCache { + public async refresh(): Promise { + const { fiats } = (await this.client.REST.get( + Routes.currenciesFiats(), + )) as RESTGetAPICurrenciesFiatsResult; + + this.splice(0, this.length, ...fiats.map((f) => new FiatCurrency(f))); + + return this; + } +} + +/** + * A class extending {@link CurrencyCache} holding exchange rates. + * + * @category Client utilities + */ +export class ExchangeRateCache extends CurrencyCache { + public async refresh(): Promise { + const { rates } = (await this.client.REST.get( + Routes.currenciesRates(), + )) as RESTGetAPICurrenciesRatesResult; + //return rates.map((r) => new ExchangeRate(r, this)); + + this.splice( + 0, + this.length, + ...rates.map((r) => new ExchangeRate(r, this.client)), + ); + + return this; + } +} diff --git a/src/structures/TipccClient.ts b/src/structures/TipccClient.ts index ea61167..b5216b7 100644 --- a/src/structures/TipccClient.ts +++ b/src/structures/TipccClient.ts @@ -1,13 +1,14 @@ import { EventEmitter } from 'node:events'; import { RequestHandler } from './RequestHandler'; import { Transaction } from './Transaction'; -import { CurrencyCache } from './CurrencyCache'; -import { CryptoCurrency, FiatCurrency } from './Currency'; +import { + CryptocurrencyCache, + ExchangeRateCache, + FiatCache, +} from './CurrencyCache'; import { ExchangeRate } from './ExchangeRate'; import { Wallet } from './Wallet'; import { - RESTGetAPICurrenciesCryptoCurrenciesResult, - RESTGetAPICurrenciesFiatsResult, RESTGetAPIAccountTransactionResult, RESTGetAPIAccountTransactionsResult, RESTGetAPIAccountWalletResult, @@ -37,19 +38,17 @@ export class TipccClient extends EventEmitter { /** The tip.cc API token this client uses */ public token: string; - /** The {@link RequestHandler} this client uses */ + /** The {@link RequestHandler} for this client */ public REST: RequestHandler; - /** The {@link CurrencyCache} for cryptocurrencies */ - public cryptos = new CurrencyCache(this._refreshCryptos); + /** The {@link CryptocurrencyCache} for this client */ + public cryptos = new CryptocurrencyCache(this); - /** The {@link CurrencyCache} for fiat currencies */ - public fiats = new CurrencyCache(this._refreshFiats); + /** The {@link FiatCache} for this client */ + public fiats = new FiatCache(this); - /** The {@link CurrencyCache} for exchange rates */ - public exchangeRates = new CurrencyCache( - this._refreshExchangeRates, - ); + /** The {@link ExchangeRateCache} for this client */ + public exchangeRates = new ExchangeRateCache(this); /** A boolean indicating whether the client is ready */ public isReady = false; @@ -180,32 +179,6 @@ export class TipccClient extends EventEmitter { } } - private async _refreshCryptos(): Promise { - const { cryptocurrencies } = (await this.REST.request( - 'GET', - Routes.currenciesCryptocurrencies(), - )) as RESTGetAPICurrenciesCryptoCurrenciesResult; - - return cryptocurrencies.map((c) => new CryptoCurrency(c)); - } - - private async _refreshFiats(): Promise { - const { fiats } = (await this.REST.request( - 'GET', - Routes.currenciesFiats(), - )) as RESTGetAPICurrenciesFiatsResult; - - return fiats.map((c) => new FiatCurrency(c)); - } - - private async _refreshExchangeRates(): Promise { - const { rates } = (await this.REST.request( - 'GET', - Routes.currenciesRates(), - )) as RESTGetAPICurrenciesRatesResult; - return rates.map((r) => new ExchangeRate(r, this)); - } - public on(s: K, f: (arg: Events[K]) => void): this { super.on(s, f); this.polling.add(s); From f9ae42b1409cba292ee759f06af4a3a6babcb5ff Mon Sep 17 00:00:00 2001 From: ZeroWave022 <36341766+ZeroWave022@users.noreply.github.com> Date: Sun, 30 Apr 2023 11:22:55 +0200 Subject: [PATCH 2/2] fix: Remove unused imports --- src/structures/TipccClient.ts | 6 ------ 1 file changed, 6 deletions(-) diff --git a/src/structures/TipccClient.ts b/src/structures/TipccClient.ts index 3218ed4..cdbdca9 100644 --- a/src/structures/TipccClient.ts +++ b/src/structures/TipccClient.ts @@ -5,12 +5,6 @@ import { ExchangeRateCache, FiatCache, } from './CurrencyCache'; -import { ExchangeRate } from './ExchangeRate'; - -import { - RESTGetAPICurrenciesRatesResult, - Routes, -} from '@tipccjs/tipcc-api-types/v0'; import { WalletManager } from './Manager/WalletManager'; import { TransactionManager } from './Manager/TransactionManager';