Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions src/structures/TipccClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ export class TipccClient extends EventEmitter {
/** The number of milliseconds between each API poll */
public pollingInterval = 10000;

/** The number of milliseconds to wait before retrying an API poll */
public retryInterval = 2000;

/** The max number of retries to poll the API, after which an error will be thrown */
public maxRetries = 5;

Expand All @@ -60,13 +63,15 @@ export class TipccClient extends EventEmitter {
* @param options The options to use
* @param options.baseUrl The base URL for requests
* @param options.pollingInterval The number of milliseconds between each API poll. Defaults to `10000`.
* @param options.retryInterval The number of milliseconds to wait before retrying an API poll. Defaults to `2000`.
* @param options.maxRetries The max number of retries to poll the API, after which an error will be thrown. Defaults to `5`.
*/
constructor(
token: string,
options: {
baseUrl?: string;
pollingInterval?: number;
retryInterval?: number;
maxRetries?: number;
} = {},
) {
Expand All @@ -75,12 +80,6 @@ export class TipccClient extends EventEmitter {
if (!/(^[A-Za-z0-9-_]*\.[A-Za-z0-9-_]*\.[A-Za-z0-9-_]*$)/.test(token))
throw new Error('Invalid token provided');

this.wallets = new WalletManager({
client: this,
});
this.transactions = new TransactionManager({
client: this,
});
this.cryptos = new CryptocurrencyCache(this);
this.fiats = new FiatCache(this);
this.exchangeRates = new ExchangeRateCache(this);
Expand All @@ -91,8 +90,19 @@ export class TipccClient extends EventEmitter {
});

if (options.pollingInterval) this.pollingInterval = options.pollingInterval;
if (options.retryInterval) this.retryInterval = options.retryInterval;
if (options.maxRetries) this.maxRetries = options.maxRetries;

this.wallets = new WalletManager({
client: this,
});
this.transactions = new TransactionManager({
client: this,
pollingInterval: this.pollingInterval,
maxPollingRetries: this.maxRetries,
retryInterval: this.retryInterval,
});

this._init();
}

Expand Down
15 changes: 14 additions & 1 deletion src/structures/managers/TransactionManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ export class TransactionManager extends EventEmitter {

private _pollingInterval = 5000;

private _retryInterval = 2000;

private _lastPoll: Date = new Date();

private _pollingTimeout: NodeJS.Timeout | null = null;
Expand All @@ -61,14 +63,22 @@ export class TransactionManager extends EventEmitter {
cacheTtl?: number;
pollingInterval?: number;
maxPollingRetries?: number;
retryInterval?: number;
}) {
const { client, cacheTtl, pollingInterval, maxPollingRetries } = payload;
const {
client,
cacheTtl,
pollingInterval,
maxPollingRetries,
retryInterval,
} = payload;
super();
this.client = client;
this.cache = new Cache(cacheTtl);

if (pollingInterval) this._pollingInterval = pollingInterval;
if (maxPollingRetries) this._maxPollingRetries = maxPollingRetries;
if (retryInterval) this._retryInterval = retryInterval;
}

public async fetch(id: string, cache = true): Promise<Transaction | null> {
Expand Down Expand Up @@ -150,6 +160,9 @@ export class TransactionManager extends EventEmitter {
transactions.set(tx.id, new Transaction(tx, this.client));
} catch (e) {
this._pollingRetries++;
await new Promise((resolve) =>
setTimeout(resolve, this._retryInterval),
);

if (this._pollingRetries >= this._maxPollingRetries)
throw new Error(
Expand Down