Skip to content

Roll a number up to a parent

Keep the number in a Property of the parent. Publish an Event on the child each time the thing you count happens, and add a parent Listener for that Event on the parent’s Template. It runs once for each Event, gets the parent, and writes the new number.

In the onboarding App, each task is a child of a checklist. The finish Mutation moves a task to done and publishes finished on it. countDone adds 1 to the count on the checklist:

onboarding.ts
const finished = task.addEvent('finished');
checklist.addParentListener('countDone', finished, async (_event, parent) => {
const { done, total } = parent.properties;
if (typeof done !== 'number') return;
const updated = await parent.updateProperties({ done: done + 1 });
const transition = updated.state?.transitions.complete;
if (done + 1 === total) await transition?.execute({});
});

addParentListener takes the Event of the child, and parent is the checklist of the finished task. The last two lines move the checklist to complete when the count reaches total.

Bob finishes the first of Dan’s three tasks:

As Bob: "Order a laptop" is done
As Bob: sees Dan's checklist, inProgress, 0 of 3 done
As Bob: sees Dan's checklist, inProgress, 1 of 3 done

Look at the count. It is still 0 when finish returns, and 1 once the script has run the queued work with harness.tickAll().

The Listener adds 1 for each Event, so two tasks finished before one run of the queued work count 2. It counts Events, not tasks: finish refuses a task that is done already, so no task counts twice.

The whole App: Employee onboarding checklists. Step by step: Handle events.