Results written ahead
Ship results written for some choices, show the one that holds, and ask System Two when none does.
Sam's card has a picture of a cake. The designer drew one for the warm card, and a model drew one for the funny card ahead of time. The app ships both, each with the tone it was drawn for. When Sam picks formal, neither fits, and System Two has to draw one. When Sam goes back to warm, the designer's cake comes back.
Written, and under what
Each picture is kept with the values it was drawn under and its writer:
import type { Written } from '@reintent/lib/written';
const CAKES: Written<string>[] = [
{ value: 'cake-warm.svg', under: { 'card#tone': 'warm' }, by: 'human' },
{ value: 'cake-funny.svg', under: { 'card#tone': 'funny' }, by: 'system-two' },
];A Written result has its value, under (a choice's key and the value it had) and by. stale(under, now) lists the keys of under that do not hold now: whose value is something else, or is not known. A choice it was written under that is not read any more is unknown, and unknown is not "holds". When the host knows a value does not matter here, it says so with irrelevant in now, and that key holds whatever it was. An empty list means the result holds.
Which one to show
import { choose } from '@reintent/lib/written';
choose(CAKES, { 'card#tone': 'funny' }).shown?.value; // → 'cake-funny.svg'
choose(CAKES, { 'card#tone': 'formal' }); // → { shown: undefined, due: true, passed: […] }choose(written, now, opts) returns a Chosen: shown, the first result that holds, and due, true when nothing holds and something had been written. passed says why each result before it was not shown, with the keys that moved (stale) and what the host's check found (fails).
The results are tried by writer first, by default in the order of BY: a Human's own ('human'), System Two's ('system-two'), System One's approval of Code's result ('system-one'), one Code carried from elsewhere ('code'). Among the same writer they keep the order given. That order is the application's priority, not Reintent's layers of a choice, and opts.order gives another. What System Two generates when nothing holds is not one of these: it is core's generated result, kept by the choice's identity and the full input stamp, and found again by them. opts.check is the host's own test of a value, returning what is wrong with it: a picture too wide for a folded card fails it, and the next one is tried.
No result is thrown away. A result that does not hold now holds again when its values come back.
A host that keeps results as it goes (a layout a person took, one System Two wrote again when asked) keeps them in createWritten({ limit }): add(key, written) puts the newest first under a key, a slide on a screen, and get(key) gives that list to choose. With a limit the oldest beyond it is let go; the rest stay, through undo too.
In the program
import { host, useChoose } from '@reintent/core';
import { useWritten } from '@reintent/lib/written';
import { card } from './card.intent';
export const Card = card(function Card() {
const tone = useChoose(card.tone);
const cake = useWritten<string>({
of: 'card',
name: 'cake',
instruction: 'Draw a birthday cake that suits this card',
uses: { 'card#tone': tone },
written: CAKES,
});
return host('card', { tone, cake: cake.value ?? 'cake-plain.svg' });
});useWritten(opts) chooses as choose does, with uses as the values now. It returns a WrittenRead, the value, who wrote it (by) and a status:
ready: a result holds, or System Two's answer is in, shown as System Two's;pending: nothing holds, and System Two has been asked for one written underuses(Generating a value);none: nothing was ever written, so the host shows its own.
Pick formal: the card shows the plain cake while System Two draws, then the new one. Pick warm: the designer's cake is back, and nothing is asked. The request is sent only where the card is shown.
Recap
- A
Writtenresult keeps its value, the values it was written under and who wrote it. stale(under, now)lists the values that moved or are not known (irrelevantsays one does not matter);chooseshows the first result that holds, by the application's order of writers (BYby default) and then in order.createWrittenkeeps a host's results by a key, newest first, forchoose.useWrittendoes the same in a program, and asks System Two for a new one when none holds.
Last updated on
Edit on GitHub