Skip to content

Commit

Permalink
Don't load onSnapshot from cache in hook listen for contract (could h…
Browse files Browse the repository at this point in the history
…elp 404 errors?)
  • Loading branch information
jahooma committed Jan 30, 2022
1 parent 967ac1b commit ac9af1a
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 4 deletions.
6 changes: 2 additions & 4 deletions web/lib/firebase/contracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
import _ from 'lodash'

import { app } from './init'
import { getValues, listenForValues } from './utils'
import { getValues, listenForValue, listenForValues } from './utils'
import { Contract } from '../../../common/contract'
import { getProbability } from '../../../common/calculate'
import { createRNG, shuffle } from '../../../common/util/random'
Expand Down Expand Up @@ -125,9 +125,7 @@ export function listenForContract(
setContract: (contract: Contract | null) => void
) {
const contractRef = doc(contractCollection, contractId)
return onSnapshot(contractRef, (contractSnap) => {
setContract((contractSnap.data() ?? null) as Contract | null)
})
return listenForValue<Contract>(contractRef, setContract)
}

function chooseRandomSubset(contracts: Contract[], count: number) {
Expand Down
2 changes: 2 additions & 0 deletions web/lib/firebase/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ export function listenForValue<T>(
setValue: (value: T | null) => void
) {
return onSnapshot(docRef, (snapshot) => {
if (snapshot.metadata.fromCache) return

const value = snapshot.exists() ? (snapshot.data() as T) : null
setValue(value)
})
Expand Down

0 comments on commit ac9af1a

Please sign in to comment.