@reintent/lib/alternatives
The other possibilities a choice could take here, each evaluated: exactly one choice changed, as a preview, and kept only when the world it gives is not the one shown now.
With a host's holds (does the world this preview gives really have that possibility?), every other choice is held where the screen shows it (freeze, by default the values read), and a flip that cannot be had gives way: the choices of the names in relax are let go in turn, until it holds or there is nothing more to let go; it is offered only when it holds. take writes the flip alone, as the person's decision; takeScheme, asked for on purpose, writes the whole scheme (the flip, and the choices it let go at the values its world shows), lists them, and is refused when the values it was computed under moved since.
Classes
StaleScheme
Defined in: packages/lib/src/alternatives.ts
A scheme computed on an evaluation that is not the one now: the choices it held that moved since.
Extends
Error
Constructors
Constructor
new StaleScheme(moved): StaleScheme;Defined in: packages/lib/src/alternatives.ts
Parameters
| Parameter | Type |
|---|---|
moved | readonly string[] |
Returns
Overrides
Error.constructorProperties
Type Aliases
Alternative
type Alternative = object;Defined in: packages/lib/src/alternatives.ts
Another possibility of one choice, and the world it gives.
Properties
| Property | Type | Description |
|---|---|---|
from | unknown | What it is now (with holds: what it is held at). |
held? | Record<string, Decided> | What it was evaluated under: every choice held, by key (only with holds). |
id | ChoiceRead["id"] | The choice's identity. |
key | string | The choice, by key. |
kind | unknown | The possibility it would take. |
name | string | The choice's name. |
relaxed? | string[] | The names that gave way for this flip to hold (only with holds). |
released? | string[] | The choices let go for it, by key (only with holds). |
result | Realization | The world it gives, previewed. |
Holds
type Holds = (result, flip) => boolean;Defined in: packages/lib/src/alternatives.ts
What the host says about a flip: does the world it gives have it?
Parameters
| Parameter | Type |
|---|---|
result | Realization |
flip | { key: string; kind: unknown; released: readonly string[]; } |
flip.key | string |
flip.kind | unknown |
flip.released | readonly string[] |
Returns
boolean
Relaxing
type Relaxing = object;Defined in: packages/lib/src/alternatives.ts
How a host holds the rest while one choice flips, and what gives way when the flip cannot be had.
Properties
| Property | Type | Description |
|---|---|---|
freeze? | Record<string, Decided> | What every other choice is held at, by key (default: the values read). |
holds | Holds | Whether the world a flip gives really has it. |
relax? | readonly string[] | The names that give way, in order, when a flip cannot be had with the rest held. |
Taken
type Taken = object;Defined in: packages/lib/src/alternatives.ts
A scheme taken: each decision written, the flip last.
Properties
Functions
alternatives()
function alternatives(
intent,
now,
provide?,
only?,
relaxing?
): Alternative[];Defined in: packages/lib/src/alternatives.ts
Each other possibility of every open choice read now, one changed at a time, previewed: kept
when the world it gives is not the one shown (only picks the choices). With relaxing, every
other choice is held, a flip gives way by the names in relax, and it is kept only where the
host's holds says the world has it.
Parameters
| Parameter | Type | Default value |
|---|---|---|
intent | Pick<Intent, "preview"> | undefined |
now | Realization | undefined |
provide | Provide | [] |
only | (r) => boolean | ... |
relaxing? | Relaxing | undefined |
Returns
Example
import { choices, createIntent, host, useChoose } from '@reintent/core';
import { alternatives } from '@reintent/lib/alternatives';
function Reply(p: { id: string }) {
const tone = useChoose(choices('formal', 'warm'), {
of: p.id,
name: 'tone',
defaultValue: 'warm',
});
const send = useChoose(choices('now', 'tomorrow'), {
of: p.id,
name: 'send',
defaultValue: 'tomorrow',
});
return host('reply', { tone, send });
}
const intent = createIntent({ program: <Reply id="r1" /> });
// Each other possibility, one choice changed at a time, previewed (nothing is written).
const now = intent.realize();
const others = alternatives(intent, now).map((a) => `${a.key}: ${a.from} → ${a.kind}`);
console.log(others); // → [ 'r1#tone: warm → formal', 'r1#send: tomorrow → now' ]take()
function take(
intent,
alt,
opts?
): void;Defined in: packages/lib/src/alternatives.ts
Takes an alternative's flip as a person's decision, in one commit: the one choice they flipped,
and nothing else. What it let go to hold (released) is not written: the rest stays what the
cascade resolves it to, not a decision nobody made. To take the whole scheme, call takeScheme.
decide writes it (default: tx.decide), for a host that decides along a context's axis.
Parameters
| Parameter | Type |
|---|---|
intent | Pick<Intent, "commit"> |
alt | Alternative |
opts | { decide?: (tx, id, value) => void; note?: string; } |
opts.decide? | (tx, id, value) => void |
opts.note? | string |
Returns
void
Example
import { createIntent, entity, host, useChoose, type HostNode } from '@reintent/core';
import { alternatives, take } from '@reintent/lib/alternatives';
const page = entity('page', {
arrange: { kinds: ['row', 'column'], default: 'row' },
paging: { kinds: ['one', 'pages'], default: 'one' },
});
const Page = page(() =>
host('page', { arrange: useChoose(page.arrange), paging: useChoose(page.paging) }),
);
const intent = createIntent({ program: <Page /> });
const [column] = alternatives(intent, intent.realize(), [], (r) => r.name === 'arrange');
take(intent, column);
console.log(Object.keys(intent.layers.choices)); // → [ 'page#arrange' ]takeScheme()
function takeScheme(
intent,
alt,
now,
opts?
): Taken;Defined in: packages/lib/src/alternatives.ts
Takes an alternative as the whole scheme it was offered as, when a person asks for that and not
only the flip: the flip, and each choice it let go at the value its world shows (seen, by key;
default: what the preview read), as the person's decisions, in one commit. It lists what it
writes (written), so the host can show the person every change they accept. Refused
(StaleScheme) when any choice the scheme held has another value now than it had when the
scheme was computed (alt.held): taking it would decide on values nobody saw. A host that held
the rest where its screen shows them (freeze) says what it holds now (held), compared in place
of the values now read.
Parameters
| Parameter | Type | Description |
|---|---|---|
intent | Pick<Intent, "commit"> | - |
alt | Alternative | - |
now | Realization | - |
opts | { decide?: (tx, id, value) => void; held?: Record<string, unknown>; note?: string; released?: readonly string[]; seen?: Record<string, unknown>; } | - |
opts.decide? | (tx, id, value) => void | - |
opts.held? | Record<string, unknown> | What the host holds every choice at now, by key (its freeze now; default: the values now read). |
opts.note? | string | - |
opts.released? | readonly string[] | Which of the choices let go to write (default: all of them). |
opts.seen? | Record<string, unknown> | - |
Returns
Example
import { createIntent, entity, host, useChoose, type HostNode } from '@reintent/core';
import { alternatives, takeScheme } from '@reintent/lib/alternatives';
const page = entity('page', {
arrange: { kinds: ['row', 'column'], default: 'row' },
paging: { kinds: ['one', 'pages'], default: 'one' },
});
const Page = page(() =>
host('page', { arrange: useChoose(page.arrange), paging: useChoose(page.paging) }),
);
const intent = createIntent({ program: <Page /> });
// The host: a column fits only on pages; paging let go, it lays the column out on pages.
const props = (r: { tree: unknown }) => (r.tree as HostNode).props;
const holds = (r: { tree: unknown }, f: { released: readonly string[] }) =>
props(r).arrange === 'row' ||
props(r).paging === 'pages' ||
f.released.includes('page#paging');
const fits = alternatives(intent, intent.realize(), [], (r) => r.name === 'arrange', {
holds,
relax: ['paging'],
});
console.log(fits.map((a) => [a.key, a.kind, a.relaxed])); // → [ [ 'page#arrange', 'column', [ 'paging' ] ] ]
// The whole scheme, asked for: the column, and the paging its world shows, in one step.
const taken = takeScheme(intent, fits[0], intent.realize(), {
seen: { 'page#paging': 'pages' },
});
console.log(taken.written.map((w) => `${w.key}=${w.value}`)); // → [ 'page#paging=pages', 'page#arrange=column' ]
console.log(props(intent.realize())); // → { arrange: 'column', paging: 'pages' }Last updated on
Edit on GitHub