From 1a5dcdedccce44c46b38318023e41466c0bf1a8b Mon Sep 17 00:00:00 2001 From: James Grugett Date: Fri, 23 Sep 2022 16:30:44 -0400 Subject: [PATCH] Delay prefetch by 1000ms. Don't prefetch portfolio history. --- common/util/time.ts | 3 +++ web/hooks/use-contracts.ts | 4 ++-- web/hooks/use-portfolio-history.ts | 4 ++-- web/hooks/use-prefetch.ts | 2 -- web/hooks/use-user-bets.ts | 4 ++-- 5 files changed, 9 insertions(+), 8 deletions(-) diff --git a/common/util/time.ts b/common/util/time.ts index 9afb8db4..81dc3600 100644 --- a/common/util/time.ts +++ b/common/util/time.ts @@ -1,3 +1,6 @@ export const MINUTE_MS = 60 * 1000 export const HOUR_MS = 60 * MINUTE_MS export const DAY_MS = 24 * HOUR_MS + +export const sleep = (ms: number) => + new Promise((resolve) => setTimeout(resolve, ms)) diff --git a/web/hooks/use-contracts.ts b/web/hooks/use-contracts.ts index 053e2386..11aae65c 100644 --- a/web/hooks/use-contracts.ts +++ b/web/hooks/use-contracts.ts @@ -11,7 +11,7 @@ import { trendingContractsQuery, } from 'web/lib/firebase/contracts' import { QueryClient, useQuery, useQueryClient } from 'react-query' -import { MINUTE_MS } from 'common/util/time' +import { MINUTE_MS, sleep } from 'common/util/time' import { query, limit } from 'firebase/firestore' import { dailyScoreIndex } from 'web/lib/service/algolia' import { CPMMBinaryContract } from 'common/contract' @@ -86,7 +86,7 @@ export const usePrefetchUserBetContracts = (userId: string) => { const queryClient = useQueryClient() return queryClient.prefetchQuery( ['contracts', 'bets', userId], - () => getUserBetContracts(userId), + () => sleep(1000).then(() => getUserBetContracts(userId)), { staleTime: 5 * MINUTE_MS } ) } diff --git a/web/hooks/use-portfolio-history.ts b/web/hooks/use-portfolio-history.ts index 6cc1a84e..1ae5e7ee 100644 --- a/web/hooks/use-portfolio-history.ts +++ b/web/hooks/use-portfolio-history.ts @@ -1,6 +1,6 @@ import { useQueryClient } from 'react-query' import { useFirestoreQueryData } from '@react-query-firebase/firestore' -import { DAY_MS, HOUR_MS, MINUTE_MS } from 'common/util/time' +import { DAY_MS, HOUR_MS, MINUTE_MS, sleep } from 'common/util/time' import { getPortfolioHistory, getPortfolioHistoryQuery, @@ -17,7 +17,7 @@ export const usePrefetchPortfolioHistory = (userId: string, period: Period) => { const cutoff = getCutoff(period) return queryClient.prefetchQuery( ['portfolio-history', userId, cutoff], - () => getPortfolioHistory(userId, cutoff), + () => sleep(1000).then(() => getPortfolioHistory(userId, cutoff)), { staleTime: 15 * MINUTE_MS } ) } diff --git a/web/hooks/use-prefetch.ts b/web/hooks/use-prefetch.ts index 46d78b3c..0e83613b 100644 --- a/web/hooks/use-prefetch.ts +++ b/web/hooks/use-prefetch.ts @@ -1,5 +1,4 @@ import { usePrefetchUserBetContracts } from './use-contracts' -import { usePrefetchPortfolioHistory } from './use-portfolio-history' import { usePrefetchUserBets } from './use-user-bets' export function usePrefetch(userId: string | undefined) { @@ -7,6 +6,5 @@ export function usePrefetch(userId: string | undefined) { return Promise.all([ usePrefetchUserBets(maybeUserId), usePrefetchUserBetContracts(maybeUserId), - usePrefetchPortfolioHistory(maybeUserId, 'weekly'), ]) } diff --git a/web/hooks/use-user-bets.ts b/web/hooks/use-user-bets.ts index 3731fb07..c28b453d 100644 --- a/web/hooks/use-user-bets.ts +++ b/web/hooks/use-user-bets.ts @@ -7,13 +7,13 @@ import { getUserBetsQuery, listenForUserContractBets, } from 'web/lib/firebase/bets' -import { MINUTE_MS } from 'common/util/time' +import { MINUTE_MS, sleep } from 'common/util/time' export const usePrefetchUserBets = (userId: string) => { const queryClient = useQueryClient() return queryClient.prefetchQuery( ['bets', userId], - () => getUserBets(userId), + () => sleep(1000).then(() => getUserBets(userId)), { staleTime: MINUTE_MS } ) }