Reintent

Declaring an entity's choices

Write a card's choices once, as data, and read and decide them by handle.

In Quick start, the tone is pieced together where it is read: useChoiceStatus(choices(...), { of: 'card', name: 'tone', defaultValue: 'warm', question, ask: ['model'] }). Its identity is written again in the buttons and in every decide, and the joke hides its condition in the code. Mistype 'tone' once, and that place reads a different choice. Sam's card can declare its choices once, as data, and every other place reads them from there.

You will learn

  • How entity declares a card's choices and the parts that never change
  • How to read a choice with useEntity or by its handle, and decide it with types
  • How a declaration says when a choice applies, what it rules out, and what System One reads
  • How one entity gets several instances with id

Declaring the card once

The card's tone and its closing line, declared together:

/** @jsxImportSource @reintent/core */
import { entity, host, useEntity } from '@reintent/core';

export type Person = { to: string; age: number; from: string };
export const GRANDMA: Person = { to: 'Grandma', age: 90, from: 'Sam' };

/** The card's choices, declared once, as data; and the line that never changes. */
export const card = entity('card', {
  tone: {
    kinds: ['warm', 'funny', 'formal'],
    default: 'warm',
    judge: 'Which tone suits a birthday card for Grandma, turning 90?',
    about: { label: 'How the card sounds' },
  },
  closing: { fixed: 'With love,' },
});

export const LOOK = {
  warm: { background: '#fdebd3', accent: '#b5562b' },
  funny: { background: '#fff2a8', accent: '#7a4fd6' },
  formal: { background: '#f1eee8', accent: '#2b2b2b' },
};

export function title(p: Person, tone: keyof typeof LOOK) {
  if (tone === 'formal') return `Many happy returns, ${p.to}.`;
  if (tone === 'warm') return `Happy ${p.age}th birthday, ${p.to}!`;
  return `${p.age} laps around the sun, ${p.to}!`;
}

/** A component of the entity: it reads the card's choices, and the fixed line with them. */
export const Card = card(function Card(p: Person) {
  const { tone, closing } = useEntity(card);
  return host('div', {
    style: {
      maxWidth: 380,
      padding: '1.5em',
      borderRadius: 12,
      background: LOOK[tone].background,
      color: '#3a2a1c',
    },
    children: [
      host('h1', { style: { color: LOOK[tone].accent }, children: title(p, tone) }),
      host('p', { children: 'Wishing you a wonderful day.' }),
      host('p', { style: { textAlign: 'right', fontStyle: 'italic' }, children: `${closing} ${p.from}` }),
    ],
  });
});

export default function App() {
  return <Card {...GRANDMA} />;
}
ChoicesC

Starting…

Realizationλ

Starting…

What happened

Nothing logged.

entity('card', fields) makes an Entity. Each field is a FieldDecl, either a choice (ChoiceDecl) or a part that never changes (FixedDecl):

FieldWhat it says
kindsthe possibilities: a list of tags, or tags with their data
priorthe code's prior by tag; the tags it leaves out share what is left
defaultthe tag while unsure; without it, the prior's likeliest, else the first
judgethe question for System One; without it, nothing is judged
giventhe entity's choices to know first; the judge reads them
applieswhen the choice applies; otherwise it is not read
forbidpossibilities ruled out while a condition holds
oran open tail: one possibility more, written by System Two
aboutanything for libraries and screens, such as a label; it reaches the read untouched
fixeda part that never changes: closing here. It has a value and no possibilities, and nothing decides it

The whole declaration is Json, and entity checks it when it is built. A default that is not one of the kinds, or a prior that sums to more than 1, throws with every error listed.

card(function Card(…) { … }) gives the component with the card's scope. Inside it, useEntity(card) reads every choice and returns them with the fixed parts, an EntityValue: { tone, closing }. The tone's question and possibilities are the ones in Quick start, so System One's recorded answer is the same, warm at 0.80.

Reading and deciding by handle

The buttons need the tone too, and they decide it:

/** @jsxImportSource @reintent/core */
import { action, entity, host, useChoiceStatus, useEntity, type ChoiceStatus, type Id } from '@reintent/core';

export type Person = { to: string; age: number; from: string };
export const GRANDMA: Person = { to: 'Grandma', age: 90, from: 'Sam' };

