@yaks/code 0.0.0-stage → 0.2.36
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/LICENSE +202 -0
- package/README.md +106 -2
- package/_dist/anatomy.d.ts +144 -0
- package/_dist/anatomy.d.ts.map +1 -0
- package/_dist/described.d.ts +31 -0
- package/_dist/described.d.ts.map +1 -0
- package/_dist/effects.d.ts +9 -0
- package/_dist/effects.d.ts.map +1 -0
- package/_dist/frames.d.ts +16 -0
- package/_dist/frames.d.ts.map +1 -0
- package/_dist/mod.d.ts +26 -0
- package/_dist/mod.d.ts.map +1 -0
- package/_dist/read.d.ts +89 -0
- package/_dist/read.d.ts.map +1 -0
- package/_dist/source.d.ts +55 -0
- package/_dist/source.d.ts.map +1 -0
- package/_dist/sync.d.ts +39 -0
- package/_dist/sync.d.ts.map +1 -0
- package/_dist/tools.d.ts +8 -0
- package/_dist/tools.d.ts.map +1 -0
- package/_dist/vocab.d.ts +6 -0
- package/_dist/vocab.d.ts.map +1 -0
- package/anatomy.js +552 -0
- package/anatomy.js.map +1 -0
- package/anatomy.ts +638 -0
- package/deno.json +1 -0
- package/described.js +274 -0
- package/described.js.map +1 -0
- package/described.ts +314 -0
- package/effects.js +24 -0
- package/effects.js.map +1 -0
- package/effects.ts +30 -0
- package/frames.js +46 -0
- package/frames.js.map +1 -0
- package/frames.ts +54 -0
- package/mod.js +26 -0
- package/mod.js.map +1 -0
- package/mod.ts +27 -0
- package/package.json +54 -4
- package/read.js +178 -0
- package/read.js.map +1 -0
- package/read.ts +249 -0
- package/source.js +238 -0
- package/source.js.map +1 -0
- package/source.ts +263 -0
- package/sync.js +345 -0
- package/sync.js.map +1 -0
- package/sync.ts +323 -0
- package/tools.js +49 -0
- package/tools.js.map +1 -0
- package/tools.ts +59 -0
- package/vocab.js +21 -0
- package/vocab.js.map +1 -0
- package/vocab.json +143 -0
- package/vocab.ts +22 -0
package/described.js
ADDED
|
@@ -0,0 +1,274 @@
|
|
|
1
|
+
// The vocabulary a graph is served with, described in it as @yaks/vocab's
|
|
2
|
+
// `_namespace`, `_comp`, `_kind`, `_effect`, `_extends`, `_prop` and `_before`
|
|
3
|
+
// entities (@yaks/vocab `toBundles`), so the graph's own model is read,
|
|
4
|
+
// searched and linked like anything else, and a page about a component, a
|
|
5
|
+
// kind or an effect resolves. Each row is its word's `$id`, so a rename
|
|
6
|
+
// patches the row it had, and the row keeps the label it had before in
|
|
7
|
+
// `former`.
|
|
8
|
+
//
|
|
9
|
+
// The served vocabulary is the one source: every document the host composed,
|
|
10
|
+
// each written with the package that brought it and that package's
|
|
11
|
+
// description, from its package.json (@yaks/cli `compose`), so a package the
|
|
12
|
+
// checkout holds and the config does not list is not described.
|
|
13
|
+
//
|
|
14
|
+
// The graph's one `_vocab` holds the hash of every row as last described, so
|
|
15
|
+
// a graph whose hash matches is known to describe it, and is read no further:
|
|
16
|
+
// a process starting over an unchanged vocabulary costs that entity. Otherwise
|
|
17
|
+
// what is written is the difference, and the new hash with it. A row already
|
|
18
|
+
// saying what the vocabulary says is left alone. A row the vocabulary stopped
|
|
19
|
+
// declaring is cleared the way a gone export is (./sync.ts): the entity stays,
|
|
20
|
+
// empty, with any note left on it, and is filled in again if the component
|
|
21
|
+
// comes back. A `_before` edge it stopped saying is unlinked.
|
|
22
|
+
//
|
|
23
|
+
// Or a graph writes none of it and derives it when read (`describing`): the
|
|
24
|
+
// rows are the vocabulary's, the same for every graph served with it, so a
|
|
25
|
+
// host keeping many graphs on one vocabulary copies it into none of them.
|
|
26
|
+
import { bare, map } from "@yaks/query";
|
|
27
|
+
import { canonicalQueries } from "@yaks/graph";
|
|
28
|
+
import { derivedEid } from "@yaks/graph/identity";
|
|
29
|
+
import { link, unlink } from "@yaks/edge";
|
|
30
|
+
import { toBundles } from "@yaks/vocab";
|
|
31
|
+
let str = (v)=>v == null ? '' : String(v);
|
|
32
|
+
// The rows a vocabulary is described in, in the order a batch writes them,
|
|
33
|
+
// so each reference lands on an entity written before it.
|
|
34
|
+
let ROWS = [
|
|
35
|
+
'_namespace',
|
|
36
|
+
'_comp',
|
|
37
|
+
'_kind',
|
|
38
|
+
'_effect',
|
|
39
|
+
'_extends',
|
|
40
|
+
'_prop',
|
|
41
|
+
'_before',
|
|
42
|
+
'_vocab'
|
|
43
|
+
];
|
|
44
|
+
let rank = (b)=>ROWS.findIndex((r)=>b[r] !== undefined);
|
|
45
|
+
// A value as a string that is the same whatever order its keys are in, and
|
|
46
|
+
// where null is the same as nothing: what a store reads back for a column a
|
|
47
|
+
// row never set.
|
|
48
|
+
let canon = (v)=>JSON.stringify(v, (_, x)=>x && typeof x == 'object' && !Array.isArray(x) ? Object.fromEntries(Object.entries(x).filter(([, y])=>y != null).toSorted(([a], [b])=>a < b ? -1 : a > b ? 1 : 0)) : x) ?? 'null';
|
|
49
|
+
/**
|
|
50
|
+
* Whether writing `fresh` over `held` would change anything: a component it
|
|
51
|
+
* patches that `held` lacks, or a property whose value differs.
|
|
52
|
+
*
|
|
53
|
+
* ```ts
|
|
54
|
+
* import { moves } from './described.ts'
|
|
55
|
+
* let at = { entity: { eid: 'c' }, _comp: { name: 'note', wire: null } }
|
|
56
|
+
* moves({ entity: { eid: 'c' }, _comp: { wire: null, name: 'note' } }, at) // false
|
|
57
|
+
* moves({ entity: { eid: 'c' }, _comp: { name: 'task' } }, at) // true
|
|
58
|
+
* moves({ entity: { eid: 'c' }, doc: { title: 'note' } }, at) // true
|
|
59
|
+
* ```
|
|
60
|
+
*/ export let moves = (fresh, held)=>!held || Object.entries(fresh).some(([name, c])=>name != 'entity' && (!held[name] || Object.entries(c).some(([k, v])=>canon(v) != canon(held[name][k]))));
|
|
61
|
+
// Two bundles of one entity, as one: what the later says over the earlier,
|
|
62
|
+
// where it says anything. Two documents of one package each describe it.
|
|
63
|
+
let merged = (a, b)=>Object.fromEntries([
|
|
64
|
+
...new Set([
|
|
65
|
+
...Object.keys(a),
|
|
66
|
+
...Object.keys(b)
|
|
67
|
+
])
|
|
68
|
+
].map((k)=>[
|
|
69
|
+
k,
|
|
70
|
+
k == 'entity' ? a.entity : {
|
|
71
|
+
...a[k],
|
|
72
|
+
...Object.fromEntries(Object.entries(b[k] ?? {}).filter(([, v])=>v != null))
|
|
73
|
+
}
|
|
74
|
+
]));
|
|
75
|
+
// The one entity that says what the rows were last described from.
|
|
76
|
+
let VOCAB = derivedEid('_vocab');
|
|
77
|
+
// The hex SHA-256 of a string.
|
|
78
|
+
let sha = async (s)=>[
|
|
79
|
+
...new Uint8Array(await crypto.subtle.digest('SHA-256', new TextEncoder().encode(s)))
|
|
80
|
+
].map((b)=>b.toString(16).padStart(2, '0')).join('');
|
|
81
|
+
// Every row `docs` describe, by eid: two documents of one package describe it
|
|
82
|
+
// as one.
|
|
83
|
+
let rows = (vocab, docs)=>{
|
|
84
|
+
let fresh = new Map();
|
|
85
|
+
for (let doc of docs){
|
|
86
|
+
for (let b of toBundles(doc, derivedEid, vocab)){
|
|
87
|
+
let e = b.edge;
|
|
88
|
+
let row = e ? link(str(e.from), '_before', str(e.to), Number(e.ord)) : b;
|
|
89
|
+
let was = fresh.get(row.entity.eid);
|
|
90
|
+
fresh.set(row.entity.eid, was ? merged(was, row) : row);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
return fresh;
|
|
94
|
+
};
|
|
95
|
+
// A `_comp` or `_prop` row written under a new label keeps the one it had,
|
|
96
|
+
// after any it had before, so a former label still reads as it (@yaks/vocab
|
|
97
|
+
// `in`). A label it comes back to is its own again.
|
|
98
|
+
let renamed = (fresh, held)=>{
|
|
99
|
+
let meta = [
|
|
100
|
+
'_comp',
|
|
101
|
+
'_prop'
|
|
102
|
+
].find((m)=>fresh[m]);
|
|
103
|
+
if (!meta) return fresh;
|
|
104
|
+
let now = fresh[meta];
|
|
105
|
+
let was = held?.[meta];
|
|
106
|
+
if (!was?.name || was.name == now.name) return fresh;
|
|
107
|
+
let former = [
|
|
108
|
+
...was.former ?? [],
|
|
109
|
+
String(was.name)
|
|
110
|
+
].filter((l)=>l != now.name);
|
|
111
|
+
return {
|
|
112
|
+
...fresh,
|
|
113
|
+
[meta]: {
|
|
114
|
+
...now,
|
|
115
|
+
former
|
|
116
|
+
}
|
|
117
|
+
};
|
|
118
|
+
};
|
|
119
|
+
/** What to write so `g` describes `docs`, the vocabulary it is served with:
|
|
120
|
+
* every row that is missing or says something else, every row it holds that
|
|
121
|
+
* `docs` no longer declares, cleared, and the hash of the rows as described
|
|
122
|
+
* (`_vocab`). A graph whose hash already matches is not read further and is
|
|
123
|
+
* written nothing; nor is one that cannot hold a vocabulary (@yaks/vocab not
|
|
124
|
+
* composed). A single-owner host can supply its previously committed docs to
|
|
125
|
+
* select only changed identities rather than read every description. */ export let described = async (g, docs, previous)=>{
|
|
126
|
+
if (!g.vocab.comp('_vocab')) return [];
|
|
127
|
+
let fresh = rows(g.vocab, docs);
|
|
128
|
+
let [was] = await g.get([
|
|
129
|
+
VOCAB
|
|
130
|
+
], [
|
|
131
|
+
'_vocab'
|
|
132
|
+
]);
|
|
133
|
+
let hash = await sha(canon([
|
|
134
|
+
...fresh.values()
|
|
135
|
+
]));
|
|
136
|
+
if (was?._vocab?.hash == hash) return [];
|
|
137
|
+
// A single-owner host can retain the last accepted declaration outside the
|
|
138
|
+
// graph. Select only changed identities; the global hash remains the
|
|
139
|
+
// commit marker, so an interrupted pass is diffed again from its old docs.
|
|
140
|
+
let before = previous && rows(g.vocab, previous);
|
|
141
|
+
let changed = before && [
|
|
142
|
+
...fresh.values()
|
|
143
|
+
].filter((b)=>canon(b) != canon(before.get(b.entity.eid)));
|
|
144
|
+
let gone = before && [
|
|
145
|
+
...before.keys()
|
|
146
|
+
].filter((id)=>!fresh.has(id));
|
|
147
|
+
let held = changed && gone ? await g.get([
|
|
148
|
+
...changed.map((b)=>b.entity.eid),
|
|
149
|
+
...gone
|
|
150
|
+
]) : (await Promise.all([
|
|
151
|
+
g.read('._namespace ?doc'),
|
|
152
|
+
g.read('._comp ?doc'),
|
|
153
|
+
g.read('._kind ?doc'),
|
|
154
|
+
g.read('._effect ?doc'),
|
|
155
|
+
g.read('._extends ?doc'),
|
|
156
|
+
g.read('._prop ?doc'),
|
|
157
|
+
g.read('._before ?edge')
|
|
158
|
+
])).flat();
|
|
159
|
+
let at = new Map(held.map((b)=>[
|
|
160
|
+
b.entity.eid,
|
|
161
|
+
b
|
|
162
|
+
]));
|
|
163
|
+
let cleared = (b)=>{
|
|
164
|
+
let e = b.edge;
|
|
165
|
+
return e ? unlink(str(e.from), '_before', str(e.to)) : {
|
|
166
|
+
entity: {
|
|
167
|
+
eid: b.entity.eid
|
|
168
|
+
},
|
|
169
|
+
[ROWS[rank(b)]]: null,
|
|
170
|
+
doc: null
|
|
171
|
+
};
|
|
172
|
+
};
|
|
173
|
+
// Compare the declared queries in the same spelling the store reads back.
|
|
174
|
+
let candidates = canonicalQueries(g.vocab)(changed ?? [
|
|
175
|
+
...fresh.values()
|
|
176
|
+
]);
|
|
177
|
+
return [
|
|
178
|
+
...candidates.filter((b)=>moves(b, at.get(b.entity.eid))).map((b)=>renamed(b, at.get(b.entity.eid))),
|
|
179
|
+
...held.filter((b)=>rank(b) >= 0 && !fresh.has(b.entity.eid)).map(cleared),
|
|
180
|
+
{
|
|
181
|
+
entity: {
|
|
182
|
+
eid: VOCAB
|
|
183
|
+
},
|
|
184
|
+
_vocab: {
|
|
185
|
+
hash
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
].toSorted((a, b)=>rank(a) - rank(b));
|
|
189
|
+
};
|
|
190
|
+
// The components a description is made of. `_vocab` is not one: it says what
|
|
191
|
+
// a graph last wrote. Only a vocabulary says what a component is, so a stored
|
|
192
|
+
// one of those is what an earlier copy left; a package can be a graph's own
|
|
193
|
+
// (@yaks/lens names one for its steps to point at).
|
|
194
|
+
let META = ROWS.slice(0, -1);
|
|
195
|
+
let SAID = META.slice(1);
|
|
196
|
+
// `!name`: an entity without it.
|
|
197
|
+
let absent = (c)=>c.op == '=' && c.value?.kind == 'scalar' && !c.value.raw;
|
|
198
|
+
// A vocabulary's description, derived once for as long as it is served.
|
|
199
|
+
let derivations = new WeakMap();
|
|
200
|
+
let derive = (vocab)=>{
|
|
201
|
+
let held = derivations.get(vocab);
|
|
202
|
+
if (!held) {
|
|
203
|
+
held = rows(vocab, vocab.docs);
|
|
204
|
+
// `rows` are write patches: null clears an old property. A derived read
|
|
205
|
+
// has no old properties to clear, and storage reads omit absent columns.
|
|
206
|
+
for (let [id, row] of held){
|
|
207
|
+
held.set(id, Object.fromEntries(Object.entries(row).map(([name, comp])=>[
|
|
208
|
+
name,
|
|
209
|
+
Object.fromEntries(Object.entries(comp).filter(([, value])=>value != null))
|
|
210
|
+
])));
|
|
211
|
+
}
|
|
212
|
+
derivations.set(vocab, held);
|
|
213
|
+
}
|
|
214
|
+
return held;
|
|
215
|
+
};
|
|
216
|
+
// Every eid a description has is a word's identity or derived from its
|
|
217
|
+
// labels (@yaks/vocab `labelled`), so only a UUID can name one.
|
|
218
|
+
let UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/;
|
|
219
|
+
// The components a description wears: its own, its title, and an edge's.
|
|
220
|
+
let WORN = new Set([
|
|
221
|
+
...META,
|
|
222
|
+
'entity',
|
|
223
|
+
'doc',
|
|
224
|
+
'edge'
|
|
225
|
+
]);
|
|
226
|
+
// Whether a query can match a description: a clause names one of its
|
|
227
|
+
// components, other than to want it or leave it out, or one of its entities.
|
|
228
|
+
// A query that requires a component no description wears (`.person`)
|
|
229
|
+
// matches none. The rows are derived only to ask whether a value that could
|
|
230
|
+
// be one of their eids is, so an isolate's first read, the one a store waking
|
|
231
|
+
// cold answers, derives nothing unless it can match a description.
|
|
232
|
+
let asks = (query, held)=>{
|
|
233
|
+
let named = (path)=>path.some((p)=>META.includes(p));
|
|
234
|
+
let says = (raw)=>raw != null && UUID.test(raw) && held().has(raw);
|
|
235
|
+
let unworn = (c)=>c.kind == 'pred' && c.op == '!' && bare(c) && !c.not && !c.where && !WORN.has(c.path[0]);
|
|
236
|
+
if (query.kind == 'and' && query.clauses.some(unworn)) return false;
|
|
237
|
+
let hit = false;
|
|
238
|
+
map(query, (c)=>{
|
|
239
|
+
hit ||= c.kind == 'pred' ? named(c.path) && c.op != '?' && !absent(c) || c.value?.kind == 'scalar' && says(c.value.raw) || c.value?.kind == 'list' && c.value.items.some((v)=>v.kind == 'scalar' && says(v.raw)) : c.kind == 'order' ? named(c.value.replace(/^-/, '').split('.')) : c.kind == 'fields' ? c.fields.some((f)=>named(f.path)) : c.kind == 'tally' || c.kind == 'distinct' ? named(c.path) : c.kind == 'refs' ? says(c.value) : false;
|
|
240
|
+
return c;
|
|
241
|
+
});
|
|
242
|
+
return hit;
|
|
243
|
+
};
|
|
244
|
+
/**
|
|
245
|
+
* The description of the vocabulary a graph is served with, derived when
|
|
246
|
+
* read rather than stored: a plugin that answers every query that can match
|
|
247
|
+
* one of its rows from what storage holds and the rows the vocabulary
|
|
248
|
+
* describes, the same rows `described` would write. A stored row on an entity
|
|
249
|
+
* the vocabulary describes, or one saying what a component is, is what an
|
|
250
|
+
* earlier copy left, and is not read. A walk from a stored entity into a
|
|
251
|
+
* description is answered by storage alone.
|
|
252
|
+
*/ export let describing = {
|
|
253
|
+
name: '@yaks/code describing',
|
|
254
|
+
view: ({ vocab }, original)=>{
|
|
255
|
+
if (!vocab?.comp('_comp')) return null;
|
|
256
|
+
if (!asks(original, ()=>derive(vocab))) return null;
|
|
257
|
+
let held = derive(vocab);
|
|
258
|
+
let stale = (b)=>held.has(b.entity.eid) || SAID.some((r)=>b[r]);
|
|
259
|
+
return {
|
|
260
|
+
original,
|
|
261
|
+
query: original,
|
|
262
|
+
vocab,
|
|
263
|
+
// The derived rows move only with the vocabulary: a write reaches the
|
|
264
|
+
// answer through what it stores, a component the query names (which a
|
|
265
|
+
// subscription notices) or a description component itself.
|
|
266
|
+
dependencies: META,
|
|
267
|
+
answer: (stored)=>[
|
|
268
|
+
...stored.filter((b)=>!stale(b)),
|
|
269
|
+
...held.values()
|
|
270
|
+
]
|
|
271
|
+
};
|
|
272
|
+
}
|
|
273
|
+
};
|
|
274
|
+
//# sourceMappingURL=described.js.map
|
package/described.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"described.js","sources":["./described.ts"],"names":[],"mappings":"AAAA,0EAA0E;AAC1E,+EAA+E;AAC/E,wEAAwE;AACxE,0EAA0E;AAC1E,wEAAwE;AACxE,uEAAuE;AACvE,YAAY;AACZ,EAAE;AACF,6EAA6E;AAC7E,mEAAmE;AACnE,6EAA6E;AAC7E,gEAAgE;AAChE,EAAE;AACF,6EAA6E;AAC7E,8EAA8E;AAC9E,+EAA+E;AAC/E,6EAA6E;AAC7E,8EAA8E;AAC9E,+EAA+E;AAC/E,2EAA2E;AAC3E,8DAA8D;AAC9D,EAAE;AACF,4EAA4E;AAC5E,2EAA2E;AAC3E,0EAA0E;AAE1E,SAAS,IAAI,EAAe,GAAG,2BAAuD;AACtF,SAEE,gBAAgB,2BAIa;AAC/B,SAAS,UAAU,oCAAyC;AAC5D,SAAS,IAAI,EAAE,MAAM,0BAA+B;AACpD,SAAS,SAAS,2BAA2D;AAE7E,IAAI,MAAM,CAAC,IAAe,KAAK,OAAO,KAAK,OAAO;AAElD,2EAA2E;AAC3E,0DAA0D;AAC1D,IAAI,OAAO;EACT;EACA;EACA;EACA;EACA;EACA;EACA;EACA;CACD;AACD,IAAI,OAAO,CAAC,IAAc,KAAK,SAAS,CAAC,CAAC,IAAM,CAAC,CAAC,EAAE,KAAK;AAEzD,2EAA2E;AAC3E,4EAA4E;AAC5E,iBAAiB;AACjB,IAAI,QAAQ,CAAC,IACX,KAAK,SAAS,CACZ,GACA,CAAC,GAAG,IACF,KAAK,OAAO,KAAK,YAAY,CAAC,MAAM,OAAO,CAAC,KACxC,OAAO,WAAW,CAClB,OAAO,OAAO,CAAC,GAAG,MAAM,CAAC,CAA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AE,IAAI,IAAI,aAC/B,MAAM,EAAE,IAAI,IACZ,EAAE,IAAI,IAAI,SACV,KAAK,EAAE,KAAK,IACZ;IACJ,OAAO;EACT;EACA,OAAO;AACT;AAEA;;;;;;;;CAQC,GACD,OAAO,IAAI,aAAqB;EAC9B,MAAM;EACN,MAAM,CAAC,EAAE,KAAK,EAAE,EAAE;IAChB,IAAI,CAAC,OAAO,KAAK,UAAU,OAAO;IAClC,IAAI,CAAC,KAAK,UAAU,IAAM,OAAO,SAAS,OAAO;IACjD,IAAI,OAAO,OAAO;IAClB,IAAI,QAAQ,CAAC,IAAc,KAAK,GAAG,CAAC,EAAE,MAAM,CAAC,GAAG,KAAK,KAAK,IAAI,CAAC,CAAC,IAAM,CAAC,CAAC,EAAE;IAC1E,OAAO;MACL;MACA,OAAO;MACP;MACA,sEAAsE;MACtE,sEAAsE;MACtE,2DAA2D;MAC3D,cAAc;MACd,QAAQ,CAAC,SAAW;aACf,OAAO,MAAM,CAAC,CAAC,IAAM,CAAC,MAAM;aAC5B,KAAK,MAAM;SACf;IACH;EACF;AACF,EAAC"}
|
package/described.ts
ADDED
|
@@ -0,0 +1,314 @@
|
|
|
1
|
+
// The vocabulary a graph is served with, described in it as @yaks/vocab's
|
|
2
|
+
// `_namespace`, `_comp`, `_kind`, `_effect`, `_extends`, `_prop` and `_before`
|
|
3
|
+
// entities (@yaks/vocab `toBundles`), so the graph's own model is read,
|
|
4
|
+
// searched and linked like anything else, and a page about a component, a
|
|
5
|
+
// kind or an effect resolves. Each row is its word's `$id`, so a rename
|
|
6
|
+
// patches the row it had, and the row keeps the label it had before in
|
|
7
|
+
// `former`.
|
|
8
|
+
//
|
|
9
|
+
// The served vocabulary is the one source: every document the host composed,
|
|
10
|
+
// each written with the package that brought it and that package's
|
|
11
|
+
// description, from its package.json (@yaks/cli `compose`), so a package the
|
|
12
|
+
// checkout holds and the config does not list is not described.
|
|
13
|
+
//
|
|
14
|
+
// The graph's one `_vocab` holds the hash of every row as last described, so
|
|
15
|
+
// a graph whose hash matches is known to describe it, and is read no further:
|
|
16
|
+
// a process starting over an unchanged vocabulary costs that entity. Otherwise
|
|
17
|
+
// what is written is the difference, and the new hash with it. A row already
|
|
18
|
+
// saying what the vocabulary says is left alone. A row the vocabulary stopped
|
|
19
|
+
// declaring is cleared the way a gone export is (./sync.ts): the entity stays,
|
|
20
|
+
// empty, with any note left on it, and is filled in again if the component
|
|
21
|
+
// comes back. A `_before` edge it stopped saying is unlinked.
|
|
22
|
+
//
|
|
23
|
+
// Or a graph writes none of it and derives it when read (`describing`): the
|
|
24
|
+
// rows are the vocabulary's, the same for every graph served with it, so a
|
|
25
|
+
// host keeping many graphs on one vocabulary copies it into none of them.
|
|
26
|
+
|
|
27
|
+
import { bare, type Clause, map, type Pred, type Query } from '@yaks/query'
|
|
28
|
+
import {
|
|
29
|
+
type Bundle,
|
|
30
|
+
canonicalQueries,
|
|
31
|
+
type Comp,
|
|
32
|
+
type Graph,
|
|
33
|
+
type Plugin,
|
|
34
|
+
} from '@yaks/graph'
|
|
35
|
+
import { derivedEid } from '@yaks/graph/identity'
|
|
36
|
+
import { link, unlink } from '@yaks/edge'
|
|
37
|
+
import { toBundles, type Vocab, type VocabDoc } from '@yaks/vocab'
|
|
38
|
+
|
|
39
|
+
let str = (v: unknown) => v == null ? '' : String(v)
|
|
40
|
+
|
|
41
|
+
// The rows a vocabulary is described in, in the order a batch writes them,
|
|
42
|
+
// so each reference lands on an entity written before it.
|
|
43
|
+
let ROWS = [
|
|
44
|
+
'_namespace',
|
|
45
|
+
'_comp',
|
|
46
|
+
'_kind',
|
|
47
|
+
'_effect',
|
|
48
|
+
'_extends',
|
|
49
|
+
'_prop',
|
|
50
|
+
'_before',
|
|
51
|
+
'_vocab',
|
|
52
|
+
]
|
|
53
|
+
let rank = (b: Bundle) => ROWS.findIndex((r) => b[r] !== undefined)
|
|
54
|
+
|
|
55
|
+
// A value as a string that is the same whatever order its keys are in, and
|
|
56
|
+
// where null is the same as nothing: what a store reads back for a column a
|
|
57
|
+
// row never set.
|
|
58
|
+
let canon = (v: unknown): string =>
|
|
59
|
+
JSON.stringify(
|
|
60
|
+
v,
|
|
61
|
+
(_, x) =>
|
|
62
|
+
x && typeof x == 'object' && !Array.isArray(x)
|
|
63
|
+
? Object.fromEntries(
|
|
64
|
+
Object.entries(x).filter(([, y]) => y != null).toSorted(([a], [b]) =>
|
|
65
|
+
a < b ? -1 : a > b ? 1 : 0
|
|
66
|
+
),
|
|
67
|
+
)
|
|
68
|
+
: x,
|
|
69
|
+
) ?? 'null'
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Whether writing `fresh` over `held` would change anything: a component it
|
|
73
|
+
* patches that `held` lacks, or a property whose value differs.
|
|
74
|
+
*
|
|
75
|
+
* ```ts
|
|
76
|
+
* import { moves } from './described.ts'
|
|
77
|
+
* let at = { entity: { eid: 'c' }, _comp: { name: 'note', wire: null } }
|
|
78
|
+
* moves({ entity: { eid: 'c' }, _comp: { wire: null, name: 'note' } }, at) // false
|
|
79
|
+
* moves({ entity: { eid: 'c' }, _comp: { name: 'task' } }, at) // true
|
|
80
|
+
* moves({ entity: { eid: 'c' }, doc: { title: 'note' } }, at) // true
|
|
81
|
+
* ```
|
|
82
|
+
*/
|
|
83
|
+
export let moves = (fresh: Bundle, held?: Bundle): boolean =>
|
|
84
|
+
!held ||
|
|
85
|
+
Object.entries(fresh).some(([name, c]) =>
|
|
86
|
+
name != 'entity' && (!held[name] ||
|
|
87
|
+
Object.entries(c as Comp).some(([k, v]) =>
|
|
88
|
+
canon(v) != canon((held[name] as Comp)[k])
|
|
89
|
+
))
|
|
90
|
+
)
|
|
91
|
+
|
|
92
|
+
// Two bundles of one entity, as one: what the later says over the earlier,
|
|
93
|
+
// where it says anything. Two documents of one package each describe it.
|
|
94
|
+
let merged = (a: Bundle, b: Bundle): Bundle =>
|
|
95
|
+
Object.fromEntries(
|
|
96
|
+
[...new Set([...Object.keys(a), ...Object.keys(b)])].map((k) => [
|
|
97
|
+
k,
|
|
98
|
+
k == 'entity' ? a.entity : {
|
|
99
|
+
...a[k] as Comp,
|
|
100
|
+
...Object.fromEntries(
|
|
101
|
+
Object.entries(b[k] as Comp ?? {}).filter(([, v]) => v != null),
|
|
102
|
+
),
|
|
103
|
+
},
|
|
104
|
+
]),
|
|
105
|
+
) as Bundle
|
|
106
|
+
|
|
107
|
+
// The one entity that says what the rows were last described from.
|
|
108
|
+
let VOCAB = derivedEid('_vocab')
|
|
109
|
+
|
|
110
|
+
// The hex SHA-256 of a string.
|
|
111
|
+
let sha = async (s: string) =>
|
|
112
|
+
[
|
|
113
|
+
...new Uint8Array(
|
|
114
|
+
await crypto.subtle.digest('SHA-256', new TextEncoder().encode(s)),
|
|
115
|
+
),
|
|
116
|
+
].map((b) => b.toString(16).padStart(2, '0')).join('')
|
|
117
|
+
|
|
118
|
+
// Every row `docs` describe, by eid: two documents of one package describe it
|
|
119
|
+
// as one.
|
|
120
|
+
let rows = (vocab: Vocab, docs: VocabDoc[]): Map<string, Bundle> => {
|
|
121
|
+
let fresh = new Map<string, Bundle>()
|
|
122
|
+
for (let doc of docs) {
|
|
123
|
+
for (let b of toBundles(doc, derivedEid, vocab) as Bundle[]) {
|
|
124
|
+
let e = b.edge as Comp | undefined
|
|
125
|
+
let row = e ? link(str(e.from), '_before', str(e.to), Number(e.ord)) : b
|
|
126
|
+
let was = fresh.get(row.entity.eid)
|
|
127
|
+
fresh.set(row.entity.eid, was ? merged(was, row) : row)
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
return fresh
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
// A `_comp` or `_prop` row written under a new label keeps the one it had,
|
|
134
|
+
// after any it had before, so a former label still reads as it (@yaks/vocab
|
|
135
|
+
// `in`). A label it comes back to is its own again.
|
|
136
|
+
let renamed = (fresh: Bundle, held?: Bundle): Bundle => {
|
|
137
|
+
let meta = ['_comp', '_prop'].find((m) => fresh[m])
|
|
138
|
+
if (!meta) return fresh
|
|
139
|
+
let now = fresh[meta] as Comp
|
|
140
|
+
let was = held?.[meta] as Comp | undefined
|
|
141
|
+
if (!was?.name || was.name == now.name) return fresh
|
|
142
|
+
let former = [...(was.former as string[] | null) ?? [], String(was.name)]
|
|
143
|
+
.filter((l) => l != now.name)
|
|
144
|
+
return { ...fresh, [meta]: { ...now, former } }
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/** What to write so `g` describes `docs`, the vocabulary it is served with:
|
|
148
|
+
* every row that is missing or says something else, every row it holds that
|
|
149
|
+
* `docs` no longer declares, cleared, and the hash of the rows as described
|
|
150
|
+
* (`_vocab`). A graph whose hash already matches is not read further and is
|
|
151
|
+
* written nothing; nor is one that cannot hold a vocabulary (@yaks/vocab not
|
|
152
|
+
* composed). A single-owner host can supply its previously committed docs to
|
|
153
|
+
* select only changed identities rather than read every description. */
|
|
154
|
+
export let described = async (
|
|
155
|
+
g: Graph,
|
|
156
|
+
docs: VocabDoc[],
|
|
157
|
+
previous?: VocabDoc[],
|
|
158
|
+
): Promise<Bundle[]> => {
|
|
159
|
+
if (!g.vocab.comp('_vocab')) return []
|
|
160
|
+
let fresh = rows(g.vocab, docs)
|
|
161
|
+
let [was] = await g.get([VOCAB], ['_vocab'])
|
|
162
|
+
let hash = await sha(canon([...fresh.values()]))
|
|
163
|
+
if ((was?._vocab as Comp | undefined)?.hash == hash) return []
|
|
164
|
+
// A single-owner host can retain the last accepted declaration outside the
|
|
165
|
+
// graph. Select only changed identities; the global hash remains the
|
|
166
|
+
// commit marker, so an interrupted pass is diffed again from its old docs.
|
|
167
|
+
let before = previous && rows(g.vocab, previous)
|
|
168
|
+
let changed = before &&
|
|
169
|
+
[...fresh.values()].filter((b) =>
|
|
170
|
+
canon(b) != canon(before.get(b.entity.eid))
|
|
171
|
+
)
|
|
172
|
+
let gone = before && [...before.keys()].filter((id) => !fresh.has(id))
|
|
173
|
+
let held = changed && gone
|
|
174
|
+
? await g.get([...changed.map((b) => b.entity.eid), ...gone])
|
|
175
|
+
: (await Promise.all([
|
|
176
|
+
g.read('._namespace ?doc'),
|
|
177
|
+
g.read('._comp ?doc'),
|
|
178
|
+
g.read('._kind ?doc'),
|
|
179
|
+
g.read('._effect ?doc'),
|
|
180
|
+
g.read('._extends ?doc'),
|
|
181
|
+
g.read('._prop ?doc'),
|
|
182
|
+
g.read('._before ?edge'),
|
|
183
|
+
])).flat()
|
|
184
|
+
let at = new Map(held.map((b) => [b.entity.eid, b]))
|
|
185
|
+
let cleared = (b: Bundle): Bundle => {
|
|
186
|
+
let e = b.edge as Comp | undefined
|
|
187
|
+
return e ? unlink(str(e.from), '_before', str(e.to)) : {
|
|
188
|
+
entity: { eid: b.entity.eid },
|
|
189
|
+
[ROWS[rank(b)]]: null,
|
|
190
|
+
doc: null,
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
// Compare the declared queries in the same spelling the store reads back.
|
|
194
|
+
let candidates = canonicalQueries(g.vocab)(changed ?? [...fresh.values()])
|
|
195
|
+
return [
|
|
196
|
+
...candidates.filter((b) => moves(b, at.get(b.entity.eid))).map((b) =>
|
|
197
|
+
renamed(b, at.get(b.entity.eid))
|
|
198
|
+
),
|
|
199
|
+
...held.filter((b) => rank(b) >= 0 && !fresh.has(b.entity.eid)).map(
|
|
200
|
+
cleared,
|
|
201
|
+
),
|
|
202
|
+
{ entity: { eid: VOCAB }, _vocab: { hash } },
|
|
203
|
+
].toSorted((a, b) => rank(a) - rank(b))
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
// The components a description is made of. `_vocab` is not one: it says what
|
|
207
|
+
// a graph last wrote. Only a vocabulary says what a component is, so a stored
|
|
208
|
+
// one of those is what an earlier copy left; a package can be a graph's own
|
|
209
|
+
// (@yaks/lens names one for its steps to point at).
|
|
210
|
+
let META = ROWS.slice(0, -1)
|
|
211
|
+
let SAID = META.slice(1)
|
|
212
|
+
|
|
213
|
+
// `!name`: an entity without it.
|
|
214
|
+
let absent = (c: Pred) =>
|
|
215
|
+
c.op == '=' && c.value?.kind == 'scalar' && !c.value.raw
|
|
216
|
+
|
|
217
|
+
// A vocabulary's description, derived once for as long as it is served.
|
|
218
|
+
let derivations = new WeakMap<Vocab, Map<string, Bundle>>()
|
|
219
|
+
let derive = (vocab: Vocab) => {
|
|
220
|
+
let held = derivations.get(vocab)
|
|
221
|
+
if (!held) {
|
|
222
|
+
held = rows(vocab, vocab.docs)
|
|
223
|
+
// `rows` are write patches: null clears an old property. A derived read
|
|
224
|
+
// has no old properties to clear, and storage reads omit absent columns.
|
|
225
|
+
for (let [id, row] of held) {
|
|
226
|
+
held.set(
|
|
227
|
+
id,
|
|
228
|
+
Object.fromEntries(
|
|
229
|
+
Object.entries(row).map(([name, comp]) => [
|
|
230
|
+
name,
|
|
231
|
+
Object.fromEntries(
|
|
232
|
+
Object.entries(comp as Comp).filter(([, value]) => value != null),
|
|
233
|
+
),
|
|
234
|
+
]),
|
|
235
|
+
) as Bundle,
|
|
236
|
+
)
|
|
237
|
+
}
|
|
238
|
+
derivations.set(vocab, held)
|
|
239
|
+
}
|
|
240
|
+
return held
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
// Every eid a description has is a word's identity or derived from its
|
|
244
|
+
// labels (@yaks/vocab `labelled`), so only a UUID can name one.
|
|
245
|
+
let UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/
|
|
246
|
+
|
|
247
|
+
// The components a description wears: its own, its title, and an edge's.
|
|
248
|
+
let WORN = new Set([...META, 'entity', 'doc', 'edge'])
|
|
249
|
+
|
|
250
|
+
// Whether a query can match a description: a clause names one of its
|
|
251
|
+
// components, other than to want it or leave it out, or one of its entities.
|
|
252
|
+
// A query that requires a component no description wears (`.person`)
|
|
253
|
+
// matches none. The rows are derived only to ask whether a value that could
|
|
254
|
+
// be one of their eids is, so an isolate's first read, the one a store waking
|
|
255
|
+
// cold answers, derives nothing unless it can match a description.
|
|
256
|
+
let asks = (query: Query, held: () => Map<string, Bundle>): boolean => {
|
|
257
|
+
let named = (path: string[]) => path.some((p) => META.includes(p))
|
|
258
|
+
let says = (raw?: string) => raw != null && UUID.test(raw) && held().has(raw)
|
|
259
|
+
let unworn = (c: Clause) =>
|
|
260
|
+
c.kind == 'pred' && c.op == '!' && bare(c) && !c.not && !c.where &&
|
|
261
|
+
!WORN.has(c.path[0])
|
|
262
|
+
if (query.kind == 'and' && query.clauses.some(unworn)) return false
|
|
263
|
+
let hit = false
|
|
264
|
+
map(query, (c) => {
|
|
265
|
+
hit ||= c.kind == 'pred'
|
|
266
|
+
? named(c.path) && c.op != '?' && !absent(c) ||
|
|
267
|
+
(c.value?.kind == 'scalar' && says(c.value.raw)) ||
|
|
268
|
+
(c.value?.kind == 'list' &&
|
|
269
|
+
c.value.items.some((v) => v.kind == 'scalar' && says(v.raw)))
|
|
270
|
+
: c.kind == 'order'
|
|
271
|
+
? named(c.value.replace(/^-/, '').split('.'))
|
|
272
|
+
: c.kind == 'fields'
|
|
273
|
+
? c.fields.some((f) => named(f.path))
|
|
274
|
+
: c.kind == 'tally' || c.kind == 'distinct'
|
|
275
|
+
? named(c.path)
|
|
276
|
+
: c.kind == 'refs'
|
|
277
|
+
? says(c.value)
|
|
278
|
+
: false
|
|
279
|
+
return c
|
|
280
|
+
})
|
|
281
|
+
return hit
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
/**
|
|
285
|
+
* The description of the vocabulary a graph is served with, derived when
|
|
286
|
+
* read rather than stored: a plugin that answers every query that can match
|
|
287
|
+
* one of its rows from what storage holds and the rows the vocabulary
|
|
288
|
+
* describes, the same rows `described` would write. A stored row on an entity
|
|
289
|
+
* the vocabulary describes, or one saying what a component is, is what an
|
|
290
|
+
* earlier copy left, and is not read. A walk from a stored entity into a
|
|
291
|
+
* description is answered by storage alone.
|
|
292
|
+
*/
|
|
293
|
+
export let describing: Plugin = {
|
|
294
|
+
name: '@yaks/code describing',
|
|
295
|
+
view: ({ vocab }, original) => {
|
|
296
|
+
if (!vocab?.comp('_comp')) return null
|
|
297
|
+
if (!asks(original, () => derive(vocab))) return null
|
|
298
|
+
let held = derive(vocab)
|
|
299
|
+
let stale = (b: Bundle) => held.has(b.entity.eid) || SAID.some((r) => b[r])
|
|
300
|
+
return {
|
|
301
|
+
original,
|
|
302
|
+
query: original,
|
|
303
|
+
vocab,
|
|
304
|
+
// The derived rows move only with the vocabulary: a write reaches the
|
|
305
|
+
// answer through what it stores, a component the query names (which a
|
|
306
|
+
// subscription notices) or a description component itself.
|
|
307
|
+
dependencies: META,
|
|
308
|
+
answer: (stored) => [
|
|
309
|
+
...stored.filter((b) => !stale(b)),
|
|
310
|
+
...held.values(),
|
|
311
|
+
],
|
|
312
|
+
}
|
|
313
|
+
},
|
|
314
|
+
}
|
package/effects.js
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
// What a host does about @yaks/code when it starts working the effects: the
|
|
2
|
+
// `@yaks/code/effects` entry point, the code behind `vocab_describe`
|
|
3
|
+
// (./vocab.json), a start-up effect (`start: true`). The process that starts
|
|
4
|
+
// working them brings the graph's description of its vocabulary into line
|
|
5
|
+
// with the vocabulary it serves (./described.ts), trusted, since those rows
|
|
6
|
+
// are `wire: false`.
|
|
7
|
+
//
|
|
8
|
+
// That start is when the served vocabulary can change: a landed change
|
|
9
|
+
// reaches it when the server restarts, and a config lists or drops a plugin
|
|
10
|
+
// the same way. A graph that already describes it is checked by the
|
|
11
|
+
// hash its `_vocab` holds, and written nothing.
|
|
12
|
+
import { described } from "./described.js";
|
|
13
|
+
export { described } from "./described.js";
|
|
14
|
+
/** The graph's vocabulary described in it, whenever a process starts working
|
|
15
|
+
* the effects. */ export let effects = (host)=>({
|
|
16
|
+
vocab_describe: async ()=>{
|
|
17
|
+
let g = host.graph;
|
|
18
|
+
let bundles = await described(g, g.vocab.docs);
|
|
19
|
+
if (bundles.length) await g.apply(bundles, {
|
|
20
|
+
trusted: true
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
//# sourceMappingURL=effects.js.map
|
package/effects.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"effects.js","sources":["./effects.ts"],"names":[],"mappings":"AAAA,4EAA4E;AAC5E,qEAAqE;AACrE,6EAA6E;AAC7E,0EAA0E;AAC1E,4EAA4E;AAC5E,qBAAqB;AACrB,EAAE;AACF,uEAAuE;AACvE,4EAA4E;AAC5E,oEAAoE;AACpE,gDAAgD;AAIhD,SAAS,SAAS,yBAAwB;AAE1C,SAAS,SAAS,yBAAwB;AAK1C;gBACgB,GAChB,OAAO,IAAI,UAAU,CAAC,OAAyB,CAAC;IAC9C,gBAAgB;MACd,IAAI,IAAI,KAAK,KAAK;MAClB,IAAI,UAAU,MAAM,UAAU,GAAG,EAAE,KAAK,CAAC,IAAI;MAC7C,IAAI,QAAQ,MAAM,EAAE,MAAM,EAAE,KAAK,CAAC,SAAS;QAAE,SAAS;MAAK;IAC7D;EACF,CAAC,EAAC"}
|
package/effects.ts
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
// What a host does about @yaks/code when it starts working the effects: the
|
|
2
|
+
// `@yaks/code/effects` entry point, the code behind `vocab_describe`
|
|
3
|
+
// (./vocab.json), a start-up effect (`start: true`). The process that starts
|
|
4
|
+
// working them brings the graph's description of its vocabulary into line
|
|
5
|
+
// with the vocabulary it serves (./described.ts), trusted, since those rows
|
|
6
|
+
// are `wire: false`.
|
|
7
|
+
//
|
|
8
|
+
// That start is when the served vocabulary can change: a landed change
|
|
9
|
+
// reaches it when the server restarts, and a config lists or drops a plugin
|
|
10
|
+
// the same way. A graph that already describes it is checked by the
|
|
11
|
+
// hash its `_vocab` holds, and written nothing.
|
|
12
|
+
|
|
13
|
+
import type { Handlers } from 'jsr:@yaks/effects@0.2.36'
|
|
14
|
+
import type { Graph } from 'jsr:@yaks/graph@0.2.36'
|
|
15
|
+
import { described } from './described.js'
|
|
16
|
+
|
|
17
|
+
export { described } from './described.js'
|
|
18
|
+
|
|
19
|
+
/** What the handler is given: the graph. */
|
|
20
|
+
export type Host = { graph: Graph }
|
|
21
|
+
|
|
22
|
+
/** The graph's vocabulary described in it, whenever a process starts working
|
|
23
|
+
* the effects. */
|
|
24
|
+
export let effects = (host: Host): Handlers => ({
|
|
25
|
+
vocab_describe: async () => {
|
|
26
|
+
let g = host.graph
|
|
27
|
+
let bundles = await described(g, g.vocab.docs)
|
|
28
|
+
if (bundles.length) await g.apply(bundles, { trusted: true })
|
|
29
|
+
},
|
|
30
|
+
})
|