Your first application
Build examples/task-app.ts: a task App whose tasks move from open to done, publish a completed Event and are counted by a Metric. Each step adds a few lines to the same file and shows the whole file with the new lines highlighted. At the end the file compiles, and Run and test it executes it against the test Runtime.
Prerequisites
Section titled “Prerequisites”- A repository checkout with dependencies installed, as described in the Introduction.
- The finished file at
alpynehq/alpyne/website/examples/task-app.ts. Read it alongside the steps, or create an empty.tsfile beside it and type each step in. - One import:
import type { AppSpec } from '@alpynehq/alpyne-types/builder';. AnAppSpecis a function that receives anAppBuilderand declares the App on it.
1. Configure an App
Section titled “1. Configure an App”Export an AppSpec named tasksApp and call app.configure('tutorialTasks') as its first statement.
import type { AppSpec } from '@alpynehq/alpyne-types/builder';
interface TaskInput { readonly id: string;}
export const tasksApp: AppSpec = (app) => { app.configure('tutorialTasks');
const task = app.addTemplate('task'); task.addProperty('title', 'string'); task.addProperty('completionNoted', 'boolean');
const open = task.addState('open').setDefault(); const done = task.addState('done'); open.addTransition('complete', done);
const completed = task.addEvent('completed'); completed.addListener('noteCompletion', async (_event, item) => { await item.updateProperties({ completionNoted: true }); });
const complete = app.addMutation('complete', async (input: TaskInput, current) => { const item = await current.templates.task.getItem(input.id); if (item === undefined) throw new Error('Task not found'); if (item.state?.value !== 'open') return;
const updated = await item.state.transitions.complete.execute({}); await updated.events.completed.publish({}); }); complete.input.addProperty('id', 'id').mandatory(); complete.output.void();
const title = app.addQuery('title', async (input: TaskInput, current) => { const item = await current.templates.task.getItem(input.id); const value = item?.properties.title; return typeof value === 'string' ? value : undefined; }); title.input.addProperty('id', 'id').mandatory(); title.output.value('string');};
export const taskApp: AppSpec = (app) => { app.configure('taskTutorial'); const tasks = app.addDependency('tasks', tasksApp); app.addMetric('completedTasks').addEvent(tasks.templates.task.events.completed);};The Builder now knows one App with the canonical name tutorialTasks. That name prefixes every PRN the App declares, so the Template in the next step becomes prn::tutorialTasks:template/task. It is also the key handlers use to reach the App, as ctx.apps.tutorialTasks. Call configure before any other declaration.
2. Declare a Template with Properties
Section titled “2. Declare a Template with Properties”Call app.addTemplate('task'), then add a title Property of type 'string' and a completionNoted Property of type 'boolean'.
import type { AppSpec } from '@alpynehq/alpyne-types/builder';
interface TaskInput { readonly id: string;}
export const tasksApp: AppSpec = (app) => { app.configure('tutorialTasks');
const task = app.addTemplate('task'); task.addProperty('title', 'string'); task.addProperty('completionNoted', 'boolean');
const open = task.addState('open').setDefault(); const done = task.addState('done'); open.addTransition('complete', done);
const completed = task.addEvent('completed'); completed.addListener('noteCompletion', async (_event, item) => { await item.updateProperties({ completionNoted: true }); });
const complete = app.addMutation('complete', async (input: TaskInput, current) => { const item = await current.templates.task.getItem(input.id); if (item === undefined) throw new Error('Task not found'); if (item.state?.value !== 'open') return;
const updated = await item.state.transitions.complete.execute({}); await updated.events.completed.publish({}); }); complete.input.addProperty('id', 'id').mandatory(); complete.output.void();
const title = app.addQuery('title', async (input: TaskInput, current) => { const item = await current.templates.task.getItem(input.id); const value = item?.properties.title; return typeof value === 'string' ? value : undefined; }); title.input.addProperty('id', 'id').mandatory(); title.output.value('string');};
export const taskApp: AppSpec = (app) => { app.configure('taskTutorial'); const tasks = app.addDependency('tasks', tasksApp); app.addMetric('completedTasks').addEvent(tasks.templates.task.events.completed);};The Builder now knows a Template whose Items carry up to two typed Properties. A declared Property is not required on every Item: a task created with only a title has no completionNoted value until a handler writes one. Handlers read a value through item.properties.<key> and check its type before using it. Define data lists every Property type.
3. Add a state machine
Section titled “3. Add a state machine”On the Template, call addState('open').setDefault() and addState('done'), then declare the transition open.addTransition('complete', done).
import type { AppSpec } from '@alpynehq/alpyne-types/builder';
interface TaskInput { readonly id: string;}
export const tasksApp: AppSpec = (app) => { app.configure('tutorialTasks');
const task = app.addTemplate('task'); task.addProperty('title', 'string'); task.addProperty('completionNoted', 'boolean');
const open = task.addState('open').setDefault(); const done = task.addState('done'); open.addTransition('complete', done);
const completed = task.addEvent('completed'); completed.addListener('noteCompletion', async (_event, item) => { await item.updateProperties({ completionNoted: true }); });
const complete = app.addMutation('complete', async (input: TaskInput, current) => { const item = await current.templates.task.getItem(input.id); if (item === undefined) throw new Error('Task not found'); if (item.state?.value !== 'open') return;
const updated = await item.state.transitions.complete.execute({}); await updated.events.completed.publish({}); }); complete.input.addProperty('id', 'id').mandatory(); complete.output.void();
const title = app.addQuery('title', async (input: TaskInput, current) => { const item = await current.templates.task.getItem(input.id); const value = item?.properties.title; return typeof value === 'string' ? value : undefined; }); title.input.addProperty('id', 'id').mandatory(); title.output.value('string');};
export const taskApp: AppSpec = (app) => { app.configure('taskTutorial'); const tasks = app.addDependency('tasks', tasksApp); app.addMetric('completedTasks').addEvent(tasks.templates.task.events.completed);};The Builder now knows a state machine with two states and one directional transition. Every new task starts in open because setDefault marks it. A handler reads the state as item.state?.value and moves it with item.state.transitions.complete.execute({}). Executing complete on a task that is not open throws, so the transition is only ever applied to its source state. Add states and transitions covers initial states and transition Grants.
4. Add an Event and a Listener
Section titled “4. Add an Event and a Listener”Call task.addEvent('completed'), then register a Listener on it with addListener('noteCompletion', handler). The handler receives the Event, the task Item, the current App and the write context. This one uses the Item to set completionNoted.
import type { AppSpec } from '@alpynehq/alpyne-types/builder';
interface TaskInput { readonly id: string;}
export const tasksApp: AppSpec = (app) => { app.configure('tutorialTasks');
const task = app.addTemplate('task'); task.addProperty('title', 'string'); task.addProperty('completionNoted', 'boolean');
const open = task.addState('open').setDefault(); const done = task.addState('done'); open.addTransition('complete', done);
const completed = task.addEvent('completed'); completed.addListener('noteCompletion', async (_event, item) => { await item.updateProperties({ completionNoted: true }); });
const complete = app.addMutation('complete', async (input: TaskInput, current) => { const item = await current.templates.task.getItem(input.id); if (item === undefined) throw new Error('Task not found'); if (item.state?.value !== 'open') return;
const updated = await item.state.transitions.complete.execute({}); await updated.events.completed.publish({}); }); complete.input.addProperty('id', 'id').mandatory(); complete.output.void();
const title = app.addQuery('title', async (input: TaskInput, current) => { const item = await current.templates.task.getItem(input.id); const value = item?.properties.title; return typeof value === 'string' ? value : undefined; }); title.input.addProperty('id', 'id').mandatory(); title.output.value('string');};
export const taskApp: AppSpec = (app) => { app.configure('taskTutorial'); const tasks = app.addDependency('tasks', tasksApp); app.addMetric('completedTasks').addEvent(tasks.templates.task.events.completed);};The Builder now knows an Event that handlers publish against a task, and one Listener that runs after the Event commits. Declaring the Listener does not run it. The reactor claims each committed completed Event and runs noteCompletion in a write execution of its own. updateProperties therefore commits separately from the write that published the Event. Handle events explains publish, immediate and Listener contexts.
5. Add a Mutation
Section titled “5. Add a Mutation”Call app.addMutation('complete', handler). Type the handler’s input as TaskInput, then declare the shape on the returned builder: input.addProperty('id', 'id').mandatory() and output.void().
import type { AppSpec } from '@alpynehq/alpyne-types/builder';
interface TaskInput { readonly id: string;}
export const tasksApp: AppSpec = (app) => { app.configure('tutorialTasks');
const task = app.addTemplate('task'); task.addProperty('title', 'string'); task.addProperty('completionNoted', 'boolean');
const open = task.addState('open').setDefault(); const done = task.addState('done'); open.addTransition('complete', done);
const completed = task.addEvent('completed'); completed.addListener('noteCompletion', async (_event, item) => { await item.updateProperties({ completionNoted: true }); });
const complete = app.addMutation('complete', async (input: TaskInput, current) => { const item = await current.templates.task.getItem(input.id); if (item === undefined) throw new Error('Task not found'); if (item.state?.value !== 'open') return;
const updated = await item.state.transitions.complete.execute({}); await updated.events.completed.publish({}); }); complete.input.addProperty('id', 'id').mandatory(); complete.output.void();
const title = app.addQuery('title', async (input: TaskInput, current) => { const item = await current.templates.task.getItem(input.id); const value = item?.properties.title; return typeof value === 'string' ? value : undefined; }); title.input.addProperty('id', 'id').mandatory(); title.output.value('string');};
export const taskApp: AppSpec = (app) => { app.configure('taskTutorial'); const tasks = app.addDependency('tasks', tasksApp); app.addMetric('completedTasks').addEvent(tasks.templates.task.events.completed);};The Builder now knows a Mutation that a write execution invokes as current.mutations.complete.handler(input, current, ctx). The handler looks the task up through current.templates.task.getItem, which returns undefined for a missing Item or an Item of another Template. It returns early when the task is not open. Otherwise it executes the transition and publishes completed on the updated Item the transition returned.
6. Add a Query
Section titled “6. Add a Query”Call app.addQuery('title', handler) with the same TaskInput, then declare input.addProperty('id', 'id').mandatory() and output.value('string').
import type { AppSpec } from '@alpynehq/alpyne-types/builder';
interface TaskInput { readonly id: string;}
export const tasksApp: AppSpec = (app) => { app.configure('tutorialTasks');
const task = app.addTemplate('task'); task.addProperty('title', 'string'); task.addProperty('completionNoted', 'boolean');
const open = task.addState('open').setDefault(); const done = task.addState('done'); open.addTransition('complete', done);
const completed = task.addEvent('completed'); completed.addListener('noteCompletion', async (_event, item) => { await item.updateProperties({ completionNoted: true }); });
const complete = app.addMutation('complete', async (input: TaskInput, current) => { const item = await current.templates.task.getItem(input.id); if (item === undefined) throw new Error('Task not found'); if (item.state?.value !== 'open') return;
const updated = await item.state.transitions.complete.execute({}); await updated.events.completed.publish({}); }); complete.input.addProperty('id', 'id').mandatory(); complete.output.void();
const title = app.addQuery('title', async (input: TaskInput, current) => { const item = await current.templates.task.getItem(input.id); const value = item?.properties.title; return typeof value === 'string' ? value : undefined; }); title.input.addProperty('id', 'id').mandatory(); title.output.value('string');};
export const taskApp: AppSpec = (app) => { app.configure('taskTutorial'); const tasks = app.addDependency('tasks', tasksApp); app.addMetric('completedTasks').addEvent(tasks.templates.task.events.completed);};The Builder now knows a Query that a read execution invokes as current.queries.title.handler(input, current, ctx). The handler receives a read App: it reads Items and returns a value, and it cannot write or publish. It returns the title when the Item exists and carries a string, and undefined otherwise. Both match the declared string output.
7. Compose a root App with a Metric
Section titled “7. Compose a root App with a Metric”Export a second AppSpec named taskApp. Configure it as taskTutorial, import the first App with app.addDependency('tasks', tasksApp), and declare app.addMetric('completedTasks').addEvent(tasks.templates.task.events.completed).
import type { AppSpec } from '@alpynehq/alpyne-types/builder';
interface TaskInput { readonly id: string;}
export const tasksApp: AppSpec = (app) => { app.configure('tutorialTasks');
const task = app.addTemplate('task'); task.addProperty('title', 'string'); task.addProperty('completionNoted', 'boolean');
const open = task.addState('open').setDefault(); const done = task.addState('done'); open.addTransition('complete', done);
const completed = task.addEvent('completed'); completed.addListener('noteCompletion', async (_event, item) => { await item.updateProperties({ completionNoted: true }); });
const complete = app.addMutation('complete', async (input: TaskInput, current) => { const item = await current.templates.task.getItem(input.id); if (item === undefined) throw new Error('Task not found'); if (item.state?.value !== 'open') return;
const updated = await item.state.transitions.complete.execute({}); await updated.events.completed.publish({}); }); complete.input.addProperty('id', 'id').mandatory(); complete.output.void();
const title = app.addQuery('title', async (input: TaskInput, current) => { const item = await current.templates.task.getItem(input.id); const value = item?.properties.title; return typeof value === 'string' ? value : undefined; }); title.input.addProperty('id', 'id').mandatory(); title.output.value('string');};
export const taskApp: AppSpec = (app) => { app.configure('taskTutorial'); const tasks = app.addDependency('tasks', tasksApp); app.addMetric('completedTasks').addEvent(tasks.templates.task.events.completed);};The Builder now knows a root App that imports tutorialTasks under the local dependency key tasks. The handle addDependency returns exposes the imported Templates, Roles and Events, so the Metric selects the completed Event without redeclaring it. A Metric with no calculation handler counts each selected Event once. taskApp is the App the harness builds on the next page.
What you built
Section titled “What you built”- Steps 1 to 3 declare data: an App with a canonical name, a Template with two Properties and a two-state machine. Data model explains Templates, Items and states.
- Steps 4 to 6 declare behavior: an Event with a Listener, a Mutation that transitions and publishes, and a Query that reads. Behavior and change explains what each may do and where it runs.
- Step 7 composes Apps: a root App imports another App and derives a Metric from its Event. Application model explains root and imported Apps and PRNs.
Related
Section titled “Related”- Run and test it to execute this App with the harness and observe each boundary.
- Define data to add more Property types, keys and hierarchy.
- Add queries and mutations for every input and output shape a Mutation or Query declares.
- Compose apps for canonical names, dependency keys and reaching imported Apps.
AppBuilderandTemplateBuilderfor every method the steps call.