/** The card's choices, declared once, as data; and the line that never changes. */
export const card = entity('card', {
  tone: {
    kinds: ['warm', 'funny', 'formal'],
    default: 'warm',
    judge: 'Which tone suits a birthday card for Grandma, turning 90?',
    about: { label: 'How the card sounds' },
  },
  closing: { fixed: 'With love,' },
});

/** What a button does, typed by the handle: a decision, or none (undefined) to forget it. */

export const LOOK = {
  warm: { background: '#fdebd3', accent: '#b5562b' },
  funny: { background: '#fff2a8', accent: '#7a4fd6' },
  formal: { background: '#f1eee8', accent: '#2b2b2b' },
};

export function title(p: Person, tone: keyof typeof LOOK) {
  if (tone === 'formal') return `Many happy returns, ${p.to}.`;
  if (tone === 'warm') return `Happy ${p.age}th birthday, ${p.to}!`;
  return `${p.age} laps around the sun, ${p.to}!`;
}

/** A component of the entity: it reads the card's choices, and the fixed line with them. */
export const Card = card(function Card(p: Person) {
  const { tone, closing } = useEntity(card);
  return host('div', {
    style: {
      maxWidth: 380,
      padding: '1.5em',
      borderRadius: 12,
      background: LOOK[tone].background,
      color: '#3a2a1c',
    },
    children: [
      host('h1', { style: { color: LOOK[tone].accent }, children: title(p, tone) }),
      host('p', { children: 'Wishing you a wonderful day.' }),
      host('p', { style: { textAlign: 'right', fontStyle: 'italic' }, children: `${closing} ${p.from}` }),
    ],
  });
});

/** A row of buttons for one choice: a decision is pressed, a value the card took itself is dotted. */
export function row<T extends string>(kinds: readonly T[], now: ChoiceStatus<T>, id: Id) {
  const decided = now.source === 'decision';
  return host('p', {
    children: [
      ...kinds.map((k) =>
        host('button', {
          disabled: decided && k === now.value,
          ...(!decided && k === now.value && { style: { textDecoration: 'underline dotted' } }),
          onClick: action.decide(id, k),
          children: k,
        }),
      ),
      host('button', {
        disabled: !decided,
        onClick: action.forget(id),
        style: { marginLeft: 12, borderStyle: 'dashed', fontStyle: 'italic' },
        children: 'no preference',
      }),
    ],
  });
}

/** The buttons read the tone by its handle, and decide it by its handle. */
export function Buttons() {
  const tone = useChoiceStatus(card.tone);
  return row(card.decl.tone.kinds, tone, card.tone());
}

export default function App() {
  return host('div', { children: [<Card {...GRANDMA} />, <Buttons />] });
}
ChoicesC

Starting…

Realizationλ

Starting…

What happened

Nothing logged.

Each choice field is a family, a ChoiceFamily. useChoiceStatus(card.tone) reads it where it stands: inside a card's scope, that card's tone; outside any, the singleton, whose of is the entity's name. Call the family with an of, and you get a handle (ChoiceRef): card.tone('me') names one instance, and card.tone() the singleton. A handle is an Id with a key, so intent.decide, intent.forget and everything else that takes an identity takes it.

A handle also types the decision. Decide in App.intent.tsx takes a value of type DecisionFor<I>, and for card.tone() that is one of the tone's kinds:

decide(card.tone(), 'funy'); // a type error: not one of the tone's kinds
decide(card.tune(), 'funny'); // a type error: the card has no tune

A read by handle takes the same options as any read, as DeclaredOptions: ask, open, a defaultValue that overrides the declaration's, and uses. useEntity(card, { tone: { ask: ['human'] } }) gives them field by field.

A choice only a funny card has

The joke from Quick start only makes sense on a funny card. The declaration says so:

/** @jsxImportSource @reintent/core */
import { action, entity, host, useChoiceStatus, useEntity, type ChoiceStatus, type Id } from '@reintent/core';

export type Person = { to: string; age: number; from: string };
export const GRANDMA: Person = { to: 'Grandma', age: 90, from: 'Sam' };

/** The card's choices, declared once, as data; and the line that never changes. */
export const card = entity('card', {
  tone: {
    kinds: ['warm', 'funny', 'formal'],
    default: 'warm',
    judge: 'Which tone suits a birthday card for Grandma, turning 90?',
    about: { label: 'How the card sounds' },
  },
  joke: { kinds: ['pun', 'tease'], prior: { pun: 0.8 }, applies: { tone: ['funny'] } },
  closing: { fixed: 'With love,' },
});

