Skip to content

Commit

Permalink
Improve code
Browse files Browse the repository at this point in the history
  • Loading branch information
selankon committed Jul 16, 2024
1 parent ea28e85 commit 6160105
Showing 1 changed file with 13 additions and 22 deletions.
35 changes: 13 additions & 22 deletions packages/extended-sdk/src/client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,37 +38,28 @@ export class ExtendedSDKClient extends VocdoniSDKClient {
})
blockTransactions = (height: number, page?: number) => ChainAPI.blockTransactions(this.url, height, page)

// blockByHeight = async (height: number): Promise<IChainBlockInfoResponse | BlockNotFoundError> => {
// try {
// return await ChainAPI.blockByHeight(this.url, height)
// } catch (error) {
// if (error instanceof ErrAPI && error.raw?.response?.status === 404) {
// return { height } as BlockNotFoundError
// }
// throw error // re-throw other errors
// }
// }
blockByHeight = (height: number) => ChainAPI.blockByHeight(this.url, height)

blockList = (from: number, listSize: number = 10): Promise<Array<IChainBlockInfoResponse | BlockNotFoundError>> => {
const promises: Promise<IChainBlockInfoResponse | BlockNotFoundError>[] = []
// If is not a number bigger than 0
if (isNaN(from)) return Promise.all(promises)
for (let i = 0; i < listSize; i++) {
if (from + i > 0)
promises.push(
(async () => {
try {
return await this.blockByHeight(from + i)
} catch (error) {
if (error instanceof ErrAPI && error.raw?.response?.status === 404) {
return { height: from + i } as BlockNotFoundError
}
throw error // re-throw other errors
if (from + i <= 0) continue
promises.push(
(async () => {
try {
return await this.blockByHeight(from + i)
} catch (error) {
if (error instanceof ErrAPI && error.raw?.response?.status === 404) {
return { height: from + i } as BlockNotFoundError
}
})()
)
throw error // re-throw other errors
}
})()
)
}

return Promise.all(promises).then((blockInfo) => {
// flatten the array[][] into array[]
// @ts-ignore
Expand Down

0 comments on commit 6160105

Please sign in to comment.