Measure the time between two events by long-pressing them
"How long after eating did he poo?" is answerable from the log, but only by reading two times off the screen and subtracting them — and the pair is often on different days, so it is rarely on screen together at all. Hold one row, hold another, and a bar along the bottom does the subtraction and keeps it until you clear it, which is what lets you change day between the two picks. Any row that is a single moment can be picked: history, notes, weigh-ins. Sleep and walk rows cannot, being spans — measuring from one would need a rule about which end, and a rule you have to remember is worse than the feature. The picks are a module-level variable rather than storage. A measurement is a question you are asking now, not a setting; but module-level is also what carries it through the re-render a background sync causes every minute, which would otherwise wipe a half-made measurement. Ids that stop resolving — deleted here, tombstoned by another device — leave the pick on the next render instead of lingering as half a pair. Two additions beyond what was asked. Holding a picked row unpicks it: that is not a third selection but an undo of one, and without it a mis-press costs a clear. And the reading is ordered by time rather than by which was pressed first, so it is always chronological and never negative — pressing upward through a log is the natural way to read it. The press mechanics are all load-bearing: a finger that travels is a scroll and cancels, a fired press swallows the click that would otherwise also open the edit dialog, and the platform's own long-press menu is suppressed. That last part needs user-select: none on the rows, which costs the ability to select a note's text to copy. Worth stating plainly — it is a real loss, taken because holding a row now means something else. checks/extract.mjs gained getters for mutable bindings while writing the checks for this. It only ever returned a let's value at load time, so measurePick went stale the moment the code reassigned it and the checks were quietly asserting against a snapshot. Any future check reading a mutable binding would have hit the same thing.
This commit is contained in:
+6
-3
@@ -78,8 +78,10 @@ function declaration(src, masked, name) {
|
||||
* load({ names, lets, stubs }) → { ...declarations, set: { <let>: fn } }
|
||||
*
|
||||
* names declarations to pull across, in dependency order
|
||||
* lets of those, the mutable ones a check needs to assign (a setter is
|
||||
* generated for each, since a check can't reach the binding otherwise)
|
||||
* lets of those, the mutable ones a check needs to reach. Each gets a setter
|
||||
* and a getter: the plain export is the value at load time, so a binding
|
||||
* the code reassigns (rather than mutates) would go stale and a check
|
||||
* would quietly assert against a snapshot.
|
||||
* stubs names the extracted code calls but which are not worth extracting —
|
||||
* DOM lookups, chartDays(), and so on
|
||||
*/
|
||||
@@ -90,9 +92,10 @@ export function load({ names, lets = [], stubs = {} }) {
|
||||
const stubNames = Object.keys(stubs);
|
||||
const exported = names.map(n => n.replace(/^.*\s/, ""));
|
||||
const setters = lets.map(n => `${n}: (v) => { ${n} = v; }`).join(", ");
|
||||
const getters = lets.map(n => `${n}: () => ${n}`).join(", ");
|
||||
const factory = new Function(...stubNames, `
|
||||
${body}
|
||||
return { ${exported.join(", ")}, set: { ${setters} } };
|
||||
return { ${exported.join(", ")}, set: { ${setters} }, get: { ${getters} } };
|
||||
`);
|
||||
return factory(...stubNames.map(n => stubs[n]));
|
||||
}
|
||||
|
||||
@@ -0,0 +1,98 @@
|
||||
// Long-press two rows and the app subtracts their times. The press itself
|
||||
// needs a finger, but everything it decides — which picks are held, what the
|
||||
// bar says — is ordinary logic, and that is where this can go quietly wrong.
|
||||
import { load } from "./extract.mjs";
|
||||
import { suite, eq, ok, report } from "./assert.mjs";
|
||||
|
||||
let rendered = 0;
|
||||
|
||||
const app = load({
|
||||
names: [
|
||||
"EVENT_LABELS", "ymd", "formatDuration", "formatTime",
|
||||
"measurePick", "toggleMeasurePick", "clearMeasure",
|
||||
"measureSummary", "measureLabel",
|
||||
],
|
||||
lets: ["measurePick"],
|
||||
stubs: { render: () => { rendered++; } },
|
||||
});
|
||||
|
||||
const at = (day, hour, min = 0) => new Date(2026, 8, day, hour, min).getTime();
|
||||
const ate = { id: "a", type: "eat", at: at(20, 12, 10) };
|
||||
const poo = { id: "b", type: "poo", at: at(20, 15, 52) };
|
||||
const pee = { id: "c", type: "pee", at: at(20, 18, 30) };
|
||||
const lateEat = { id: "d", type: "eat", at: at(19, 18, 30) }; // the evening before
|
||||
const events = [ate, poo, pee, lateEat];
|
||||
|
||||
const pick = (...ids) => { app.set.measurePick([]); ids.forEach(app.toggleMeasurePick); };
|
||||
const held = () => app.get.measurePick();
|
||||
|
||||
suite("what a press does to the pick");
|
||||
{
|
||||
pick("a");
|
||||
eq(held(), ["a"], "one press holds one");
|
||||
pick("a", "b");
|
||||
eq(held(), ["a", "b"], "a second press holds the pair");
|
||||
|
||||
// The user's choice: a third is refused rather than rolling the pair on.
|
||||
pick("a", "b", "c");
|
||||
eq(held(), ["a", "b"], "a third press is ignored while two are held");
|
||||
|
||||
// Not a third selection but an undo of one — a mis-press costs one press
|
||||
// rather than starting over.
|
||||
pick("a", "b");
|
||||
app.toggleMeasurePick("a");
|
||||
eq(held(), ["b"], "pressing a picked row unpicks it");
|
||||
app.toggleMeasurePick("c");
|
||||
eq(held(), ["b", "c"], "…leaving room for a different second");
|
||||
|
||||
pick("a", "b");
|
||||
app.clearMeasure();
|
||||
eq(held(), [], "clearing drops both");
|
||||
}
|
||||
|
||||
suite("the reading");
|
||||
{
|
||||
const two = app.measureSummary(["a", "b"], events);
|
||||
ok(two.show && two.complete, "two picks give a complete reading");
|
||||
eq(two.duration, "3h 42m", "12:10 to 15:52 is 3h 42m");
|
||||
|
||||
// Pressed newest-first, which is the natural way to scan a log upward.
|
||||
const reversed = app.measureSummary(["b", "a"], events);
|
||||
eq(reversed.duration, "3h 42m", "the order they were pressed in doesn't change the gap");
|
||||
eq(reversed.text, two.text, "…and it still reads chronologically, earliest first");
|
||||
ok(/Ate/.test(two.text) && /Poo/.test(two.text), "both events are named");
|
||||
}
|
||||
|
||||
suite("a pair that straddles midnight");
|
||||
{
|
||||
const overnight = app.measureSummary(["d", "b"], events); // 19th 18:30 → 20th 15:52
|
||||
eq(overnight.duration, "21h 22m", "the gap crosses the day boundary correctly");
|
||||
ok(/Sep/.test(overnight.text),
|
||||
"the dates are named, since two bare times would be ambiguous across days");
|
||||
ok(!/Sep/.test(app.measureSummary(["a", "b"], events).text),
|
||||
"…but a same-day pair stays uncluttered");
|
||||
}
|
||||
|
||||
suite("an incomplete or stale pick");
|
||||
{
|
||||
const one = app.measureSummary(["a"], events);
|
||||
ok(one.show && !one.complete, "one pick shows the bar without a duration");
|
||||
ok(/long-press another/.test(one.text), "…and asks for the second");
|
||||
|
||||
eq(app.measureSummary([], events).show, false, "nothing picked hides the bar");
|
||||
|
||||
// Deleted here, or tombstoned by another device mid-measurement.
|
||||
const stale = app.measureSummary(["a", "gone"], events);
|
||||
eq(stale.ids, ["a"], "an id that no longer resolves is dropped from the pick");
|
||||
ok(!stale.complete, "…so what is left is one pick, not a broken pair");
|
||||
eq(app.measureSummary(["gone", "also-gone"], events).show, false,
|
||||
"both gone hides the bar rather than showing an empty one");
|
||||
}
|
||||
|
||||
suite("the label");
|
||||
{
|
||||
eq(app.measureLabel(ate), `Ate ${app.formatTime(ate.at)}`, "type and time");
|
||||
ok(/Sep 20/.test(app.measureLabel(ate, true)), "with the date when asked for");
|
||||
}
|
||||
|
||||
export default report("measure");
|
||||
Reference in New Issue
Block a user