Skip to content

Commit

Permalink
resolve single point of failure issue
Browse files Browse the repository at this point in the history
  • Loading branch information
Aliiiu committed Sep 19, 2024
1 parent 1931435 commit 40ca489
Showing 1 changed file with 35 additions and 11 deletions.
46 changes: 35 additions & 11 deletions src/utils/use-stats-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,53 @@ const getMappedValue = (object: Dict) => {
return val
}

function timeoutPromise(promise: Promise<any>, ms: number): Promise<any> {
let timeoutId: NodeJS.Timeout
const timeoutPromise = new Promise((_, reject) => {
timeoutId = setTimeout(() => {
reject(new Error(`Promise timed out after ${ms} ms`))
}, ms)
})

return Promise.race([promise, timeoutPromise]).finally(() =>
clearTimeout(timeoutId),
)
}

export function useStatsData() {
const [data, setData] = useState<Dict>({})
const [totals, setTotals] = useState<Dict>({})
const isFetched = useRef(false)

useEffect(() => {
const fetchData = async () => {
const [holders, volume, iq, lp, social, ep] = await Promise.all([
getTokenHolders(),
getVolume(),
getIQ(),
getLPs(),
getSocialData(),
getEpData(),
const TIMEOUT = 10000

const results = await Promise.allSettled([
timeoutPromise(getTokenHolders(), TIMEOUT),
timeoutPromise(getVolume(), TIMEOUT),
timeoutPromise(getIQ(), TIMEOUT),
timeoutPromise(getLPs(), TIMEOUT),
timeoutPromise(getSocialData(), TIMEOUT),
timeoutPromise(getEpData(), TIMEOUT),
])

const newData = { ...holders, ...volume, ...iq, ...lp, ...social, ...ep }
const newData: Dict = {}
results.forEach((result, index) => {
if (result.status === 'fulfilled') {
Object.assign(newData, result.value)
} else {
console.error(
`Failed to fetch data for index ${index}:`,
result.reason,
)
}
})

setData(newData)

setTotals({
holders: getMappedValue(holders.holders),
volume: getMappedValue(volume.volume),
holders: getMappedValue(newData.holders),
volume: getMappedValue(newData.volume),
})
}
if (!isFetched.current) {
Expand Down

0 comments on commit 40ca489

Please sign in to comment.