Skip to content

Commit 1bf03e8

Browse files
committed
refactor: use new util function
1 parent 02e92d0 commit 1bf03e8

File tree

2 files changed

+23
-27
lines changed

2 files changed

+23
-27
lines changed

frontend/src/ts/test/focus.ts

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import * as LiveBurst from "./live-burst";
44
import * as LiveAcc from "./live-acc";
55
import * as TimerProgress from "./timer-progress";
66
import * as PageTransition from "../states/page-transition";
7+
import { requestDebouncedAnimationFrame } from "../utils/debounced-animation-frame";
78

89
const unfocusPx = 3;
910
let state = false;
@@ -41,13 +42,13 @@ function initializeCache(): void {
4142
// with cursor is a special case that is only used on the initial page load
4243
// to avoid the cursor being invisible and confusing the user
4344
export function set(value: boolean, withCursor = false): void {
44-
initializeCache();
45+
requestDebouncedAnimationFrame("focus.set", () => {
46+
initializeCache();
4547

46-
if (value && !state) {
47-
state = true;
48+
if (value && !state) {
49+
state = true;
4850

49-
// batch DOM operations for better performance
50-
requestAnimationFrame(() => {
51+
// batch DOM operations for better performance
5152
if (cache.focus) {
5253
for (const el of cache.focus) {
5354
el.classList.add("focus");
@@ -58,17 +59,15 @@ export function set(value: boolean, withCursor = false): void {
5859
el.style.cursor = "none";
5960
}
6061
}
61-
});
6262

63-
Caret.stopAnimation();
64-
LiveSpeed.show();
65-
LiveBurst.show();
66-
LiveAcc.show();
67-
TimerProgress.show();
68-
} else if (!value && state) {
69-
state = false;
63+
Caret.stopAnimation();
64+
LiveSpeed.show();
65+
LiveBurst.show();
66+
LiveAcc.show();
67+
TimerProgress.show();
68+
} else if (!value && state) {
69+
state = false;
7070

71-
requestAnimationFrame(() => {
7271
if (cache.focus) {
7372
for (const el of cache.focus) {
7473
el.classList.remove("focus");
@@ -79,14 +78,14 @@ export function set(value: boolean, withCursor = false): void {
7978
el.style.cursor = "";
8079
}
8180
}
82-
});
8381

84-
Caret.startAnimation();
85-
LiveSpeed.hide();
86-
LiveBurst.hide();
87-
LiveAcc.hide();
88-
TimerProgress.hide();
89-
}
82+
Caret.startAnimation();
83+
LiveSpeed.hide();
84+
LiveBurst.hide();
85+
LiveAcc.hide();
86+
TimerProgress.hide();
87+
}
88+
});
9089
}
9190

9291
$(document).on("mousemove", function (event) {

frontend/src/ts/test/test-ui.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import { TimerColor, TimerOpacity } from "@monkeytype/schemas/configs";
2323
import { convertRemToPixels } from "../utils/numbers";
2424
import { findSingleActiveFunboxWithFunction } from "./funbox/list";
2525
import * as TestState from "./test-state";
26+
import { requestDebouncedAnimationFrame } from "../utils/debounced-animation-frame";
2627

2728
const debouncedZipfCheck = debounce(250, async () => {
2829
const supports = await JSONData.checkIfLanguageSupportsZipf(Config.language);
@@ -491,13 +492,9 @@ export function appendEmptyWordElement(
491492
`<div class='word' data-wordindex='${index}'><letter class='invisible'>_</letter></div>`
492493
);
493494
}
494-
let updateWordsInputPositionAnimationFrameId: null | number = null;
495+
495496
export function updateWordsInputPosition(): void {
496-
if (updateWordsInputPositionAnimationFrameId !== null) {
497-
cancelAnimationFrame(updateWordsInputPositionAnimationFrameId);
498-
}
499-
updateWordsInputPositionAnimationFrameId = requestAnimationFrame(() => {
500-
updateWordsInputPositionAnimationFrameId = null;
497+
requestDebouncedAnimationFrame("test-ui.updateWordsInputPosition", () => {
501498
if (ActivePage.get() !== "test") return;
502499
const isTestRightToLeft = TestState.isDirectionReversed
503500
? !TestState.isLanguageRightToLeft

0 commit comments

Comments
 (0)