Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions client2/src/components/sidebar/game/d3-line-chart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ export const D3LineChart: React.FC<LineChartProps> = ({ data, width, height, mar
const svgRef = useRef<SVGSVGElement | null>(null)

useEffect(() => {
if (data.length === 0) return

// The topleft of this container is the origin of everything. Nothing
// can be drawn outside this container.
const svg = d3
Expand Down Expand Up @@ -86,6 +84,7 @@ export const D3LineChart: React.FC<LineChartProps> = ({ data, width, height, mar
const tooltip = svg.append('g')

function pointerMoved(event: MouseEvent) {
if (data.length === 0) return
if (
d3.pointer(event)[0] < margin.left ||
d3.pointer(event)[0] > width - margin.right ||
Expand Down
15 changes: 13 additions & 2 deletions client2/src/components/sidebar/game/resource-graph.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useContext, useState } from 'react'
import React, { useContext, useRef, useState } from 'react'
import { LineChart, Line, XAxis, YAxis, CartesianGrid, Tooltip, Legend, ResponsiveContainer } from 'recharts'
import { AppContext, useAppContext } from '../../../app-context'
import { useListenEvent, EventType } from '../../../app-events'
Expand Down Expand Up @@ -42,7 +42,18 @@ export const ResourceGraph: React.FC<Props> = (props: Props) => {
const appContext = useAppContext()
const forceUpdate = useForceUpdate()

useListenEvent(EventType.TURN_PROGRESS, forceUpdate)
const target_update_ms = 1000 / 15 // 15 fps
const delayedUpdate = useRef<NodeJS.Timeout | undefined>(undefined)
const lastUpdateTime = useRef<number>(0)
useListenEvent(EventType.TURN_PROGRESS, () => {
if (delayedUpdate.current !== undefined) clearTimeout(delayedUpdate.current)
if (Date.now() - lastUpdateTime.current < target_update_ms) {
delayedUpdate.current = setTimeout(forceUpdate, target_update_ms - (Date.now() - lastUpdateTime.current))
} else {
lastUpdateTime.current = Date.now()
forceUpdate()
}
})

return (
<div className="mt-2 px-2 w-full">
Expand Down