/** What a button does, typed by the handle: a decision, or none (undefined) to forget it. */

export const LOOK = {
  warm: { background: '#fdebd3', accent: '#b5562b' },
  funny: { background: '#fff2a8', accent: '#7a4fd6' },
  formal: { background: '#f1eee8', accent: '#2b2b2b' },
};

export function title(p: Person, tone: keyof typeof LOOK, joke?: 'pun' | 'tease') {
  if (tone === 'formal') return `Many happy returns, ${p.to}.`;
  if (tone === 'warm') return `Happy ${p.age}th birthday, ${p.to}!`;
  return joke === 'tease'
    ? `${p.to}, you don’t look a day over ${p.age - 1}!`
    : `${p.age} laps around the sun, ${p.to}!`;
}

/** A component of the entity: it reads the card's choices, and the fixed line with them. */
export const Card = card(function Card(p: Person) {
  // The joke is there only when the tone is funny.
  const { tone, joke, closing } = useEntity(card);
  return host('div', {
    style: {
      maxWidth: 380,
      padding: '1.5em',
      borderRadius: 12,
      background: LOOK[tone].background,
      color: '#3a2a1c',
    },
    children: [
      host('h1', { style: { color: LOOK[tone].accent }, children: title(p, tone, joke) }),
      host('p', { children: 'Wishing you a wonderful day.' }),
      host('p', { style: { textAlign: 'right', fontStyle: 'italic' }, children: `${closing} ${p.from}` }),
    ],
  });
});

/** A row of buttons for one choice: a decision is pressed, a value the card took itself is dotted. */
export function row<T extends string>(kinds: readonly T[], now: ChoiceStatus<T>, id: Id) {
  const decided = now.source === 'decision';
  return host('p', {
    children: [
      ...kinds.map((k) =>
        host('button', {
          disabled: decided && k === now.value,
          ...(!decided && k === now.value && { style: { textDecoration: 'underline dotted' } }),
          onClick: action.decide(id, k),
          children: k,
        }),
      ),
      host('button', {
        disabled: !decided,
        onClick: action.forget(id),
        style: { marginLeft: 12, borderStyle: 'dashed', fontStyle: 'italic' },
        children: 'no preference',
      }),
    ],
  });
}

/** The buttons read the tone by its handle, and decide it by its handle. */
export function Buttons() {
  const tone = useChoiceStatus(card.tone);
  // The joke depends on the tone: the read hands it in, and gets undefined where it does not apply.
  const joke = useChoiceStatus(card.joke, { given: { tone: tone.value } });
  return host('div', {
    children: [
      row(card.decl.tone.kinds, tone, card.tone()),
      joke && row(card.decl.joke.kinds, joke, card.joke()),
    ],
  });
}

export default function App() {
  return host('div', { children: [<Card {...GRANDMA} />, <Buttons />] });
}
ChoicesC

Starting…

Realizationλ

Starting…

What happened

Nothing logged.

applies: { tone: ['funny'] } is a Condition: the joke applies while the tone is funny. { '!tone': ['formal'] } would say the opposite, any tone but formal. Where the joke does not apply, it is not read, judged or asked about, and useEntity leaves it out. Press funny, and the joke appears in Choices. It has no question for System One, and its prior leans to the pun at 0.8, so the pun settles it.

Buttons reads the joke by its handle, useChoiceStatus(card.joke, { given: { tone: tone.value } }). The joke depends on the tone, so the read hands the tone in, and the read gives undefined where the joke does not apply. Leave given out, and it is a type error and an error at run time.

Ruling out a possibility

A border goes around the card: flowers, stars or plain. Stars look out of place on a formal card:

/** @jsxImportSource @reintent/core */
import {
  action,
  entity,
  host,
  useChoiceStatus,
  useEntity,
  type ChoiceStatus,
  type Id,
  type ValueOf,
} from '@reintent/core';

export type Person = { to: string; age: number; from: string };
export const GRANDMA: Person = { to: 'Grandma', age: 90, from: 'Sam' };

