@yaks/api 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.
Files changed (152) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +956 -2
  3. package/_dist/actor.d.ts +59 -0
  4. package/_dist/actor.d.ts.map +1 -0
  5. package/_dist/address.d.ts +33 -0
  6. package/_dist/address.d.ts.map +1 -0
  7. package/_dist/asset.d.ts +15 -0
  8. package/_dist/asset.d.ts.map +1 -0
  9. package/_dist/batches.d.ts +4 -0
  10. package/_dist/batches.d.ts.map +1 -0
  11. package/_dist/body.d.ts +7 -0
  12. package/_dist/body.d.ts.map +1 -0
  13. package/_dist/deno.d.ts +38 -0
  14. package/_dist/deno.d.ts.map +1 -0
  15. package/_dist/doors.d.ts +134 -0
  16. package/_dist/doors.d.ts.map +1 -0
  17. package/_dist/graph.d.ts +17 -0
  18. package/_dist/graph.d.ts.map +1 -0
  19. package/_dist/headers.d.ts +5 -0
  20. package/_dist/headers.d.ts.map +1 -0
  21. package/_dist/log.d.ts +253 -0
  22. package/_dist/log.d.ts.map +1 -0
  23. package/_dist/mod.d.ts +71 -0
  24. package/_dist/mod.d.ts.map +1 -0
  25. package/_dist/publish.d.ts +7 -0
  26. package/_dist/publish.d.ts.map +1 -0
  27. package/_dist/reached.d.ts +145 -0
  28. package/_dist/reached.d.ts.map +1 -0
  29. package/_dist/receipts.d.ts +153 -0
  30. package/_dist/receipts.d.ts.map +1 -0
  31. package/_dist/refuse.d.ts +51 -0
  32. package/_dist/refuse.d.ts.map +1 -0
  33. package/_dist/relay.d.ts +3 -0
  34. package/_dist/relay.d.ts.map +1 -0
  35. package/_dist/request.d.ts +79 -0
  36. package/_dist/request.d.ts.map +1 -0
  37. package/_dist/requests.d.ts +7 -0
  38. package/_dist/requests.d.ts.map +1 -0
  39. package/_dist/route.d.ts +71 -0
  40. package/_dist/route.d.ts.map +1 -0
  41. package/_dist/routed.d.ts +33 -0
  42. package/_dist/routed.d.ts.map +1 -0
  43. package/_dist/routes.d.ts +46 -0
  44. package/_dist/routes.d.ts.map +1 -0
  45. package/_dist/save.d.ts +6 -0
  46. package/_dist/save.d.ts.map +1 -0
  47. package/_dist/socket.d.ts +73 -0
  48. package/_dist/socket.d.ts.map +1 -0
  49. package/_dist/subs.d.ts +134 -0
  50. package/_dist/subs.d.ts.map +1 -0
  51. package/_dist/timing.d.ts +8 -0
  52. package/_dist/timing.d.ts.map +1 -0
  53. package/_dist/tools.d.ts +80 -0
  54. package/_dist/tools.d.ts.map +1 -0
  55. package/_dist/vocab.d.ts +6 -0
  56. package/_dist/vocab.d.ts.map +1 -0
  57. package/access.js +32 -0
  58. package/access.js.map +1 -0
  59. package/access.ts +42 -0
  60. package/actor.js +75 -0
  61. package/actor.js.map +1 -0
  62. package/actor.ts +106 -0
  63. package/address.js +42 -0
  64. package/address.js.map +1 -0
  65. package/address.ts +49 -0
  66. package/admission.js +71 -0
  67. package/admission.js.map +1 -0
  68. package/admission.ts +64 -0
  69. package/asset.js +37 -0
  70. package/asset.js.map +1 -0
  71. package/asset.ts +47 -0
  72. package/batches.js +92 -0
  73. package/batches.js.map +1 -0
  74. package/batches.ts +96 -0
  75. package/body.js +51 -0
  76. package/body.js.map +1 -0
  77. package/body.ts +82 -0
  78. package/deno.js +88 -0
  79. package/deno.js.map +1 -0
  80. package/deno.json +1 -0
  81. package/deno.ts +150 -0
  82. package/doors.js +435 -0
  83. package/doors.js.map +1 -0
  84. package/doors.ts +570 -0
  85. package/graph.js +12 -0
  86. package/graph.js.map +1 -0
  87. package/graph.ts +23 -0
  88. package/headers.js +5 -0
  89. package/headers.js.map +1 -0
  90. package/headers.ts +9 -0
  91. package/interest.js +224 -0
  92. package/interest.js.map +1 -0
  93. package/interest.ts +264 -0
  94. package/log.js +1011 -0
  95. package/log.js.map +1 -0
  96. package/log.ts +1035 -0
  97. package/mod.js +69 -0
  98. package/mod.js.map +1 -0
  99. package/mod.ts +142 -0
  100. package/package.json +75 -4
  101. package/peer_query.js +164 -0
  102. package/peer_query.js.map +1 -0
  103. package/peer_query.ts +158 -0
  104. package/publish.js +7 -0
  105. package/publish.js.map +1 -0
  106. package/publish.ts +16 -0
  107. package/reached.js +456 -0
  108. package/reached.js.map +1 -0
  109. package/reached.ts +549 -0
  110. package/receipts.js +543 -0
  111. package/receipts.js.map +1 -0
  112. package/receipts.ts +749 -0
  113. package/refuse.js +120 -0
  114. package/refuse.js.map +1 -0
  115. package/refuse.ts +179 -0
  116. package/relay.js +249 -0
  117. package/relay.js.map +1 -0
  118. package/relay.ts +284 -0
  119. package/request.js +218 -0
  120. package/request.js.map +1 -0
  121. package/request.ts +235 -0
  122. package/requests.js +250 -0
  123. package/requests.js.map +1 -0
  124. package/requests.ts +257 -0
  125. package/route.js +139 -0
  126. package/route.js.map +1 -0
  127. package/route.ts +228 -0
  128. package/routed.js +25 -0
  129. package/routed.js.map +1 -0
  130. package/routed.ts +53 -0
  131. package/routes.js +154 -0
  132. package/routes.js.map +1 -0
  133. package/routes.ts +201 -0
  134. package/save.js +341 -0
  135. package/save.js.map +1 -0
  136. package/save.ts +393 -0
  137. package/socket.js +398 -0
  138. package/socket.js.map +1 -0
  139. package/socket.ts +496 -0
  140. package/subs.js +2295 -0
  141. package/subs.js.map +1 -0
  142. package/subs.ts +2979 -0
  143. package/timing.js +17 -0
  144. package/timing.js.map +1 -0
  145. package/timing.ts +26 -0
  146. package/tools.js +251 -0
  147. package/tools.js.map +1 -0
  148. package/tools.ts +315 -0
  149. package/vocab.js +18 -0
  150. package/vocab.js.map +1 -0
  151. package/vocab.json +93 -0
  152. package/vocab.ts +18 -0
