Skip to content

Open-source gym & body-weight tracker, now in the App Store (iPhone + Apple Watch) and as an Android APK. Plan routines, log workouts, see which muscles are trained, import from Strong/Hevy/FitNotes. Self-host it or use it on the phone alone.

perf: exercise clips off screen are paused

Duarte Santos3f006ec9a257
diff --git a/frontend/src/components/Media.autoplay.test.jsx b/frontend/src/components/Media.autoplay.test.jsx
index 52dbc358f..0bf64a97f 100644
--- a/frontend/src/components/Media.autoplay.test.jsx
+++ b/frontend/src/components/Media.autoplay.test.jsx
@@ -1,6 +1,6 @@
// @vitest-environment happy-dom
import { describe, expect, it, vi } from 'vitest'
-import { autoplayMuted } from './Media.jsx'
+import { autoplayClip, autoplayMuted } from './Media.jsx'
// Android's WebView autoplays only a video that carries the muted *attribute*; React sets the
// property alone, and the catalogue loops sat on their first frame in the phone app.
@@ -16,3 +16,27 @@ describe('autoplayMuted', () => {
})
it('ignores the unmount call', () => { expect(() => autoplayMuted(null)).not.toThrow() })
})
+
+// A clip plays while it is on screen: one scrolled away is paused, and started again when it is back.
+describe('autoplayClip', () => {
+ it('pauses a clip out of view and plays it again in view, and stops watching it on unmount', () => {
+ const seen = new Set()
+ let report = null
+ vi.stubGlobal('IntersectionObserver', class {
+ constructor(cb) { report = cb }
+ observe(el) { seen.add(el) }
+ unobserve(el) { seen.delete(el) }
+ })
+ const el = { muted: false, attrs: {}, setAttribute(k, v) { this.attrs[k] = v }, play: vi.fn(() => Promise.resolve()), pause: vi.fn() }
+ const stop = autoplayClip(el)
+ expect(el.play).toHaveBeenCalledTimes(1)
+ expect(seen.has(el)).toBe(true)
+ report([{ target: el, isIntersecting: false }])
+ expect(el.pause).toHaveBeenCalledTimes(1)
+ report([{ target: el, isIntersecting: true }])
+ expect(el.play).toHaveBeenCalledTimes(2)
+ stop()
+ expect(seen.has(el)).toBe(false)
+ vi.unstubAllGlobals()
+ })
+})
diff --git a/frontend/src/components/Media.jsx b/frontend/src/components/Media.jsx
index 2405a1b0d..7df1fec50 100644
--- a/frontend/src/components/Media.jsx
+++ b/frontend/src/components/Media.jsx
@@ -64,7 +64,7 @@ function BuiltinMedia({ ex, id, compact, minimizable }) {
: showGif && clip && clipSrc
// No poster: Android's WebView went back to it at every loop and stalled there (the
// still above stands in while the clip loads, so nothing is lost without it).
- ? <video ref={autoplayMuted} className="catvid" src={clipSrc} autoPlay muted loop playsInline disablePictureInPicture
+ ? <video ref={autoplayClip} className="catvid" src={clipSrc} autoPlay muted loop playsInline disablePictureInPicture
aria-label={exerciseNameFor(ex)} onError={onError} />
: showGif && clip
// the phone app is still reading the clip into memory: the still holds its place
@@ -141,6 +141,27 @@ export function autoplayMuted(el) {
if (p && typeof p.catch === 'function') p.catch(() => {})
}
+// An exercise's clip plays while it is on screen. Each one used to loop for as long as it was
+// mounted: in the List layout every exercise of the workout has one, and five clips nobody could
+// see decoded ~28 frames a second each (headless Chrome, synthetic clips). One observer for all of
+// them pauses a clip that scrolls out of view and starts it again just before it comes back.
+// Without IntersectionObserver (old browsers, tests) the clips simply loop as before.
+let clipObserver = null
+const clipsInView = () => clipObserver || (clipObserver = new IntersectionObserver(entries => {
+ for (const e of entries) {
+ const v = e.target
+ if (e.isIntersecting) { const p = v.play?.(); if (p && typeof p.catch === 'function') p.catch(() => {}) }
+ else v.pause?.()
+ }
+}, { rootMargin: '100px 0px' }))
+export function autoplayClip(el) {
+ autoplayMuted(el)
+ if (!el || typeof IntersectionObserver === 'undefined') return
+ const io = clipsInView()
+ io.observe(el)
+ return () => io.unobserve(el)
+}
+
// A still that will not load (offline and never cached, a lapsed session on a gated instance, a
// CDN hiccup) gets the same neutral tile as an exercise without media, instead of the browser's
// broken-image glyph in a list of them (#281). The failure is remembered per image, so a list