/** The card's choices, declared once, as data; and the line that never changes. */
export const card = entity('card', {
  tone: {
    kinds: ['warm', 'funny', 'formal'],
    default: 'warm',
    judge: 'Which tone suits a birthday card for Grandma, turning 90?',
    about: { label: 'How the card sounds' },
  },
  joke: { kinds: ['pun', 'tease'], prior: { pun: 0.8 }, applies: { tone: ['funny'] } },
  border: {
    kinds: { flowers: { color: '#e889a8' }, stars: { count: 9 }, plain: {} },
    forbid: [{ when: { tone: ['formal'] }, kinds: ['stars'], why: 'a formal card has no stars' }],
  },
  closing: { fixed: 'With love,' },
});

/** What a button does, typed by the handle: a decision, or none (undefined) to forget it. */

export const LOOK = {
  warm: { background: '#fdebd3', accent: '#b5562b' },
  funny: { background: '#fff2a8', accent: '#7a4fd6' },
  formal: { background: '#f1eee8', accent: '#2b2b2b' },
};

export function title(p: Person, tone: keyof typeof LOOK, joke?: 'pun' | 'tease') {
  if (tone === 'formal') return `Many happy returns, ${p.to}.`;
  if (tone === 'warm') return `Happy ${p.age}th birthday, ${p.to}!`;
  return joke === 'tease'
    ? `${p.to}, you don’t look a day over ${p.age - 1}!`
    : `${p.age} laps around the sun, ${p.to}!`;
}

/** Each kind of border, drawn from its own data. */
function Edge(p: { border: ValueOf<typeof card.decl.border> }) {
  const b = p.border;
  if (b.kind === 'flowers')
    return host('p', { style: { color: b.color, margin: 0 }, children: '✿ ✿ ✿ ✿ ✿ ✿' });
  if (b.kind === 'stars') return host('p', { style: { margin: 0 }, children: '★'.repeat(b.count) });
  return null;
}

/** A component of the entity: it reads the card's choices, and the fixed line with them. */
export const Card = card(function Card(p: Person) {
  // The joke is there only when the tone is funny.
  const { tone, joke, border, closing } = useEntity(card);
  return host('div', {
    style: {
      maxWidth: 380,
      padding: '1.5em',
      borderRadius: 12,
      background: LOOK[tone].background,
      color: '#3a2a1c',
    },
    children: [
      <Edge border={border} />,
      host('h1', { style: { color: LOOK[tone].accent }, children: title(p, tone, joke) }),
      host('p', { children: 'Wishing you a wonderful day.' }),
      host('p', { style: { textAlign: 'right', fontStyle: 'italic' }, children: `${closing} ${p.from}` }),
      <Edge border={border} />,
    ],
  });
});

/** A row of buttons for one choice: a decision is pressed, a value the card took itself is dotted. */
export function row<T extends string>(kinds: readonly T[], now: ChoiceStatus<T>, id: Id) {
  const decided = now.source === 'decision';
  return host('p', {
    children: [
      ...kinds.map((k) =>
        host('button', {
          disabled: decided && k === now.value,
          ...(!decided && k === now.value && { style: { textDecoration: 'underline dotted' } }),
          onClick: action.decide(id, k),
          children: k,
        }),
      ),
      host('button', {
        disabled: !decided,
        onClick: action.forget(id),
        style: { marginLeft: 12, borderStyle: 'dashed', fontStyle: 'italic' },
        children: 'no preference',
      }),
    ],
  });
}

/** A choice's status with its tag for a value: what a row of buttons compares. */
const tagged = <T extends { kind: string }>(s: ChoiceStatus<T>): ChoiceStatus<T['kind']> => ({
  ...s,
  value: s.value.kind,
});

/** The buttons read the tone by its handle, and decide it by its handle. */
export function Buttons() {
  const tone = useChoiceStatus(card.tone);
  // The joke depends on the tone: the read hands it in, and gets undefined where it does not apply.
  const joke = useChoiceStatus(card.joke, { given: { tone: tone.value } });
  const border = useChoiceStatus(card.border, { given: { tone: tone.value } });
  return host('div', {
    children: [
      row(card.decl.tone.kinds, tone, card.tone()),
      joke && row(card.decl.joke.kinds, joke, card.joke()),
      row(['flowers', 'stars', 'plain'], tagged(border), card.border()),
    ],
  });
}

