Repository navigation
perf: pause image loading animations when tab is hidden #1422
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
MrPunyapal
wants to merge
4
commits into
dev-null-sh:production
from
MrPunyapal:perf/pause-image-scramble-in-hidden-tabs
+153
−9
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
db1fd34
perf: pause image loading animations when tab is hidden
MrPunyapal 0866957
fix: clear scrambled image frames when reduced motion is enabled
MrPunyapal 79bcc6a
fix: stop scrambling immediately when reduced motion changes
MrPunyapal b0134a5
style: keep updated source within project formatting conventions
MrPunyapal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| /// <reference types="node" /> | ||
| import assert from 'node:assert/strict'; | ||
| import * as React from 'react'; | ||
| import { afterEach, test, vi } from 'vite-plus/test'; | ||
|
|
||
| const notify = vi.fn(); | ||
| let unsubscribe: () => void; | ||
| vi.doMock('react', () => ({ | ||
| ...React, | ||
| useSyncExternalStore: ( | ||
| subscribe: (callback: () => void) => () => void, | ||
| snapshot: () => number | null, | ||
| ) => { | ||
| unsubscribe = subscribe(notify); | ||
| return snapshot(); | ||
| }, | ||
| })); | ||
|
|
||
| afterEach(() => { | ||
| unsubscribe?.(); | ||
| vi.useRealTimers(); | ||
| vi.unstubAllGlobals(); | ||
| }); | ||
|
|
||
| test('pauses the image loading animation offscreen and resumes on return', async () => { | ||
| vi.useFakeTimers(); | ||
| let reducedMotion = false; | ||
| let onMotionChange: (() => void) | undefined; | ||
| vi.stubGlobal('window', { | ||
| matchMedia: () => ({ | ||
| get matches() { | ||
| return reducedMotion; | ||
| }, | ||
| addEventListener: (name: string, callback: () => void) => { | ||
| if (name === 'change') onMotionChange = callback; | ||
| }, | ||
| removeEventListener: () => { | ||
| onMotionChange = undefined; | ||
| }, | ||
| }), | ||
| }); | ||
| const events = new Map<string, () => void>(); | ||
| const document = { | ||
| visibilityState: 'visible', | ||
| addEventListener: (event: string, handler: () => void) => events.set(event, handler), | ||
| removeEventListener: (event: string) => events.delete(event), | ||
| }; | ||
| vi.stubGlobal('document', document); | ||
| const { useScrambledHash } = await import( | ||
| '../../resources/js/hooks/use-scrambled-hash' | ||
| ); | ||
|
|
||
| assert.equal(useScrambledHash('abcd').length, 4); | ||
| vi.advanceTimersByTime(140); | ||
| assert.equal(notify.mock.calls.length, 2); | ||
|
|
||
| document.visibilityState = 'hidden'; | ||
| events.get('visibilitychange')?.(); | ||
| vi.advanceTimersByTime(700); | ||
| assert.equal(notify.mock.calls.length, 2); | ||
|
|
||
| document.visibilityState = 'visible'; | ||
| events.get('visibilitychange')?.(); | ||
| vi.advanceTimersByTime(70); | ||
| assert.equal(notify.mock.calls.length, 3); | ||
|
|
||
| reducedMotion = true; | ||
| onMotionChange?.(); | ||
| assert.equal(notify.mock.calls.length, 4); | ||
| assert.ok(useScrambledHash('abcd').every(({ isLocked }) => isLocked)); | ||
| vi.advanceTimersByTime(700); | ||
| assert.equal(notify.mock.calls.length, 4); | ||
|
|
||
| reducedMotion = false; | ||
| onMotionChange?.(); | ||
| vi.advanceTimersByTime(70); | ||
| assert.equal(notify.mock.calls.length, 5); | ||
|
|
||
| document.visibilityState = 'hidden'; | ||
| events.get('visibilitychange')?.(); | ||
| reducedMotion = true; | ||
| document.visibilityState = 'visible'; | ||
| events.get('visibilitychange')?.(); | ||
| assert.equal(notify.mock.calls.length, 6); | ||
| assert.ok(useScrambledHash('abcd').every(({ isLocked }) => isLocked)); | ||
| vi.advanceTimersByTime(700); | ||
| assert.equal(notify.mock.calls.length, 6); | ||
|
|
||
| unsubscribe(); | ||
| assert.equal(onMotionChange, undefined); | ||
| assert.equal(events.has('visibilitychange'), false); | ||
| }); | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Include this test in the frontend CI run.
The frontend CI job runs
test:unit, which selects onlytests/Unit/*.test.ts. It therefore skips this.test.tsxfile, even though a focused run passes. Run this test throughvp testin CI, or convert it to a.test.tstest compatible with the existing Node runner. Adding.tsxto the Node glob alone will not work because Node’s type-stripping runner does not support.tsx. (raw.githubusercontent.com)