From 2e214cab7ae79c237a84bb5c423c92d777a3eefb Mon Sep 17 00:00:00 2001 From: Marshall Polaris Date: Thu, 5 May 2022 09:51:09 -0700 Subject: [PATCH] Kill warmup spam (#133) * Only warm up sell bet function once * Only warm up create user function once --- web/components/bets-list.tsx | 5 +++-- web/lib/firebase/users.ts | 4 +++- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/web/components/bets-list.tsx b/web/components/bets-list.tsx index 5d595cf7..9bad3627 100644 --- a/web/components/bets-list.tsx +++ b/web/components/bets-list.tsx @@ -553,10 +553,11 @@ function BetRow(props: { bet: Bet; contract: Contract; saleBet?: Bet }) { ) } +const warmUpSellBet = _.throttle(() => sellBet({}).catch(), 5000 /* ms */) + function SellButton(props: { contract: Contract; bet: Bet }) { useEffect(() => { - // warm up cloud function - sellBet({}).catch() + warmUpSellBet() }, []) const { contract, bet } = props diff --git a/web/lib/firebase/users.ts b/web/lib/firebase/users.ts index e07d138c..8b8523c6 100644 --- a/web/lib/firebase/users.ts +++ b/web/lib/firebase/users.ts @@ -77,11 +77,13 @@ const CACHED_USER_KEY = 'CACHED_USER_KEY' // used to avoid weird race condition let createUserPromise: Promise | undefined = undefined +const warmUpCreateUser = _.throttle(createUser, 5000 /* ms */) + export function listenForLogin(onUser: (user: User | null) => void) { const cachedUser = localStorage.getItem(CACHED_USER_KEY) onUser(cachedUser ? JSON.parse(cachedUser) : null) - if (!cachedUser) createUser() // warm up cloud function + if (!cachedUser) warmUpCreateUser() return onAuthStateChanged(auth, async (fbUser) => { if (fbUser) {