export default function App() {
  return host('div', { children: [<Card {...GRANDMA} />, <Buttons />] });
}
ChoicesC

Starting…

Realizationλ

Starting…

What happened

Nothing logged.

kinds here are tags with data, so the border's value is { kind: 'stars', count: 9 }, and the type of each value is ValueOf the declaration. forbid takes stars out of the possibilities while the tone is formal. A prior, if there is one, is spread over what is left.

Press stars, then formal. The stars are gone, and the border falls back to flowers, its first kind. Sam's decision is kept, waiting: press warm, and the stars come back.

When nothing is left

If forbid rules out every possibility, the choice cannot settle, with the reason 'none'. It becomes a request for someone to change the card, which When no possibility will do handles.

What System One reads

Which border suits the card depends on its tone. Ask System One, and tell it the tone:

/** @jsxImportSource @reintent/core */
import {
  action,
  entity,
  host,
  useChoiceStatus,
  useEntity,
  type ChoiceStatus,
  type Id,
  type ValueOf,
} from '@reintent/core';

export type Person = { to: string; age: number; from: string };
export const GRANDMA: Person = { to: 'Grandma', age: 90, from: 'Sam' };

/** The card's choices, declared once, as data; and the line that never changes. */
export const card = entity('card', {
  tone: {
    kinds: ['warm', 'funny', 'formal'],
    default: 'warm',
    judge: 'Which tone suits a birthday card for Grandma, turning 90?',
    about: { label: 'How the card sounds' },
  },
  joke: { kinds: ['pun', 'tease'], prior: { pun: 0.8 }, applies: { tone: ['funny'] } },
  border: {
    kinds: { flowers: { color: '#e889a8' }, stars: { count: 9 }, plain: {} },
    given: ['tone'],
    judge: 'Which border suits this birthday card?',
    forbid: [{ when: { tone: ['formal'] }, kinds: ['stars'], why: 'a formal card has no stars' }],
  },
  closing: { fixed: 'With love,' },
});

/** What a button does, typed by the handle: a decision, or none (undefined) to forget it. */

export const LOOK = {
  warm: { background: '#fdebd3', accent: '#b5562b' },
  funny: { background: '#fff2a8', accent: '#7a4fd6' },
  formal: { background: '#f1eee8', accent: '#2b2b2b' },
};

export function title(p: Person, tone: keyof typeof LOOK, joke?: 'pun' | 'tease') {
  if (tone === 'formal') return `Many happy returns, ${p.to}.`;
  if (tone === 'warm') return `Happy ${p.age}th birthday, ${p.to}!`;
  return joke === 'tease'
    ? `${p.to}, you don’t look a day over ${p.age - 1}!`
    : `${p.age} laps around the sun, ${p.to}!`;
}

/** Each kind of border, drawn from its own data. */
function Edge(p: { border: ValueOf<typeof card.decl.border> }) {
  const b = p.border;
  if (b.kind === 'flowers')
    return host('p', { style: { color: b.color, margin: 0 }, children: '✿ ✿ ✿ ✿ ✿ ✿' });
  if (b.kind === 'stars') return host('p', { style: { margin: 0 }, children: '★'.repeat(b.count) });
  return null;
}

/** A component of the entity: it reads the card's choices, and the fixed line with them. */
export const Card = card(function Card(p: Person) {
  // The joke is there only when the tone is funny.
  const { tone, joke, border, closing } = useEntity(card);
  return host('div', {
    style: {
      maxWidth: 380,
      padding: '1.5em',
      borderRadius: 12,
      background: LOOK[tone].background,
      color: '#3a2a1c',
    },
    children: [
      <Edge border={border} />,
      host('h1', { style: { color: LOOK[tone].accent }, children: title(p, tone, joke) }),
      host('p', { children: 'Wishing you a wonderful day.' }),
      host('p', { style: { textAlign: 'right', fontStyle: 'italic' }, children: `${closing} ${p.from}` }),
      <Edge border={border} />,
    ],
  });
});

/** A row of buttons for one choice: a decision is pressed, a value the card took itself is dotted. */
export function row<T extends string>(kinds: readonly T[], now: ChoiceStatus<T>, id: Id) {
  const decided = now.source === 'decision';
  return host('p', {
    children: [
      ...kinds.map((k) =>
        host('button', {
          disabled: decided && k === now.value,
          ...(!decided && k === now.value && { style: { textDecoration: 'underline dotted' } }),
          onClick: action.decide(id, k),
          children: k,
        }),
      ),
      host('button', {
        disabled: !decided,
        onClick: action.forget(id),
        style: { marginLeft: 12, borderStyle: 'dashed', fontStyle: 'italic' },
        children: 'no preference',
      }),
    ],
  });
}

