Skip to content

Commit

Permalink
feat(dataloader): prepare for policy verification
Browse files Browse the repository at this point in the history
  • Loading branch information
epiqueras committed Jan 11, 2019
1 parent 9b4bb4e commit 9b717a9
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 45 deletions.
54 changes: 26 additions & 28 deletions src/bootstrap/dataloader.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,48 +2,46 @@ import { useRef, useState } from 'react'
import Dataloader from 'dataloader'
import archon from './archon'

export const dataloader = new Dataloader(URLs =>
Promise.all(URLs.map(URL => fetch(URL).then(res => res.json())))
)

export const useDataloader = () => {
const [state, setState] = useState({})
const loadedRef = useRef({})
return URL =>
loadedRef.current[URL]
? state[URL]
: dataloader.load(URL).then(json => {
loadedRef.current[URL] = true
setState(state => ({ ...state, [URL]: json }))
}) && undefined
const funcs = {
getEvidence: (contractAddress, arbitratorAddress, disputeID, options) =>
archon.arbitrable
.getEvidence(contractAddress, arbitratorAddress, disputeID, {
strictHashes: true,
...options
})
.catch(() => null),
getMetaEvidence: (contractAddress, arbitratorAddress, disputeID, options) =>
archon.arbitrable
.getDispute(contractAddress, arbitratorAddress, disputeID, {
strictHashes: true,
...options
})
.then(d =>
archon.arbitrable.getMetaEvidence(contractAddress, d.metaEvidenceID, {
strictHashes: true,
...options
})
)
.catch(() => null),
load: URI => fetch(URI).then(res => res.json())
}

export const archonArbitrableDataloaders = [
'getDispute',
'getMetaEvidence',
'getEvidence'
].reduce((acc, f) => {
export const dataloaders = Object.keys(funcs).reduce((acc, f) => {
acc[f] = new Dataloader(
argsArr =>
Promise.all(
argsArr.map(args => archon.arbitrable[f](...args).catch(() => null))
),
argsArr => Promise.all(argsArr.map(args => funcs[f](...args))),
{ cacheKeyFn: JSON.stringify }
)
return acc
}, {})

export const useArchonArbitrableDataloader = Object.keys(
archonArbitrableDataloaders
).reduce((acc, f) => {
export const useDataloader = Object.keys(dataloaders).reduce((acc, f) => {
acc[f] = () => {
const [state, setState] = useState({})
const loadedRef = useRef({})
return (...args) => {
const key = JSON.stringify(args)
return loadedRef.current[key]
? state[key]
: archonArbitrableDataloaders[f].load(args).then(res => {
: dataloaders[f].load(args).then(res => {
loadedRef.current[key] = true
setState(state => ({ ...state, [key]: res }))
}) && undefined
Expand Down
16 changes: 4 additions & 12 deletions src/components/case-card.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { ReactComponent as Gavel } from '../assets/images/gavel.svg'
import PropTypes from 'prop-types'
import TimeAgo from '../components/time-ago'
import styled from 'styled-components/macro'
import { useArchonArbitrableDataloader } from '../bootstrap/dataloader'
import { useDataloader } from '../bootstrap/dataloader'

const StyledCard = styled(Card)`
margin: 10px 0;
Expand Down Expand Up @@ -83,8 +83,7 @@ const CaseCard = ({ ID }) => {
const drizzleState = useDrizzleState(drizzleState => ({
account: drizzleState.accounts[0]
}))
const getDispute = useArchonArbitrableDataloader.getDispute()
const getMetaEvidence = useArchonArbitrableDataloader.getMetaEvidence()
const getMetaEvidence = useDataloader.getMetaEvidence()
const dispute = useCacheCall('KlerosLiquid', 'disputes', ID)
const dispute2 = useCacheCall('KlerosLiquid', 'getDispute', ID)
const draws = useCacheEvents(
Expand Down Expand Up @@ -143,19 +142,12 @@ const CaseCard = ({ ID }) => {
return disputeData
})
let metaEvidence
if (dispute) {
const dispute2 = getDispute(
if (dispute)
metaEvidence = getMetaEvidence(
dispute.arbitrated,
drizzle.contracts.KlerosLiquid.address,
ID
)
if (dispute2)
metaEvidence = getMetaEvidence(
dispute.arbitrated,
dispute2.metaEvidenceID
)
else if (dispute2 === null) metaEvidence = null
}
return (
<StyledCard
actions={useMemo(
Expand Down
2 changes: 1 addition & 1 deletion src/components/court-card.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ const CourtCard = ({ ID, onClick, onStakeClick: _onStakeClick }) => {
const drizzleState = useDrizzleState(drizzleState => ({
account: drizzleState.accounts[0]
}))
const load = useDataloader()
const load = useDataloader.load()
let name
const policy = useCacheCall('PolicyRegistry', 'policies', ID)
if (policy) {
Expand Down
2 changes: 1 addition & 1 deletion src/components/court-cascader-modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ const StyledBreadcrumbs = styled(Breadcrumbs)`
`
const CourtCascaderModal = ({ onClick }) => {
const { useCacheCall } = useDrizzle()
const load = useDataloader()
const load = useDataloader.load()
const [subcourtIDs, setSubcourtIDs] = useState(['0'])
const options = useCacheCall(['PolicyRegistry', 'KlerosLiquid'], call => {
const options = [
Expand Down
2 changes: 1 addition & 1 deletion src/components/court-drawer.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ const StyledDiv = styled.div`
`
const CourtDrawer = ({ ID, onClose }) => {
const { useCacheCall } = useDrizzle()
const load = useDataloader()
const load = useDataloader.load()
const [activeIndex, setActiveIndex] = useState()
const subcourts = useCacheCall(['PolicyRegistry', 'KlerosLiquid'], call => {
const subcourts = []
Expand Down
2 changes: 1 addition & 1 deletion src/components/courts-list-card.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const CourtsListCard = () => {
const drizzleState = useDrizzleState(drizzleState => ({
account: drizzleState.accounts[0]
}))
const load = useDataloader()
const load = useDataloader.load()
const subcourtIDs = useCacheCall(
'KlerosLiquid',
'getJuror',
Expand Down
2 changes: 1 addition & 1 deletion src/components/pnk-stats-list-card.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const PNKStatsListCard = () => {
const drizzleState = useDrizzleState(drizzleState => ({
account: drizzleState.accounts[0]
}))
const load = useDataloader()
const load = useDataloader.load()
const subcourtIDs = useCacheCall(
'KlerosLiquid',
'getJuror',
Expand Down

0 comments on commit 9b717a9

Please sign in to comment.