From 775abfb8fdd01f2c616046a8b3576d85177313ae Mon Sep 17 00:00:00 2001 From: Thibault Duplessis Date: Tue, 10 Dec 2024 10:00:10 +0100 Subject: [PATCH] use ui/common memoize --- ui/lobby/src/view/realTime/chart.ts | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/ui/lobby/src/view/realTime/chart.ts b/ui/lobby/src/view/realTime/chart.ts index 1858d0f11e1a4..50a963df151d8 100644 --- a/ui/lobby/src/view/realTime/chart.ts +++ b/ui/lobby/src/view/realTime/chart.ts @@ -4,6 +4,7 @@ import { bind } from 'common/snabbdom'; import { h, type VNode } from 'snabbdom'; import type { Hook } from '../../interfaces'; import perfIcons from 'common/perfIcons'; +import { memoize } from 'common'; const percents = (v: number) => v + '%'; @@ -30,21 +31,15 @@ const clockX = (dur: number) => { return Math.round((durLog(Math.min(clockMax, dur || clockMax)) / durLog(clockMax)) * 100); }; -let iconTranslateAmts: [number, number] | null = null; - -function getIconTranslateAmts(): [number, number] { - if (iconTranslateAmts) { - return iconTranslateAmts; - } +const iconTranslateAmts: () => [number, number] = memoize<[number, number]>(() => { const chart = document.querySelector('.hooks__chart') as HTMLElement; const fontSize = parseFloat(window.getComputedStyle(chart).fontSize); - iconTranslateAmts = [(fontSize / chart.clientWidth) * 95, (fontSize / chart.clientHeight) * 75]; - return iconTranslateAmts; -} + return [(fontSize / chart.clientWidth) * 95, (fontSize / chart.clientHeight) * 75]; +}); function renderPlot(ctrl: LobbyController, hook: Hook) { - const bottom = Math.max(0, ratingY(hook.rating) - getIconTranslateAmts()[1]), - left = Math.max(0, clockX(hook.t) - getIconTranslateAmts()[0]), + const bottom = Math.max(0, ratingY(hook.rating) - iconTranslateAmts()[1]), + left = Math.max(0, clockX(hook.t) - iconTranslateAmts()[0]), klass = [ hook.id, 'plot.new',