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.
JavaScript apple-watchbodyweightdockerfitnessfitness-trackergymhealthiosmcpnodejspasskeysprogressive-web-applicationspwareactself-hostedself-hostingvitewebauthnweightliftingworkout-tracker
Custom exercises: a file picked in an editor left open for more than an hour is not collected before it is saved
Duarte Santos8a1ac8d02a4a
diff --git a/frontend/src/components/CustomMediaField.jsx b/frontend/src/components/CustomMediaField.jsxindex 8c76e99c7..af2b7a19b 100644--- a/frontend/src/components/CustomMediaField.jsx+++ b/frontend/src/components/CustomMediaField.jsx@@ -60,6 +60,14 @@ export default function CustomMediaField({ media, url, onChange }) {return () => { alive = false }}, [])const m = mediaOf({ media })+ // The draft's files stay out of the local clean-up for as long as this form is open: its+ // one-hour grace would otherwise take a file picked in a form left open longer, and the saved+ // exercise would point at nothing.+ const mainHash = m?.hash, posterHash = m?.poster?.hash+ useEffect(() => {+ if (!mainHash) return undefined+ return mediaStore.hold([mainHash, posterHash])+ }, [mainHash, posterHash])// Signed in to a server that answered without a `media` block: it predates the feature or has// MEDIA_UPLOADS=0. Nothing would ever reach it. A config not known yet (an offline start) is// not that answer — a file picked then waits here and goes up once the server is reached. Adiff --git a/frontend/src/lib/media-store.js b/frontend/src/lib/media-store.jsindex 04cdd3360..2ae41975e 100644--- a/frontend/src/lib/media-store.js+++ b/frontend/src/lib/media-store.js@@ -73,6 +73,7 @@ export function createMediaStore(backend, {// hash → { url, refs, timer, file } (file: a phone's file URL, which is never revoked)const urls = new Map()const making = new Map() // hash → the url() in progress, so two callers make one+ const held = new Map() // hash → how many open editors hold it as their draft (hold)const emit = () => { for (const fn of listeners) { try { fn() } catch { /* a listener's own problem */ } } }@@ -100,6 +101,21 @@ export function createMediaStore(backend, {const store = {ready,+ /** Marks files as a draft's (an editor that is open) until the returned function is called.+ * The local clean-up keeps what is held however long ago it was put: its one-hour grace is+ * for a draft abandoned, not for one still being written. In memory only — a draft does not+ * outlive its tab. */+ hold(hashes) {+ const hs = [...new Set(hashes)].filter(Boolean)+ for (const h of hs) held.set(h, (held.get(h) || 0) + 1)+ let done = false+ return () => {+ if (done) return+ done = true+ for (const h of hs) { const n = (held.get(h) || 0) - 1; if (n > 0) held.set(h, n); else held.delete(h) }+ }+ },+ isHeld: hash => held.has(hash),get persistent() { return persistent },get backendName() { return be.name },subscribe(fn) { listeners.add(fn); return () => listeners.delete(fn) },diff --git a/frontend/src/lib/media-sync.js b/frontend/src/lib/media-sync.jsindex cc20e8724..655b374ba 100644--- a/frontend/src/lib/media-sync.js+++ b/frontend/src/lib/media-sync.js@@ -262,8 +262,8 @@ export function createMediaSync(deps = {}) {/*** Deletes the local files nothing needs any more. Live = what the state in memory references,* what the saved copy references (another tab may be on an older or newer one), what any stash- * references, and anything put in the last hour (a file picked in an editor that is still- * open). Signed in, the store is then kept under LOCAL_CACHE_MAX_MB by evicting main files+ * references, what an open editor holds as its draft (mediaStore.hold), and anything put in the+ * last hour (a draft abandoned without closing, or another tab's). Signed in, the store is then kept under LOCAL_CACHE_MAX_MB by evicting main files* that are safely on the server, least recently shown first; posters the state references and* pending files are never evicted, and a guest's or a local phone's referenced files are never* evicted at all — for them this is the only copy.@@ -293,18 +293,20 @@ export function createMediaSync(deps = {}) {try {if (d.nativeLoad) { const saved = await d.nativeLoad(); if (saved) for (const h of referencedHashes(saved)) live.add(h) }} catch { return } // the mirror could not be read: delete nothing rather than guess+ // A file an open editor holds as its draft is live too, however long the editor has been open.+ const held = r => typeof d.media.isHeld === 'function' && d.media.isHeld(r.hash)const cutoff = d.now() - LOCAL_GC_GRACE_MSconst all = await d.media.list()- for (const r of all) if (!live.has(r.hash) && (r.putAt || 0) < cutoff) await d.media.remove(r.hash)+ for (const r of all) if (!live.has(r.hash) && !held(r) && (r.putAt || 0) < cutoff) await d.media.remove(r.hash)if (!st.user) return- const left = (await d.media.list()).filter(r => live.has(r.hash) || (r.putAt || 0) >= cutoff)+ const left = (await d.media.list()).filter(r => live.has(r.hash) || held(r) || (r.putAt || 0) >= cutoff)let bytes = left.reduce((n, r) => n + (r.size || 0), 0)if (bytes <= LOCAL_CACHE_MAX_MB * MB) returnconst refs = referencedFiles(st.S)const posters = new Set(refs.filter(f => f.poster).map(f => f.hash))const inState = new Set(refs.map(f => f.hash))const candidates = left- .filter(r => !r.pending && !posters.has(r.hash) && (r.putAt || 0) < cutoff)+ .filter(r => !r.pending && !posters.has(r.hash) && !held(r) && (r.putAt || 0) < cutoff).sort((a, b) => (inState.has(a.hash) - inState.has(b.hash)) || ((a.shownAt || 0) - (b.shownAt || 0)))for (const r of candidates) {if (bytes <= LOCAL_CACHE_MAX_MB * MB) breakdiff --git a/frontend/src/lib/media-sync.test.js b/frontend/src/lib/media-sync.test.jsindex 5c39ed536..ae9d8ea8b 100644--- a/frontend/src/lib/media-sync.test.js+++ b/frontend/src/lib/media-sync.test.js@@ -263,6 +263,24 @@ describe('localMediaGc', () => {expect((await media.list()).map(r => r.hash).sort()).toEqual([hex('a'), hex('b'), hex('c'), hex('d'), hex('9')].sort())})+ it('keeps the draft of an editor left open past the hour, and lets it go once the editor closes', async () => {+ await put(hex('a')); await put(hex('b'))+ const release = media.hold([hex('a'), hex('b')])+ const release2 = media.hold([hex('a')]) // the same draft in a second form+ clock += 2 * 3600000+ const sync = make(appStore({ S: { customEx: [] }, user: null }))+ await sync.localMediaGc()+ expect(await media.has(hex('a'))).toBe(true)+ expect(await media.has(hex('b'))).toBe(true)+ release(); release() // a second call changes nothing+ await sync.localMediaGc()+ expect(await media.has(hex('a'))).toBe(true) // still held by the second form+ expect(await media.has(hex('b'))).toBe(false)+ release2()+ await sync.localMediaGc()+ expect(await media.has(hex('a'))).toBe(false)+ })+it('deletes nothing when the stash cannot be read', async () => {await put(hex('a'))clock += 2 * 3600000diff --git a/frontend/src/sheets.custom-media.test.jsx b/frontend/src/sheets.custom-media.test.jsxindex fb84b13ca..44f741024 100644--- a/frontend/src/sheets.custom-media.test.jsx+++ b/frontend/src/sheets.custom-media.test.jsx@@ -113,6 +113,17 @@ describe('custom exercise photo, GIF or video, and link', () => {expect(JSON.stringify(S())).not.toContain('IMG_0042')})+ it('holds the draft\'s files out of the local clean-up while the form is open', async () => {+ customExSheet(null)+ const form = renderTop()+ await pick(form)+ expect(media.isHeld(HASH)).toBe(true)+ expect(media.isHeld(POSTER)).toBe(true)+ act(() => { mounted.splice(0).forEach(root => root.unmount()) })+ expect(media.isHeld(HASH)).toBe(false)+ expect(media.isHeld(POSTER)).toBe(false)+ })+it('Remove takes the photo off on save', async () => {seed(custom({ media: REF }))customExSheet(S().customEx[0])