Skip to content
Merged
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
41 changes: 20 additions & 21 deletions frontend/src/ts/test/focus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import * as LiveBurst from "./live-burst";
import * as LiveAcc from "./live-acc";
import * as TimerProgress from "./timer-progress";
import * as PageTransition from "../states/page-transition";
import { requestDebouncedAnimationFrame } from "../utils/debounced-animation-frame";

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

if (value && !state) {
state = true;
if (value && !state) {
state = true;

// batch DOM operations for better performance
requestAnimationFrame(() => {
// batch DOM operations for better performance
if (cache.focus) {
for (const el of cache.focus) {
el.classList.add("focus");
Expand All @@ -58,17 +59,15 @@ export function set(value: boolean, withCursor = false): void {
el.style.cursor = "none";
}
}
});

Caret.stopAnimation();
LiveSpeed.show();
LiveBurst.show();
LiveAcc.show();
TimerProgress.show();
} else if (!value && state) {
state = false;
Caret.stopAnimation();
LiveSpeed.show();
LiveBurst.show();
LiveAcc.show();
TimerProgress.show();
} else if (!value && state) {
state = false;

requestAnimationFrame(() => {
if (cache.focus) {
for (const el of cache.focus) {
el.classList.remove("focus");
Expand All @@ -79,14 +78,14 @@ export function set(value: boolean, withCursor = false): void {
el.style.cursor = "";
}
}
});

Caret.startAnimation();
LiveSpeed.hide();
LiveBurst.hide();
LiveAcc.hide();
TimerProgress.hide();
}
Caret.startAnimation();
LiveSpeed.hide();
LiveBurst.hide();
LiveAcc.hide();
TimerProgress.hide();
}
});
}

$(document).on("mousemove", function (event) {
Expand Down
9 changes: 3 additions & 6 deletions frontend/src/ts/test/test-ui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { TimerColor, TimerOpacity } from "@monkeytype/schemas/configs";
import { convertRemToPixels } from "../utils/numbers";
import { findSingleActiveFunboxWithFunction } from "./funbox/list";
import * as TestState from "./test-state";
import { requestDebouncedAnimationFrame } from "../utils/debounced-animation-frame";

const debouncedZipfCheck = debounce(250, async () => {
const supports = await JSONData.checkIfLanguageSupportsZipf(Config.language);
Expand Down Expand Up @@ -491,13 +492,9 @@ export function appendEmptyWordElement(
`<div class='word' data-wordindex='${index}'><letter class='invisible'>_</letter></div>`
);
}
let updateWordsInputPositionAnimationFrameId: null | number = null;

export function updateWordsInputPosition(): void {
if (updateWordsInputPositionAnimationFrameId !== null) {
cancelAnimationFrame(updateWordsInputPositionAnimationFrameId);
}
updateWordsInputPositionAnimationFrameId = requestAnimationFrame(() => {
updateWordsInputPositionAnimationFrameId = null;
requestDebouncedAnimationFrame("test-ui.updateWordsInputPosition", () => {
if (ActivePage.get() !== "test") return;
const isTestRightToLeft = TestState.isDirectionReversed
? !TestState.isLanguageRightToLeft
Expand Down
21 changes: 21 additions & 0 deletions frontend/src/ts/utils/debounced-animation-frame.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const pendingFrames = new Map<string, number>();

export function requestDebouncedAnimationFrame(
frameId: string,
callback: () => void
): void {
cancelIfPending(frameId);
const frame = requestAnimationFrame(() => {
pendingFrames.delete(frameId);
callback();
});
pendingFrames.set(frameId, frame);
}

function cancelIfPending(frameId: string): void {
const pending = pendingFrames.get(frameId);
if (pending !== undefined) {
cancelAnimationFrame(pending);
pendingFrames.delete(frameId);
}
}
Loading