add option to not create fresh APIs for every read

This commit is contained in:
nitowa
2022-03-28 05:30:40 +02:00
parent 0291f7b85b
commit a9d3549d3e
3 changed files with 47 additions and 33 deletions
+9 -2
View File
@@ -14,6 +14,13 @@ export type PublicKey = string
export type Amount = number
export type TxHash = string
export type Options = {
debug: boolean,
connectionTimeout: number
debug?: boolean
connectionTimeout?: number
readFreshApi?: boolean
}
export const defaultOptions = {
debug: false,
connectionTimeout: 100000,
readFreshApi: true
}
+15 -6
View File
@@ -1,4 +1,4 @@
import { Memo, Options } from '../util/types'
import { defaultOptions, Memo, Options } from '../util/types'
import { Client, Payment, TxResponse, Wallet } from 'xrpl'
import * as zlib from 'zlib'
@@ -18,11 +18,13 @@ export class xrpIO {
constructor(
private server: string,
private options: Options = {
debug: false,
connectionTimeout: 100000
}
private options: Options = defaultOptions
) {
this.options.debug = this.options.debug ? Boolean(this.options.debug) : defaultOptions.debug
this.options.connectionTimeout = this.options.connectionTimeout ? Number(this.options.connectionTimeout) : defaultOptions.connectionTimeout
this.options.readFreshApi = this.options.readFreshApi ? Boolean(this.options.readFreshApi) : defaultOptions.readFreshApi
this.api = new Client(server, {
connectionTimeout: this.options.connectionTimeout
})
@@ -101,8 +103,9 @@ export class xrpIO {
private async getTransaction(hash: string): Promise<TxResponse> {
this.dbg("Getting Tx", hash)
let _api = await this.cloneApi()
if (this.options.readFreshApi) {
let _api = await this.cloneApi()
while (true) {
try {
const response = await _api.request({
@@ -117,6 +120,12 @@ export class xrpIO {
_api = await this.cloneApi()
}
}
}else{
return await this.api.request({
command: 'tx',
transaction: hash,
})
}
}
public async readRaw(hash: string): Promise<Memo> {
-2
View File
@@ -9,8 +9,6 @@ let sendWallet: Wallet
let receiveWallet: Wallet
let api: xrpIO
const ONLY_LARGE_TESTS = true
describe('XRPIO', () => {
before(async function(){
this.timeout(15000)