-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(tangle-dapp): Fetch all NFTs at once
- Loading branch information
1 parent
bfe3cd1
commit 84a52ce
Showing
14 changed files
with
158 additions
and
120 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,46 @@ | ||
import { useEffect, useRef, useState } from 'react'; | ||
|
||
export enum PollingPrimaryCacheKey { | ||
EXCHANGE_RATE, | ||
CONTRACT_READ_SUBSCRIPTION, | ||
LS_ERC20_BALANCE, | ||
} | ||
import { useCallback, useEffect, useState } from 'react'; | ||
|
||
export type PollingOptions<T> = { | ||
fetcher: (() => Promise<T> | T) | null; | ||
effect: (() => Promise<T> | T) | null; | ||
refreshInterval?: number; | ||
}; | ||
|
||
const usePolling = <T>({ | ||
fetcher, | ||
// Default to a 6 second refresh interval. | ||
refreshInterval = 6_000, | ||
effect, | ||
// Default to a 12 second refresh interval. | ||
refreshInterval = 12_000, | ||
}: PollingOptions<T>) => { | ||
const [value, setValue] = useState<T | null>(null); | ||
const [isRefreshing, setIsRefreshing] = useState(false); | ||
const lastUpdatedTimestampRef = useRef(Date.now()); | ||
|
||
useEffect(() => { | ||
const intervalHandle = setInterval(async () => { | ||
// Fetcher isn't ready to be called yet. | ||
if (fetcher === null) { | ||
return; | ||
} | ||
const refresh = useCallback(async () => { | ||
// Fetcher isn't ready to be called yet. | ||
if (effect === null) { | ||
return; | ||
} | ||
|
||
const now = Date.now(); | ||
const difference = now - lastUpdatedTimestampRef.current; | ||
setIsRefreshing(true); | ||
await effect(); | ||
setIsRefreshing(false); | ||
}, [effect]); | ||
|
||
// Don't refresh if the last refresh was less than | ||
// the refresh interval ago. This prevents issues where | ||
// the fetcher is unstable and the setInterval gets called | ||
// multiple times before the fetcher resolves. | ||
if (difference < refreshInterval) { | ||
return; | ||
} | ||
useEffect(() => { | ||
let intervalHandle: ReturnType<typeof setInterval> | null = null; | ||
|
||
(async () => { | ||
// Call it immediately to avoid initial delay. | ||
await refresh(); | ||
|
||
setIsRefreshing(true); | ||
setValue(await fetcher()); | ||
lastUpdatedTimestampRef.current = now; | ||
setIsRefreshing(false); | ||
}, refreshInterval); | ||
intervalHandle = setInterval(refresh, refreshInterval); | ||
})(); | ||
|
||
return () => { | ||
clearInterval(intervalHandle); | ||
if (intervalHandle !== null) { | ||
clearInterval(intervalHandle); | ||
} | ||
}; | ||
}, [fetcher, refreshInterval]); | ||
}, [effect, refresh, refreshInterval]); | ||
|
||
return { value, isRefreshing }; | ||
return isRefreshing; | ||
}; | ||
|
||
export default usePolling; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.