Entities and their parts
Name a part by the entity it belongs to, read a declared choice as a branch, and read keys back, all in user space.
Sam's slides are made of parts: a row of pictures, a picture, a caption. A decision about a picture is about that picture of that slide, wherever the row it stands in moves. Core declares entities and reads their choices; naming a part by the slide it belongs to, whatever stands between them, is composed here, on core's public entry.
You will learn
- How
ownerandusePartname a part by the entity it belongs to - How
redeclaremakes K a pure function of a component's props - How
BranchOnreads a declared choice as core's<Branch> - How
choicesOf,kindsOf,choiceInandelementsOfread declarations and children as data - How
idOfKeyreads a key back, and whatfingerprintis for
A part of an owner
import { createIntent, entity, host, useChoose } from '@reintent/core';
import { owner, useOwner, usePart } from '@reintent/lib/entities';
const slide = entity('slide', { paging: { kinds: ['one', 'split'], default: 'one' } });
const picture = entity('picture', { fit: { kinds: ['keep', 'fill'], default: 'keep' } });
const Slide = owner(slide, (p: { children?: unknown }) =>
host('slide', { paging: useChoose(slide.paging), children: p.children }),
);
const Row = (p: { children?: unknown }) => host('row', { children: p.children });
const Picture = (p: { id: string }) =>
host('picture', { fit: useChoose(picture.fit(usePart(slide, p.id))) });
const program = (
<Slide id="gallery">
<Row>
<Picture id="g0" />
</Row>
</Slide>
);
createIntent({ program }).realize().reads.map((r) => r.key); // [ 'gallery#paging', 'gallery/g0#fit' ]owner(slide, Component) is core's entity component, slide(Component), which also names its instance for its parts. usePart(slide, id) is a part's instance under the nearest slide, gallery/g0, and the picture reads its fit by the family's handle for it, picture.fit('gallery/g0'). The row is no entity, so it adds nothing to the name: move the picture into another row and its key, and Sam's decision about it, stay. useOwner(slide) is the nearest slide's instance itself, for what a part says about its slide besides a read (what a judge reads, a host node's owner).
The slide names its instance in a context the program gives itself, and such a context names no read (core's rule, Contexts). The parts still follow it: it is part of the program, and a read under it is evaluated again when it changes. Owners chain through owners: a slide inside <Deck id="talk"> made with owner names its pictures talk/gallery/g0.
K from props
Sam drags a picture somewhere none of its places are, and the slide learns the place. The slide's K for that picture grows: a change of the program, not of the choices. The component is given the declaration as a prop, and reads it with redeclare(e, fields), the entity with those choices declared here, under the same name:
import { redeclare } from '@reintent/lib/entities';
const Man = man((p: { hat?: ChoiceDecl }) => {
const m = p.hat ? redeclare(man, { hat: p.hat }) : man;
return host('man', { hat: useChoose(m.hat) });
});
<Man hat={{ kinds: ['usual', 'big', 'towering'], default: 'big' }} />;K is a pure function of the props, and the identity is the entity's, man#hat, so what was decided holds. The prop is changed by an edit of the program (A program System Two wrote), one element at a time: a beat's learned place is that beat's.
A declared choice as a branch
import { BranchOn } from '@reintent/lib/entities';
const dream = entity('dream', {
ending: { kinds: ['waits', 'gone'], default: 'waits', judge: 'Is he still there?' },
});
<BranchOn entity={dream} field="ending" ask={['human']} cases={{ waits: <Man />, gone: null }} />;BranchOn is core's <Branch>, given the choice's identity (dream#ending, or another instance with of), its possibilities, question, words and default from the declaration. Pattern matching stays core's: λ holds the same branch node, and inside <World each> each case is a path of its own. choicesOf(decl) is what it reads: the declaration's kinds as choices, weighted by its prior, and its default. A declaration with conditions (given, applies, forbid) is read by its handle with useChoose instead.
Declarations and children as data
kindsOf(decl) gives a declared choice's kinds, tag to data, and choiceIn(fields, name) one choice of an entity's fields (man.decl), not a fixed part. elementsOf(children) gives the elements among a component's children, as a flat list: what a row holds.
Keys
import { keyOf } from '@reintent/core';
import { fingerprint, idOfKey } from '@reintent/lib/keys';
idOfKey('gallery/g0#fit'); // { of: 'gallery/g0', name: 'fit' }
keyOf(idOfKey('deck#3#title')) === 'deck#3#title'; // true
fingerprint(programText); // a short name for a versionCore's keyOf refuses a name that is empty or has # in it, and an empty entity, so every key it makes splits back at its last #. idOfKey(key) reads it back, and throws for anything that is not such a key: a string with no # never reads as an empty name. fingerprint(value) is a short name for a value, the same for the same data: a name to tell versions apart in a list, never a proof that two values are equal.
Recap
owner(e, Component)names an entity's instance for its parts;usePart(e, id)anduseOwner(e)name a part, and its choices are read by the family's handle.redeclare(e, fields)reads declarations a component is given, so K is a pure function of its props.BranchOnis core's<Branch>on a declared choice;choicesOfgives its K and default.kindsOf,choiceInandelementsOfread declarations and children as data.idOfKeyiskeyOf's inverse;fingerprintnames a value, and proves nothing.
Last updated on
Edit on GitHub