Skip to content

Commit

Permalink
Merge pull request #404 from poap-xyz/no-caching-remote-force
Browse files Browse the repository at this point in the history
No caching / remote force
  • Loading branch information
jm42 authored Nov 24, 2024
2 parents 72c70c6 + c5d3938 commit cdd5650
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 230 deletions.
6 changes: 1 addition & 5 deletions src/components/Status.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,22 @@ import 'styles/status.css'

function Status({
loading,
caching,
error,
}: {
loading?: boolean
caching?: boolean
error?: boolean
}) {
if (!loading && !caching && !error) {
if (!loading && !error) {
return null
}
return (
<div
className={clsx('status',
loading && 'status-loading',
caching && 'status-caching',
error && 'status-error',
)}
>
{loading && 'Loading'}
{caching && 'Caching'}
{error && 'Error'}
</div>
)
Expand Down
73 changes: 32 additions & 41 deletions src/hooks/useEventInCommon.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
import { useCallback, useEffect, useState } from 'react'
import { AbortedError } from 'models/error'
import { filterInCommon } from 'models/in-common'
import { Drop } from 'models/drop'
import { POAP } from 'models/poap'
import { Progress } from 'models/http'
import { InCommon } from 'models/api'
import { getInCommonEventsWithProgress, putEventInCommon } from 'loaders/api'
import { filterInCommon } from 'models/in-common'
import { getInCommonEventsWithProgress } from 'loaders/api'
import { scanAddress } from 'loaders/poap'

function useEventInCommon(eventId: number, owners: string[], force: boolean = false): {
function useEventInCommon(
eventId: number,
owners: string[],
force: boolean = false,
local: boolean = false,
): {
completedEventInCommon: boolean
loadingEventInCommon: boolean
loadedInCommonProgress: { progress: number; estimated: number | null; rate: number | null } | null
loadedOwners: number
ownersErrors: Array<{ address: string; error: Error }>
inCommon: InCommon
events: Record<number, Drop>
caching: boolean
cachingError: Error | null
cachedTs: number | null
fetchEventInCommon: () => () => void
retryAddress: (address: string) => void
Expand All @@ -29,8 +32,6 @@ function useEventInCommon(eventId: number, owners: string[], force: boolean = fa
const [errors, setErrors] = useState<Array<{ address: string; error: Error }>>([])
const [inCommon, setInCommon] = useState<InCommon>({})
const [events, setEvents] = useState<Record<number, Drop>>({})
const [caching, setCaching] = useState<boolean>(false)
const [cachingError, setCachingError] = useState<Error | null>(null)
const [cachedTs, setCachedTs] = useState<number | null>(null)

useEffect(
Expand All @@ -39,22 +40,7 @@ function useEventInCommon(eventId: number, owners: string[], force: boolean = fa
const inCommonProcessed = filterInCommon(inCommon)

if (Object.keys(inCommonProcessed).length > 0) {
setCaching(true)
setCachingError(null)
putEventInCommon(eventId, inCommonProcessed).then(
() => {
setCaching(false)
setCachedTs(Math.trunc(Date.now() / 1000))
},
(err) => {
console.error(err)
setCaching(false)
setCachingError(new Error(
'Could not cache drop',
{ cause: err }
))
}
)
setCachedTs(Math.trunc(Date.now() / 1000))
}
}
},
Expand All @@ -72,6 +58,21 @@ function useEventInCommon(eventId: number, owners: string[], force: boolean = fa
setErrors((prevErrors) => [...prevErrors, { address, error }])
}

function removeError(address: string): void {
setErrors((prevErrors) => {
if (prevErrors == null) {
return []
}
const newErrors = []
for (const { error, address: errorAddress } of prevErrors) {
if (errorAddress !== address) {
newErrors.push({ error, address: errorAddress })
}
}
return newErrors
})
}

const fetchAddressInCommon = useCallback(
async (address: string, abortSignal: AbortSignal) => {
let tokens: POAP[]
Expand Down Expand Up @@ -136,7 +137,7 @@ function useEventInCommon(eventId: number, owners: string[], force: boolean = fa
{}
)
setCompleted(false)
if (force) {
if (local) {
fetchOwnersInCommon(controllers).finally(() => {
setCompleted(true)
})
Expand All @@ -157,7 +158,8 @@ function useEventInCommon(eventId: number, owners: string[], force: boolean = fa
} else {
setLoadedProgress(null)
}
}
},
/*refresh*/force
).then(
(result) => {
setLoadedProgress(null)
Expand Down Expand Up @@ -193,24 +195,15 @@ function useEventInCommon(eventId: number, owners: string[], force: boolean = fa
setInCommon({})
}
},
[eventId, owners, force, fetchOwnersInCommon]
[eventId, owners, force, local, fetchOwnersInCommon]
)

function retryAddress(address: string): () => void {
setErrors((prevErrors) => {
if (prevErrors == null) {
return []
}
const newErrors = []
for (const { error, address: errorAddress } of prevErrors) {
if (errorAddress !== address) {
newErrors.push({ error, address: errorAddress })
}
}
return newErrors
})
removeError(address)
const controller = new AbortController()
fetchAddressInCommon(address, controller.signal)
fetchAddressInCommon(address, controller.signal).catch((err) => {
addError(address, err)
})
return () => {
controller.abort()
}
Expand All @@ -224,8 +217,6 @@ function useEventInCommon(eventId: number, owners: string[], force: boolean = fa
ownersErrors: errors,
inCommon,
events,
caching,
cachingError,
cachedTs,
fetchEventInCommon,
retryAddress,
Expand Down
96 changes: 12 additions & 84 deletions src/hooks/useEventsInCommon.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,19 @@
import { useCallback, useEffect, useState } from 'react'
import { getInCommonEventsWithProgress, putEventInCommon } from 'loaders/api'
import { getInCommonEventsWithProgress } from 'loaders/api'
import { scanAddress } from 'loaders/poap'
import { AbortedError } from 'models/error'
import { filterInCommon } from 'models/in-common'
import { POAP } from 'models/poap'
import { Drop } from 'models/drop'
import { Progress } from 'models/http'
import { InCommon } from 'models/api'

interface EventsInCommon {
events: Record<number, Drop>
inCommon: InCommon
ts: number | null
}
import { EventsInCommon, InCommon } from 'models/api'
import { filterInCommon } from 'models/in-common'

function useEventsInCommon(
eventIds: number[],
eventsOwners: InCommon,
all: boolean = false,
force: boolean = false,
local: boolean = false,
): {
completedEventsInCommon: boolean
completedInCommonEvents: Record<number, boolean>
Expand All @@ -27,8 +22,6 @@ function useEventsInCommon(
loadedEventsProgress: Record<number, Progress>
loadedEventsOwners: Record<number, number>
eventsInCommon: Record<number, EventsInCommon>
cachingEvents: Record<number, boolean>
cachingEventsErrors: Record<number, Error>
fetchEventsInCommon: () => () => void
retryEventAddressInCommon: (eventId: number, address: string) => () => void
} {
Expand All @@ -38,8 +31,6 @@ function useEventsInCommon(
const [loadedProgress, setLoadedProgress] = useState<Record<number, Progress>>({})
const [loadedOwners, setLoadedOwners] = useState<Record<number, number>>({})
const [inCommon, setInCommon] = useState<Record<number, EventsInCommon>>({})
const [caching, setCaching] = useState<Record<number, boolean>>({})
const [cachingErrors, setCachingErrors] = useState<Record<number, Error>>({})

useEffect(
() => {
Expand All @@ -48,36 +39,18 @@ function useEventsInCommon(
completed[eventId] &&
(loadedOwners[eventId] ?? 0) === eventsOwners[eventId].length &&
inCommon[eventId] != null &&
inCommon[eventId].ts == null &&
!caching[eventId]
inCommon[eventId].ts == null
) {
const inCommonProcessed = filterInCommon(
inCommon[eventId].inCommon
)
if (Object.keys(inCommonProcessed).length > 0) {
removeCachingError(eventId)
addCaching(eventId)
putEventInCommon(eventId, inCommonProcessed).then(
() => {
updateCachedTs(eventId)
removeCaching(eventId)
},
(err) => {
removeCaching(eventId)
if (!(err instanceof AbortedError)) {
console.error(err)
updateCachingError(
eventId,
new Error('Could not cache drop', { cause: err })
)
}
}
)
updateCachedTs(eventId)
}
}
}
},
[eventIds, eventsOwners, loadedOwners, completed, caching, inCommon]
[eventIds, eventsOwners, loadedOwners, completed, inCommon]
)

function addCompleted(eventId: number): void {
Expand Down Expand Up @@ -323,50 +296,6 @@ function useEventsInCommon(
}))
}

function addCaching(eventId: number): void {
setCaching((alsoCaching) => ({
...alsoCaching,
[eventId]: true,
}))
}

function removeCaching(eventId: number): void {
setCaching((alsoCaching) => {
if (alsoCaching == null) {
return {}
}
const newCaching: Record<number, boolean> = {}
for (const [cachingEventId, caching] of Object.entries(alsoCaching)) {
if (String(eventId) !== String(cachingEventId)) {
newCaching[cachingEventId] = caching
}
}
return newCaching
})
}

function updateCachingError(eventId: number, err: Error): void {
setCachingErrors((prevErrors) => ({
...prevErrors,
[eventId]: err,
}))
}

function removeCachingError(eventId: number): void {
setCachingErrors((alsoErrors) => {
if (alsoErrors == null) {
return {}
}
const newErrors: Record<number, Error> = {}
for (const [errorEventId, error] of Object.entries(alsoErrors)) {
if (String(eventId) !== String(errorEventId)) {
newErrors[errorEventId] = error
}
}
return newErrors
})
}

function updateCachedTs(eventId: number, ts?: number): void {
if (ts == null) {
ts = Math.trunc(Date.now() / 1000)
Expand Down Expand Up @@ -449,13 +378,13 @@ function useEventsInCommon(
controllers: Record<string, AbortController>,
controller: AbortController,
) => {
if (force) {
if (local) {
await processEvent(eventId, addresses, controllers)
} else {
removeCompleted(eventId)
addLoading(eventId)
addLoadedProgress(eventId)
let result
let result: EventsInCommon | null = null
try {
result = await getInCommonEventsWithProgress(
eventId,
Expand All @@ -466,7 +395,8 @@ function useEventsInCommon(
} else {
removeLoadedProgress(eventId)
}
}
},
/*refresh*/force
)
} catch (err: unknown) {
removeLoadedProgress(eventId)
Expand All @@ -491,7 +421,7 @@ function useEventsInCommon(
}
}
},
[force, processEvent]
[force, local, processEvent]
)

const fetchEventsInCommon = useCallback(
Expand Down Expand Up @@ -570,8 +500,6 @@ function useEventsInCommon(
loadedEventsProgress: loadedProgress,
loadedEventsOwners: loadedOwners,
eventsInCommon: inCommon,
cachingEvents: caching,
cachingEventsErrors: cachingErrors,
fetchEventsInCommon,
retryEventAddressInCommon,
}
Expand Down
Loading

0 comments on commit cdd5650

Please sign in to comment.