Waiting on a person
A wait stops a run until a person answers it. The run takes no resources while it waits, and it can wait for days.
A wait and its window
This claim routes by what was lost, then asks an adjuster to approve a large one.
import { act, choice, party, process, wait } from '@trestlerun/process';
// What a claim is about: a car by its plate, or a home by its rooms.
export type Loss = { kind: 'car'; plate: string } | { kind: 'home'; rooms: number };
const adjuster = party({ name: 'adjuster' });
// Value a claim by what was lost, then pay it, a large one once a person approves.
export const claim = process({
name: 'claim',
body: async ({ loss, amount }: { loss: Loss; amount: number }) => {
const assessed = await choice({
name: 'what was lost',
value: loss,
selector: 'kind',
cases: {
car: (car) =>
act({ name: 'value the car', body: () => car.plate.length * 1000 }),
home: (home) =>
act({ name: 'value the home', body: () => home.rooms * 5000 }),
},
});
const isLarge = await choice({
name: 'large',
condition: amount > assessed,
});
if (isLarge) {
const review = await wait({
name: 'approve',
party: adjuster,
window: '3 days',
});
const isApproved = await choice({
name: 'approved',
condition: review.isAnswered && review.answer === true,
});
if (!isApproved) {
return 'refused';
}
}
await act({ name: 'pay', body: () => undefined });
return 'paid';
},
});import { act, choice, party, process, wait } from '@trestlerun/process';
// What a claim is about: a car by its plate, or a home by its rooms.
export type Loss =
| { kind: 'car'; plate: string }
| { kind: 'home'; rooms: number };
const adjuster = party({ name: 'adjuster' });
// Value a claim by what was lost, then pay it, a large one once a
// person approves.
export const claim = process({
name: 'claim',
body: async ({ loss, amount }: { loss: Loss; amount: number }) => {
const assessed = await choice({
name: 'what was lost',
value: loss,
selector: 'kind',
cases: {
car: (car) =>
act({
name: 'value the car',
body: () => car.plate.length * 1000,
}),
home: (home) =>
act({
name: 'value the home',
body: () => home.rooms * 5000,
}),
},
});
const isLarge = await choice({
name: 'large',
condition: amount > assessed,
});
if (isLarge) {
const review = await wait({
name: 'approve',
party: adjuster,
window: '3 days',
});
const isApproved = await choice({
name: 'approved',
condition: review.isAnswered && review.answer === true,
});
if (!isApproved) {
return 'refused';
}
}
await act({ name: 'pay', body: () => undefined });
return 'paid';
},
});import {
act,
choice,
party,
process,
wait,
} from '@trestlerun/process';
// What a claim is about: a car by its plate, or a home by
// its rooms.
export type Loss =
| { kind: 'car'; plate: string }
| { kind: 'home'; rooms: number };
const adjuster = party({ name: 'adjuster' });
// Value a claim by what was lost, then pay it, a large one
// once a person approves.
export const claim = process({
name: 'claim',
body: async ({
loss,
amount,
}: {
loss: Loss;
amount: number;
}) => {
const assessed = await choice({
name: 'what was lost',
value: loss,
selector: 'kind',
cases: {
car: (car) =>
act({
name: 'value the car',
body: () => car.plate.length * 1000,
}),
home: (home) =>
act({
name: 'value the home',
body: () => home.rooms * 5000,
}),
},
});
const isLarge = await choice({
name: 'large',
condition: amount > assessed,
});
if (isLarge) {
const review = await wait({
name: 'approve',
party: adjuster,
window: '3 days',
});
const isApproved = await choice({
name: 'approved',
condition:
review.isAnswered && review.answer === true,
});
if (!isApproved) {
return 'refused';
}
}
await act({ name: 'pay', body: () => undefined });
return 'paid';
},
});import {
act,
choice,
party,
process,
wait,
} from '@trestlerun/process';
// What a claim is about: a car by its plate, or a
// home by its rooms.
export type Loss =
| { kind: 'car'; plate: string }
| { kind: 'home'; rooms: number };
const adjuster = party({ name: 'adjuster' });
// Value a claim by what was lost, then pay it, a
// large one once a person approves.
export const claim = process({
name: 'claim',
body: async ({
loss,
amount,
}: {
loss: Loss;
amount: number;
}) => {
const assessed = await choice({
name: 'what was lost',
value: loss,
selector: 'kind',
cases: {
car: (car) =>
act({
name: 'value the car',
body: () => car.plate.length * 1000,
}),
home: (home) =>
act({
name: 'value the home',
body: () => home.rooms * 5000,
}),
},
});
const isLarge = await choice({
name: 'large',
condition: amount > assessed,
});
if (isLarge) {
const review = await wait({
name: 'approve',
party: adjuster,
window: '3 days',
});
const isApproved = await choice({
name: 'approved',
condition:
review.isAnswered &&
review.answer === true,
});
if (!isApproved) {
return 'refused';
}
}
await act({
name: 'pay',
body: () => undefined,
});
return 'paid';
},
});import {
act,
choice,
party,
process,
wait,
} from '@trestlerun/process';
// What a claim is about: a car by its
// plate, or a home by its rooms.
export type Loss =
| { kind: 'car'; plate: string }
| { kind: 'home'; rooms: number };
const adjuster = party({
name: 'adjuster',
});
// Value a claim by what was lost, then
// pay it, a large one once a person
// approves.
export const claim = process({
name: 'claim',
body: async ({
loss,
amount,
}: {
loss: Loss;
amount: number;
}) => {
const assessed = await choice({
name: 'what was lost',
value: loss,
selector: 'kind',
cases: {
car: (car) =>
act({
name: 'value the car',
body: () =>
car.plate.length * 1000,
}),
home: (home) =>
act({
name: 'value the home',
body: () => home.rooms * 5000,
}),
},
});
const isLarge = await choice({
name: 'large',
condition: amount > assessed,
});
if (isLarge) {
const review = await wait({
name: 'approve',
party: adjuster,
window: '3 days',
});
const isApproved = await choice({
name: 'approved',
condition:
review.isAnswered &&
review.answer === true,
});
if (!isApproved) {
return 'refused';
}
}
await act({
name: 'pay',
body: () => undefined,
});
return 'paid';
},
});import {
act,
choice,
party,
process,
wait,
} from '@trestlerun/process';
// What a claim is about: a car by
// its plate, or a home by its
// rooms.
export type Loss =
| { kind: 'car'; plate: string }
| { kind: 'home'; rooms: number };
const adjuster = party({
name: 'adjuster',
});
// Value a claim by what was lost,
// then pay it, a large one once a
// person approves.
export const claim = process({
name: 'claim',
body: async ({
loss,
amount,
}: {
loss: Loss;
amount: number;
}) => {
const assessed = await choice({
name: 'what was lost',
value: loss,
selector: 'kind',
cases: {
car: (car) =>
act({
name: 'value the car',
body: () =>
car.plate.length *
1000,
}),
home: (home) =>
act({
name: 'value the home',
body: () =>
home.rooms * 5000,
}),
},
});
const isLarge = await choice({
name: 'large',
condition: amount > assessed,
});
if (isLarge) {
const review = await wait({
name: 'approve',
party: adjuster,
window: '3 days',
});
const isApproved =
await choice({
name: 'approved',
condition:
review.isAnswered &&
review.answer === true,
});
if (!isApproved) {
return 'refused';
}
}
await act({
name: 'pay',
body: () => undefined,
});
return 'paid';
},
});import {
act,
choice,
party,
process,
wait,
} from '@trestlerun/process';
// What a claim is about: a
// car by its plate, or a home
// by its rooms.
export type Loss =
| {
kind: 'car';
plate: string;
}
| {
kind: 'home';
rooms: number;
};
const adjuster = party({
name: 'adjuster',
});
// Value a claim by what was
// lost, then pay it, a large
// one once a person approves.
export const claim = process({
name: 'claim',
body: async ({
loss,
amount,
}: {
loss: Loss;
amount: number;
}) => {
const assessed =
await choice({
name: 'what was lost',
value: loss,
selector: 'kind',
cases: {
car: (car) =>
act({
name: 'value the car',
body: () =>
car.plate
.length *
1000,
}),
home: (home) =>
act({
name: 'value the home',
body: () =>
home.rooms *
5000,
}),
},
});
const isLarge =
await choice({
name: 'large',
condition:
amount > assessed,
});
if (isLarge) {
const review =
await wait({
name: 'approve',
party: adjuster,
window: '3 days',
});
const isApproved =
await choice({
name: 'approved',
condition:
review.isAnswered &&
review.answer ===
true,
});
if (!isApproved) {
return 'refused';
}
}
await act({
name: 'pay',
body: () => undefined,
});
return 'paid';
},
});The wait names the party who answers it, here the adjuster, and a window of three days. When the window closes with no answer, the run goes on with review.isAnswered false.
Answering a wait
Whoever operates the process answers the wait by its name, from the console or in code, and the run goes on from the wait with the answer it was given.