trebol 0.1.0 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +18 -3
- package/dist/cli.js +13790 -11417
- package/dist/fonts/OFL.txt +93 -0
- package/dist/fonts/inter-cyrillic-ext.woff2 +0 -0
- package/dist/fonts/inter-cyrillic.woff2 +0 -0
- package/dist/fonts/inter-greek-ext.woff2 +0 -0
- package/dist/fonts/inter-greek.woff2 +0 -0
- package/dist/fonts/inter-latin-ext.woff2 +0 -0
- package/dist/fonts/inter-latin.woff2 +0 -0
- package/dist/fonts/inter-vietnamese.woff2 +0 -0
- package/dist/lib/core/access.d.ts +10 -0
- package/dist/lib/core/access.js +210 -0
- package/dist/lib/core/base.d.ts +4 -0
- package/dist/lib/core/base.js +50 -0
- package/dist/lib/core/bitset.d.ts +9 -0
- package/dist/lib/core/bitset.js +75 -0
- package/dist/lib/core/boot.d.ts +18 -0
- package/dist/lib/core/boot.js +73 -0
- package/dist/lib/core/compare.d.ts +9 -0
- package/dist/lib/core/compare.js +156 -0
- package/dist/lib/core/ctx.d.ts +36 -0
- package/dist/lib/core/ctx.js +251 -0
- package/dist/lib/core/dec.d.ts +28 -0
- package/dist/lib/core/dec.js +158 -0
- package/dist/lib/core/errors.d.ts +15 -0
- package/dist/lib/core/errors.js +44 -0
- package/dist/lib/core/escapes.d.ts +14 -0
- package/dist/lib/core/escapes.js +80 -0
- package/dist/lib/core/eval.d.ts +220 -0
- package/dist/lib/core/eval.js +666 -0
- package/dist/lib/core/func.d.ts +68 -0
- package/dist/lib/core/func.js +129 -0
- package/dist/lib/core/hash.d.ts +5 -0
- package/dist/lib/core/hash.js +155 -0
- package/dist/lib/core/index.d.ts +18 -0
- package/dist/lib/core/index.js +20 -0
- package/dist/lib/core/int64.d.ts +22 -0
- package/dist/lib/core/int64.js +113 -0
- package/dist/lib/core/kinds.d.ts +67 -0
- package/dist/lib/core/kinds.js +134 -0
- package/dist/lib/core/lexer.d.ts +13 -0
- package/dist/lib/core/lexer.js +702 -0
- package/dist/lib/core/mezz.d.ts +1 -0
- package/dist/lib/core/mezz.js +360 -0
- package/dist/lib/core/mold.d.ts +20 -0
- package/dist/lib/core/mold.js +434 -0
- package/dist/lib/core/natives/control.d.ts +1 -0
- package/dist/lib/core/natives/control.js +374 -0
- package/dist/lib/core/natives/csv.d.ts +15 -0
- package/dist/lib/core/natives/csv.js +315 -0
- package/dist/lib/core/natives/data.d.ts +11 -0
- package/dist/lib/core/natives/data.js +367 -0
- package/dist/lib/core/natives/define.d.ts +17 -0
- package/dist/lib/core/natives/define.js +47 -0
- package/dist/lib/core/natives/encode.d.ts +3 -0
- package/dist/lib/core/natives/encode.js +187 -0
- package/dist/lib/core/natives/json.d.ts +5 -0
- package/dist/lib/core/natives/json.js +273 -0
- package/dist/lib/core/natives/math.d.ts +3 -0
- package/dist/lib/core/natives/math.js +500 -0
- package/dist/lib/core/natives/parse.d.ts +1 -0
- package/dist/lib/core/natives/parse.js +600 -0
- package/dist/lib/core/natives/series.d.ts +4 -0
- package/dist/lib/core/natives/series.js +519 -0
- package/dist/lib/core/natives/text.d.ts +1 -0
- package/dist/lib/core/natives/text.js +487 -0
- package/dist/lib/core/natives/xml.d.ts +9 -0
- package/dist/lib/core/natives/xml.js +242 -0
- package/dist/lib/core/series.d.ts +26 -0
- package/dist/lib/core/series.js +73 -0
- package/dist/lib/core/symbols.d.ts +9 -0
- package/dist/lib/core/symbols.js +44 -0
- package/dist/lib/core/values.d.ts +206 -0
- package/dist/lib/core/values.js +223 -0
- package/dist/lib/host/browser.d.ts +4 -0
- package/dist/lib/host/browser.js +4974 -0
- package/dist/lib/host/chart.d.ts +26 -0
- package/dist/lib/host/chart.js +158 -0
- package/dist/lib/host/compress.d.ts +1 -0
- package/dist/lib/host/compress.js +37 -0
- package/dist/lib/host/devlog.d.ts +11 -0
- package/dist/lib/host/devlog.js +13 -0
- package/dist/lib/host/devtools.d.ts +23 -0
- package/dist/lib/host/devtools.js +626 -0
- package/dist/lib/host/draw.d.ts +31 -0
- package/dist/lib/host/draw.js +348 -0
- package/dist/lib/host/fonts/OFL.txt +93 -0
- package/dist/lib/host/fonts/inter-cyrillic-ext.woff2 +0 -0
- package/dist/lib/host/fonts/inter-cyrillic.woff2 +0 -0
- package/dist/lib/host/fonts/inter-greek-ext.woff2 +0 -0
- package/dist/lib/host/fonts/inter-greek.woff2 +0 -0
- package/dist/lib/host/fonts/inter-latin-ext.woff2 +0 -0
- package/dist/lib/host/fonts/inter-latin.woff2 +0 -0
- package/dist/lib/host/fonts/inter-vietnamese.woff2 +0 -0
- package/dist/lib/host/fonts.d.ts +11 -0
- package/dist/lib/host/fonts.js +29 -0
- package/dist/lib/host/icons.d.ts +3 -0
- package/dist/lib/host/icons.js +97 -0
- package/dist/lib/host/index.d.ts +40 -0
- package/dist/lib/host/index.js +192 -0
- package/dist/lib/host/js.d.ts +5 -0
- package/dist/lib/host/js.js +130 -0
- package/dist/lib/host/locale.d.ts +40 -0
- package/dist/lib/host/locale.js +241 -0
- package/dist/lib/host/markdown.d.ts +30 -0
- package/dist/lib/host/markdown.js +161 -0
- package/dist/lib/host/modules.d.ts +14 -0
- package/dist/lib/host/modules.js +300 -0
- package/dist/lib/host/natives.d.ts +6 -0
- package/dist/lib/host/natives.js +98 -0
- package/dist/lib/host/net.d.ts +12 -0
- package/dist/lib/host/net.js +549 -0
- package/dist/lib/host/orbit.d.ts +121 -0
- package/dist/lib/host/orbit.js +374 -0
- package/dist/lib/host/page.d.ts +9 -0
- package/dist/lib/host/page.js +55 -0
- package/dist/lib/host/rows.d.ts +29 -0
- package/dist/lib/host/rows.js +109 -0
- package/dist/lib/host/scheduler.d.ts +51 -0
- package/dist/lib/host/scheduler.js +197 -0
- package/dist/lib/host/theme.d.ts +58 -0
- package/dist/lib/host/theme.js +1667 -0
- package/dist/lib/ui/i18n.trb +117 -0
- package/dist/lib/ui/test.trb +565 -0
- package/dist/lib/ui/ui.trb +1678 -0
- package/dist/trebol.js +1427 -138
- package/editors/vscode/README.md +15 -0
- package/editors/vscode/language-configuration.json +28 -0
- package/editors/vscode/package.json +42 -0
- package/editors/vscode/syntaxes/trebol.tmLanguage.json +237 -0
- package/package.json +23 -7
- package/src/core/boot.ts +1 -1
- package/src/core/func.ts +4 -0
- package/src/core/natives/control.ts +1 -1
- package/src/core/natives/series.ts +9 -1
- package/src/core/natives/text.ts +2 -0
- package/src/host/browser.ts +1235 -83
- package/src/host/devlog.ts +16 -0
- package/src/host/devtools.ts +641 -0
- package/src/host/fonts/OFL.txt +93 -0
- package/src/host/fonts/inter-cyrillic-ext.woff2 +0 -0
- package/src/host/fonts/inter-cyrillic.woff2 +0 -0
- package/src/host/fonts/inter-greek-ext.woff2 +0 -0
- package/src/host/fonts/inter-greek.woff2 +0 -0
- package/src/host/fonts/inter-latin-ext.woff2 +0 -0
- package/src/host/fonts/inter-latin.woff2 +0 -0
- package/src/host/fonts/inter-vietnamese.woff2 +0 -0
- package/src/host/fonts.ts +39 -0
- package/src/host/index.ts +5 -3
- package/src/host/js.ts +127 -0
- package/src/host/markdown.ts +9 -0
- package/src/host/modules.ts +41 -2
- package/src/host/net.ts +9 -1
- package/src/host/orbit.ts +432 -0
- package/src/host/rows.ts +30 -0
- package/src/host/scheduler.ts +62 -36
- package/src/host/theme.ts +270 -6
- package/src/ui/i18n.trb +10 -0
- package/src/ui/test.trb +565 -0
- package/src/ui/ui.trb +564 -90
|
@@ -0,0 +1,374 @@
|
|
|
1
|
+
// An orbit: one node of a graph in the centre and its neighbours on rings around it.
|
|
2
|
+
//
|
|
3
|
+
// orbit "Organization" [
|
|
4
|
+
// focus pleno
|
|
5
|
+
// nodes [pleno "Pleno municipal" [primary count 9] marta "Marta Ibáñez" [accent]]
|
|
6
|
+
// edges [pleno marta "Members" marta pleno "Holder" [dashed]]
|
|
7
|
+
// trail [ayuntamiento organos]
|
|
8
|
+
// ]
|
|
9
|
+
//
|
|
10
|
+
// The person moves through the graph one node at a time; the app gives the
|
|
11
|
+
// node in the centre and what is around it each time. This module reads the
|
|
12
|
+
// block, places the nodes and builds each bubble as plain elements in unit
|
|
13
|
+
// space (radius 1), scaled by its radius; the browser host animates them.
|
|
14
|
+
import { K, I64, spell, text } from "../core/index.js";
|
|
15
|
+
import { svgEl, drawColor } from "./draw.js";
|
|
16
|
+
const HINT_WORDS = ['many', 'dashed', 'count', 'steps', 'at'];
|
|
17
|
+
const EDGE_LOOKS = ['solid', 'dashed', 'faint'];
|
|
18
|
+
/** What tells nodes apart: an id is a word, an integer, a string or an issue. */
|
|
19
|
+
export function idKey(c) {
|
|
20
|
+
switch (c.t) {
|
|
21
|
+
case K.Word:
|
|
22
|
+
case K.LitWord: return `w:${spell(c.sym)}`;
|
|
23
|
+
case K.Integer: return `i:${I64.toString(c.v)}`;
|
|
24
|
+
case K.String: return `s:${text(c)}`;
|
|
25
|
+
case K.Issue: return `#:${spell(c.sym)}`;
|
|
26
|
+
default: return null;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
const isText = (c) => c !== undefined && (c.t === K.String || c.t === K.Map);
|
|
30
|
+
const wordOf = (c) => (c !== undefined && c.t === K.Word ? spell(c.sym) : null);
|
|
31
|
+
const intOf = (c) => (c !== undefined && c.t === K.Integer ? I64.toNumber(c.v) : null);
|
|
32
|
+
const cellsOf = (b) => b.s.data.slice(b.i);
|
|
33
|
+
class OrbitError extends Error {
|
|
34
|
+
}
|
|
35
|
+
/** The orbit a block describes, and what was wrong with it; what is wrong is left out. */
|
|
36
|
+
export function readOrbit(data) {
|
|
37
|
+
const spec = { focus: null, nodes: new Map(), edges: [], trail: [] };
|
|
38
|
+
if (data.t !== K.Block)
|
|
39
|
+
return { spec, error: data.t === K.None ? null : 'orbit: an orbit takes a block' };
|
|
40
|
+
try {
|
|
41
|
+
const d = cellsOf(data);
|
|
42
|
+
for (let i = 0; i < d.length; i++) {
|
|
43
|
+
const w = wordOf(d[i]);
|
|
44
|
+
const arg = d[++i];
|
|
45
|
+
if (w === 'focus') {
|
|
46
|
+
const k = arg === undefined ? null : idKey(arg);
|
|
47
|
+
if (k === null)
|
|
48
|
+
throw new OrbitError('focus takes the id of a node');
|
|
49
|
+
spec.focus = k;
|
|
50
|
+
}
|
|
51
|
+
else if (w === 'nodes' && arg?.t === K.Block)
|
|
52
|
+
readNodes(cellsOf(arg), spec);
|
|
53
|
+
else if (w === 'edges' && arg?.t === K.Block)
|
|
54
|
+
readEdges(cellsOf(arg), spec);
|
|
55
|
+
else if (w === 'trail' && arg?.t === K.Block) {
|
|
56
|
+
for (const c of cellsOf(arg)) {
|
|
57
|
+
const k = idKey(c);
|
|
58
|
+
if (k === null)
|
|
59
|
+
throw new OrbitError('trail takes the ids of nodes');
|
|
60
|
+
spec.trail.push(k);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
else
|
|
64
|
+
throw new OrbitError('an orbit takes focus id, nodes [...], edges [...] and trail [...]');
|
|
65
|
+
}
|
|
66
|
+
return { spec, error: null };
|
|
67
|
+
}
|
|
68
|
+
catch (e) {
|
|
69
|
+
if (e instanceof OrbitError)
|
|
70
|
+
return { spec, error: `orbit: ${e.message}` };
|
|
71
|
+
throw e;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
function readNodes(d, spec) {
|
|
75
|
+
for (let i = 0; i < d.length; i++) {
|
|
76
|
+
const id = d[i];
|
|
77
|
+
const key = idKey(id);
|
|
78
|
+
if (key === null || !isText(d[i + 1]))
|
|
79
|
+
throw new OrbitError('nodes takes an id and a title for each, then a block of hints if it has any');
|
|
80
|
+
const node = { key, id, title: d[++i], color: 'var(--tb-primary)', count: null, many: false, dashed: false, steps: 0, at: 0 };
|
|
81
|
+
if (d[i + 1]?.t === K.Block)
|
|
82
|
+
readHints(cellsOf(d[++i]), node);
|
|
83
|
+
spec.nodes.set(key, node);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
function readHints(h, node) {
|
|
87
|
+
for (let i = 0; i < h.length; i++) {
|
|
88
|
+
const w = wordOf(h[i]);
|
|
89
|
+
if (w === 'many')
|
|
90
|
+
node.many = true;
|
|
91
|
+
else if (w === 'dashed')
|
|
92
|
+
node.dashed = true;
|
|
93
|
+
else if (w === 'count') {
|
|
94
|
+
const n = intOf(h[++i]);
|
|
95
|
+
if (n === null)
|
|
96
|
+
throw new OrbitError('count takes an integer');
|
|
97
|
+
node.count = n;
|
|
98
|
+
}
|
|
99
|
+
else if (w === 'steps') {
|
|
100
|
+
const n = intOf(h[++i]);
|
|
101
|
+
const at = wordOf(h[i + 1]) === 'at' ? intOf(h[i += 2]) : 0;
|
|
102
|
+
if (n === null || n < 1 || at === null)
|
|
103
|
+
throw new OrbitError('steps takes how many, then at and the current one: steps 5 at 2');
|
|
104
|
+
node.steps = n;
|
|
105
|
+
node.at = Math.max(0, Math.min(n, at));
|
|
106
|
+
}
|
|
107
|
+
else {
|
|
108
|
+
const c = w === null || HINT_WORDS.includes(w) ? undefined : drawColor(w);
|
|
109
|
+
if (c === undefined || c === null)
|
|
110
|
+
throw new OrbitError(`a node's hints are a role or color, many, dashed, count n and steps n at k, not ${w ?? 'that'}`);
|
|
111
|
+
node.color = c;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
function readEdges(d, spec) {
|
|
116
|
+
for (let i = 0; i < d.length; i++) {
|
|
117
|
+
const from = idKey(d[i]);
|
|
118
|
+
const to = d[i + 1] === undefined ? null : idKey(d[++i]);
|
|
119
|
+
if (from === null || to === null)
|
|
120
|
+
throw new OrbitError('edges takes two ids for each, then a label and a block with solid, dashed or faint if it has them');
|
|
121
|
+
const label = isText(d[i + 1]) ? d[++i] : null;
|
|
122
|
+
let look = 'solid';
|
|
123
|
+
if (d[i + 1]?.t === K.Block) {
|
|
124
|
+
const b = cellsOf(d[++i]);
|
|
125
|
+
const w = wordOf(b[0]);
|
|
126
|
+
if (b.length !== 1 || w === null || !EDGE_LOOKS.includes(w))
|
|
127
|
+
throw new OrbitError('an edge looks solid, dashed or faint');
|
|
128
|
+
look = w;
|
|
129
|
+
}
|
|
130
|
+
spec.edges.push({ from, to, label, look });
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
export const FOCUS_R = 92;
|
|
134
|
+
/** Neighbours on show at once; past that, a "+N more" bubble pages through them. */
|
|
135
|
+
export const PAGE = 30;
|
|
136
|
+
/** Past this many neighbours, edge labels show only on hover. */
|
|
137
|
+
export const MANY = 18;
|
|
138
|
+
const TRAIL_SHOWN = 4;
|
|
139
|
+
/** The key of the "+N more" bubble: no id gives it. */
|
|
140
|
+
export const MORE = '\u0000more';
|
|
141
|
+
/** The neighbours of the focus: every node given that is neither it nor in the trail, in the order given. */
|
|
142
|
+
export function neighbours(spec) {
|
|
143
|
+
const back = new Set(spec.trail);
|
|
144
|
+
return [...spec.nodes.keys()].filter(k => k !== spec.focus && !back.has(k));
|
|
145
|
+
}
|
|
146
|
+
/** Places the focus, its neighbours on rings (the given page of them) and the last few of the trail. */
|
|
147
|
+
export function layoutOrbit(spec, page = 0) {
|
|
148
|
+
const placed = [];
|
|
149
|
+
if (spec.focus === null)
|
|
150
|
+
return { placed, box: { x0: -FOCUS_R, y0: -FOCUS_R, x1: FOCUS_R, y1: FOCUS_R }, left: 0, pages: 1 };
|
|
151
|
+
placed.push({ key: spec.focus, role: 'focus', x: 0, y: 0, r: FOCUS_R, o: 1 });
|
|
152
|
+
let near = neighbours(spec);
|
|
153
|
+
const pages = Math.max(1, Math.ceil(near.length / PAGE));
|
|
154
|
+
let left = 0;
|
|
155
|
+
if (near.length > PAGE) {
|
|
156
|
+
const start = (page % pages) * PAGE;
|
|
157
|
+
left = near.length - Math.min(near.length, start + PAGE);
|
|
158
|
+
near = near.slice(start, start + PAGE);
|
|
159
|
+
}
|
|
160
|
+
const count = near.length + (pages > 1 ? 1 : 0);
|
|
161
|
+
// fewer neighbours, bigger bubbles
|
|
162
|
+
const r = Math.max(24, Math.min(62, 66 - count * 1.6));
|
|
163
|
+
const slots = [];
|
|
164
|
+
let R = FOCUS_R + r + 120;
|
|
165
|
+
let outer = FOCUS_R;
|
|
166
|
+
for (let i = 0, ring = 0; i < count; ring++) {
|
|
167
|
+
const fits = Math.max(6, Math.floor((2 * Math.PI * R) / (2 * r + 30)));
|
|
168
|
+
const here = Math.min(fits, count - i);
|
|
169
|
+
for (let k = 0; k < here; k++) {
|
|
170
|
+
// every other ring turns half a step, so bubbles do not line up behind each other
|
|
171
|
+
const a = -Math.PI / 2 + (2 * Math.PI * k) / here + (ring % 2 === 1 ? Math.PI / here : 0);
|
|
172
|
+
slots.push({ x: Math.cos(a) * R, y: Math.sin(a) * R });
|
|
173
|
+
}
|
|
174
|
+
i += here;
|
|
175
|
+
outer = R + r;
|
|
176
|
+
R += 2 * r + 46;
|
|
177
|
+
}
|
|
178
|
+
near.forEach((key, j) => placed.push({ key, role: 'near', ...slots[j], r, o: 1 }));
|
|
179
|
+
if (pages > 1)
|
|
180
|
+
placed.push({ key: MORE, role: 'more', ...slots[near.length], r, o: 1 });
|
|
181
|
+
// the trail goes up and to the left, the most recent nearest, each smaller and fainter
|
|
182
|
+
const back = spec.trail.filter(k => k !== spec.focus && spec.nodes.has(k)).slice(-TRAIL_SHOWN).reverse();
|
|
183
|
+
const dir = -Math.PI * 0.9;
|
|
184
|
+
const base = outer + 150;
|
|
185
|
+
back.forEach((key, j) => {
|
|
186
|
+
const d = base + j * 120;
|
|
187
|
+
placed.push({ key, role: 'trail', x: Math.cos(dir) * d, y: Math.sin(dir) * d, r: 46 - j * 7, o: 0.8 - j * 0.17 });
|
|
188
|
+
});
|
|
189
|
+
// the camera fits every bubble, with room for the titles beside the small ones
|
|
190
|
+
const box = { x0: Infinity, y0: Infinity, x1: -Infinity, y1: -Infinity };
|
|
191
|
+
for (const p of placed) {
|
|
192
|
+
const w = p.r * 1.5 + 40;
|
|
193
|
+
const h = p.r * 1.5 + 24;
|
|
194
|
+
box.x0 = Math.min(box.x0, p.x - w);
|
|
195
|
+
box.x1 = Math.max(box.x1, p.x + w);
|
|
196
|
+
box.y0 = Math.min(box.y0, p.y - h);
|
|
197
|
+
box.y1 = Math.max(box.y1, p.y + h);
|
|
198
|
+
}
|
|
199
|
+
return { placed, box, left, pages };
|
|
200
|
+
}
|
|
201
|
+
/** The edges to draw between bubbles on show: those given between the focus and its neighbours, and the trail's dotted line. */
|
|
202
|
+
export function shownEdges(spec, placed) {
|
|
203
|
+
const role = new Map(placed.map(p => [p.key, p.role]));
|
|
204
|
+
const out = [];
|
|
205
|
+
const seen = new Set();
|
|
206
|
+
for (const e of spec.edges) {
|
|
207
|
+
const a = role.get(e.from);
|
|
208
|
+
const b = role.get(e.to);
|
|
209
|
+
if ((a !== 'focus' && a !== 'near') || (b !== 'focus' && b !== 'near') || e.from === e.to)
|
|
210
|
+
continue;
|
|
211
|
+
// one line per pair: the first edge given between them says how it looks
|
|
212
|
+
const pair = [e.from, e.to].sort().join('\u0001');
|
|
213
|
+
if (seen.has(pair))
|
|
214
|
+
continue;
|
|
215
|
+
seen.add(pair);
|
|
216
|
+
// between two neighbours the line bends outwards, so it does not cross the focus
|
|
217
|
+
out.push({ ...e, curve: a === 'near' && b === 'near' ? 0.35 : 0, trail: false });
|
|
218
|
+
}
|
|
219
|
+
if (role.has(MORE))
|
|
220
|
+
out.push({ from: spec.focus, to: MORE, label: null, look: 'faint', curve: 0, trail: false });
|
|
221
|
+
let prev = spec.focus;
|
|
222
|
+
for (const p of placed) {
|
|
223
|
+
if (p.role !== 'trail')
|
|
224
|
+
continue;
|
|
225
|
+
out.push({ from: prev, to: p.key, label: null, look: 'faint', curve: 0, trail: true });
|
|
226
|
+
prev = p.key;
|
|
227
|
+
}
|
|
228
|
+
return out;
|
|
229
|
+
}
|
|
230
|
+
/** The label of the edge between the focus and a neighbour, either way round; null if none. */
|
|
231
|
+
export function edgeLabel(spec, key) {
|
|
232
|
+
for (const e of spec.edges) {
|
|
233
|
+
if (e.label !== null && ((e.from === spec.focus && e.to === key) || (e.to === spec.focus && e.from === key)))
|
|
234
|
+
return e.label;
|
|
235
|
+
}
|
|
236
|
+
return null;
|
|
237
|
+
}
|
|
238
|
+
// --- a bubble ---------------------------------------------------------------------------------
|
|
239
|
+
/** A title in lines of at most `max` characters, three lines at most. */
|
|
240
|
+
export function wrapTitle(title, max) {
|
|
241
|
+
const lines = [];
|
|
242
|
+
let line = '';
|
|
243
|
+
for (const w of title.split(/\s+/).filter(w => w !== '')) {
|
|
244
|
+
if (line !== '' && [...`${line} ${w}`].length > max) {
|
|
245
|
+
lines.push(line);
|
|
246
|
+
line = w;
|
|
247
|
+
}
|
|
248
|
+
else
|
|
249
|
+
line = line === '' ? w : `${line} ${w}`;
|
|
250
|
+
}
|
|
251
|
+
if (line !== '')
|
|
252
|
+
lines.push(line);
|
|
253
|
+
if (lines.length > 3) {
|
|
254
|
+
lines.length = 3;
|
|
255
|
+
lines[2] = `${[...lines[2]].slice(0, Math.max(1, max - 1)).join('')}…`;
|
|
256
|
+
}
|
|
257
|
+
return lines;
|
|
258
|
+
}
|
|
259
|
+
/** An arc of a circle of radius r from angle a0 to a1, as path data. */
|
|
260
|
+
export function arcPath(r, a0, a1) {
|
|
261
|
+
const p = (a) => `${round(Math.cos(a) * r)},${round(Math.sin(a) * r)}`;
|
|
262
|
+
return `M${p(a0)} A${r},${r} 0 ${a1 - a0 > Math.PI ? 1 : 0} 1 ${p(a1)}`;
|
|
263
|
+
}
|
|
264
|
+
const round = (n) => String(Math.round(n * 1000) / 1000);
|
|
265
|
+
/** What a bubble draws, in unit space: halo, body, its rings, moons, title and count. */
|
|
266
|
+
export function bubble(b) {
|
|
267
|
+
const n = b.node;
|
|
268
|
+
const c = b.color;
|
|
269
|
+
const focus = b.role === 'focus';
|
|
270
|
+
const out = [
|
|
271
|
+
svgEl('circle', { class: 'tb-orbit-halo', r: 1.45, fill: `url(#${b.halo})` }),
|
|
272
|
+
svgEl('circle', {
|
|
273
|
+
class: 'tb-orbit-body', r: 1, fill: `url(#${b.body})`, stroke: c, 'stroke-width': focus ? 2 : 1.4,
|
|
274
|
+
...(n?.dashed === true ? { 'stroke-dasharray': '4 4' } : {}),
|
|
275
|
+
...(b.role === 'trail' ? { 'stroke-opacity': 0.6 } : {}),
|
|
276
|
+
}),
|
|
277
|
+
];
|
|
278
|
+
// a node that stands for many wears a second ring
|
|
279
|
+
if (n?.many === true)
|
|
280
|
+
out.push(svgEl('circle', { class: 'tb-orbit-line', r: 1.09, stroke: c, 'stroke-opacity': 0.55 }));
|
|
281
|
+
// steps: those done filled, the current one thicker and pulsing, those to come faint
|
|
282
|
+
if (n !== null && n.steps > 0) {
|
|
283
|
+
const gap = n.steps > 1 ? 0.09 : 0;
|
|
284
|
+
for (let j = 0; j < n.steps; j++) {
|
|
285
|
+
const a0 = -Math.PI / 2 + (2 * Math.PI * j) / n.steps + gap / 2;
|
|
286
|
+
const a1 = a0 + (2 * Math.PI) / n.steps - gap;
|
|
287
|
+
const now = j + 1 === n.at;
|
|
288
|
+
out.push(svgEl('path', {
|
|
289
|
+
class: now ? 'tb-orbit-step tb-orbit-now' : 'tb-orbit-step', d: arcPath(1.16, a0, n.steps > 1 ? a1 : a0 + 2 * Math.PI - 0.001), stroke: c,
|
|
290
|
+
'stroke-opacity': j + 1 < n.at ? 0.9 : now ? 1 : 0.18, 'stroke-width': now ? 4 : 3,
|
|
291
|
+
}));
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
// moons: one per thing it holds, up to 14, turning slowly
|
|
295
|
+
const count = n?.count ?? 0;
|
|
296
|
+
if (count > 0 && !focus && b.role !== 'more') {
|
|
297
|
+
const k = Math.min(count, 14);
|
|
298
|
+
const moons = [];
|
|
299
|
+
for (let j = 0; j < k; j++) {
|
|
300
|
+
const a = (2 * Math.PI * j) / k;
|
|
301
|
+
moons.push(svgEl('circle', { cx: Math.cos(a) * 1.24, cy: Math.sin(a) * 1.24, r: 0.05, fill: c, 'fill-opacity': 0.8 }));
|
|
302
|
+
}
|
|
303
|
+
out.push(svgEl('g', { class: 'tb-orbit-spin tb-orbit-fast' }, moons));
|
|
304
|
+
}
|
|
305
|
+
// titles inside big bubbles, under small ones, never smaller on screen than the page's small text
|
|
306
|
+
const px = 1 / (b.r * b.scale);
|
|
307
|
+
const inside = focus || (b.r >= 44 && 12 * px <= 0.26);
|
|
308
|
+
const fs = focus ? Math.max(0.2, 14 * px) : inside ? Math.max(0.24, 12 * px) : 12.5 * px;
|
|
309
|
+
const lines = wrapTitle(b.title, focus ? Math.max(8, Math.floor(3.2 / fs)) : inside ? 13 : 18);
|
|
310
|
+
const top = inside ? -((lines.length - 1) * fs * 1.15) / 2 : b.above ? -1.34 - fs * 0.6 - (lines.length - 1) * fs * 1.15 : 1.34 + fs * 0.6;
|
|
311
|
+
out.push(svgEl('text', { class: 'tb-orbit-title', 'font-size': fs, 'font-weight': focus ? 600 : 500 }, lines.map((l, j) => svgEl('tspan', { x: 0, y: top + j * fs * 1.15 }, [], l))));
|
|
312
|
+
if (count > 0 && inside) {
|
|
313
|
+
const y = focus ? top - fs * 1.2 : top + (lines.length - 1) * fs * 1.15 + 0.3;
|
|
314
|
+
out.push(svgEl('text', { class: 'tb-orbit-count', 'font-size': focus ? Math.max(0.12, 10 * px) : Math.max(0.16, 10 * px), y }, [], String(count)));
|
|
315
|
+
}
|
|
316
|
+
// the focus has two rings turning slowly, one each way
|
|
317
|
+
if (focus) {
|
|
318
|
+
out.push(svgEl('g', { class: 'tb-orbit-spin' }, [svgEl('circle', { class: 'tb-orbit-line', r: 1.24, stroke: c, 'stroke-opacity': 0.35, 'stroke-dasharray': '2 10' })]));
|
|
319
|
+
out.push(svgEl('g', { class: 'tb-orbit-spin tb-orbit-rev' }, [svgEl('circle', { class: 'tb-orbit-line', r: 1.42, stroke: c, 'stroke-opacity': 0.18, 'stroke-dasharray': '40 14 4 14' })]));
|
|
320
|
+
}
|
|
321
|
+
return out;
|
|
322
|
+
}
|
|
323
|
+
/** The two gradients of a color: a body lighter in the middle and a soft halo. */
|
|
324
|
+
export function gradients(color, body, halo) {
|
|
325
|
+
const stop = (offset, opacity) => svgEl('stop', { offset, 'stop-color': color, 'stop-opacity': opacity });
|
|
326
|
+
return [
|
|
327
|
+
svgEl('radialGradient', { id: body, cx: '50%', cy: '40%', r: '60%' }, [stop('0%', 0.05), stop('75%', 0.16), stop('100%', 0.38)]),
|
|
328
|
+
svgEl('radialGradient', { id: halo }, [stop('55%', 0), stop('72%', 0.22), stop('100%', 0)]),
|
|
329
|
+
];
|
|
330
|
+
}
|
|
331
|
+
/** Where an edge's line goes, from bubble a to bubble b, cut at their borders; and the middle and angle for its label. */
|
|
332
|
+
export function edgePath(a, b, curve) {
|
|
333
|
+
const dx = b.x - a.x;
|
|
334
|
+
const dy = b.y - a.y;
|
|
335
|
+
const len = Math.hypot(dx, dy) || 1;
|
|
336
|
+
const ux = dx / len;
|
|
337
|
+
const uy = dy / len;
|
|
338
|
+
const x1 = a.x + ux * a.r * 1.04;
|
|
339
|
+
const y1 = a.y + uy * a.r * 1.04;
|
|
340
|
+
const x2 = b.x - ux * b.r * 1.04;
|
|
341
|
+
const y2 = b.y - uy * b.r * 1.04;
|
|
342
|
+
let mx = (x1 + x2) / 2;
|
|
343
|
+
let my = (y1 + y2) / 2;
|
|
344
|
+
let d = `M${round(x1)},${round(y1)} L${round(x2)},${round(y2)}`;
|
|
345
|
+
if (curve !== 0) {
|
|
346
|
+
const l = Math.hypot(x2 - x1, y2 - y1) || 1;
|
|
347
|
+
let nx = -(y2 - y1) / l;
|
|
348
|
+
let ny = (x2 - x1) / l;
|
|
349
|
+
// bend away from the centre
|
|
350
|
+
if (nx * mx + ny * my < 0) {
|
|
351
|
+
nx = -nx;
|
|
352
|
+
ny = -ny;
|
|
353
|
+
}
|
|
354
|
+
const cx = mx + nx * l * curve;
|
|
355
|
+
const cy = my + ny * l * curve;
|
|
356
|
+
d = `M${round(x1)},${round(y1)} Q${round(cx)},${round(cy)} ${round(x2)},${round(y2)}`;
|
|
357
|
+
mx = (x1 + 2 * cx + x2) / 4;
|
|
358
|
+
my = (y1 + 2 * cy + y2) / 4;
|
|
359
|
+
}
|
|
360
|
+
// labels never read upside down
|
|
361
|
+
let angle = (Math.atan2(y2 - y1, x2 - x1) * 180) / Math.PI;
|
|
362
|
+
if (angle > 90)
|
|
363
|
+
angle -= 180;
|
|
364
|
+
if (angle < -90)
|
|
365
|
+
angle += 180;
|
|
366
|
+
return { d, x: mx, y: my, angle };
|
|
367
|
+
}
|
|
368
|
+
/** The ease of a move: slow at both ends. */
|
|
369
|
+
export const ease = (k) => (k < 0.5 ? 4 * k * k * k : 1 - Math.pow(-2 * k + 2, 3) / 2);
|
|
370
|
+
/** Where the camera goes to show the whole box in a view of w by h: centred on it, zoomed to fit, within limits. */
|
|
371
|
+
export function fitCamera(w, h, box) {
|
|
372
|
+
const s = Math.max(0.15, Math.min(1.25, w / (box.x1 - box.x0), h / (box.y1 - box.y0)));
|
|
373
|
+
return { x: -((box.x0 + box.x1) / 2) * s, y: -((box.y0 + box.y1) / 2) * s, s };
|
|
374
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/** Where the page natives reach the browser. Tests and the console point them elsewhere. */
|
|
2
|
+
export declare const page: {
|
|
3
|
+
/** Opens an address in a new tab; false when this host cannot. */
|
|
4
|
+
open: (url: string) => boolean;
|
|
5
|
+
/** Puts text on the clipboard; null when this host has no clipboard. */
|
|
6
|
+
writeClipboard: (s: string) => Promise<boolean> | null;
|
|
7
|
+
/** The text on the clipboard, or null when there is none or the user refused; null at once when this host has no clipboard. */
|
|
8
|
+
readClipboard: () => Promise<string | null> | null;
|
|
9
|
+
};
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
// What a program asks of the browser around the page: open another address in
|
|
2
|
+
// a new tab, and put text on the clipboard or take it from there, as R3's
|
|
3
|
+
// browse and Red's write-clipboard / read-clipboard.
|
|
4
|
+
//
|
|
5
|
+
// They go through `page`, so the console can open the system's browser and
|
|
6
|
+
// the tests can watch what was asked; a host without them gives an access error.
|
|
7
|
+
import { NONE, UNSET, native, value, string, text, logic, throwError } from "../core/index.js";
|
|
8
|
+
const nav = () => globalThis.navigator;
|
|
9
|
+
/** Where the page natives reach the browser. Tests and the console point them elsewhere. */
|
|
10
|
+
export const page = {
|
|
11
|
+
/** Opens an address in a new tab; false when this host cannot. */
|
|
12
|
+
open: (url) => {
|
|
13
|
+
const win = globalThis.window;
|
|
14
|
+
if (win === undefined)
|
|
15
|
+
return false;
|
|
16
|
+
// with noopener window.open gives null, so the new tab is cut off by hand
|
|
17
|
+
const tab = win.open(url, '_blank');
|
|
18
|
+
if (tab !== null)
|
|
19
|
+
tab.opener = null;
|
|
20
|
+
return true;
|
|
21
|
+
},
|
|
22
|
+
/** Puts text on the clipboard; null when this host has no clipboard. */
|
|
23
|
+
writeClipboard: (s) => {
|
|
24
|
+
const c = nav()?.clipboard;
|
|
25
|
+
return c === undefined ? null : c.writeText(s).then(() => true, () => false);
|
|
26
|
+
},
|
|
27
|
+
/** The text on the clipboard, or null when there is none or the user refused; null at once when this host has no clipboard. */
|
|
28
|
+
readClipboard: () => {
|
|
29
|
+
const c = nav()?.clipboard;
|
|
30
|
+
return c === undefined ? null : c.readText().then(s => s, () => null);
|
|
31
|
+
},
|
|
32
|
+
};
|
|
33
|
+
native('browse', '["Opens a web address in a new tab." url [url! file!]]', (_t, f) => {
|
|
34
|
+
if (!page.open(text(f.args[0])))
|
|
35
|
+
return throwError('access', 'no-browse', 'this host cannot open web addresses');
|
|
36
|
+
return value(UNSET);
|
|
37
|
+
});
|
|
38
|
+
native('write-clipboard', '["Puts text on the clipboard; true when it is there." data [string!]]', (_t, f, v) => {
|
|
39
|
+
if (f.phase === 1)
|
|
40
|
+
return value(v);
|
|
41
|
+
f.phase = 1;
|
|
42
|
+
const p = page.writeClipboard(text(f.args[0]));
|
|
43
|
+
if (p === null)
|
|
44
|
+
return throwError('access', 'no-clipboard', 'this host has no clipboard');
|
|
45
|
+
return { k: 'await', p: p.then(logic) };
|
|
46
|
+
});
|
|
47
|
+
native('read-clipboard', '["The text on the clipboard, or none; the browser may ask the user first."]', (_t, f, v) => {
|
|
48
|
+
if (f.phase === 1)
|
|
49
|
+
return value(v);
|
|
50
|
+
f.phase = 1;
|
|
51
|
+
const p = page.readClipboard();
|
|
52
|
+
if (p === null)
|
|
53
|
+
return throwError('access', 'no-clipboard', 'this host has no clipboard');
|
|
54
|
+
return { k: 'await', p: p.then((s) => s === null || s === '' ? NONE : string(s)) };
|
|
55
|
+
});
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { type Cell, type Sym } from '../core/index.ts';
|
|
2
|
+
/** A field of a map! or object! row; map keys may be words or strings. */
|
|
3
|
+
export declare function rowField(row: Cell, sym: Sym): Cell;
|
|
4
|
+
/**
|
|
5
|
+
* A row of data that heads or closes a group: [head 1 "Title"], whose title may
|
|
6
|
+
* be a text per language, or [foot 1 #(amount: ...)], whose row is shown as the
|
|
7
|
+
* totals are. The level is 1 or 2; 2 is indented.
|
|
8
|
+
*/
|
|
9
|
+
export declare function groupRow(c: Cell): {
|
|
10
|
+
kind: 'head' | 'foot';
|
|
11
|
+
level: 1 | 2;
|
|
12
|
+
value: Cell;
|
|
13
|
+
} | null;
|
|
14
|
+
/** Sorts the rows between the heads and feet of groups, each run on its own; those stay where they are. */
|
|
15
|
+
export declare function sortGroups(rows: Cell[], col: Sym, desc: boolean, language: string, shows: (v: Cell) => string): Cell[];
|
|
16
|
+
/** From this many rows on, a table or list draws only the ones near the screen. */
|
|
17
|
+
export declare const DRAW_ALL_UP_TO = 200;
|
|
18
|
+
/**
|
|
19
|
+
* The rows to draw, [first, end), of `count` rows each about `height` pixels tall,
|
|
20
|
+
* when the screen shows from `top` to `top + seen` pixels measured from the first
|
|
21
|
+
* row's top. The rows drawn now, `now`, are kept while they cover what is seen.
|
|
22
|
+
*/
|
|
23
|
+
export declare function rowWindow(count: number, height: number, top: number, seen: number, now?: {
|
|
24
|
+
first: number;
|
|
25
|
+
end: number;
|
|
26
|
+
}): {
|
|
27
|
+
first: number;
|
|
28
|
+
end: number;
|
|
29
|
+
};
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
// The rows of a table or list: their fields, the heads and feet of groups
|
|
2
|
+
// among them, and the order a sortable table puts them in. No DOM here, so
|
|
3
|
+
// the tests run it in Node.
|
|
4
|
+
import { K, NONE, ctxFind, canon, intern, spell, text, compare, } from "../core/index.js";
|
|
5
|
+
/** A field of a map! or object! row; map keys may be words or strings. */
|
|
6
|
+
export function rowField(row, sym) {
|
|
7
|
+
if (row.t === K.Object) {
|
|
8
|
+
const i = ctxFind(row.ctx, sym);
|
|
9
|
+
return i < 0 ? NONE : row.ctx.vals[i];
|
|
10
|
+
}
|
|
11
|
+
if (row.t === K.Map) {
|
|
12
|
+
const d = row.s.data;
|
|
13
|
+
const c = canon(sym);
|
|
14
|
+
for (let i = 0; i + 1 < d.length; i += 2) {
|
|
15
|
+
const k = d[i];
|
|
16
|
+
if (k.t === K.Word || k.t === K.SetWord || k.t === K.LitWord) {
|
|
17
|
+
if (canon(k.sym) === c)
|
|
18
|
+
return d[i + 1];
|
|
19
|
+
}
|
|
20
|
+
else if (k.t === K.String && canon(intern(text(k))) === c) {
|
|
21
|
+
return d[i + 1];
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
return NONE;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* A row of data that heads or closes a group: [head 1 "Title"], whose title may
|
|
29
|
+
* be a text per language, or [foot 1 #(amount: ...)], whose row is shown as the
|
|
30
|
+
* totals are. The level is 1 or 2; 2 is indented.
|
|
31
|
+
*/
|
|
32
|
+
export function groupRow(c) {
|
|
33
|
+
if (c.t !== K.Block)
|
|
34
|
+
return null;
|
|
35
|
+
const [w, n, v] = c.s.data.slice(c.i);
|
|
36
|
+
if (w === undefined || n === undefined || v === undefined || w.t !== K.Word || n.t !== K.Integer)
|
|
37
|
+
return null;
|
|
38
|
+
const kind = spell(w.sym).toLowerCase();
|
|
39
|
+
if (kind !== 'head' && kind !== 'foot')
|
|
40
|
+
return null;
|
|
41
|
+
return { kind, level: Number(n.v) >= 2 ? 2 : 1, value: v };
|
|
42
|
+
}
|
|
43
|
+
/** Sorts the rows between the heads and feet of groups, each run on its own; those stay where they are. */
|
|
44
|
+
export function sortGroups(rows, col, desc, language, shows) {
|
|
45
|
+
const out = [];
|
|
46
|
+
let run = [];
|
|
47
|
+
for (const r of rows) {
|
|
48
|
+
if (groupRow(r) === null) {
|
|
49
|
+
run.push(r);
|
|
50
|
+
continue;
|
|
51
|
+
}
|
|
52
|
+
out.push(...sortRows(run, col, desc, language, shows), r);
|
|
53
|
+
run = [];
|
|
54
|
+
}
|
|
55
|
+
return out.concat(sortRows(run, col, desc, language, shows));
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Rows by the native value of a column: numbers, amounts and dates by value, texts as
|
|
59
|
+
* the language orders them; empty cells go last either way. Ties keep their order.
|
|
60
|
+
*/
|
|
61
|
+
function sortRows(rows, col, desc, language, shows) {
|
|
62
|
+
const collator = new Intl.Collator(language, { numeric: true });
|
|
63
|
+
const blank = (v) => v.t === K.None || (v.t === K.String && text(v) === '');
|
|
64
|
+
const order = (a, b) => {
|
|
65
|
+
if (a.t === K.String && b.t === K.String)
|
|
66
|
+
return collator.compare(text(a), text(b));
|
|
67
|
+
// values that cannot be ordered together (a text and a number) go by what they show
|
|
68
|
+
return compare(a, b) ?? collator.compare(shows(a), shows(b));
|
|
69
|
+
};
|
|
70
|
+
return rows
|
|
71
|
+
.map(row => ({ row, v: rowField(row, col) }))
|
|
72
|
+
.sort((a, b) => {
|
|
73
|
+
const ea = blank(a.v);
|
|
74
|
+
const eb = blank(b.v);
|
|
75
|
+
if (ea || eb)
|
|
76
|
+
return ea === eb ? 0 : ea ? 1 : -1;
|
|
77
|
+
return desc ? order(b.v, a.v) : order(a.v, b.v);
|
|
78
|
+
})
|
|
79
|
+
.map(x => x.row);
|
|
80
|
+
}
|
|
81
|
+
// --- drawing only the rows near the screen ---------------------------------------------
|
|
82
|
+
/** From this many rows on, a table or list draws only the ones near the screen. */
|
|
83
|
+
export const DRAW_ALL_UP_TO = 200;
|
|
84
|
+
/** Rows drawn beyond each edge of the screen, so a short scroll needs no new rows. */
|
|
85
|
+
const BEYOND = 30;
|
|
86
|
+
/** Rows drawn at least, even when the rows are all out of sight. */
|
|
87
|
+
const AT_LEAST = 60;
|
|
88
|
+
/**
|
|
89
|
+
* The rows to draw, [first, end), of `count` rows each about `height` pixels tall,
|
|
90
|
+
* when the screen shows from `top` to `top + seen` pixels measured from the first
|
|
91
|
+
* row's top. The rows drawn now, `now`, are kept while they cover what is seen.
|
|
92
|
+
*/
|
|
93
|
+
export function rowWindow(count, height, top, seen, now) {
|
|
94
|
+
const h = Math.max(1, height);
|
|
95
|
+
const clamp = (n) => Math.min(count, Math.max(0, n));
|
|
96
|
+
const from = clamp(Math.floor(top / h));
|
|
97
|
+
const to = clamp(Math.ceil((top + seen) / h));
|
|
98
|
+
if (now !== undefined && now.end <= count && now.end - now.first >= Math.min(count, AT_LEAST) &&
|
|
99
|
+
now.first <= from && to <= now.end)
|
|
100
|
+
return now;
|
|
101
|
+
let first = clamp(from - BEYOND);
|
|
102
|
+
let end = clamp(to + BEYOND);
|
|
103
|
+
if (end - first < AT_LEAST) {
|
|
104
|
+
if (first === 0 || end < count)
|
|
105
|
+
end = clamp(first + AT_LEAST);
|
|
106
|
+
first = clamp(end - AT_LEAST);
|
|
107
|
+
}
|
|
108
|
+
return { first, end };
|
|
109
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { type Cell, type BlockCell, type ObjectCell, type Runtime, type Task, type Series } from '../core/index.ts';
|
|
2
|
+
/** Steps a task runs before the scheduler yields to the host. */
|
|
3
|
+
export declare const SLICE = 10000;
|
|
4
|
+
export interface HostTask {
|
|
5
|
+
readonly id: number;
|
|
6
|
+
readonly task: Task;
|
|
7
|
+
state: 'ready' | 'waiting' | 'done' | 'failed' | 'cancelled';
|
|
8
|
+
result: Cell;
|
|
9
|
+
error: ObjectCell | null;
|
|
10
|
+
/** Aborts the operation the task is waiting on, if any. */
|
|
11
|
+
abort: (() => void) | null;
|
|
12
|
+
/** Called when the task finishes, fails or is cancelled. */
|
|
13
|
+
readonly watchers: Array<(t: HostTask) => void>;
|
|
14
|
+
}
|
|
15
|
+
export interface Scheduler {
|
|
16
|
+
readonly rt: Runtime;
|
|
17
|
+
readonly ready: HostTask[];
|
|
18
|
+
readonly byTask: WeakMap<Task, HostTask>;
|
|
19
|
+
/** Reports errors of tasks nobody waits for. */
|
|
20
|
+
onError: (e: ObjectCell, t: HostTask) => void;
|
|
21
|
+
pumping: boolean;
|
|
22
|
+
live: number;
|
|
23
|
+
/** Called after each round of slices: the browser host syncs the DOM here. */
|
|
24
|
+
afterPump: (() => void) | null;
|
|
25
|
+
}
|
|
26
|
+
/** Stores a host object and returns a handle! cell for it. */
|
|
27
|
+
export declare function makeHandle(kind: string, obj: unknown): Cell;
|
|
28
|
+
export declare function handleOf<T>(c: Cell, kind: string): T | null;
|
|
29
|
+
export declare function dropHandle(c: Cell): void;
|
|
30
|
+
export declare function makeScheduler(rt: Runtime, onError: Scheduler['onError']): Scheduler;
|
|
31
|
+
/** The scheduler running a task's runtime. */
|
|
32
|
+
export declare function schedulerOf(t: Task): Scheduler;
|
|
33
|
+
/** Starts a task for `code` (a block bound to the user context or deeper). */
|
|
34
|
+
export declare function spawn(sch: Scheduler, s: Series<Cell>, i?: number): HostTask;
|
|
35
|
+
/**
|
|
36
|
+
* Runs a handler block as a new task with some words bound to values, e.g.
|
|
37
|
+
* `value` for a message. The block is copied, so handlers never share state.
|
|
38
|
+
*/
|
|
39
|
+
export declare function spawnHandler(sch: Scheduler, body: BlockCell, words: Record<string, Cell>): HostTask;
|
|
40
|
+
/**
|
|
41
|
+
* Starts a task and runs it at once, before any other, until it ends or waits;
|
|
42
|
+
* a long one goes on in turn with the rest. The browser host runs reactions
|
|
43
|
+
* this way, between a round and the repaint that follows it.
|
|
44
|
+
*/
|
|
45
|
+
export declare function runNow(sch: Scheduler, s: Series<Cell>, i?: number): HostTask;
|
|
46
|
+
/** A rejected promise's reason as a Trebol error!. */
|
|
47
|
+
export declare function asError(e: unknown): ObjectCell;
|
|
48
|
+
/** Cancels a task: aborts what it waits on; watchers see a `cancelled` error. */
|
|
49
|
+
export declare function cancelTask(sch: Scheduler, ht: HostTask): void;
|
|
50
|
+
/** A promise for the task's result; rejects with its error. */
|
|
51
|
+
export declare function taskResult(ht: HostTask): Promise<Cell>;
|