Skip to content

Commit

Permalink
Improve performance on election map
Browse files Browse the repository at this point in the history
  • Loading branch information
dtinth committed Mar 22, 2019
1 parent edac5a5 commit 39516a0
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 27 deletions.
5 changes: 3 additions & 2 deletions src/components/ElectionMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
import { SvgChart, helper } from "d3kit"
import { createComponent } from "react-d3kit"
import { parties } from "../models/information"
import { memo } from "react"
const maps = require("../models/information/_map.json")

const partyLookup = keyBy(parties, p => p.id)
Expand Down Expand Up @@ -293,6 +294,6 @@ class ElectionMap extends SvgChart {
}
}

export default /** @type {React.FunctionComponent<Props>} */ (createComponent(
ElectionMap
export default /** @type {React.FunctionComponent<Props>} */ (memo(
createComponent(ElectionMap)
))
40 changes: 20 additions & 20 deletions src/components/ElectionMapContainer.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import ElectionMap from "./ElectionMap"
import { useState, useEffect } from "react"
import { useState, useEffect, useMemo, useCallback } from "react"
import { zones, parties } from "../models/information"
import React from "react"
import _ from "lodash"
Expand Down Expand Up @@ -53,8 +53,21 @@ function getMapData(summaryState) {
export default function ElectionMapContainer() {
const summaryState = useSummaryData()
const [mapTip, setMapTip] = useState(null)
const mapZones = getMapData(summaryState)
const mapZones = useMemo(() => getMapData(summaryState), [summaryState])

const onInit = useCallback(map => {}, [])
const onZoneMouseenter = useCallback((zone, mouseEvent) => {
setMapTip({ zone, mouseEvent })
}, [])
const onZoneMousemove = useCallback((zone, mouseEvent) => {
setMapTip({ zone, mouseEvent })
}, [])
const onZoneMouseleave = useCallback((zone, mouseEvent) => {
setMapTip(null)
}, [])
const onZoneClick = useCallback(zone => {
console.log(zone)
}, [])
return (
<div>
{mapTip && (
Expand All @@ -79,24 +92,11 @@ export default function ElectionMapContainer() {
)}
<ElectionMap
data={mapZones}
onInit={map => {
// console.log('map', map);
}}
onZoneMouseenter={(zone, mouseEvent) => {
setMapTip({ zone, mouseEvent })
// console.log('zone', zone);
}}
onZoneMousemove={(zone, mouseEvent) => {
setMapTip({ zone, mouseEvent })
// console.log('zone', zone);
}}
onZoneMouseleave={(zone, mouseEvent) => {
setMapTip(null)
// console.log('zone', zone);
}}
onZoneClick={zone => {
// console.log('zoneClick', zone)
}}
onInit={onInit}
onZoneMouseenter={onZoneMouseenter}
onZoneMousemove={onZoneMousemove}
onZoneMouseleave={onZoneMouseleave}
onZoneClick={onZoneClick}
/>
</div>
)
Expand Down
16 changes: 11 additions & 5 deletions src/models/LiveDataSubscription.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,12 +138,18 @@ function getLatestDataFileState(fileName) {
function useInertState(state) {
const ref = useRef(state)
const combine = (previous, current) => {
return {
...previous,
...current,
completed: current.completed || previous.completed,
data: current.data || previous.data,
if (
(!current.completed && previous.completed) ||
(!current.data && previous.data)
) {
return {
...previous,
...current,
completed: current.completed || previous.completed,
data: current.data || previous.data,
}
}
return current
}
const result = combine(ref.current, state)
useEffect(() => {
Expand Down

2 comments on commit 39516a0

@0pdd
Copy link

@0pdd 0pdd commented on 39516a0 Mar 22, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle 1-2a60074f disappeared from src/models/datasource.d.ts, that's why I closed #23. Please, remember that the puzzle was not necessarily removed in this particular commit. Maybe it happened earlier, but we discovered this fact only now.

@0pdd
Copy link

@0pdd 0pdd commented on 39516a0 Mar 22, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle 30-25ad3860 disappeared from src/components/ZoneMasterView.js, that's why I closed #71. Please, remember that the puzzle was not necessarily removed in this particular commit. Maybe it happened earlier, but we discovered this fact only now.

Please sign in to comment.