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.

Sync: a routine only the other copy has lands next to its old neighbour instead of at the end, so an Undo that meets an edit from another device keeps its place

Duarte Santos1135ea120e71
diff --git a/frontend/src/lib/sync-merge.js b/frontend/src/lib/sync-merge.js
index 073ed1212..22653bde2 100644
--- a/frontend/src/lib/sync-merge.js
+++ b/frontend/src/lib/sync-merge.js
@@ -22,7 +22,8 @@
* copies' lists (the kept version's in its order, then the other's extras), so a photo added
* on one device survives the other's edit of the same workout — or its own photo
* (mergeWorkoutMedia)
- * - routines: union by id in the newer copy's order; of an id that both have, the version
+ * - routines: union by id in the newer copy's order, a routine only the older copy has next to
+ * its neighbour there (unionByNeighbours); of an id that both have, the version
* edited last by its own `_ts` (stampRoutines), the newer copy's on a tie
* - bodyweight: union by day, the later-edited (`t`) entry of a day that both have
* - favEx: ordered set union, the newer copy first
@@ -94,6 +95,30 @@ export function unionById(newer = [], older = [], key = x => x?.id) {
return out
}
+/**
+ * unionById for a list whose order the user set (routines): `newer`'s entries in their order, and
+ * each entry only `older` has goes in right after its nearest earlier neighbour in `older` (at
+ * the start when it has none there). Appending them sent a routine that one device put back
+ * (Plan's Undo) to the end of the list whenever the other device's copy was the newer one.
+ */
+export function unionByNeighbours(newer = [], older = [], key = x => x?.id) {
+ const out = unionById(newer, [], key)
+ const at = new Set(out.map(key).filter(k => k != null))
+ const keyless = []
+ let prev = null
+ for (const x of list(older)) {
+ const k = key(x)
+ if (k == null) { keyless.push(x); continue }
+ if (!at.has(k)) {
+ const i = prev == null ? -1 : out.findIndex(y => key(y) === prev)
+ out.splice(i + 1, 0, x)
+ at.add(k)
+ }
+ prev = k
+ }
+ return out.concat(keyless)
+}
+
const workoutKey = w => (w?.id != null ? w.id : `${w?.d}|${w?.start}`)
/**
@@ -545,7 +570,7 @@ export function mergeStates(a0, b0, { prefer } = {}) {
}
out.workouts.sort(byDayStart)
for (const f of ['routines', 'customEx', 'equipProfiles', 'gymCards']) {
- if (list(n[f]).length || list(o[f]).length) out[f] = unionById(n[f], o[f]).map(clone)
+ if (list(n[f]).length || list(o[f]).length) out[f] = (f === 'routines' ? unionByNeighbours : unionById)(n[f], o[f]).map(clone)
}
// A routine edited on both sides keeps the version edited last. Taking the newer copy's
// version dropped a plan edit made on one device whenever the other had since logged a set or
diff --git a/frontend/src/lib/sync-merge.test.js b/frontend/src/lib/sync-merge.test.js
index 36cd7bcd3..7f48ad98e 100644
--- a/frontend/src/lib/sync-merge.test.js
+++ b/frontend/src/lib/sync-merge.test.js
@@ -272,6 +272,18 @@ describe('routines keep the version edited last', () => {
expect(mergeStates(desk, phone).routines).toEqual(m.routines)
})
+ it('a routine only the older copy has comes back next to its old neighbour, not at the end', () => {
+ // Device A deleted Pull and then undid it; B, which had pulled the delete, duplicated Leg
+ // meanwhile and is the newer copy.
+ const a = base({ _ts: 100, routines: [r('push', 5, 1), r('pull', 5, 90), r('leg', 5, 1), r('cond', 5, 1)] })
+ const b = base({ _ts: 200, routines: [r('push', 5, 1), r('leg', 5, 1), r('leg2', 5, 150), r('cond', 5, 1)] })
+ expect(ids(mergeStates(a, b).routines)).toEqual(['push', 'pull', 'leg', 'leg2', 'cond'])
+ // first in the older copy: first in the merge; two in a row keep their order
+ const c = base({ _ts: 100, routines: [r('x', 5, 1), r('y', 5, 1), r('push', 5, 1)] })
+ const d = base({ _ts: 200, routines: [r('push', 5, 1), r('leg', 5, 1)] })
+ expect(ids(mergeStates(c, d).routines)).toEqual(['x', 'y', 'push', 'leg'])
+ })
+
it('without stamps, or on a tie, the newer copy\'s version stays', () => {
const a = base({ _ts: 100, routines: [r('push', 15)] })
const b = base({ _ts: 200, routines: [r('push', 10)] })