Only warm up create user function once

This commit is contained in:
Marshall Polaris 2022-05-04 14:13:51 -07:00
parent 9eb7335cf6
commit 3bc885cb53

View File

@ -77,11 +77,13 @@ const CACHED_USER_KEY = 'CACHED_USER_KEY'
// used to avoid weird race condition // used to avoid weird race condition
let createUserPromise: Promise<User | null> | undefined = undefined let createUserPromise: Promise<User | null> | undefined = undefined
const warmUpCreateUser = _.throttle(createUser, 5000 /* ms */)
export function listenForLogin(onUser: (user: User | null) => void) { export function listenForLogin(onUser: (user: User | null) => void) {
const cachedUser = localStorage.getItem(CACHED_USER_KEY) const cachedUser = localStorage.getItem(CACHED_USER_KEY)
onUser(cachedUser ? JSON.parse(cachedUser) : null) onUser(cachedUser ? JSON.parse(cachedUser) : null)
if (!cachedUser) createUser() // warm up cloud function if (!cachedUser) warmUpCreateUser()
return onAuthStateChanged(auth, async (fbUser) => { return onAuthStateChanged(auth, async (fbUser) => {
if (fbUser) { if (fbUser) {