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: remember a catalogue exercise's muscles for Stats

Duarte Santos94292fad133a
diff --git a/frontend/src/lib/exercises.js b/frontend/src/lib/exercises.js
index 1f18230c1..e5fa87b65 100644
--- a/frontend/src/lib/exercises.js
+++ b/frontend/src/lib/exercises.js
@@ -93,6 +93,10 @@ export function registerCustom(list) {
xs.forEach(e => { EXIDX[e.id] = e })
}
+// A built-in catalogue entry itself (never a copy, a snapshot or a custom one). Those objects never
+// change while the app runs, so what is derived from them alone can be remembered (muscles.js).
+export const isBuiltinEx = ex => BUILTIN.has(ex)
+
// Whether an exercise is one of the user's own (#378, #358). Where it lives decides, not the
// `custom` flag alone: a plan import before 1.3.8 (a shared plan file, a plan from the AI coach)
// stored its exercises without the flag, and those were left with no Edit, no Delete and a
diff --git a/frontend/src/lib/muscles.js b/frontend/src/lib/muscles.js
index 13a2d0e1a..b62d0d3a1 100644
--- a/frontend/src/lib/muscles.js
+++ b/frontend/src/lib/muscles.js
@@ -8,7 +8,7 @@
// ankles, "cardiovascular system") maps to null and is dropped rather than guessed at.
import { isWarmupRow } from './workout-model.js'
-import { EXIDX, smOf } from './exercises.js'
+import { EXIDX, smOf, isBuiltinEx } from './exercises.js'
import { todayISO, weekKey, MONDAY } from './format.js'
import { queueOf } from './queue.js'
import { workoutAt } from './history.js'
@@ -186,9 +186,24 @@ export function matchesMuscleGroups(ex, requested) {
return wanted.some(group => groups.has(group))
}
+// Fatigue, strength and the balance map ask for the muscles of every entry of every workout on
+// each Stats render. A catalogue exercise always answers the same, so its answer is kept (and
+// handed out as a copy, since callers are free to change what they get).
+const builtinWeights = new WeakMap()
+const builtinMuscles = new WeakMap()
+
/** Complete muscle-weight metadata, including explicit zero-credit associations. */
export function muscleWeightsOf(ex) {
if (!ex) return {}
+ if (isBuiltinEx(ex)) {
+ let w = builtinWeights.get(ex)
+ if (!w) builtinWeights.set(ex, w = weightsOf(ex))
+ return { ...w }
+ }
+ return weightsOf(ex)
+}
+
+function weightsOf(ex) {
const sourceEx = metadataOf(ex)
if (sourceEx !== ex) return muscleWeightsOf(sourceEx)
if (ex.muscleWeights && typeof ex.muscleWeights === 'object' && !Array.isArray(ex.muscleWeights)) {
@@ -223,8 +238,14 @@ export function muscleWeightsOf(ex) {
/** Muscles one exercise trains: positive effective stimulus only. */
export function musclesOf(ex) {
- return Object.fromEntries(Object.entries(muscleWeightsOf(ex)).filter(([, weight]) => weight > 0))
+ if (ex && isBuiltinEx(ex)) {
+ let m = builtinMuscles.get(ex)
+ if (!m) builtinMuscles.set(ex, m = positiveOf(muscleWeightsOf(ex)))
+ return { ...m }
+ }
+ return positiveOf(muscleWeightsOf(ex))
}
+const positiveOf = weights => Object.fromEntries(Object.entries(weights).filter(([, weight]) => weight > 0))
/** Snapshot display and weighted muscle metadata into a completed history entry. */
export function exerciseMuscleSnapshot(ex) {
diff --git a/frontend/src/lib/muscles.test.js b/frontend/src/lib/muscles.test.js
index ce6f9b80c..59ec8837f 100644
--- a/frontend/src/lib/muscles.test.js
+++ b/frontend/src/lib/muscles.test.js
@@ -302,3 +302,24 @@ describe('MUSCLE_NAME as i18n keys', () => {
}
})
})
+
+// Catalogue answers are remembered for Stats (muscleWeightsOf/musclesOf run per entry of every
+// workout); what a caller gets is still its own object, and anything not built in is read fresh.
+describe('remembered catalogue muscles', () => {
+ it('hands out an equal copy each time, so a caller changing it changes nothing', () => {
+ const ex = EXIDX['0025']
+ const a = musclesOf(ex), b = muscleWeightsOf(ex)
+ expect(Object.keys(a).length).toBeGreaterThan(0)
+ a.chest = 99; b.chest = 99; a.made_up = 1
+ expect(musclesOf(ex)).not.toBe(a)
+ expect(musclesOf(ex)).toEqual(musclesOf({ ...ex }))
+ expect(muscleWeightsOf(ex)).toEqual(muscleWeightsOf({ ...ex }))
+ })
+
+ it('reads an exercise of your own again after you edit it', () => {
+ const own = { id: 'c-q1', n: 'Mine', tg: 'pectorals' }
+ expect(Object.keys(musclesOf(own))).toEqual(['chest'])
+ own.tg = 'biceps'
+ expect(Object.keys(musclesOf(own))).toEqual(['biceps'])
+ })
+})