/** A choice's status with its tag for a value: what a row of buttons compares. */
const tagged = <T extends { kind: string }>(s: ChoiceStatus<T>): ChoiceStatus<T['kind']> => ({
  ...s,
  value: s.value.kind,
});

/** The buttons read the tone by its handle, and decide it by its handle. */
export function Buttons() {
  const tone = useChoiceStatus(card.tone);
  // The joke depends on the tone: the read hands it in, and gets undefined where it does not apply.
  const joke = useChoiceStatus(card.joke, { given: { tone: tone.value } });
  const border = useChoiceStatus(card.border, { given: { tone: tone.value } });
  return host('div', {
    children: [
      row(card.decl.tone.kinds, tone, card.tone()),
      joke && row(card.decl.joke.kinds, joke, card.joke()),
      row(['flowers', 'stars', 'plain'], tagged(border), card.border()),
    ],
  });
}

export default function App() {
  return host('div', { children: [<Card {...GRANDMA} />, <Buttons />] });
}
ChoicesC

Starting…

Realizationλ

Starting…

What happened

Nothing logged.

given: ['tone'] lists what the border depends on. The judge reads it: What happened shows the border judged with { tone: 'warm' }, and System One picks a plain edge at 0.50. Press funny, and it is asked again with the new tone. It leans to stars, 0.45 to flowers' 0.35, which is not sure enough, so the border shows its default, flowers. On a formal card it is asked over flowers and plain alone.

The fields in applies and forbid count as dependencies too: they set the order of reads, and a read hands them in. Only given goes to the judge. GivenOf is the type of everything a choice depends on, and DeclaredArgs makes given required where there is something in it.

One entity, several instances

On the card the whole family signs, every grandchild picks a sign-off. The sign-off is declared once, and each grandchild is an instance:

/** @jsxImportSource @reintent/core */
import { action, createContext, entity, host, useChoose, type DecisionFor, type Id } from '@reintent/core';

/** A signer of the family card: one entity, an instance for each grandchild. */
export const signer = entity('signer', {
  signoff: { kinds: ['Love', 'Hugs', 'Cheers'] },
});

export const FAMILY = [
  { id: 'sam', name: 'Sam' },
  { id: 'mia', name: 'Mia' },
  { id: 'leo', name: 'Leo' },
];

/** What a button does, typed by the handle. */
export const Decide = createContext<<I extends Id>(id: I, value: DecisionFor<I>) => void>('Decide', {
  key: () => undefined,
});

/** A component of the entity: it reads the sign-off of its own instance, and writes no id. */
export const Signer = signer(function Signer(p: { name: string }) {
  const signoff = useChoose(signer.signoff);
  return host('p', {
    style: { textAlign: 'right', fontStyle: 'italic', margin: 0 },
    children: `${signoff}, ${p.name}`,
  });
});

/** The buttons under the card read the same instance through a Provider. */
function SignoffButtons(p: { id: string; name: string }) {
  const signoff = useChoose(signer.signoff);
  return host('p', {
    children: [
      host('small', { style: { display: 'inline-block', width: 40 }, children: p.name }),
      ...signer.decl.signoff.kinds.map((s) =>
        host('button', {
          disabled: s === signoff,
          onClick: action.decide(signer.signoff(p.id), s),
          children: s,
        }),
      ),
    ],
  });
}

export function Card() {
  return host('div', {
    style: { maxWidth: 380, padding: '1.5em', borderRadius: 12, background: '#fdebd3', color: '#3a2a1c' },
    children: [
      host('h1', { style: { color: '#b5562b' }, children: 'Happy 90th birthday, Grandma!' }),
      host('p', { children: 'Wishing you a wonderful day.' }),
      // One instance per grandchild: the id comes from the data, and is the element's key.
      FAMILY.map((s) => <Signer id={s.id} name={s.name} />),
    ],
  });
}

