Reintent

@reintent/lib/entities

Entities and their parts, composed in user space on core's public entry (0047 §1): an entity declared once (core's `entity`), its instances named by a library context, its choices read by family handles (`pair.arrange(of)`).

owner(e, Component)          an entity component that also names its instance for its parts
useOwner(e)                  the nearest instance of owner `e` (its singleton when none)
usePart(e, id)               a part's instance under the nearest owner: `{owner}/{id}`
redeclare(e, fields)         the entity with some of its declarations given here: a pure
                             function of props, for K that varies with the program
choicesOf(decl)              a declared choice's K and default, as `choices` and a value
BranchOn                     core's `<Branch>` on a declared choice: identity, K, question,
                             words and default from the declaration
choiceIn(fields, name)       one choice of an entity's fields (not a fixed part)
kindsOf(decl)                a declared choice's kinds: tag → data
elementsOf(children)         the elements among children, a flat list

A part is named by the owner it belongs to, whatever stands between them: a picture of a slide is gallery/g0 in any row (gallery/g0#fit). The owner names its instance in a context the program provides, which names no read (core's context rule): what is read is still tracked, as any value of the program is. Owners chain through owners of this library (deck/gallery/g0 for a slide owned by a deck); an owner's own reads use core's entity naming, the same name at the top.

New, replacing #356's entity(…, { in }) and useInstance in core, after #312's Page context and family handles.

Functions

BranchOn()

function BranchOn(p): IntentElement;

Defined in: packages/lib/src/entities.ts

Core's <Branch> on a declared choice of an entity: its identity (of, by default the entity's singleton), its K, question, words (about) and default from the declaration. Pattern matching stays core's: λ holds the same branch node, and inside <World each> each case is a path.

Parameters

ParameterType
pobject & Omit<BranchProps<Possibility>, "of" | "name" | "about" | "defaultValue" | "question" | "on" | "cases"> & object

Returns

IntentElement

Example

import { createIntent, entity, host, type HostNode } from '@reintent/core';
import { BranchOn } from '@reintent/lib/entities';

const dream = entity('dream', {
  ending: { kinds: ['waits', 'gone'], default: 'waits', judge: 'Is he still there?' },
});
const program = (
  <BranchOn
    entity={dream}
    field="ending"
    ask={['human']}
    cases={{ waits: host('man'), gone: host('empty') }}
  />
);
const branch = createIntent({ program }).realize().tree as HostNode;
console.log(branch.kind, branch.props.chosen); // → 'branch' 'waits'

choiceIn()

function choiceIn(fields, name): ChoiceDecl | undefined;

Defined in: packages/lib/src/entities.ts

One choice of an entity's fields, by name: undefined for none, or a fixed part.

Parameters

ParameterType
fieldsFields
namestring

Returns

ChoiceDecl | undefined


choicesOf()

function choicesOf(decl): object;

Defined in: packages/lib/src/entities.ts

A declared choice as choices and a default: each kind a possibility (a tag alone, or { kind, ...data }), weighted by its prior (renormalised), else equally. For a declaration with conditions (given, applies, forbid), read its handle with useChoose instead.

Parameters

ParameterType
declChoiceDecl

Returns

object

NameType
defaultValuePossibility | undefined
KChoices<Possibility>

elementsOf()

function elementsOf(children): IntentElement[];

Defined in: packages/lib/src/entities.ts

The elements among children, a flat list (childrenOf, then elements only).

Parameters

ParameterType
childrenChild

Returns

IntentElement[]


kindsOf()

function kindsOf(decl): Record<string, Record<string, unknown>>;

Defined in: packages/lib/src/entities.ts

A declared choice's kinds, tag → data (a kind listed by name has {}).

Parameters

ParameterType
declPick<ChoiceDecl, "kinds">

Returns

Record<string, Record<string, unknown>>

Example

import { kindsOf } from '@reintent/lib/entities';

console.log(kindsOf({ kinds: ['usual', 'big'] })); // → { usual: {}, big: {} }
console.log(kindsOf({ kinds: { usual: { scale: 1 } } })); // → { usual: { scale: 1 } }

owner()

function owner<P>(e, component): Component<P & object>;

Defined in: packages/lib/src/entities.ts

An entity component (core's e(Component)) that also names its instance for its parts: the instance is its id (the entity's name when none), under the nearest owner of this library.

Type Parameters

Type Parameter
P

Parameters

ParameterType
eAnyEntity
componentComponent<P>

Returns

Component<P & object>

Example

import { createIntent, entity, host, useChoose } from '@reintent/core';
import { owner, 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 }),
);
// A row is no entity: whatever stands between, a picture is the slide's.
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>
);
console.log(
  createIntent({ program })
    .realize()
    .reads.map((r) => r.key),
);
// → [ 'gallery#paging', 'gallery/g0#fit' ]

redeclare()

function redeclare<E>(e, fields): E;

Defined in: packages/lib/src/entities.ts

The entity with some of its choices declared here instead: the same name (so the same instances and identities, man#hat), its fields as given. K that varies with the program is a pure function of its props: a component that is given a declaration reads it through this, and an edit of the program (@reintent/lib/program-edit) changes the prop. Checked as entity checks a declaration (every error, thrown), once for each declaration written the same.

Type Parameters

Type Parameter
E extends AnyEntity

Parameters

ParameterType
eE
fieldsFields

Returns

E

Example

import { createIntent, entity, host, useChoose, type ChoiceDecl } from '@reintent/core';
import { redeclare } from '@reintent/lib/entities';

const man = entity('man', { hat: { kinds: ['usual', 'big'], default: 'big' } });
const Man = man((p: { hat?: ChoiceDecl }) => {
  const m = p.hat ? redeclare(man, { hat: p.hat }) : man;
  return host('man', { hat: useChoose(m.hat) });
});

const hats = { kinds: ['usual', 'big', 'towering'], default: 'big' };
const intent = createIntent({ program: <Man hat={hats} /> });
intent.decide({ of: 'man', name: 'hat' }, 'towering');
console.log(intent.realize().reads.map((r) => [r.key, r.value, r.kinds]));
// → [ [ 'man#hat', 'towering', [ 'usual', 'big', 'towering' ] ] ]

useOwner()

function useOwner(e): string;

Defined in: packages/lib/src/entities.ts

The nearest instance of owner e here: its of (gallery), or the entity's name (its singleton) when there is none around.

Parameters

ParameterType
e{ name: string; }
e.namestring

Returns

string


usePart()

function usePart(e, id): string;

Defined in: packages/lib/src/entities.ts

A part's instance: id under the nearest instance of owner e (gallery/g0). Read its choices by the family's handle for it: useChoose(picture.fit(usePart(slide, p.id))).

Parameters

ParameterType
e{ name: string; }
e.namestring
idstring

Returns

string

Last updated on

Edit on GitHub