Skip to content

Commit 2aec0c4

Browse files
committed
Use memoize for gcalc
1 parent 983c147 commit 2aec0c4

File tree

1 file changed

+6
-7
lines changed

1 file changed

+6
-7
lines changed

pages/tools/gachacalc.tsx

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import {
22
BarElement, CategoryScale, Chart as ChartJS, Filler, Legend, LinearScale, LineElement, PointElement, Tooltip
33
} from "chart.js"
44
import Head from "next/head"
5-
import { EffectCallback, useEffect, useState } from "react"
5+
import { EffectCallback, useEffect, useState, useMemo } from "react"
66
import { Bar, Line } from "react-chartjs-2"
77
import FormattedLink from "../../components/FormattedLink"
88
import Main from "../../components/Main"
@@ -92,12 +92,15 @@ export default function GachaCalc({ location }: { location: string }) {
9292
const [guaranteed, setGuaranteed] = useState(false)
9393
const [guaranteedPity, setGuaranteedPity] = useState(0)
9494

95-
const [calculated, setCalculated] = useState([[]] as ReducedSim[][])
96-
9795
const [gachaName, setGacha] = useState(Object.values(gachas).map(g => g.bannerName)[0])
9896

9997
const banner = Object.values(gachas).find(x => x.bannerName == gachaName) ?? Object.values(gachas)[0]
10098

99+
const calculated = useMemo(
100+
() => calcSimsRegular(current, pity, pulls, guaranteed, guaranteedPity, banner),
101+
[current, pity, pulls, guaranteed, guaranteedPity, banner]
102+
)
103+
101104
function delayed(f: EffectCallback) {
102105
const timeout = setTimeout(() => {
103106
f()
@@ -111,10 +114,6 @@ export default function GachaCalc({ location }: { location: string }) {
111114
useEffect(() => delayed(() => { if (current > banner.maxConst) setCurrent(banner.maxConst) }), [banner, current])
112115
useEffect(() => delayed(() => { if (current < banner.minConst) setCurrent(banner.minConst) }), [current, banner])
113116

114-
useEffect(
115-
() => setCalculated(calcSimsRegular(current, pity, pulls, guaranteed, guaranteedPity, banner)),
116-
[current, pity, pulls, guaranteed, guaranteedPity, banner]
117-
)
118117
const consts = []
119118
for (let i = current; i <= banner.maxConst; i++) consts.push(i)
120119

0 commit comments

Comments
 (0)