export default function App() {
  return host('div', {
    children: [
      <Card />,
      FAMILY.map((s) => (
        <signer.Provider id={s.id}>
          <SignoffButtons {...s} />
        </signer.Provider>
      )),
    ],
  });
}
ChoicesC

Starting…

Realizationλ

Starting…

What happened

Nothing logged.

  • <Signer id="mia" name="Mia" /> names the instance once. Inside it, useChoose(signer.signoff) reads mia#signoff, and the component writes no identity. The id is also the element's key, so the list needs no key.
  • <signer.Provider id="mia"> gives the same instance to components that are not the entity's own. The buttons under the card read Mia's sign-off through it.
  • intent.decide(signer.signoff('leo'), 'Cheers') decides one instance by its handle, and useEntity(signer, 'mia') reads one instance from anywhere.

Without an id, <Signer /> uses signer as its relative name. At the root, this is the singleton. An id inside another entity's instance chains: a signer mia inside a <Card id="grandma"> is grandma/mia. Only entities make the chain; other components, <Branch> and lists leave the identity as it is. Identity, ownership, and lifetime covers absolute ids and stored decisions.

Identity, ownership, and lifetime

An entity declares a group of choices. An instance gives those choices an address: the instance grandma and the field tone form grandma#tone. The Intent stores decisions at these addresses.

An entity component or Provider resolves its id in one of two ways:

  • A relative id is one name. Inside <Card id="grandma">, <Signer id="mia"> resolves to grandma/mia: Mia's sign-off on Grandma's card. Each card can have its own Mia sign-off.
  • An absolute id begins with /. <Signer id="/mia"> resolves to mia wherever it appears. <Picture id="/talk/gallery"> resolves to talk/gallery. Children with relative ids extend that address.

Use a relative id for a part whose identity belongs to its parent. Use an absolute id to share an instance across locations or keep its identity when moving it:

<List id="drafts">
  <Card id="/grandma" /> {/* grandma#tone in any list */}
  <Card id="note" /> {/* drafts/note#tone */}
</List>

The leading / selects an address from the root of the Intent. That address can be used for the first time here, or shared with another occurrence. Both occurrences read the same stored decisions; each evaluates its component in the contexts provided where it appears. Ordinary components leave entity addresses unchanged.

In a handle, write the resolved address: card.tone('grandma') targets <Card id="/grandma" />. The leading slash is syntax for the component's id. Instance addresses share one namespace within an Intent, so distinct entities need distinct addresses when their choice names overlap.

The Intent keeps decisions independently of component evaluation. Removing an instance from the program leaves its decisions available when that address is read again. A later decide replaces a decision; intent.forget(card.tone('grandma')) removes it so the lower layers can take effect. Resetting or restoring the Intent's state can also change its decisions. To persist them across Intent instances, save and restore the state explicitly.

Renaming a relative instance or changing its owning path gives it a new address. During reprogramming, intent.review(proposal).accept(migrate) can migrate decisions as it installs the new program. The configured keep-decisions rule checks whether the draft preserves the decisions read by the current program.

View identity and stored decisions

React associates local component state with identity in the render tree; SwiftUI ties @State storage to view identity. Longer-lived application data can be owned outside those views. Reintent keeps its decision records in the Intent, keyed by choice identity. Entity paths determine their addresses; removing a component leaves those records in place. See React's Preserving and Resetting State and Apple's Demystify SwiftUI.

A tone nobody listed

or: { kind: 'custom', instruction: '…' } adds one possibility more, whose value System Two writes when it is chosen. Generating a value covers it.

The types behind a declaration

Fields is a declaration, TailType the type of what an or tail writes, Declared a family or a handle, and ReadOf and StatusOf what useChoose and useChoiceStatus give for one: T | undefined where the declaration has applies.

Recap

  • entity(name, fields) declares an entity's choices once, as JSON, and checks them when built. fixed fields never change.
  • card(Component) gives a component with the entity's scope; useEntity reads every choice that applies, with the fixed parts.
  • card.tone reads the choice in scope; card.tone(of) is a handle, an Id that types intent.decide.
  • applies says when a choice is read, forbid what it rules out, and given what its judge reads.
  • <Signer id> and <signer.Provider id> name an instance; the default relative name is signer.
  • A relative id is a part of the instance around it; an absolute id (/grandma) is that instance wherever it is placed. The Intent retains its decisions when the component is removed.

Last updated on

Edit on GitHub