package/interest.js ADDED
@@ -0,0 +1,224 @@
1
+ // What a subscription's answer is read from, taken off its query once when it
2
+ // opens, so a commit that touches none of it does not run the query again. A
3
+ // refresh subscription runs its whole query on every commit it is asked about,
4
+ // so a commit that could not change the answer is a read wasted on the read
5
+ // thread every tab and every `/query` shares.
6
+ //
7
+ // own components every member wears: presence and direct positive property
8
+ // tests required by the query, a computed property's among them when
9
+ // its value needs the component (the store's `worn`), and the
10
+ // reference a walk toward its target steps from. An entity missing one
11
+ // cannot join.
12
+ // near the components the query reads on each entity itself, which stand
13
+ // in for `own` when there is no presence test to narrow it.
14
+ // far components read on other entities whose owner cannot be located.
15
+ // via components whose reference names the entity with the computed value.
16
+ // An entry changing `session.status`, for example, names its session.
17
+ // fixed exact reference values every member has, which exclude unrelated
18
+ // writes from a window's whole-answer refresh.
19
+ // refs the entity a backlink query (`.refs=<eid>`) names, and every
20
+ // reference property that can name it: only an entity holding one
21
+ // equal to it can join, so a write to anything else leaves the answer
22
+ // where it was, windowed or not.
23
+ // alone each entity alone says whether it is in the answer: the query reads
24
+ // nothing far or via another entity, and keeps no cursor (`.after`)
25
+ // and no backlink (`.refs`). Deleting an entity outside such an answer
26
+ // cannot move it.
27
+ //
28
+ // unseen the query reads a computed component, whose entities are in no
29
+ // commit's bundles: the journal's `_change` gains rows with every
30
+ // commit, beside what it applied. Every commit reads it again, whole.
31
+ //
32
+ // A walk (`.filed.project->P-19`, `.requires->T-42`) reads its path on every
33
+ // entity it passes through, none of which a commit's bundles name as a step:
34
+ // each hop of a chain of references is far, and so are a relation's edges,
35
+ // which wear `edge` beside their tag (@yaks/edge).
36
+ //
37
+ // `null` is a query this cannot place — a text term, a neighbour, a computed
38
+ // property that declares no `reads` — and every commit reaches it.
39
+ import { bare } from "@yaks/query";
40
+ import { minted } from "@yaks/graph";
41
+ // Clauses that shape or bound the answer without reading any component.
42
+ let QUIET = new Set([
43
+ 'every',
44
+ 'never'
45
+ ]);
46
+ class Opaque extends Error {
47
+ }
48
+ // A disjunction requires only what every arm requires; a conjunction requires
49
+ // all of its clauses. Missing properties satisfy absence and not-equals, and
50
+ // an empty equality alternative can do the same.
51
+ let missing = (c)=>c.kind == 'pred' && c.op == '=' && (c.value?.kind == 'scalar' && !c.value.raw || c.value?.kind == 'list' && c.value.items.some((v)=>v.kind == 'scalar' && !v.raw));
52
+ // The hops a walk's path steps through when it is a chain of reference
53
+ // properties (`.filed.project`), or `undefined` for a relation's name.
54
+ let chain = (v, path)=>{
55
+ let hops = [];
56
+ try {
57
+ hops = v.aim(path.join('.'));
58
+ } catch {
59
+ // a relation's name, which is no path
60
+ }
61
+ return hops.length > 0 && hops.every((h)=>h.prop && v.prop(h.comp, h.prop)?.category == 'ref') ? hops : undefined;
62
+ };
63
+ let required = (c, v, worn)=>{
64
+ if (c.kind == 'and') {
65
+ return new Set(c.clauses.flatMap((part)=>[
66
+ ...required(part, v, worn)
67
+ ]));
68
+ }
69
+ if (c.kind == 'or') {
70
+ let [first, ...rest] = c.clauses.map((part)=>required(part, v, worn));
71
+ return new Set([
72
+ ...first
73
+ ].filter((comp)=>rest.every((arm)=>arm.has(comp))));
74
+ }
75
+ // What walks a chain toward its target starts from a reference of its own.
76
+ if (c.kind == 'walk') {
77
+ let hops = c.dir == '->' ? chain(v, c.path) : undefined;
78
+ return new Set(hops ? [
79
+ hops[0].comp
80
+ ] : []);
81
+ }
82
+ if (c.kind != 'pred' || c.not || c.where || v.assoc(c.path[0]) || c.op == '?' || c.op == '!=' || missing(c)) return new Set();
83
+ if (c.path.length == 1 && c.path[0] == 'kind') return new Set();
84
+ let hops = v.aim(c.path.join('.'), bare(c));
85
+ return hops.length == 1 && hops[0].comp != 'entity' && (!hops[0].prop || worn(hops[0].comp, hops[0].prop)) ? new Set([
86
+ hops[0].comp
87
+ ]) : new Set();
88
+ };
89
+ /** What a parsed query reads, or `null` when it cannot be said. */ export let interest = (ast, v, worn)=>{
90
+ let near = new Set();
91
+ let far = new Set();
92
+ let via = new Map();
93
+ let whole = false;
94
+ let alone = true;
95
+ let unseen = false;
96
+ let path = (p, facet, into)=>{
97
+ let assoc = v.assoc(p[0]);
98
+ let hops = assoc ? [
99
+ assoc,
100
+ ...p.length > 1 ? v.aim(p.slice(1).join('.')) : []
101
+ ] : v.aim(p.join('.'), facet);
102
+ hops.forEach((h, i)=>{
103
+ if (v.comp(h.comp)?.computed) unseen = true;
104
+ (i || assoc ? far : into).add(h.comp);
105
+ // A status ladder reads marks on this same entity. They can change
106
+ // membership without a patch to the component whose status is queried.
107
+ if (h.prop == 'status') {
108
+ for (let rung of v.comp(h.comp)?.ladder?.rungs ?? []){
109
+ ;
110
+ (i || assoc ? far : into).add(rung.comp);
111
+ }
112
+ }
113
+ let d = v.prop(h.comp, h.prop);
114
+ if (!d?.computed) return;
115
+ if (!d.reads) throw new Opaque();
116
+ for (let r of d.reads){
117
+ let [comp, prop, extra] = r.split('.');
118
+ if (!i && !assoc && comp == h.comp) into.add(comp);
119
+ else if (!i && !assoc && prop && !extra && v.prop(comp, prop)?.ref == h.comp && (!via.has(comp) || via.get(comp) == prop)) via.set(comp, prop);
120
+ else far.add(comp);
121
+ }
122
+ });
123
+ };
124
+ // The components a walk steps through (@yaks/match `stepOf`): each hop of a
125
+ // chain of reference properties, or the edges of a relation.
126
+ let steps = (p)=>{
127
+ let hops = chain(v, p);
128
+ if (!hops && p.length == 1 && v.comp('edge')) return far.add('edge');
129
+ if (!hops) throw new Opaque();
130
+ for (let h of hops){
131
+ far.add(h.comp);
132
+ if (v.comp(h.comp)?.computed || v.prop(h.comp, h.prop)?.computed) {
133
+ unseen = true;
134
+ }
135
+ }
136
+ };
137
+ let walk = (cs, into)=>{
138
+ for (let c of cs){
139
+ if (c.kind == 'and' || c.kind == 'or') walk(c.clauses, into);
140
+ else if (c.kind == 'pred') {
141
+ if (c.path.length == 1 && c.path[0] == 'kind') {
142
+ // The most specific kind is decided by this entity's components.
143
+ // Any earlier kind can displace the one the query selects.
144
+ for (let kind of v.kinds)into.add(kind);
145
+ continue;
146
+ }
147
+ path(c.path, bare(c), into);
148
+ if (c.where) walk([
149
+ c.where
150
+ ], far);
151
+ } else if (c.kind == 'order') {
152
+ whole = true;
153
+ path(c.value.replace(/^-/, '').split('.'), false, into);
154
+ } else if (c.kind == 'tally' || c.kind == 'distinct') {
155
+ whole = true;
156
+ path(c.path, false, into);
157
+ } else if (c.kind == 'fields') {
158
+ for (let f of c.fields)path(f.path, false, into);
159
+ } else if (c.kind == 'walk') {
160
+ steps(c.path);
161
+ } else if (c.kind == 'refs' && c.op == '=' && c.value) {
162
+ alone = false;
163
+ for (let [comp] of v.refProps())into.add(comp);
164
+ } else if (c.kind == 'count' || c.kind == 'limit') {
165
+ whole = true;
166
+ } else if (c.kind == 'after') {
167
+ whole = true;
168
+ alone = false;
169
+ } else if (!QUIET.has(c.kind)) throw new Opaque();
170
+ }
171
+ };
172
+ try {
173
+ walk(ast.clauses, near);
174
+ let own = [
175
+ ...required(ast, v, worn)
176
+ ];
177
+ // An exact reference at the top level of an AND belongs to every
178
+ // member. It can rule out a changed nonmember before a window re-reads.
179
+ let fixed = ast.clauses.flatMap((c)=>{
180
+ if (c.kind != 'pred' || c.path.length != 2 || c.op != '=' || c.value?.kind != 'scalar' || !c.value.raw || v.assoc(c.path[0])) return [];
181
+ let [hop, ...more] = v.aim(c.path.join('.'));
182
+ return !more.length && worn(hop.comp, hop.prop) && v.prop(hop.comp, hop.prop)?.category == 'ref' ? [
183
+ {
184
+ ...hop,
185
+ value: c.value.raw
186
+ }
187
+ ] : [];
188
+ });
189
+ // A backlink query at the top level of an AND, naming an eid as stored
190
+ // (a person's id such as P-19 is resolved only by the read).
191
+ let backlink = ast.clauses.find((c)=>c.kind == 'refs' && c.op == '=' && minted(c.value));
192
+ let refs = backlink && {
193
+ eid: backlink.value,
194
+ props: v.refProps()
195
+ };
196
+ alone &&= !far.size && !via.size && !unseen;
197
+ return {
198
+ own,
199
+ near,
200
+ far,
201
+ via,
202
+ fixed,
203
+ refs,
204
+ whole,
205
+ alone,
206
+ unseen
207
+ };
208
+ } catch {
209
+ return null;
210
+ }
211
+ };
212
+ let any = (want, has)=>[
213
+ ...has
214
+ ].some((c)=>want.has(c));
215
+ /** Whether an entity, as it is now, can be in a backlink query's answer: it
216
+ * holds a reference to the entity the query names. Any entity can be, for a
217
+ * query that names none. */ export let refers = (i, b)=>!i.refs || i.refs.props.some(([comp, prop])=>b?.[comp]?.[prop] === i.refs.eid);
218
+ /**
219
+ * Whether a change to one entity can move the answer: `named` are the
220
+ * components the change wrote or removed, `worn` the ones the entity wears
221
+ * now. A member, or an entity storage no longer holds, is always asked about
222
+ * by the caller; this decides the rest.
223
+ */ export let cares = (i, named, worn)=>any(i.far, named) || any(i.far, worn) || (i.own.length ? i.own.every((c)=>worn.has(c)) : any(i.near, named) || any(i.near, worn));
224
+ //# sourceMappingURL=interest.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"interest.js","sources":["./interest.ts"],"names":[],"mappings":"AAAA,8EAA8E;AAC9E,6EAA6E;AAC7E,+EAA+E;AAC/E,4EAA4E;AAC5E,8CAA8C;AAC9C,EAAE;AACF,+EAA+E;AAC/E,6EAA6E;AAC7E,sEAAsE;AACtE,+EAA+E;AAC/E,uBAAuB;AACvB,4EAA4E;AAC5E,oEAAoE;AACpE,2EAA2E;AAC3E,+EAA+E;AAC/E,8EAA8E;AAC9E,2EAA2E;AAC3E,uDAAuD;AACvD,uEAAuE;AACvE,0EAA0E;AAC1E,8EAA8E;AAC9E,yCAAyC;AACzC,8EAA8E;AAC9E,4EAA4E;AAC5E,+EAA+E;AAC/E,0BAA0B;AAC1B,EAAE;AACF,0EAA0E;AAC1E,0EAA0E;AAC1E,8EAA8E;AAC9E,EAAE;AACF,6EAA6E;AAC7E,6EAA6E;AAC7E,2EAA2E;AAC3E,mDAAmD;AACnD,EAAE;AACF,6EAA6E;AAC7E,mEAAmE;AAEnE,SAAmB,IAAI,2BAAwD;AAE/E,SAAsB,MAAM,2BAAgC;AAe5D,wEAAwE;AACxE,IAAI,QAAQ,IAAI,IAAI;EAAC;EAAS;CAAQ;AAEtC,MAAM,eAAe;AAAO;AAE5B,8EAA8E;AAC9E,6EAA6E;AAC7E,iDAAiD;AACjD,IAAI,UAAU,CAAC,IACb,EAAE,IAAI,IAAI,UAAU,EAAE,EAAE,IAAI,OAA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B,MAC5B;OAAI;GAAI,CAAC,IAAI,CAAC,CAAC,IAAM,KAAK,GAAG,CAAC;AAEhC;;0BAE0B,GAC1B,OAAO,IAAI,SAAS,CAAC,GAAa,IAChC,CAAC,EAAE,IAAI,IACP,EAAE,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC,MAAM,KAAK,GAC7B,AAAC,GAAG,CAAC,KAAK,EAA0C,CAAC,KAAK,KAAK,EAAE,IAAI,CAAE,GAAG,EAC3E;AAEH;;;;;CAKC,GACD,OAAO,IAAI,QAAQ,CACjB,GACA,OACA,OAEA,IAAI,EAAE,GAAG,EAAE,UAAU,IAAI,EAAE,GAAG,EAAE,SAChC,CAAC,EAAE,GAAG,CAAC,MAAM,GACT,EAAE,GAAG,CAAC,KAAK,CAAC,CAAC,IAAM,KAAK,GAAG,CAAC,MAC5B,IAAI,EAAE,IAAI,EAAE,UAAU,IAAI,EAAE,IAAI,EAAE,KAAK,EAAC"}
package/interest.ts ADDED
@@ -0,0 +1,264 @@
1
+ // What a subscription's answer is read from, taken off its query once when it
2
+ // opens, so a commit that touches none of it does not run the query again. A
3
+ // refresh subscription runs its whole query on every commit it is asked about,
4
+ // so a commit that could not change the answer is a read wasted on the read
5
+ // thread every tab and every `/query` shares.
6
+ //
7
+ // own components every member wears: presence and direct positive property
8
+ // tests required by the query, a computed property's among them when
9
+ // its value needs the component (the store's `worn`), and the
10
+ // reference a walk toward its target steps from. An entity missing one
11
+ // cannot join.
12
+ // near the components the query reads on each entity itself, which stand
13
+ // in for `own` when there is no presence test to narrow it.
14
+ // far components read on other entities whose owner cannot be located.
15
+ // via components whose reference names the entity with the computed value.
16
+ // An entry changing `session.status`, for example, names its session.
17
+ // fixed exact reference values every member has, which exclude unrelated
18
+ // writes from a window's whole-answer refresh.
19
+ // refs the entity a backlink query (`.refs=<eid>`) names, and every
20
+ // reference property that can name it: only an entity holding one
21
+ // equal to it can join, so a write to anything else leaves the answer
22
+ // where it was, windowed or not.
23
+ // alone each entity alone says whether it is in the answer: the query reads
24
+ // nothing far or via another entity, and keeps no cursor (`.after`)
25
+ // and no backlink (`.refs`). Deleting an entity outside such an answer
26
+ // cannot move it.
27
+ //
28
+ // unseen the query reads a computed component, whose entities are in no
29
+ // commit's bundles: the journal's `_change` gains rows with every
30
+ // commit, beside what it applied. Every commit reads it again, whole.
31
+ //
32
+ // A walk (`.filed.project->P-19`, `.requires->T-42`) reads its path on every
33
+ // entity it passes through, none of which a commit's bundles name as a step:
34
+ // each hop of a chain of references is far, and so are a relation's edges,
35
+ // which wear `edge` beside their tag (@yaks/edge).
36
+ //
37
+ // `null` is a query this cannot place — a text term, a neighbour, a computed
38
+ // property that declares no `reads` — and every commit reaches it.
39
+
40
+ import { type And, bare, type Clause, type Refs } from '@yaks/query'
41
+ import type { Hop, Vocab } from 'jsr:@yaks/vocab@0.2.36'
42
+ import { type Bundle, minted } from '@yaks/graph'
43
+
44
+ /** The components one subscription's answer is read from. */
45
+ export type Interest = {
46
+ own: string[]
47
+ near: Set<string>
48
+ far: Set<string>
49
+ via: Map<string, string>
50
+ fixed: { comp: string; prop: string; value: string }[]
51
+ refs?: { eid: string; props: [string, string][] }
52
+ whole: boolean
53
+ alone: boolean
54
+ unseen: boolean
55
+ }
56
+
57
+ // Clauses that shape or bound the answer without reading any component.
58
+ let QUIET = new Set(['every', 'never'])
59
+
60
+ class Opaque extends Error {}
61
+
62
+ // A disjunction requires only what every arm requires; a conjunction requires
63
+ // all of its clauses. Missing properties satisfy absence and not-equals, and
64
+ // an empty equality alternative can do the same.
65
+ let missing = (c: Clause): boolean =>
66
+ c.kind == 'pred' && c.op == '=' && (
67
+ c.value?.kind == 'scalar' && !c.value.raw ||
68
+ c.value?.kind == 'list' &&
69
+ c.value.items.some((v) => v.kind == 'scalar' && !v.raw)
70
+ )
71
+
72
+ // The hops a walk's path steps through when it is a chain of reference
73
+ // properties (`.filed.project`), or `undefined` for a relation's name.
74
+ let chain = (v: Vocab, path: string[]): Hop[] | undefined => {
75
+ let hops: Hop[] = []
76
+ try {
77
+ hops = v.aim(path.join('.'))
78
+ } catch {
79
+ // a relation's name, which is no path
80
+ }
81
+ return hops.length > 0 &&
82
+ hops.every((h) => h.prop && v.prop(h.comp, h.prop)?.category == 'ref')
83
+ ? hops
84
+ : undefined
85
+ }
86
+
87
+ let required = (
88
+ c: Clause,
89
+ v: Vocab,
90
+ worn: (comp: string, prop: string) => boolean,
91
+ ): Set<string> => {
92
+ if (c.kind == 'and') {
93
+ return new Set(c.clauses.flatMap((part) => [...required(part, v, worn)]))
94
+ }
95
+ if (c.kind == 'or') {
96
+ let [first, ...rest] = c.clauses.map((part) => required(part, v, worn))
97
+ return new Set(
98
+ [...first].filter((comp) => rest.every((arm) => arm.has(comp))),
99
+ )
100
+ }
101
+ // What walks a chain toward its target starts from a reference of its own.
102
+ if (c.kind == 'walk') {
103
+ let hops = c.dir == '->' ? chain(v, c.path) : undefined
104
+ return new Set(hops ? [hops[0].comp] : [])
105
+ }
106
+ if (
107
+ c.kind != 'pred' || c.not || c.where || v.assoc(c.path[0]) ||
108
+ c.op == '?' || c.op == '!=' || missing(c)
109
+ ) return new Set()
110
+ if (c.path.length == 1 && c.path[0] == 'kind') return new Set()
111
+ let hops = v.aim(c.path.join('.'), bare(c))
112
+ return hops.length == 1 && hops[0].comp != 'entity' &&
113
+ (!hops[0].prop || worn(hops[0].comp, hops[0].prop))
114
+ ? new Set([hops[0].comp])
115
+ : new Set()
116
+ }
117
+
118
+ /** What a parsed query reads, or `null` when it cannot be said. */
119
+ export let interest = (
120
+ ast: And,
121
+ v: Vocab,
122
+ worn: (comp: string, prop: string) => boolean,
123
+ ): Interest | null => {
124
+ let near = new Set<string>()
125
+ let far = new Set<string>()
126
+ let via = new Map<string, string>()
127
+ let whole = false
128
+ let alone = true
129
+ let unseen = false
130
+ let path = (p: string[], facet: boolean, into: Set<string>) => {
131
+ let assoc = v.assoc(p[0])
132
+ let hops = assoc
133
+ ? [assoc, ...p.length > 1 ? v.aim(p.slice(1).join('.')) : []]
134
+ : v.aim(p.join('.'), facet)
135
+ hops.forEach((h, i) => {
136
+ if (v.comp(h.comp)?.computed) unseen = true
137
+ ;(i || assoc ? far : into).add(h.comp)
138
+ // A status ladder reads marks on this same entity. They can change
139
+ // membership without a patch to the component whose status is queried.
140
+ if (h.prop == 'status') {
141
+ for (let rung of v.comp(h.comp)?.ladder?.rungs ?? []) {
142
+ ;(i || assoc ? far : into).add(rung.comp)
143
+ }
144
+ }
145
+ let d = v.prop(h.comp, h.prop)
146
+ if (!d?.computed) return
147
+ if (!d.reads) throw new Opaque()
148
+ for (let r of d.reads) {
149
+ let [comp, prop, extra] = r.split('.')
150
+ if (
151
+ !i && !assoc && comp == h.comp
152
+ ) into.add(comp)
153
+ else if (
154
+ !i && !assoc && prop && !extra &&
155
+ v.prop(comp, prop)?.ref == h.comp &&
156
+ (!via.has(comp) || via.get(comp) == prop)
157
+ ) via.set(comp, prop)
158
+ else far.add(comp)
159
+ }
160
+ })
161
+ }
162
+ // The components a walk steps through (@yaks/match `stepOf`): each hop of a
163
+ // chain of reference properties, or the edges of a relation.
164
+ let steps = (p: string[]) => {
165
+ let hops = chain(v, p)
166
+ if (!hops && p.length == 1 && v.comp('edge')) return far.add('edge')
167
+ if (!hops) throw new Opaque()
168
+ for (let h of hops) {
169
+ far.add(h.comp)
170
+ if (v.comp(h.comp)?.computed || v.prop(h.comp, h.prop)?.computed) {
171
+ unseen = true
172
+ }
173
+ }
174
+ }
175
+ let walk = (cs: Clause[], into: Set<string>) => {
176
+ for (let c of cs) {
177
+ if (c.kind == 'and' || c.kind == 'or') walk(c.clauses, into)
178
+ else if (c.kind == 'pred') {
179
+ if (c.path.length == 1 && c.path[0] == 'kind') {
180
+ // The most specific kind is decided by this entity's components.
181
+ // Any earlier kind can displace the one the query selects.
182
+ for (let kind of v.kinds) into.add(kind)
183
+ continue
184
+ }
185
+ path(c.path, bare(c), into)
186
+ if (c.where) walk([c.where], far)
187
+ } else if (c.kind == 'order') {
188
+ whole = true
189
+ path(c.value.replace(/^-/, '').split('.'), false, into)
190
+ } else if (c.kind == 'tally' || c.kind == 'distinct') {
191
+ whole = true
192
+ path(c.path, false, into)
193
+ } else if (c.kind == 'fields') {
194
+ for (let f of c.fields) path(f.path, false, into)
195
+ } else if (c.kind == 'walk') {
196
+ steps(c.path)
197
+ } else if (c.kind == 'refs' && c.op == '=' && c.value) {
198
+ alone = false
199
+ for (let [comp] of v.refProps()) into.add(comp)
200
+ } else if (c.kind == 'count' || c.kind == 'limit') {
201
+ whole = true
202
+ } else if (c.kind == 'after') {
203
+ whole = true
204
+ alone = false
205
+ } else if (!QUIET.has(c.kind)) throw new Opaque()
206
+ }
207
+ }
208
+ try {
209
+ walk(ast.clauses, near)
210
+ let own = [...required(ast, v, worn)]
211
+ // An exact reference at the top level of an AND belongs to every
212
+ // member. It can rule out a changed nonmember before a window re-reads.
213
+ let fixed = ast.clauses.flatMap((c) => {
214
+ if (
215
+ c.kind != 'pred' || c.path.length != 2 || c.op != '=' ||
216
+ c.value?.kind != 'scalar' || !c.value.raw ||
217
+ v.assoc(c.path[0])
218
+ ) return []
219
+ let [hop, ...more] = v.aim(c.path.join('.'))
220
+ return !more.length && worn(hop.comp, hop.prop) &&
221
+ v.prop(hop.comp, hop.prop)?.category == 'ref'
222
+ ? [{ ...hop, value: c.value.raw }]
223
+ : []
224
+ })
225
+ // A backlink query at the top level of an AND, naming an eid as stored
226
+ // (a person's id such as P-19 is resolved only by the read).
227
+ let backlink = ast.clauses.find((c): c is Refs =>
228
+ c.kind == 'refs' && c.op == '=' && minted(c.value)
229
+ )
230
+ let refs = backlink && { eid: backlink.value, props: v.refProps() }
231
+ alone &&= !far.size && !via.size && !unseen
232
+ return { own, near, far, via, fixed, refs, whole, alone, unseen }
233
+ } catch {
234
+ return null
235
+ }
236
+ }
237
+
238
+ let any = (want: Set<string>, has: Set<string>) =>
239
+ [...has].some((c) => want.has(c))
240
+
241
+ /** Whether an entity, as it is now, can be in a backlink query's answer: it
242
+ * holds a reference to the entity the query names. Any entity can be, for a
243
+ * query that names none. */
244
+ export let refers = (i: Interest, b: Bundle | undefined): boolean =>
245
+ !i.refs ||
246
+ i.refs.props.some(([comp, prop]) =>
247
+ (b?.[comp] as Record<string, unknown> | undefined)?.[prop] === i.refs!.eid
248
+ )
249
+
250
+ /**
251
+ * Whether a change to one entity can move the answer: `named` are the
252
+ * components the change wrote or removed, `worn` the ones the entity wears
253
+ * now. A member, or an entity storage no longer holds, is always asked about
254
+ * by the caller; this decides the rest.
255
+ */
256
+ export let cares = (
257
+ i: Interest,
258
+ named: Set<string>,
259
+ worn: Set<string>,
260
+ ): boolean =>
261
+ any(i.far, named) || any(i.far, worn) ||
262
+ (i.own.length
263
+ ? i.own.every((c) => worn.has(c))
264
+ : any(i.near, named) || any(i.near, worn))