@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.
- package/LICENSE +202 -0
- package/README.md +956 -2
- package/_dist/actor.d.ts +59 -0
- package/_dist/actor.d.ts.map +1 -0
- package/_dist/address.d.ts +33 -0
- package/_dist/address.d.ts.map +1 -0
- package/_dist/asset.d.ts +15 -0
- package/_dist/asset.d.ts.map +1 -0
- package/_dist/batches.d.ts +4 -0
- package/_dist/batches.d.ts.map +1 -0
- package/_dist/body.d.ts +7 -0
- package/_dist/body.d.ts.map +1 -0
- package/_dist/deno.d.ts +38 -0
- package/_dist/deno.d.ts.map +1 -0
- package/_dist/doors.d.ts +134 -0
- package/_dist/doors.d.ts.map +1 -0
- package/_dist/graph.d.ts +17 -0
- package/_dist/graph.d.ts.map +1 -0
- package/_dist/headers.d.ts +5 -0
- package/_dist/headers.d.ts.map +1 -0
- package/_dist/log.d.ts +253 -0
- package/_dist/log.d.ts.map +1 -0
- package/_dist/mod.d.ts +71 -0
- package/_dist/mod.d.ts.map +1 -0
- package/_dist/publish.d.ts +7 -0
- package/_dist/publish.d.ts.map +1 -0
- package/_dist/reached.d.ts +145 -0
- package/_dist/reached.d.ts.map +1 -0
- package/_dist/receipts.d.ts +153 -0
- package/_dist/receipts.d.ts.map +1 -0
- package/_dist/refuse.d.ts +51 -0
- package/_dist/refuse.d.ts.map +1 -0
- package/_dist/relay.d.ts +3 -0
- package/_dist/relay.d.ts.map +1 -0
- package/_dist/request.d.ts +79 -0
- package/_dist/request.d.ts.map +1 -0
- package/_dist/requests.d.ts +7 -0
- package/_dist/requests.d.ts.map +1 -0
- package/_dist/route.d.ts +71 -0
- package/_dist/route.d.ts.map +1 -0
- package/_dist/routed.d.ts +33 -0
- package/_dist/routed.d.ts.map +1 -0
- package/_dist/routes.d.ts +46 -0
- package/_dist/routes.d.ts.map +1 -0
- package/_dist/save.d.ts +6 -0
- package/_dist/save.d.ts.map +1 -0
- package/_dist/socket.d.ts +73 -0
- package/_dist/socket.d.ts.map +1 -0
- package/_dist/subs.d.ts +134 -0
- package/_dist/subs.d.ts.map +1 -0
- package/_dist/timing.d.ts +8 -0
- package/_dist/timing.d.ts.map +1 -0
- package/_dist/tools.d.ts +80 -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/access.js +32 -0
- package/access.js.map +1 -0
- package/access.ts +42 -0
- package/actor.js +75 -0
- package/actor.js.map +1 -0
- package/actor.ts +106 -0
- package/address.js +42 -0
- package/address.js.map +1 -0
- package/address.ts +49 -0
- package/admission.js +71 -0
- package/admission.js.map +1 -0
- package/admission.ts +64 -0
- package/asset.js +37 -0
- package/asset.js.map +1 -0
- package/asset.ts +47 -0
- package/batches.js +92 -0
- package/batches.js.map +1 -0
- package/batches.ts +96 -0
- package/body.js +51 -0
- package/body.js.map +1 -0
- package/body.ts +82 -0
- package/deno.js +88 -0
- package/deno.js.map +1 -0
- package/deno.json +1 -0
- package/deno.ts +150 -0
- package/doors.js +435 -0
- package/doors.js.map +1 -0
- package/doors.ts +570 -0
- package/graph.js +12 -0
- package/graph.js.map +1 -0
- package/graph.ts +23 -0
- package/headers.js +5 -0
- package/headers.js.map +1 -0
- package/headers.ts +9 -0
- package/interest.js +224 -0
- package/interest.js.map +1 -0
- package/interest.ts +264 -0
- package/log.js +1011 -0
- package/log.js.map +1 -0
- package/log.ts +1035 -0
- package/mod.js +69 -0
- package/mod.js.map +1 -0
- package/mod.ts +142 -0
- package/package.json +75 -4
- package/peer_query.js +164 -0
- package/peer_query.js.map +1 -0
- package/peer_query.ts +158 -0
- package/publish.js +7 -0
- package/publish.js.map +1 -0
- package/publish.ts +16 -0
- package/reached.js +456 -0
- package/reached.js.map +1 -0
- package/reached.ts +549 -0
- package/receipts.js +543 -0
- package/receipts.js.map +1 -0
- package/receipts.ts +749 -0
- package/refuse.js +120 -0
- package/refuse.js.map +1 -0
- package/refuse.ts +179 -0
- package/relay.js +249 -0
- package/relay.js.map +1 -0
- package/relay.ts +284 -0
- package/request.js +218 -0
- package/request.js.map +1 -0
- package/request.ts +235 -0
- package/requests.js +250 -0
- package/requests.js.map +1 -0
- package/requests.ts +257 -0
- package/route.js +139 -0
- package/route.js.map +1 -0
- package/route.ts +228 -0
- package/routed.js +25 -0
- package/routed.js.map +1 -0
- package/routed.ts +53 -0
- package/routes.js +154 -0
- package/routes.js.map +1 -0
- package/routes.ts +201 -0
- package/save.js +341 -0
- package/save.js.map +1 -0
- package/save.ts +393 -0
- package/socket.js +398 -0
- package/socket.js.map +1 -0
- package/socket.ts +496 -0
- package/subs.js +2295 -0
- package/subs.js.map +1 -0
- package/subs.ts +2979 -0
- package/timing.js +17 -0
- package/timing.js.map +1 -0
- package/timing.ts +26 -0
- package/tools.js +251 -0
- package/tools.js.map +1 -0
- package/tools.ts +315 -0
- package/vocab.js +18 -0
- package/vocab.js.map +1 -0
- package/vocab.json +93 -0
- package/vocab.ts +18 -0
package/subs.js
ADDED
|
@@ -0,0 +1,2295 @@
|
|
|
1
|
+
// Subscriptions: a saved query whose result is pushed again whenever a
|
|
2
|
+
// committed transaction changes it.
|
|
3
|
+
//
|
|
4
|
+
// The registry registers a hook on the graph's own `effect` phase, so every
|
|
5
|
+
// commit is seen — the ones that arrived through `POST /apply` and the ones
|
|
6
|
+
// the application wrote straight to the graph. A commit is handled in two
|
|
7
|
+
// steps: read the changed entities once, whole, then test them against each
|
|
8
|
+
// subscription.
|
|
9
|
+
//
|
|
10
|
+
// The writer does not wait for that. The hook starts the pass and returns, so a
|
|
11
|
+
// tool call's bookkeeping rows are answered at their commit, however many tabs
|
|
12
|
+
// are open and however long their windows take to read. Commits that arrive
|
|
13
|
+
// while a pass is still reading join the next one, so a busy writer costs the
|
|
14
|
+
// subscribers one more pass, not one per commit.
|
|
15
|
+
//
|
|
16
|
+
// Two modes, chosen when the subscription opens:
|
|
17
|
+
//
|
|
18
|
+
// Routed the query asks only about each entity itself, so it joins
|
|
19
|
+
// the registry's network (@yaks/match `net`), which every
|
|
20
|
+
// subscription of that kind shares. A changed entity moves
|
|
21
|
+
// through the network once, into the subscriptions it matches
|
|
22
|
+
// and out of the ones it left — the query is never run again,
|
|
23
|
+
// however large the set is, and a commit costs what it
|
|
24
|
+
// changed, not how many subscriptions are open.
|
|
25
|
+
// Refresh the query follows a reference, walks, counts, orders or
|
|
26
|
+
// limits, so its result can change when an entity the query
|
|
27
|
+
// never named does. A dependency with a reference back to the
|
|
28
|
+
// answer refreshes that entity alone. An unlocated dependency
|
|
29
|
+
// or a window refreshes the whole query (./interest.ts).
|
|
30
|
+
//
|
|
31
|
+
// The membership Set is what makes "this entity no longer matches" as cheap
|
|
32
|
+
// as "this entity now matches": a client cannot work out that something left
|
|
33
|
+
// its set — it never sees the row that stopped matching — so the server is
|
|
34
|
+
// what remembers who is in.
|
|
35
|
+
import { asker, asking } from "@yaks/graph/asker";
|
|
36
|
+
import { namesBetween } from "@yaks/graph";
|
|
37
|
+
import { after, groupBy, isPromise, over } from "@yaks/fp";
|
|
38
|
+
import { during, link, parent, peek, scope, unlink } from "@yaks/trace";
|
|
39
|
+
import { coalesced, composed, dead, formed } from "@yaks/graph/bundle";
|
|
40
|
+
import { transient } from "@yaks/graph/read";
|
|
41
|
+
import { aggregate, flat, named, only, project, projection, reduced } from "@yaks/graph/read";
|
|
42
|
+
import { comps } from "@yaks/graph/bundle";
|
|
43
|
+
import { Refused } from "@yaks/graph/errors";
|
|
44
|
+
import { matcher, net, rows as matchRows } from "@yaks/match";
|
|
45
|
+
import { bare, drifts, line, parse, whole as said, windowOf } from "@yaks/query";
|
|
46
|
+
import { paceOf, saveOf, syncOf } from "@yaks/vocab";
|
|
47
|
+
import { published } from "./publish.js";
|
|
48
|
+
import { cares, interest, refers } from "./interest.js";
|
|
49
|
+
import { peerPlan, peerTargets } from "./peer_query.js";
|
|
50
|
+
import { fault, refusal } from "./refuse.js";
|
|
51
|
+
import { relay as relaying } from "./relay.js";
|
|
52
|
+
import { saving } from "./save.js";
|
|
53
|
+
// Whether a frame says what the store holds: rows, members leaving, or a
|
|
54
|
+
// reduction. One carrying only relayed or transient values says what peers
|
|
55
|
+
// are doing, which no revision of the store orders.
|
|
56
|
+
let says = (f)=>!!(f.bundles || f.gone || f.peers || f.peerGone) || 'count' in f || 'tally' in f || 'distinct' in f;
|
|
57
|
+
const HELD = 512, KEPT = 8192, KEEP = 256, OPENED = 32_768;
|
|
58
|
+
// Membership and projection use storage's vocabulary; delivery uses the caller's.
|
|
59
|
+
// A subscriber's line is its own whole query (@yaks/query `whole`).
|
|
60
|
+
let queryOf = (sub, q = sub.query)=>{
|
|
61
|
+
if (!sub.ast) return said(q);
|
|
62
|
+
if (q == sub.query) return sub.ast;
|
|
63
|
+
return {
|
|
64
|
+
...sub.ast,
|
|
65
|
+
clauses: [
|
|
66
|
+
...sub.ast.clauses,
|
|
67
|
+
...parse(q.slice(sub.query.length)).clauses
|
|
68
|
+
]
|
|
69
|
+
};
|
|
70
|
+
};
|
|
71
|
+
let answered = (found)=>({
|
|
72
|
+
found,
|
|
73
|
+
reached: [],
|
|
74
|
+
covers: new Map()
|
|
75
|
+
});
|
|
76
|
+
let whole = (b)=>b;
|
|
77
|
+
// Work several owners share, cancelled only once every owner has let go:
|
|
78
|
+
// one owner cancelling ends no answer another still waits on. An owner
|
|
79
|
+
// without a signal never lets go.
|
|
80
|
+
let jointly = (work)=>{
|
|
81
|
+
let joint = new AbortController();
|
|
82
|
+
let owners = 0, held = false;
|
|
83
|
+
let offs = [];
|
|
84
|
+
let value = work(joint.signal);
|
|
85
|
+
let settled = !isPromise(value);
|
|
86
|
+
let settle = ()=>{
|
|
87
|
+
settled = true;
|
|
88
|
+
for (let off of offs.splice(0))off();
|
|
89
|
+
};
|
|
90
|
+
if (isPromise(value)) value.then(settle, settle);
|
|
91
|
+
return {
|
|
92
|
+
value,
|
|
93
|
+
cancelled: joint.signal,
|
|
94
|
+
share: (signal)=>{
|
|
95
|
+
if (settled || signal?.aborted) return;
|
|
96
|
+
if (!signal) return void (held = true);
|
|
97
|
+
owners++;
|
|
98
|
+
let leave = ()=>{
|
|
99
|
+
if (!--owners && !held) joint.abort(signal.reason);
|
|
100
|
+
};
|
|
101
|
+
signal.addEventListener('abort', leave, {
|
|
102
|
+
once: true
|
|
103
|
+
});
|
|
104
|
+
offs.push(()=>signal.removeEventListener('abort', leave));
|
|
105
|
+
}
|
|
106
|
+
};
|
|
107
|
+
};
|
|
108
|
+
let covering = (bundles, of)=>Object.fromEntries(bundles.map((b)=>[
|
|
109
|
+
b.entity.eid,
|
|
110
|
+
of(b.entity.eid)
|
|
111
|
+
]));
|
|
112
|
+
// Whether a subscription holds the members of an answer each entity alone
|
|
113
|
+
// decides (./interest.ts `alone`), so that deleting an entity outside them
|
|
114
|
+
// cannot move it. An aggregate holds only its value, a deferred watch nothing
|
|
115
|
+
// yet, and a peer or view answer is read from more than storage.
|
|
116
|
+
let keeps = (sub)=>!!sub.reads?.alone && !sub.agg && !sub.deferred && !sub.peer && !sub.view;
|
|
117
|
+
// A write matters when it changes what the query tests or sends. A whole-row
|
|
118
|
+
// projection also observes every component of an entity already in its set.
|
|
119
|
+
// A deletion matters unless the subscription keeps its members and the entity
|
|
120
|
+
// is not one of them.
|
|
121
|
+
let notices = (sub, b)=>{
|
|
122
|
+
if (b.$delete) return !keeps(sub) || sub.members.has(b.entity.eid);
|
|
123
|
+
if (!sub.reads || sub.reads.unseen || sub.deferred && sub.want === null) return true;
|
|
124
|
+
if (sub.want === null && sub.members.has(b.entity.eid)) return true;
|
|
125
|
+
let i = sub.reads;
|
|
126
|
+
if (b.created && !i.own.length) return true;
|
|
127
|
+
return Object.keys(b).some((c)=>c != 'entity' && (i.near.has(c) || i.far.has(c) || i.via.has(c)));
|
|
128
|
+
};
|
|
129
|
+
// `get` is only for deciding changed entities' membership and filling their
|
|
130
|
+
// frames. Read the union of what open queries test and project, unless one
|
|
131
|
+
// query needs the entire row or a peer cache must retain it.
|
|
132
|
+
let components = (subs)=>{
|
|
133
|
+
let names = new Set();
|
|
134
|
+
for (let s of subs){
|
|
135
|
+
if (s.peer || !s.reads || s.want === null) return undefined;
|
|
136
|
+
for (let c of s.reads.near)names.add(c);
|
|
137
|
+
for (let c of s.reads.far)names.add(c);
|
|
138
|
+
for (let c of s.reads.via.keys())names.add(c);
|
|
139
|
+
for (let c of s.want ?? [])names.add(c);
|
|
140
|
+
}
|
|
141
|
+
names.delete('entity');
|
|
142
|
+
return [
|
|
143
|
+
...names
|
|
144
|
+
];
|
|
145
|
+
};
|
|
146
|
+
let touches = (applied, now)=>{
|
|
147
|
+
let out = new Map();
|
|
148
|
+
for (let b of now){
|
|
149
|
+
out.set(b.entity.eid, {
|
|
150
|
+
named: new Set(),
|
|
151
|
+
props: new Set(),
|
|
152
|
+
worn: new Set(Object.keys(b)),
|
|
153
|
+
bundle: b,
|
|
154
|
+
born: false
|
|
155
|
+
});
|
|
156
|
+
}
|
|
157
|
+
for (let b of applied){
|
|
158
|
+
let t = out.get(b.entity.eid) ?? {
|
|
159
|
+
named: new Set(),
|
|
160
|
+
props: new Set(),
|
|
161
|
+
worn: null,
|
|
162
|
+
born: false
|
|
163
|
+
};
|
|
164
|
+
out.set(b.entity.eid, t);
|
|
165
|
+
if (b.$delete) t.worn = null;
|
|
166
|
+
if (b.created) t.born = true;
|
|
167
|
+
for (let k of Object.keys(b)){
|
|
168
|
+
if (k == 'entity' || k[0] == '$') continue;
|
|
169
|
+
t.named.add(k);
|
|
170
|
+
let patch = b[k];
|
|
171
|
+
if (patch && typeof patch == 'object') {
|
|
172
|
+
for (let prop of Object.keys(patch))t.props.add(k + '.' + prop);
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
return out;
|
|
177
|
+
};
|
|
178
|
+
// The answer's entities a write can have moved. `null` means that the query
|
|
179
|
+
// must run whole: a far dependency has no owner, or a reference moved/deleted
|
|
180
|
+
// and the old owner cannot be read after the commit.
|
|
181
|
+
let affected = (sub, touch, relevant)=>{
|
|
182
|
+
let i = sub.reads;
|
|
183
|
+
if (!i || i.unseen) return null;
|
|
184
|
+
let ids = new Set();
|
|
185
|
+
for (let [eid, t] of touch){
|
|
186
|
+
if (!relevant.has(eid)) continue;
|
|
187
|
+
if (!t.worn) return null;
|
|
188
|
+
if ([
|
|
189
|
+
...i.far
|
|
190
|
+
].some((c)=>t.named.has(c) || t.worn.has(c))) {
|
|
191
|
+
return null;
|
|
192
|
+
}
|
|
193
|
+
if (sub.members.has(eid) || refers(i, t.bundle) && (t.born && !i.own.length || cares(i, t.named, t.worn) && i.fixed.every(({ comp, prop, value })=>t.bundle?.[comp]?.[prop] === value))) ids.add(eid);
|
|
194
|
+
for (let [comp, prop] of i.via){
|
|
195
|
+
if (!t.named.has(comp) && !t.worn.has(comp)) continue;
|
|
196
|
+
let owner = t.bundle?.[comp]?.[prop];
|
|
197
|
+
if (typeof owner != 'string') return null;
|
|
198
|
+
if (t.props.has(comp + '.' + prop) && !t.born) return null;
|
|
199
|
+
ids.add(owner);
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
return ids.size ? i.whole ? null : ids : undefined;
|
|
203
|
+
};
|
|
204
|
+
/**
|
|
205
|
+
* A subscription registry over a graph. It registers an `effect` hook on that
|
|
206
|
+
* graph, so every transaction that commits — through this API or not —
|
|
207
|
+
* reaches whoever is watching. Build one per graph;
|
|
208
|
+
* {@link https://jsr.io/@yaks/api | api()} makes one when you do not pass
|
|
209
|
+
* your own.
|
|
210
|
+
*
|
|
211
|
+
* ```ts ignore
|
|
212
|
+
* let subs = subscriptions(graph)
|
|
213
|
+
* subs.open(sink, 'cheap', '.book&.book.price<20')
|
|
214
|
+
* ```
|
|
215
|
+
*/ export let subscriptions = (graph, opts = {})=>{
|
|
216
|
+
let base = graph;
|
|
217
|
+
let ending = new AbortController();
|
|
218
|
+
let readingOpts = (opts)=>{
|
|
219
|
+
ending.signal.throwIfAborted();
|
|
220
|
+
return {
|
|
221
|
+
...opts,
|
|
222
|
+
signal: opts?.signal ? AbortSignal.any([
|
|
223
|
+
ending.signal,
|
|
224
|
+
opts.signal
|
|
225
|
+
]) : ending.signal
|
|
226
|
+
};
|
|
227
|
+
};
|
|
228
|
+
graph = {
|
|
229
|
+
...base,
|
|
230
|
+
get vocab () {
|
|
231
|
+
return base.vocab;
|
|
232
|
+
},
|
|
233
|
+
read: (q, opts)=>base.read(q, readingOpts(opts)),
|
|
234
|
+
rows: (q, opts)=>base.rows(q, readingOpts(opts)),
|
|
235
|
+
get: (ids, comps, opts)=>base.get(ids, comps, readingOpts(opts))
|
|
236
|
+
};
|
|
237
|
+
// The store's revision as this registry knows it (Frame `revision`): it
|
|
238
|
+
// moves on with every commit the registry hears, and with every change it
|
|
239
|
+
// learns of only from the store's token. It starts at the time, so a
|
|
240
|
+
// registry made again after a restart starts past every frame the one
|
|
241
|
+
// before it sent.
|
|
242
|
+
let now = opts.now ?? Date.now;
|
|
243
|
+
let rev = now();
|
|
244
|
+
let advance = ()=>{
|
|
245
|
+
rev = Math.max(rev + 1, now());
|
|
246
|
+
opened.clear();
|
|
247
|
+
aims.clear();
|
|
248
|
+
};
|
|
249
|
+
let revision = opts.revision?.();
|
|
250
|
+
let knownRevision = after(revision, (value)=>{
|
|
251
|
+
revision = value;
|
|
252
|
+
});
|
|
253
|
+
if (isPromise(knownRevision)) {
|
|
254
|
+
knownRevision = knownRevision.catch((error)=>{
|
|
255
|
+
revision = undefined;
|
|
256
|
+
fault(error, 'store revision');
|
|
257
|
+
});
|
|
258
|
+
}
|
|
259
|
+
let held = new Map();
|
|
260
|
+
let all = ()=>[
|
|
261
|
+
...held.values()
|
|
262
|
+
].flatMap((m)=>[
|
|
263
|
+
...m.values()
|
|
264
|
+
].filter((s)=>!s.loading));
|
|
265
|
+
let durableNet = net(graph.vocab);
|
|
266
|
+
let peerNet = net(graph.vocab);
|
|
267
|
+
// Keep storage rows only while a peer value makes their entity relevant.
|
|
268
|
+
// A missing row is cached too: a peer-only entity must not hit storage on
|
|
269
|
+
// every movement.
|
|
270
|
+
let peerRows = new Map();
|
|
271
|
+
// A reference's durable answer changes only with a commit that writes its
|
|
272
|
+
// component, or writes an entity among its rows. Keep its full rows across
|
|
273
|
+
// peer movements and other commits, including empty answers, with a fixed
|
|
274
|
+
// bound for spaces that hold many peer values at once.
|
|
275
|
+
let backlinks = new Map();
|
|
276
|
+
const BACKLINK_LIMIT = 512;
|
|
277
|
+
let linkKey = (comp, prop, peer)=>JSON.stringify([
|
|
278
|
+
comp,
|
|
279
|
+
prop,
|
|
280
|
+
peer
|
|
281
|
+
]);
|
|
282
|
+
let linked = (ref, targets)=>{
|
|
283
|
+
let answers = new Map();
|
|
284
|
+
for (let peer of targets){
|
|
285
|
+
let rows = backlinks.get(linkKey(ref.comp, ref.prop, peer))?.rows;
|
|
286
|
+
if (rows) answers.set(peer, rows);
|
|
287
|
+
}
|
|
288
|
+
let missing = targets.filter((peer)=>!answers.has(peer));
|
|
289
|
+
let fresh = missing.length ? after(graph.read(line`.${ref.comp}.${ref.prop}=${missing}`, {
|
|
290
|
+
durable: true,
|
|
291
|
+
native: true
|
|
292
|
+
}), (refs)=>refs.length ? graph.get(refs.map((b)=>b.entity.eid), undefined, {
|
|
293
|
+
native: true
|
|
294
|
+
}) : []) : [];
|
|
295
|
+
return after(fresh, (rows)=>{
|
|
296
|
+
for (let peer of missing){
|
|
297
|
+
let key = linkKey(ref.comp, ref.prop, peer);
|
|
298
|
+
let matching = rows.filter((b)=>b[ref.comp]?.[ref.prop] == peer);
|
|
299
|
+
answers.set(peer, matching);
|
|
300
|
+
backlinks.set(key, {
|
|
301
|
+
comp: ref.comp,
|
|
302
|
+
peer,
|
|
303
|
+
rows: matching
|
|
304
|
+
});
|
|
305
|
+
}
|
|
306
|
+
while(backlinks.size > BACKLINK_LIMIT){
|
|
307
|
+
backlinks.delete(backlinks.keys().next().value);
|
|
308
|
+
}
|
|
309
|
+
let held = peers.referencing(ref.comp, ref.prop, targets);
|
|
310
|
+
return [
|
|
311
|
+
...targets.flatMap((peer)=>answers.get(peer) ?? []),
|
|
312
|
+
...held
|
|
313
|
+
];
|
|
314
|
+
});
|
|
315
|
+
};
|
|
316
|
+
let scopedNets = new Map();
|
|
317
|
+
let network = (sub)=>{
|
|
318
|
+
if (sub.network) return sub.network;
|
|
319
|
+
if (sub.opts?.person === undefined && !graph.scope(sub.opts).visibility) return sub.network = sub.peer ? peerNet : durableNet;
|
|
320
|
+
let key = JSON.stringify([
|
|
321
|
+
sub.peer,
|
|
322
|
+
sub.opts ?? null
|
|
323
|
+
]);
|
|
324
|
+
let found = scopedNets.get(key);
|
|
325
|
+
if (!found) scopedNets.set(key, found = net(graph.vocab));
|
|
326
|
+
return sub.network = found;
|
|
327
|
+
};
|
|
328
|
+
let dropNetwork = (sub)=>{
|
|
329
|
+
let routing = network(sub);
|
|
330
|
+
routing.drop(sub);
|
|
331
|
+
sub.routed = false;
|
|
332
|
+
if ([
|
|
333
|
+
...all(),
|
|
334
|
+
...kept.values()
|
|
335
|
+
].some((s)=>s.routed && s.network === routing)) return;
|
|
336
|
+
for (let [key, found] of scopedNets){
|
|
337
|
+
if (found === routing) scopedNets.delete(key);
|
|
338
|
+
}
|
|
339
|
+
};
|
|
340
|
+
let routable = (sub)=>!!sub.reads?.alone && !sub.reads.unseen && !sub.reads.far.size && !sub.reads.via.size && !sub.plan?.reaches.length && sub.opts?.now == null;
|
|
341
|
+
let peerComp = (name)=>syncOf(graph.vocab, name) == 'peers';
|
|
342
|
+
let references = (sub)=>{
|
|
343
|
+
let vocab = sub.view?.vocab ?? graph.vocab;
|
|
344
|
+
return [
|
|
345
|
+
...sub.want ?? vocab.all
|
|
346
|
+
].some((name)=>vocab.props(name).some((prop)=>vocab.prop(name, prop)?.category == 'ref'));
|
|
347
|
+
};
|
|
348
|
+
// Whether an answer can show a reference to `eid`: one its sent rows hold,
|
|
349
|
+
// or any while its rows are still being read or are read from more than
|
|
350
|
+
// its members (a view, a projection's peers, relayed values).
|
|
351
|
+
let shows = (sub, eid)=>sub.loading || !!sub.view || !!sub.plan?.reaches.length || sub.peer || [
|
|
352
|
+
...sub.targets.values()
|
|
353
|
+
].some((to)=>to.includes(eid));
|
|
354
|
+
// Reference values depend on their targets' visibility, even when the
|
|
355
|
+
// unchanged referring entity remains in a routed or retained answer. A
|
|
356
|
+
// deleted target can only hide the references to it, so it moves only an
|
|
357
|
+
// answer that shows one; a write to what the policy reads may also reveal
|
|
358
|
+
// a reference the answer hides.
|
|
359
|
+
let policyMoved = (sub, applied)=>!!sub.policyDependencies?.length && applied.some((b)=>dead(b) || sub.policyDependencies.some((name)=>name in b)) || sub.policyRefs && sub.policy !== undefined && (!sub.policy || applied.some((b)=>dead(b) ? shows(sub, b.entity.eid) : comps(b).some(([name])=>sub.policy.near.has(name) || sub.policy.far.has(name) || sub.policy.via.has(name))));
|
|
360
|
+
// Policies read dependencies without changing what the caller selected or
|
|
361
|
+
// requiring their private components on otherwise public members.
|
|
362
|
+
let dependencies = (reads, readOpts)=>{
|
|
363
|
+
let visibility = graph.scope(readOpts).visibility;
|
|
364
|
+
let policies = [
|
|
365
|
+
visibility?.entity,
|
|
366
|
+
...Object.values(visibility?.components ?? {})
|
|
367
|
+
].filter((q)=>q !== undefined);
|
|
368
|
+
if (!reads || !policies.length) return reads;
|
|
369
|
+
let policy = interest({
|
|
370
|
+
kind: 'and',
|
|
371
|
+
clauses: policies.map((q)=>typeof q == 'string' ? parse(q) : q)
|
|
372
|
+
}, graph.vocab, graph.worn);
|
|
373
|
+
if (!policy) return null;
|
|
374
|
+
let via = new Map(reads.via), far = new Set([
|
|
375
|
+
...reads.far,
|
|
376
|
+
...policy.far
|
|
377
|
+
]);
|
|
378
|
+
for (let [comp, prop] of policy.via){
|
|
379
|
+
if (via.has(comp) && via.get(comp) != prop) {
|
|
380
|
+
via.delete(comp);
|
|
381
|
+
far.add(comp);
|
|
382
|
+
} else if (!far.has(comp)) via.set(comp, prop);
|
|
383
|
+
}
|
|
384
|
+
return {
|
|
385
|
+
...reads,
|
|
386
|
+
near: new Set([
|
|
387
|
+
...reads.near,
|
|
388
|
+
...policy.near
|
|
389
|
+
]),
|
|
390
|
+
far,
|
|
391
|
+
via,
|
|
392
|
+
whole: reads.whole || policy.whole,
|
|
393
|
+
alone: reads.alone && policy.alone,
|
|
394
|
+
unseen: reads.unseen || policy.unseen
|
|
395
|
+
};
|
|
396
|
+
};
|
|
397
|
+
let pendingWork;
|
|
398
|
+
let observers = new Set();
|
|
399
|
+
let observed = [];
|
|
400
|
+
let observing = false;
|
|
401
|
+
let notify = (bundles)=>{
|
|
402
|
+
if (!observers.size || !bundles.length) return;
|
|
403
|
+
observed.push(...bundles);
|
|
404
|
+
if (observing) return;
|
|
405
|
+
observing = true;
|
|
406
|
+
let released = ()=>{
|
|
407
|
+
// Registry work never awaits an observer: it may read this registry or
|
|
408
|
+
// write through a graph hook that queues more registry work.
|
|
409
|
+
if (pendingWork) return void pendingWork.then(released);
|
|
410
|
+
observing = false;
|
|
411
|
+
let changes = coalesced(observed);
|
|
412
|
+
observed = [];
|
|
413
|
+
for (let fn of observers){
|
|
414
|
+
try {
|
|
415
|
+
let out = fn(changes);
|
|
416
|
+
if (isPromise(out)) out.catch((err)=>fault(err, 'peer observer'));
|
|
417
|
+
} catch (err) {
|
|
418
|
+
fault(err, 'peer observer');
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
};
|
|
422
|
+
queueMicrotask(released);
|
|
423
|
+
};
|
|
424
|
+
let ordered = (fn)=>{
|
|
425
|
+
let out = pendingWork ? pendingWork.then(fn) : fn();
|
|
426
|
+
if (isPromise(out)) {
|
|
427
|
+
let done = out.then(()=>{}, ()=>{});
|
|
428
|
+
pendingWork = done;
|
|
429
|
+
done.then(()=>{
|
|
430
|
+
if (pendingWork === done) pendingWork = undefined;
|
|
431
|
+
});
|
|
432
|
+
}
|
|
433
|
+
return out;
|
|
434
|
+
};
|
|
435
|
+
// Saved peer edits belong to the writer, beyond subscription observation.
|
|
436
|
+
let saves = saving(base, (fn, ms)=>(opts.timer ?? ((fn, ms)=>{
|
|
437
|
+
let t = setTimeout(fn, ms);
|
|
438
|
+
return ()=>clearTimeout(t);
|
|
439
|
+
}))(()=>{
|
|
440
|
+
let out = ordered(fn);
|
|
441
|
+
if (isPromise(out)) out.catch((err)=>fault(err, 'peer saving'));
|
|
442
|
+
}, ms), (sink, err)=>{
|
|
443
|
+
fault(err, 'peer saving');
|
|
444
|
+
sink({
|
|
445
|
+
id: '',
|
|
446
|
+
refused: refusal(err)
|
|
447
|
+
});
|
|
448
|
+
}, opts.now);
|
|
449
|
+
let saved = (bundles)=>bundles.some((b)=>comps(b).some(([comp])=>saveOf(graph.vocab, comp) != null));
|
|
450
|
+
let relays = new Map();
|
|
451
|
+
let shared = new Map();
|
|
452
|
+
let opened = new Map();
|
|
453
|
+
let aims = new Map();
|
|
454
|
+
// Moving peer predicates can ask the same stored question. Keep that
|
|
455
|
+
// answer across watches, including empty answers, until a commit moves it.
|
|
456
|
+
let durableCandidates = new Map();
|
|
457
|
+
// The closed subscriptions keeping a shared answer, longest unasked first.
|
|
458
|
+
let kept = new Map();
|
|
459
|
+
let keyOf = (sub)=>JSON.stringify([
|
|
460
|
+
sub.query,
|
|
461
|
+
sub.opts ?? null
|
|
462
|
+
]);
|
|
463
|
+
let unshare = (share)=>{
|
|
464
|
+
if (shared.get(share.key) === share) shared.delete(share.key);
|
|
465
|
+
for (let s of share.users){
|
|
466
|
+
s.shared = undefined;
|
|
467
|
+
if (!s.kept) continue;
|
|
468
|
+
kept.delete(share.key);
|
|
469
|
+
dropNetwork(s);
|
|
470
|
+
}
|
|
471
|
+
share.users.clear();
|
|
472
|
+
};
|
|
473
|
+
// A routed subscription that reads only what each entity holds, now and
|
|
474
|
+
// later alike, takes up the answer others asking the same share, or starts
|
|
475
|
+
// one for them when its own is small enough to hold.
|
|
476
|
+
let join = (sub, ast, bundles)=>{
|
|
477
|
+
if (sub.view || sub.peer || sub.agg || sub.plan || sub.reads?.via.size || moving(ast)) return;
|
|
478
|
+
let share = shared.get(keyOf(sub));
|
|
479
|
+
if (!share) {
|
|
480
|
+
if (bundles.length > HELD) return;
|
|
481
|
+
share = {
|
|
482
|
+
key: keyOf(sub),
|
|
483
|
+
rows: new Map(bundles.map((b)=>[
|
|
484
|
+
b.entity.eid,
|
|
485
|
+
b
|
|
486
|
+
])),
|
|
487
|
+
users: new Set()
|
|
488
|
+
};
|
|
489
|
+
shared.set(share.key, share);
|
|
490
|
+
}
|
|
491
|
+
for (let k of share.users){
|
|
492
|
+
if (!k.kept) continue;
|
|
493
|
+
share.users.delete(k);
|
|
494
|
+
kept.delete(share.key);
|
|
495
|
+
dropNetwork(k);
|
|
496
|
+
}
|
|
497
|
+
share.users.add(sub);
|
|
498
|
+
sub.shared = share;
|
|
499
|
+
};
|
|
500
|
+
// A subscription let go of, by its sink closing it or by a new one under
|
|
501
|
+
// its id: the network lets go of it too, unless it is the last keeping a
|
|
502
|
+
// shared answer, which it keeps current while the answer stays kept.
|
|
503
|
+
let forget = (sub, keep = true)=>{
|
|
504
|
+
if (!sub) return;
|
|
505
|
+
sub.abort?.abort();
|
|
506
|
+
sub.abort = undefined;
|
|
507
|
+
if (!sub.routed) return;
|
|
508
|
+
let share = sub.shared;
|
|
509
|
+
share?.users.delete(sub);
|
|
510
|
+
if (share && !share.users.size) {
|
|
511
|
+
if (!keep) return unshare(share);
|
|
512
|
+
sub.kept = true;
|
|
513
|
+
share.users.add(sub);
|
|
514
|
+
kept.set(share.key, sub);
|
|
515
|
+
let rows = 0, answers = 0;
|
|
516
|
+
for (let k of [
|
|
517
|
+
...kept.values()
|
|
518
|
+
].reverse()){
|
|
519
|
+
rows += k.shared.rows.size;
|
|
520
|
+
answers++;
|
|
521
|
+
if (rows > KEPT || answers > (opts.keep ?? KEEP)) unshare(k.shared);
|
|
522
|
+
}
|
|
523
|
+
return;
|
|
524
|
+
}
|
|
525
|
+
dropNetwork(sub);
|
|
526
|
+
};
|
|
527
|
+
// A subscription whose query is refused is closed, not kept: a query the
|
|
528
|
+
// graph cannot answer would otherwise throw on every commit for the life of
|
|
529
|
+
// the socket.
|
|
530
|
+
let cut = (sub, err)=>{
|
|
531
|
+
if (held.get(sub.sink)?.get(sub.id) !== sub) return;
|
|
532
|
+
held.get(sub.sink)?.delete(sub.id);
|
|
533
|
+
forget(sub, false);
|
|
534
|
+
fault(err, 'subscription');
|
|
535
|
+
try {
|
|
536
|
+
opts.refused?.(err, sub.query);
|
|
537
|
+
} catch (why) {
|
|
538
|
+
fault(why, 'subscription refused');
|
|
539
|
+
}
|
|
540
|
+
sub.sink({
|
|
541
|
+
id: sub.id,
|
|
542
|
+
refused: refusal(err)
|
|
543
|
+
});
|
|
544
|
+
};
|
|
545
|
+
let delivery = (sink, id, readOpts, raw = false)=>{
|
|
546
|
+
let owner = ()=>held.get(sink)?.get(id);
|
|
547
|
+
let sub;
|
|
548
|
+
let sendPublished = (frame)=>{
|
|
549
|
+
sub ??= owner();
|
|
550
|
+
if (sub && owner() !== sub) return;
|
|
551
|
+
let out = {
|
|
552
|
+
...frame
|
|
553
|
+
};
|
|
554
|
+
if (says(frame)) {
|
|
555
|
+
out.revision = rev;
|
|
556
|
+
if (sub?.said !== undefined) out.since = sub.said;
|
|
557
|
+
if (sub) sub.said = rev;
|
|
558
|
+
}
|
|
559
|
+
for (let key of [
|
|
560
|
+
'bundles',
|
|
561
|
+
'peers',
|
|
562
|
+
'relay'
|
|
563
|
+
]){
|
|
564
|
+
if (frame[key]) out[key] = published(graph.vocab, frame[key]);
|
|
565
|
+
}
|
|
566
|
+
for (let key of [
|
|
567
|
+
'coverage',
|
|
568
|
+
'peerCoverage'
|
|
569
|
+
]){
|
|
570
|
+
if (!frame[key]) continue;
|
|
571
|
+
out[key] = Object.fromEntries(Object.entries(frame[key]).map(([eid, coverage])=>[
|
|
572
|
+
eid,
|
|
573
|
+
coverage === true ? true : Object.fromEntries(Object.entries(coverage).filter(([comp])=>syncOf(graph.vocab, comp) != 'none'))
|
|
574
|
+
]));
|
|
575
|
+
}
|
|
576
|
+
if (frame.transient) {
|
|
577
|
+
out.transient = frame.transient.filter((f)=>syncOf(graph.vocab, f.component) != 'none');
|
|
578
|
+
}
|
|
579
|
+
return sink(out);
|
|
580
|
+
};
|
|
581
|
+
if (!graph.rewrites(readOpts)) return sendPublished;
|
|
582
|
+
let seen = new Set();
|
|
583
|
+
let riders = new Set();
|
|
584
|
+
let patchOpts = {
|
|
585
|
+
...readOpts,
|
|
586
|
+
patch: true
|
|
587
|
+
};
|
|
588
|
+
let waiting;
|
|
589
|
+
// A stored subscription's own rows were read under this reader's scopes,
|
|
590
|
+
// by its query or by the routing that screens a commit's rows for its
|
|
591
|
+
// network (`routeAll`), so a scope's hooks pass them as screened, and the
|
|
592
|
+
// coverage naming those same entities with them. What peers relay, what
|
|
593
|
+
// a raw feed carries and what a view assembles were not.
|
|
594
|
+
let screened = (key)=>key == 'bundles' && !raw && !!sub && !sub.view && !sub.peer;
|
|
595
|
+
let send = (frame)=>{
|
|
596
|
+
let rewritten = {
|
|
597
|
+
...frame
|
|
598
|
+
};
|
|
599
|
+
return after(over([
|
|
600
|
+
'bundles',
|
|
601
|
+
'peers',
|
|
602
|
+
'relay'
|
|
603
|
+
], (key)=>{
|
|
604
|
+
let bundles = frame[key];
|
|
605
|
+
if (!bundles) return;
|
|
606
|
+
return after(graph.answer(published(graph.vocab, bundles), key == 'relay' || raw && key == 'bundles' ? patchOpts : readOpts, screened(key)), (out)=>{
|
|
607
|
+
rewritten[key] = raw && key == 'bundles' ? out.filter((b)=>!dead(b) || seen.has(b.entity.eid)) : out;
|
|
608
|
+
});
|
|
609
|
+
}), ()=>after(over([
|
|
610
|
+
'coverage',
|
|
611
|
+
'peerCoverage'
|
|
612
|
+
], (key)=>{
|
|
613
|
+
let coverage = frame[key];
|
|
614
|
+
if (!coverage) return;
|
|
615
|
+
let whole = Object.keys(coverage).filter((eid)=>coverage[eid] === true);
|
|
616
|
+
let got = whole.length ? graph.get(whole, undefined, readOpts) : [];
|
|
617
|
+
return after(got, (held)=>{
|
|
618
|
+
let rows = Object.entries(coverage).flatMap(([eid, scope])=>{
|
|
619
|
+
if (scope === true) {
|
|
620
|
+
return held.filter((b)=>b.entity.eid == eid);
|
|
621
|
+
}
|
|
622
|
+
return [
|
|
623
|
+
{
|
|
624
|
+
entity: {
|
|
625
|
+
eid
|
|
626
|
+
},
|
|
627
|
+
...Object.fromEntries(Object.entries(scope).map(([comp, props])=>[
|
|
628
|
+
comp,
|
|
629
|
+
props === true ? {} : Object.fromEntries(props.map((p)=>[
|
|
630
|
+
p,
|
|
631
|
+
null
|
|
632
|
+
]))
|
|
633
|
+
]))
|
|
634
|
+
}
|
|
635
|
+
];
|
|
636
|
+
});
|
|
637
|
+
return after(graph.answer(published(graph.vocab, rows), readOpts, key == 'coverage' && screened('bundles')), (out)=>{
|
|
638
|
+
rewritten[key] = Object.fromEntries(out.map((row)=>[
|
|
639
|
+
row.entity.eid,
|
|
640
|
+
Object.fromEntries(comps(row).map(([name, props])=>{
|
|
641
|
+
let original = coverage[row.entity.eid];
|
|
642
|
+
let fields = props && typeof props == 'object' ? Object.keys(props) : [];
|
|
643
|
+
return [
|
|
644
|
+
name,
|
|
645
|
+
original === true || original[name] === true || !fields.length ? true : fields
|
|
646
|
+
];
|
|
647
|
+
}))
|
|
648
|
+
]));
|
|
649
|
+
});
|
|
650
|
+
});
|
|
651
|
+
}), ()=>{
|
|
652
|
+
for (let b of [
|
|
653
|
+
...rewritten.bundles ?? [],
|
|
654
|
+
...rewritten.relay ?? []
|
|
655
|
+
])seen.add(b.entity.eid);
|
|
656
|
+
for (let b of rewritten.peers ?? [])riders.add(b.entity.eid);
|
|
657
|
+
if (frame.gone) {
|
|
658
|
+
rewritten.gone = frame.gone.filter((eid)=>seen.has(eid));
|
|
659
|
+
}
|
|
660
|
+
if (frame.peerGone) {
|
|
661
|
+
rewritten.peerGone = frame.peerGone.filter((eid)=>riders.has(eid));
|
|
662
|
+
}
|
|
663
|
+
if (frame.transientReset) {
|
|
664
|
+
rewritten.transientReset = frame.transientReset.filter((eid)=>seen.has(eid));
|
|
665
|
+
}
|
|
666
|
+
for (let eid of rewritten.gone ?? [])seen.delete(eid);
|
|
667
|
+
for (let eid of rewritten.peerGone ?? [])riders.delete(eid);
|
|
668
|
+
if (!frame.transient?.length) return sendPublished(rewritten);
|
|
669
|
+
let rows = frame.transient.map((f)=>({
|
|
670
|
+
entity: {
|
|
671
|
+
eid: f.entity
|
|
672
|
+
},
|
|
673
|
+
[f.component]: {
|
|
674
|
+
[f.property]: null
|
|
675
|
+
}
|
|
676
|
+
}));
|
|
677
|
+
return after(graph.answer(rows, patchOpts), (visible)=>{
|
|
678
|
+
rewritten.transient = frame.transient.filter((f)=>visible.some((b)=>b.entity.eid == f.entity && b[f.component] && f.property in b[f.component]));
|
|
679
|
+
return sendPublished(rewritten);
|
|
680
|
+
});
|
|
681
|
+
}));
|
|
682
|
+
};
|
|
683
|
+
return (frame)=>{
|
|
684
|
+
sub ??= owner();
|
|
685
|
+
let fail = (err)=>{
|
|
686
|
+
if (sub) cut(sub, err);
|
|
687
|
+
};
|
|
688
|
+
try {
|
|
689
|
+
let out = waiting ? waiting.then(()=>send(frame)) : send(frame);
|
|
690
|
+
if (isPromise(out)) {
|
|
691
|
+
let pending = out.catch(fail);
|
|
692
|
+
waiting = pending;
|
|
693
|
+
pending.then(()=>{
|
|
694
|
+
if (waiting === pending) waiting = undefined;
|
|
695
|
+
});
|
|
696
|
+
return pending;
|
|
697
|
+
}
|
|
698
|
+
} catch (err) {
|
|
699
|
+
fail(err);
|
|
700
|
+
}
|
|
701
|
+
};
|
|
702
|
+
};
|
|
703
|
+
let attempt = (sub, fn)=>{
|
|
704
|
+
if (!sub.kept && held.get(sub.sink)?.get(sub.id) !== sub) return;
|
|
705
|
+
try {
|
|
706
|
+
let out = fn();
|
|
707
|
+
return isPromise(out) ? out.catch((err)=>cut(sub, err)) : out;
|
|
708
|
+
} catch (err) {
|
|
709
|
+
cut(sub, err);
|
|
710
|
+
}
|
|
711
|
+
};
|
|
712
|
+
const rememberFields = (sub, bundles)=>{
|
|
713
|
+
let vocab = sub.view?.vocab ?? graph.vocab;
|
|
714
|
+
for (const b of bundles){
|
|
715
|
+
let to = comps(b).flatMap(([comp, value])=>Object.entries(value ?? {}).flatMap(([prop, v])=>vocab.prop(comp, prop)?.category != 'ref' ? [] : [
|
|
716
|
+
v
|
|
717
|
+
].flat().filter((e)=>typeof e == 'string')));
|
|
718
|
+
if (to.length) sub.targets.set(b.entity.eid, to);
|
|
719
|
+
else sub.targets.delete(b.entity.eid);
|
|
720
|
+
sub.fields.set(b.entity.eid, new Set(Object.entries(b).flatMap(([comp, value])=>value && typeof value == 'object' ? Object.keys(value).map((prop)=>comp + '.' + prop) : [])));
|
|
721
|
+
}
|
|
722
|
+
};
|
|
723
|
+
const visible = (sub, f)=>sub.members.has(f.entity) && sub.fields.get(f.entity)?.has(f.component + '.' + f.property);
|
|
724
|
+
let reading = (sub, signal = sub.abort?.signal)=>({
|
|
725
|
+
...sub.opts,
|
|
726
|
+
signal
|
|
727
|
+
});
|
|
728
|
+
// What a subscription's query answers now. A projection whose paths reach
|
|
729
|
+
// other entities is read as its rows (@yaks/graph `project`), which tell the
|
|
730
|
+
// entities it selects from the ones they reach; any other query is the
|
|
731
|
+
// graph's read, the same answer `/query` gives.
|
|
732
|
+
// Under the subscription's own signal, or one it shares with others.
|
|
733
|
+
let ask = (sub, q, signal)=>asking(sub.asker, ()=>asked(sub, q, signal));
|
|
734
|
+
let asked = (sub, q, signal)=>sub.view ? viewed(sub.view, reading(sub, signal)) : sub.plan?.reaches.length ? project(graph, sub.plan, {
|
|
735
|
+
...reading(sub, signal),
|
|
736
|
+
durable: true,
|
|
737
|
+
native: true
|
|
738
|
+
}) : after(windowRows(sub, q, signal), answered);
|
|
739
|
+
let windowRows = (sub, q, signal)=>{
|
|
740
|
+
let query = queryOf(sub, q);
|
|
741
|
+
let optsForRead = {
|
|
742
|
+
...reading(sub, signal),
|
|
743
|
+
durable: true,
|
|
744
|
+
native: !!sub.ast || graph.rewrites(sub.opts) || sub.opts?.native
|
|
745
|
+
};
|
|
746
|
+
let read = (query)=>graph.read(query, optsForRead);
|
|
747
|
+
return opts.readWindow ? opts.readWindow(query, read, optsForRead.signal) : read(query);
|
|
748
|
+
};
|
|
749
|
+
// The bundles a frame carries, and what each covers where a projection
|
|
750
|
+
// narrowed them, so a replica clears only a property the projection named.
|
|
751
|
+
// A projection that reaches sends its riders whole each time, and names the
|
|
752
|
+
// ones no path reaches any more.
|
|
753
|
+
let framed = (sub, bundles, { reached, covers } = answered([]), initial = false)=>{
|
|
754
|
+
// Current held values outrun saved snapshots. New replicas initialize
|
|
755
|
+
// from the saved value; subsequent updates carry the live one when held.
|
|
756
|
+
if (!initial && !sub.view) {
|
|
757
|
+
let latest = (b)=>{
|
|
758
|
+
let out = {
|
|
759
|
+
...b
|
|
760
|
+
};
|
|
761
|
+
let held = peers.values([
|
|
762
|
+
b.entity.eid
|
|
763
|
+
])[0];
|
|
764
|
+
for (let [comp, patch] of held ? comps(held) : []){
|
|
765
|
+
if (saveOf(graph.vocab, comp) != null) out[comp] = patch;
|
|
766
|
+
}
|
|
767
|
+
return out;
|
|
768
|
+
};
|
|
769
|
+
bundles = bundles.map((b)=>sub.cut(latest(b)));
|
|
770
|
+
reached = reached.map((b)=>{
|
|
771
|
+
let out = latest(b), coverage = covers.get(b.entity.eid);
|
|
772
|
+
if (!coverage) return b;
|
|
773
|
+
return {
|
|
774
|
+
entity: out.entity,
|
|
775
|
+
...Object.fromEntries(Object.entries(coverage).flatMap(([comp, props])=>{
|
|
776
|
+
let value = out[comp];
|
|
777
|
+
return value ? [
|
|
778
|
+
[
|
|
779
|
+
comp,
|
|
780
|
+
Object.fromEntries(props.filter((p)=>p in value).map((p)=>[
|
|
781
|
+
p,
|
|
782
|
+
value[p]
|
|
783
|
+
]))
|
|
784
|
+
]
|
|
785
|
+
] : [];
|
|
786
|
+
}))
|
|
787
|
+
};
|
|
788
|
+
});
|
|
789
|
+
}
|
|
790
|
+
let p = sub.plan;
|
|
791
|
+
if (!p) {
|
|
792
|
+
// A component-selected answer knows whole components, not the entity.
|
|
793
|
+
// Include absent requested components so snapshots clear stale values.
|
|
794
|
+
if (sub.want == null) return {
|
|
795
|
+
bundles
|
|
796
|
+
};
|
|
797
|
+
let scope = Object.fromEntries([
|
|
798
|
+
...sub.want
|
|
799
|
+
].map((comp)=>[
|
|
800
|
+
comp,
|
|
801
|
+
true
|
|
802
|
+
]));
|
|
803
|
+
return {
|
|
804
|
+
bundles,
|
|
805
|
+
coverage: covering(bundles, ()=>scope)
|
|
806
|
+
};
|
|
807
|
+
}
|
|
808
|
+
let own = {
|
|
809
|
+
bundles,
|
|
810
|
+
coverage: covering(bundles, ()=>p.own)
|
|
811
|
+
};
|
|
812
|
+
if (!p.reaches.length) return own;
|
|
813
|
+
let now = new Set(reached.map((b)=>b.entity.eid));
|
|
814
|
+
let peerGone = [
|
|
815
|
+
...sub.riders
|
|
816
|
+
].filter((eid)=>!now.has(eid));
|
|
817
|
+
sub.riders = now;
|
|
818
|
+
return {
|
|
819
|
+
...own,
|
|
820
|
+
peers: reached,
|
|
821
|
+
peerCoverage: covering(reached, (eid)=>covers.get(eid)),
|
|
822
|
+
...peerGone.length ? {
|
|
823
|
+
peerGone
|
|
824
|
+
} : {}
|
|
825
|
+
};
|
|
826
|
+
};
|
|
827
|
+
// A path no component declares (`.kind`) may be anything: it drifts.
|
|
828
|
+
let moving = (ast)=>drifts(ast, (c)=>{
|
|
829
|
+
let leaf = graph.vocab.aim(c.path.join('.'), bare(c)).at(-1);
|
|
830
|
+
return leaf && graph.vocab.prop(leaf.comp, leaf.prop)?.scalar;
|
|
831
|
+
});
|
|
832
|
+
let windowed = (ast)=>{
|
|
833
|
+
let { limit, after } = windowOf(ast);
|
|
834
|
+
return limit != null || after != null;
|
|
835
|
+
};
|
|
836
|
+
let parallel = (items, run)=>{
|
|
837
|
+
let work = items.map(run).filter(isPromise);
|
|
838
|
+
if (work.length) return Promise.all(work).then(()=>{});
|
|
839
|
+
};
|
|
840
|
+
// What an opening read, shared by every opening of the same query under the
|
|
841
|
+
// same options while the store and its words stand as they were: the
|
|
842
|
+
// sockets of one reader restoring the same watches on a wake, and the
|
|
843
|
+
// pages that open them one message at a time, read each answer once.
|
|
844
|
+
// A commit lets them all go (`advance`), and an answer joined while its read
|
|
845
|
+
// is still running is cancelled only once every opening joined has let go.
|
|
846
|
+
// Settled answers are kept up to OPENED rows between them. A registry that
|
|
847
|
+
// learns of commits from the store's token may not have heard of one yet,
|
|
848
|
+
// so it keeps none past its read.
|
|
849
|
+
let opening = (sub, read)=>{
|
|
850
|
+
let key = keyOf(sub);
|
|
851
|
+
let held = opened.get(key);
|
|
852
|
+
if (held && !held.joint.cancelled.aborted) {
|
|
853
|
+
opened.delete(key);
|
|
854
|
+
opened.set(key, held);
|
|
855
|
+
} else {
|
|
856
|
+
let joint = jointly(read);
|
|
857
|
+
let entry = held = {
|
|
858
|
+
joint,
|
|
859
|
+
rows: 0
|
|
860
|
+
};
|
|
861
|
+
opened.set(key, entry);
|
|
862
|
+
let release = ()=>{
|
|
863
|
+
if (opened.get(key) === entry) opened.delete(key);
|
|
864
|
+
};
|
|
865
|
+
let keep = (answer)=>{
|
|
866
|
+
if (opts.revision) return release();
|
|
867
|
+
entry.rows = answer.found.length + answer.reached.length;
|
|
868
|
+
let rows = 0;
|
|
869
|
+
for (let o of opened.values())rows += o.rows;
|
|
870
|
+
// Past the bound, the latest asked before this one go first: openings
|
|
871
|
+
// that come round in the same order again (each socket's page asking
|
|
872
|
+
// its watches in turn) find the earlier answers still here, and the
|
|
873
|
+
// one just read is kept for the openings asking it next.
|
|
874
|
+
for (let [k, o] of [
|
|
875
|
+
...opened
|
|
876
|
+
].reverse()){
|
|
877
|
+
if (rows <= OPENED) break;
|
|
878
|
+
if (o === entry) continue;
|
|
879
|
+
opened.delete(k);
|
|
880
|
+
rows -= o.rows;
|
|
881
|
+
}
|
|
882
|
+
};
|
|
883
|
+
if (isPromise(joint.value)) joint.value.then(keep, release);
|
|
884
|
+
else keep(joint.value);
|
|
885
|
+
}
|
|
886
|
+
held.joint.share(sub.abort?.signal);
|
|
887
|
+
return held.joint.value;
|
|
888
|
+
};
|
|
889
|
+
// The query an opening asks once the graph has said what its words name
|
|
890
|
+
// (an `.after` anchor, a reader's rewrites), the same for every opening of
|
|
891
|
+
// it under the same options until a commit (`advance`), as `opening` keeps
|
|
892
|
+
// its answer.
|
|
893
|
+
let aimed = (sub, ask)=>{
|
|
894
|
+
if (opts.revision) return ask();
|
|
895
|
+
let key = keyOf(sub);
|
|
896
|
+
let held = aims.get(key);
|
|
897
|
+
if (!held) {
|
|
898
|
+
if (aims.size >= KEEP) aims.delete(aims.keys().next().value);
|
|
899
|
+
aims.set(key, held = ask());
|
|
900
|
+
if (isPromise(held)) held.catch(()=>aims.delete(key));
|
|
901
|
+
}
|
|
902
|
+
return held;
|
|
903
|
+
};
|
|
904
|
+
// Only completed answers are shared: pending work still belongs to the
|
|
905
|
+
// opening that may cancel it.
|
|
906
|
+
let cache = (map, key, value)=>{
|
|
907
|
+
if (!map) return;
|
|
908
|
+
if (isPromise(value)) value.then((v)=>map.set(key, v), ()=>{});
|
|
909
|
+
else map.set(key, value);
|
|
910
|
+
};
|
|
911
|
+
// A slow opening owns only its own read; a replaced or closed opening owes
|
|
912
|
+
// no answer. One a commit crossed still answers with what it read, and is
|
|
913
|
+
// then read again in turn, as a live answer a commit invalidates is: an
|
|
914
|
+
// answer the box's commits move faster than it can be read would otherwise
|
|
915
|
+
// be read again for as long as they keep coming, and its page never hears it.
|
|
916
|
+
let initial = (sub, value, use)=>{
|
|
917
|
+
if (held.get(sub.sink)?.get(sub.id) !== sub) return;
|
|
918
|
+
let crossed = sub.dirty;
|
|
919
|
+
sub.dirty = false;
|
|
920
|
+
sub.loading = false;
|
|
921
|
+
let sent = use(value);
|
|
922
|
+
if (crossed && isPromise(sent)) sent.then(()=>caughtUp(sub), ()=>{});
|
|
923
|
+
else if (crossed) caughtUp(sub);
|
|
924
|
+
return sent;
|
|
925
|
+
};
|
|
926
|
+
// Not awaited by the opening: it is ordered work, and an opening may be
|
|
927
|
+
// part of ordered work itself (`resync`).
|
|
928
|
+
let caughtUp = (sub)=>{
|
|
929
|
+
let out = ordered(()=>attempt(sub, ()=>reread(sub, ask)));
|
|
930
|
+
if (isPromise(out)) out.catch((err)=>fault(err, 'subscription catch-up'));
|
|
931
|
+
};
|
|
932
|
+
let open = (sink, id, query, readOpts, answers, deferStatic = false, reset = false)=>{
|
|
933
|
+
flush();
|
|
934
|
+
let mine = held.get(sink) ?? new Map();
|
|
935
|
+
held.set(sink, mine);
|
|
936
|
+
let line = query === true ? '' : query;
|
|
937
|
+
let options = readOpts;
|
|
938
|
+
let abort = new AbortController();
|
|
939
|
+
readOpts = {
|
|
940
|
+
...readOpts,
|
|
941
|
+
signal: abort.signal
|
|
942
|
+
};
|
|
943
|
+
let visibility = graph.scope(readOpts).visibility;
|
|
944
|
+
let sub = {
|
|
945
|
+
abort,
|
|
946
|
+
policyDependencies: visibility?.dependencies,
|
|
947
|
+
loading: query !== true,
|
|
948
|
+
policy: visibility?.entity === undefined ? undefined : interest(typeof visibility.entity == 'string' ? parse(visibility.entity) : visibility.entity, graph.vocab, graph.worn),
|
|
949
|
+
id,
|
|
950
|
+
sink,
|
|
951
|
+
raw: query === true,
|
|
952
|
+
query: line,
|
|
953
|
+
opts: options,
|
|
954
|
+
asker: asker(),
|
|
955
|
+
send: delivery(sink, id, readOpts, query === true),
|
|
956
|
+
members: new Set(),
|
|
957
|
+
fields: new Map(),
|
|
958
|
+
targets: new Map(),
|
|
959
|
+
routed: false,
|
|
960
|
+
peer: false,
|
|
961
|
+
cut: whole,
|
|
962
|
+
riders: new Set()
|
|
963
|
+
};
|
|
964
|
+
let deliver = sub.send;
|
|
965
|
+
sub.send = (frame)=>held.get(sink)?.get(id) === sub ? deliver(frame) : undefined;
|
|
966
|
+
forget(mine.get(id));
|
|
967
|
+
mine.set(id, sub);
|
|
968
|
+
// a raw feed carries whole transactions, not a membership set
|
|
969
|
+
if (sub.raw) return;
|
|
970
|
+
return attempt(sub, ()=>{
|
|
971
|
+
// Parsed here, before the network is asked, so a query that cannot be
|
|
972
|
+
// parsed is refused rather than quietly demoted to a subscription that
|
|
973
|
+
// runs it again on every commit forever.
|
|
974
|
+
return after(graph.view(line, readOpts), (view)=>{
|
|
975
|
+
if (view) {
|
|
976
|
+
sub.view = view;
|
|
977
|
+
sub.ast = view.original;
|
|
978
|
+
sub.window = windowed(view.original);
|
|
979
|
+
sub.reads = dependencies(interest(view.original, view.vocab, graph.worn), readOpts);
|
|
980
|
+
sub.peer = view.dependencies?.some(peerComp) ?? false;
|
|
981
|
+
sub.agg = aggregate(view.original);
|
|
982
|
+
sub.want = named(view.vocab, view.original);
|
|
983
|
+
sub.plan = projection(view.vocab, view.original);
|
|
984
|
+
sub.cut = sub.plan?.cut ?? only(sub.want);
|
|
985
|
+
sub.policyRefs = references(sub);
|
|
986
|
+
// The view already speaks the caller's words.
|
|
987
|
+
let deliver = delivery(sink, id, {
|
|
988
|
+
...readOpts,
|
|
989
|
+
native: true
|
|
990
|
+
});
|
|
991
|
+
sub.send = (frame)=>held.get(sink)?.get(id) === sub ? deliver(frame) : undefined;
|
|
992
|
+
if (sub.agg) return tell(sub, true, answers);
|
|
993
|
+
let loaded = opening(sub, (signal)=>viewed(view, {
|
|
994
|
+
...readOpts,
|
|
995
|
+
signal
|
|
996
|
+
}));
|
|
997
|
+
return after(loaded, (answer)=>initial(sub, answer, (answer)=>{
|
|
998
|
+
for (let b of answer.found)sub.members.add(b.entity.eid);
|
|
999
|
+
rememberFields(sub, answer.found);
|
|
1000
|
+
return sub.send({
|
|
1001
|
+
id,
|
|
1002
|
+
...framed(sub, answer.found, answer, true),
|
|
1003
|
+
...reset ? {
|
|
1004
|
+
reset: true
|
|
1005
|
+
} : {}
|
|
1006
|
+
});
|
|
1007
|
+
}));
|
|
1008
|
+
}
|
|
1009
|
+
let parsed = parse(line);
|
|
1010
|
+
return after(graph.rewrites(readOpts) || parsed.clauses.some((c)=>c.kind == 'after') ? aimed(sub, ()=>graph.ask(line, readOpts)) : line, (q)=>{
|
|
1011
|
+
let ast = typeof q == 'string' ? parsed : q;
|
|
1012
|
+
if (typeof q != 'string') sub.ast = ast;
|
|
1013
|
+
sub.window = windowed(ast);
|
|
1014
|
+
sub.reads = dependencies(interest(ast, graph.vocab, graph.worn), readOpts);
|
|
1015
|
+
let plan = peerPlan(ast, graph.vocab);
|
|
1016
|
+
sub.peer = plan.peers;
|
|
1017
|
+
sub.durable = plan.durable;
|
|
1018
|
+
sub.ref = plan.ref;
|
|
1019
|
+
sub.agg = aggregate(ast);
|
|
1020
|
+
sub.want = named(graph.vocab, ast, q !== line);
|
|
1021
|
+
sub.plan = projection(graph.vocab, ast);
|
|
1022
|
+
sub.cut = sub.plan?.cut ?? only(sub.want);
|
|
1023
|
+
sub.policyRefs = references(sub);
|
|
1024
|
+
if (deferStatic && !sub.peer && sub.reads && !sub.reads.unseen && sub.opts?.now == null && !sub.agg && !moving(ast) && [
|
|
1025
|
+
...sub.reads.near,
|
|
1026
|
+
...sub.reads.far,
|
|
1027
|
+
...sub.reads.via.keys()
|
|
1028
|
+
].every((c)=>syncOf(graph.vocab, c) != 'peers')) {
|
|
1029
|
+
sub.deferred = true;
|
|
1030
|
+
sub.loading = false;
|
|
1031
|
+
return;
|
|
1032
|
+
}
|
|
1033
|
+
if (sub.agg) return tell(sub, true, answers);
|
|
1034
|
+
if (sub.peer && sub.plan?.reaches.length) {
|
|
1035
|
+
throw new Refused('a projection through a reference is not read over relayed values');
|
|
1036
|
+
}
|
|
1037
|
+
let share = shared.get(keyOf(sub));
|
|
1038
|
+
let loaded = share ? answered([
|
|
1039
|
+
...share.rows.values()
|
|
1040
|
+
]) : sub.peer ? after(read(sub), answered) : opening(sub, (signal)=>ask(sub, line, signal));
|
|
1041
|
+
return after(loaded, (answer)=>initial(sub, answer, (answer)=>{
|
|
1042
|
+
let bundles = answer.found;
|
|
1043
|
+
for (let b of bundles)sub.members.add(b.entity.eid);
|
|
1044
|
+
// A query over a computed component is never routed: the entities it
|
|
1045
|
+
// selects move without a bundle that names them (./interest.ts). Nor
|
|
1046
|
+
// is a projection that reaches: what it reaches moves without the
|
|
1047
|
+
// entities it selects moving.
|
|
1048
|
+
if (held.get(sink)?.get(id) === sub && routable(sub)) {
|
|
1049
|
+
sub.routed = network(sub).add(sub, ast, sub.members);
|
|
1050
|
+
if (sub.routed) join(sub, ast, bundles);
|
|
1051
|
+
}
|
|
1052
|
+
rememberFields(sub, bundles);
|
|
1053
|
+
const snapshots = live.snapshots().filter((f)=>visible(sub, f));
|
|
1054
|
+
// The relayed values other connections already hold for this set, so
|
|
1055
|
+
// a subscriber that arrives late still sees the cursors that were
|
|
1056
|
+
// there before it.
|
|
1057
|
+
let now = peers.snapshot(sink, (eid)=>sub.members.has(eid));
|
|
1058
|
+
return sub.send({
|
|
1059
|
+
id,
|
|
1060
|
+
...framed(sub, bundles, answer, true),
|
|
1061
|
+
...reset ? {
|
|
1062
|
+
reset: true
|
|
1063
|
+
} : {},
|
|
1064
|
+
transientReset: bundles.map((b)=>b.entity.eid),
|
|
1065
|
+
...snapshots.length ? {
|
|
1066
|
+
transient: snapshots
|
|
1067
|
+
} : {},
|
|
1068
|
+
...now.length ? {
|
|
1069
|
+
relay: now
|
|
1070
|
+
} : {}
|
|
1071
|
+
});
|
|
1072
|
+
}));
|
|
1073
|
+
});
|
|
1074
|
+
});
|
|
1075
|
+
});
|
|
1076
|
+
};
|
|
1077
|
+
// An aggregate's answer, sent when it is new: always on open, and after a
|
|
1078
|
+
// commit only when the value moved. A count or a tally is a question about
|
|
1079
|
+
// the whole set, so it is asked again after every commit.
|
|
1080
|
+
let tell = (sub, first = false, answers)=>{
|
|
1081
|
+
let key = sub.opts ? JSON.stringify([
|
|
1082
|
+
sub.query,
|
|
1083
|
+
sub.opts
|
|
1084
|
+
]) : sub.query;
|
|
1085
|
+
let value = answers?.get(key);
|
|
1086
|
+
if (!value) {
|
|
1087
|
+
value = sub.view ? after(viewValues(sub.view, reading(sub)), (bundles)=>reduced(sub.agg, matchRows(sub.view.original, sub.view.vocab, sub.opts)(bundles))) : after(sub.peer ? after(source(sub), (bundles)=>matchRows(queryOf(sub), graph.vocab, sub.opts)(bundles)) : graph.rows(queryOf(sub), {
|
|
1088
|
+
...reading(sub),
|
|
1089
|
+
durable: true,
|
|
1090
|
+
native: !!sub.ast || graph.rewrites(sub.opts) || sub.opts?.native
|
|
1091
|
+
}), (rows)=>reduced(sub.agg, rows));
|
|
1092
|
+
cache(answers, key, value);
|
|
1093
|
+
}
|
|
1094
|
+
let send = (value)=>{
|
|
1095
|
+
let answer = JSON.stringify(value);
|
|
1096
|
+
if (!first && answer == sub.answer) {
|
|
1097
|
+
return;
|
|
1098
|
+
}
|
|
1099
|
+
sub.answer = answer;
|
|
1100
|
+
return sub.send({
|
|
1101
|
+
id: sub.id,
|
|
1102
|
+
...value
|
|
1103
|
+
});
|
|
1104
|
+
};
|
|
1105
|
+
return after(value, (value)=>first ? initial(sub, value, send) : send(value));
|
|
1106
|
+
};
|
|
1107
|
+
// What a commit did to one routed subscription, sent cut to what the query
|
|
1108
|
+
// names.
|
|
1109
|
+
let send = (sub, moved)=>{
|
|
1110
|
+
if (!moved) return;
|
|
1111
|
+
let { bundles, joined, gone } = moved;
|
|
1112
|
+
let share = sub.shared;
|
|
1113
|
+
if (share) {
|
|
1114
|
+
for (let b of bundles)share.rows.set(b.entity.eid, b);
|
|
1115
|
+
for (let eid of gone)share.rows.delete(eid);
|
|
1116
|
+
if (share.rows.size > HELD) unshare(share);
|
|
1117
|
+
}
|
|
1118
|
+
if (sub.kept) return;
|
|
1119
|
+
rememberFields(sub, bundles);
|
|
1120
|
+
for (const eid of gone){
|
|
1121
|
+
sub.fields.delete(eid);
|
|
1122
|
+
sub.targets.delete(eid);
|
|
1123
|
+
}
|
|
1124
|
+
if (bundles.length || gone.length) {
|
|
1125
|
+
return sub.send({
|
|
1126
|
+
id: sub.id,
|
|
1127
|
+
...framed(sub, bundles),
|
|
1128
|
+
gone,
|
|
1129
|
+
...hail(sub, joined)
|
|
1130
|
+
});
|
|
1131
|
+
}
|
|
1132
|
+
};
|
|
1133
|
+
// What the peers are already saying about entities that just joined a set,
|
|
1134
|
+
// as a subscription that opens is told it: a writer relays a value when it
|
|
1135
|
+
// changes, so one relayed before its entity joined would otherwise not be
|
|
1136
|
+
// heard again until it moved.
|
|
1137
|
+
let hail = (sub, joined)=>{
|
|
1138
|
+
if (!joined.length || sub.view) return {};
|
|
1139
|
+
let fresh = new Set(joined);
|
|
1140
|
+
let now = peers.snapshot(sub.sink, (eid)=>fresh.has(eid));
|
|
1141
|
+
return now.length ? {
|
|
1142
|
+
relay: now
|
|
1143
|
+
} : {};
|
|
1144
|
+
};
|
|
1145
|
+
// A refresh runs against the affected entities where it can locate them.
|
|
1146
|
+
// A limit, order or unlocated dependency still needs the whole answer, and
|
|
1147
|
+
// so does a projection that reaches: what it reaches is the whole answer's.
|
|
1148
|
+
let push = (sub, scope, load = ask, prepared, joinsOnly = false)=>{
|
|
1149
|
+
if (sub.agg) return tell(sub);
|
|
1150
|
+
if (sub.plan?.reaches.length) scope = undefined;
|
|
1151
|
+
let query = scope ? sub.query + '&.entity.eid=' + [
|
|
1152
|
+
...scope
|
|
1153
|
+
].join(',') : sub.query;
|
|
1154
|
+
let loaded = sub.view ? viewed(sub.view, reading(sub)) : sub.peer ? after(read(sub, scope, prepared), answered) : load(sub, query);
|
|
1155
|
+
return after(loaded, (answer)=>{
|
|
1156
|
+
let set = answer.found;
|
|
1157
|
+
let ids = new Set(set.map((b)=>b.entity.eid));
|
|
1158
|
+
let gone = [
|
|
1159
|
+
...scope ?? sub.members
|
|
1160
|
+
].filter((e)=>sub.members.has(e) && !ids.has(e));
|
|
1161
|
+
// An entity can join without being touched: a hop or a computed
|
|
1162
|
+
// property moved it from the far side.
|
|
1163
|
+
let joined = [
|
|
1164
|
+
...ids
|
|
1165
|
+
].filter((e)=>!sub.members.has(e));
|
|
1166
|
+
if (scope) {
|
|
1167
|
+
for (let e of gone)sub.members.delete(e);
|
|
1168
|
+
for (let e of ids)sub.members.add(e);
|
|
1169
|
+
} else sub.members = ids;
|
|
1170
|
+
if (sub.routed) network(sub).add(sub, queryOf(sub), sub.members);
|
|
1171
|
+
// A far peer movement changes membership, not stored answer rows.
|
|
1172
|
+
// Existing members need no second copy of their unchanged row.
|
|
1173
|
+
let changed = joinsOnly ? set.filter((b)=>joined.includes(b.entity.eid)) : set;
|
|
1174
|
+
rememberFields(sub, changed);
|
|
1175
|
+
for (let e of gone){
|
|
1176
|
+
sub.fields.delete(e);
|
|
1177
|
+
sub.targets.delete(e);
|
|
1178
|
+
}
|
|
1179
|
+
let frame = framed(sub, changed, answer);
|
|
1180
|
+
if (changed.length || gone.length || frame.peerGone) {
|
|
1181
|
+
return sub.send({
|
|
1182
|
+
id: sub.id,
|
|
1183
|
+
...frame,
|
|
1184
|
+
...sub.window && !scope && !joinsOnly ? {
|
|
1185
|
+
reset: true
|
|
1186
|
+
} : {},
|
|
1187
|
+
gone,
|
|
1188
|
+
...hail(sub, joined)
|
|
1189
|
+
});
|
|
1190
|
+
}
|
|
1191
|
+
});
|
|
1192
|
+
};
|
|
1193
|
+
// One pass over the transactions that committed since the last: each one
|
|
1194
|
+
// to the raw feeds, then all of them at once to the queries.
|
|
1195
|
+
let fanout = (txs)=>{
|
|
1196
|
+
let target = opts.activity ?? base;
|
|
1197
|
+
let c = peek(target);
|
|
1198
|
+
if (!c) return commitNow(txs);
|
|
1199
|
+
// Refreshes can run after the writer has already answered. The box host
|
|
1200
|
+
// treats a missing/completed parent as an independent request; a Store
|
|
1201
|
+
// still recording that write keeps this pass in its existing tree.
|
|
1202
|
+
return scope(undefined, ()=>during(c.begin({
|
|
1203
|
+
kind: 'request',
|
|
1204
|
+
name: 'ws refresh',
|
|
1205
|
+
package: '@yaks/api',
|
|
1206
|
+
parent: parent(target, txs[0])
|
|
1207
|
+
}), ()=>during(c.begin({
|
|
1208
|
+
kind: 'fanout',
|
|
1209
|
+
name: 'subscriptions',
|
|
1210
|
+
package: '@yaks/api'
|
|
1211
|
+
}), ()=>commitNow(txs), 'ok', ()=>({
|
|
1212
|
+
transactions: txs.length,
|
|
1213
|
+
bundles: txs.reduce((n, b)=>n + b.length, 0),
|
|
1214
|
+
subscriptions: all().length
|
|
1215
|
+
}))));
|
|
1216
|
+
};
|
|
1217
|
+
// A live answer asked again whole, and what moved sent: the set as it now
|
|
1218
|
+
// stands, what left it and what joined.
|
|
1219
|
+
let reread = (s, load)=>{
|
|
1220
|
+
// An answer asked again whole is no longer one kept current.
|
|
1221
|
+
if (s.shared) unshare(s.shared);
|
|
1222
|
+
if (s.kept) return;
|
|
1223
|
+
let reset = s.deferred;
|
|
1224
|
+
if (s.agg) return tell(s);
|
|
1225
|
+
let loaded = s.view ? viewed(s.view, reading(s)) : s.peer ? after(read(s), answered) : load(s, s.query);
|
|
1226
|
+
return after(loaded, (answer)=>{
|
|
1227
|
+
s.deferred = false;
|
|
1228
|
+
let set = answer.found;
|
|
1229
|
+
let ids = new Set(set.map((b)=>b.entity.eid));
|
|
1230
|
+
let gone = [
|
|
1231
|
+
...s.members
|
|
1232
|
+
].filter((id)=>!ids.has(id));
|
|
1233
|
+
let joined = [
|
|
1234
|
+
...ids
|
|
1235
|
+
].filter((id)=>!s.members.has(id));
|
|
1236
|
+
s.members = ids;
|
|
1237
|
+
for (let e of gone)s.targets.delete(e);
|
|
1238
|
+
if (reset && routable(s)) s.routed = network(s).add(s, queryOf(s), ids);
|
|
1239
|
+
else if (s.routed) network(s).add(s, queryOf(s), ids);
|
|
1240
|
+
if (s.routed) join(s, s.ast ?? parse(s.query), set);
|
|
1241
|
+
rememberFields(s, set);
|
|
1242
|
+
return s.send({
|
|
1243
|
+
id: s.id,
|
|
1244
|
+
...framed(s, set, answer),
|
|
1245
|
+
...reset || s.window ? {
|
|
1246
|
+
reset: true
|
|
1247
|
+
} : {},
|
|
1248
|
+
gone,
|
|
1249
|
+
...hail(s, joined)
|
|
1250
|
+
});
|
|
1251
|
+
});
|
|
1252
|
+
};
|
|
1253
|
+
let commitNow = (txs)=>{
|
|
1254
|
+
if (ending.signal.aborted) return;
|
|
1255
|
+
flush();
|
|
1256
|
+
let applied = txs.flat();
|
|
1257
|
+
let wrote = new Set(applied.map((b)=>b.entity.eid));
|
|
1258
|
+
let named = new Set(applied.flatMap((b)=>Object.keys(b)));
|
|
1259
|
+
for (let [key, link] of backlinks){
|
|
1260
|
+
if (named.has(link.comp) || link.rows.some((b)=>wrote.has(b.entity.eid))) backlinks.delete(key);
|
|
1261
|
+
}
|
|
1262
|
+
for (let [key, held] of durableCandidates){
|
|
1263
|
+
if (opts.invalidate || policyMoved(held.sub, applied) || applied.some((b)=>notices(held.sub, b))) durableCandidates.delete(key);
|
|
1264
|
+
}
|
|
1265
|
+
let subs = all();
|
|
1266
|
+
// A raw feed sends each transaction to a client, and this hook is handed
|
|
1267
|
+
// what the phases passed to each other — one patch each, with the `$`
|
|
1268
|
+
// keys still on them. So it is composed here, the same way `apply()`
|
|
1269
|
+
// composes what it returns: a subscriber receives exactly what the writer
|
|
1270
|
+
// did.
|
|
1271
|
+
let raw = subs.filter((s)=>s.raw);
|
|
1272
|
+
for (let tx of raw.length ? txs : []){
|
|
1273
|
+
let bundles = composed(tx);
|
|
1274
|
+
for (let s of raw)s.send({
|
|
1275
|
+
id: s.id,
|
|
1276
|
+
bundles
|
|
1277
|
+
});
|
|
1278
|
+
}
|
|
1279
|
+
let queries = [
|
|
1280
|
+
...subs.filter((s)=>!s.raw),
|
|
1281
|
+
...kept.values()
|
|
1282
|
+
];
|
|
1283
|
+
let invalidated = new Set(queries.filter((s)=>s.deferred && applied.some((b)=>notices(s, b)) || policyMoved(s, applied) || opts.invalidate?.(s.query, applied) || s.view && applied.some((b)=>b.$delete || !s.view.dependencies || comps(b).some(([comp])=>s.view.dependencies.includes(comp)))));
|
|
1284
|
+
for (let s of invalidated){
|
|
1285
|
+
if (!s.policyDependencies?.length) continue;
|
|
1286
|
+
let visibility = graph.scope(s.opts).visibility;
|
|
1287
|
+
let query = visibility?.entity;
|
|
1288
|
+
s.policy = query === undefined ? undefined : interest(typeof query == 'string' ? parse(query) : query, graph.vocab, graph.worn);
|
|
1289
|
+
s.reads = dependencies(interest(parse(s.query), graph.vocab, graph.worn), s.opts);
|
|
1290
|
+
}
|
|
1291
|
+
let noticedBy = new Map(queries.map((s)=>[
|
|
1292
|
+
s,
|
|
1293
|
+
new Set(applied.filter((b)=>notices(s, b)).map((b)=>b.entity.eid))
|
|
1294
|
+
]));
|
|
1295
|
+
let relevant = new Set(queries.filter((s)=>invalidated.has(s) || noticedBy.get(s).size));
|
|
1296
|
+
for (let s of relevant)if (s.peer) s.candidates = undefined;
|
|
1297
|
+
let touched = [
|
|
1298
|
+
...new Set(applied.filter((b)=>peerRows.has(b.entity.eid) || invalidated.size || [
|
|
1299
|
+
...noticedBy.values()
|
|
1300
|
+
].some((ids)=>ids.has(b.entity.eid))).map((b)=>b.entity.eid))
|
|
1301
|
+
];
|
|
1302
|
+
if (!relevant.size && !touched.length) return;
|
|
1303
|
+
// One read for every subscription asking the same thing in this pass,
|
|
1304
|
+
// owned by all of them: a socket that drops mid-pass cancels no answer
|
|
1305
|
+
// another page is waiting on.
|
|
1306
|
+
let reads = new Map();
|
|
1307
|
+
let load = (s, q)=>{
|
|
1308
|
+
let key = s.opts ? JSON.stringify([
|
|
1309
|
+
q,
|
|
1310
|
+
s.opts
|
|
1311
|
+
]) : q;
|
|
1312
|
+
let got = reads.get(key);
|
|
1313
|
+
if (!got || got.cancelled.aborted) {
|
|
1314
|
+
reads.set(key, got = jointly((signal)=>ask(s, q, signal)));
|
|
1315
|
+
}
|
|
1316
|
+
got.share(s.abort?.signal);
|
|
1317
|
+
return got.value;
|
|
1318
|
+
};
|
|
1319
|
+
let names = touched.some((eid)=>peerRows.has(eid)) ? undefined : components(queries);
|
|
1320
|
+
return after(graph.get(touched, names, {
|
|
1321
|
+
native: true
|
|
1322
|
+
}), (now)=>{
|
|
1323
|
+
let changed = new Map(now.map((b)=>[
|
|
1324
|
+
b.entity.eid,
|
|
1325
|
+
b
|
|
1326
|
+
]));
|
|
1327
|
+
for (let eid of touched){
|
|
1328
|
+
if (peerRows.has(eid)) {
|
|
1329
|
+
let row = changed.get(eid);
|
|
1330
|
+
peerRows.set(eid, row ? stored(row) : null);
|
|
1331
|
+
}
|
|
1332
|
+
}
|
|
1333
|
+
if (!queries.length) return;
|
|
1334
|
+
let touch = touches(applied, now);
|
|
1335
|
+
return after(routeAll(now, touched, false), (durable)=>after(routeAll(overlay(now.map(stored), peers.values(touched)), touched, true), (peer)=>{
|
|
1336
|
+
let routing = new Map([
|
|
1337
|
+
...durable,
|
|
1338
|
+
...peer
|
|
1339
|
+
]);
|
|
1340
|
+
return after(over(queries, (s)=>attempt(s, ()=>{
|
|
1341
|
+
if (!relevant.has(s)) return;
|
|
1342
|
+
if (invalidated.has(s)) return reread(s, load);
|
|
1343
|
+
if (s.routed && !s.reads?.via.size) {
|
|
1344
|
+
let moved = routing.get(s);
|
|
1345
|
+
if (moved) {
|
|
1346
|
+
let joined = new Set(moved.joined);
|
|
1347
|
+
moved.bundles = moved.bundles.filter((b)=>joined.has(b.entity.eid) || noticedBy.get(s).has(b.entity.eid));
|
|
1348
|
+
}
|
|
1349
|
+
return send(s, moved);
|
|
1350
|
+
}
|
|
1351
|
+
// An ordered window can change only at the named local rows.
|
|
1352
|
+
// Re-select its existing members plus those rows, not every old
|
|
1353
|
+
// entry. A missing replacement (delete/filter exit) falls back to
|
|
1354
|
+
// the full query, since an unseen row may fill the window then.
|
|
1355
|
+
let ast = s.ast ?? parse(s.query);
|
|
1356
|
+
let limit = ast.clauses.find((c)=>c.kind == 'limit');
|
|
1357
|
+
if (limit?.kind == 'limit' && !s.peer && !s.view && !s.agg && s.reads && !s.reads.unseen && !s.reads.far.size && !s.reads.via.size && s.reads.own.length && !ast.clauses.some((c)=>c.kind == 'after') && (s.members.size < limit.n || ![
|
|
1358
|
+
...noticedBy.get(s)
|
|
1359
|
+
].some((id)=>s.members.has(id)))) {
|
|
1360
|
+
let local = affected({
|
|
1361
|
+
...s,
|
|
1362
|
+
reads: {
|
|
1363
|
+
...s.reads,
|
|
1364
|
+
whole: false
|
|
1365
|
+
}
|
|
1366
|
+
}, touch, noticedBy.get(s));
|
|
1367
|
+
if (local === undefined) return;
|
|
1368
|
+
if (local) {
|
|
1369
|
+
if (s.members.size < limit.n && local.size == 1 && [
|
|
1370
|
+
...s.members
|
|
1371
|
+
].every((id)=>local.has(id)) && !s.plan?.reaches.length) {
|
|
1372
|
+
// Commit facts can answer the whole window when only
|
|
1373
|
+
// one entity can belong to it. Otherwise the Store
|
|
1374
|
+
// must put affected and unchanged rows in query order.
|
|
1375
|
+
return after(graph.answer(now.filter((b)=>local.has(b.entity.eid)), {
|
|
1376
|
+
...s.opts,
|
|
1377
|
+
native: true
|
|
1378
|
+
}), (rows)=>push(s, undefined, ()=>answered(matcher(queryOf(s), graph.vocab, s.opts)(rows).map(s.cut))));
|
|
1379
|
+
}
|
|
1380
|
+
let candidates = [
|
|
1381
|
+
...new Set([
|
|
1382
|
+
...s.members,
|
|
1383
|
+
...local
|
|
1384
|
+
])
|
|
1385
|
+
];
|
|
1386
|
+
if (candidates.some((id)=>!/^[a-zA-Z0-9_-]+$/.test(id))) return push(s, undefined, load);
|
|
1387
|
+
let q = s.query + '&.entity.eid=' + candidates.join(',');
|
|
1388
|
+
let loaded = load(s, q);
|
|
1389
|
+
return after(loaded, (answer)=>{
|
|
1390
|
+
return push(s, undefined, ()=>s.members.size >= limit.n && answer.found.length < limit.n ? load(s, s.query) : answer);
|
|
1391
|
+
});
|
|
1392
|
+
}
|
|
1393
|
+
}
|
|
1394
|
+
let scope = affected(s, touch, noticedBy.get(s));
|
|
1395
|
+
if (scope === undefined) {
|
|
1396
|
+
return;
|
|
1397
|
+
}
|
|
1398
|
+
return push(s, scope ?? undefined, load);
|
|
1399
|
+
})), ()=>undefined);
|
|
1400
|
+
}));
|
|
1401
|
+
});
|
|
1402
|
+
};
|
|
1403
|
+
// A commit that finds earlier work still running waits for it, beside every
|
|
1404
|
+
// commit that arrives meanwhile: when their turn comes they are one pass.
|
|
1405
|
+
let waiting;
|
|
1406
|
+
let commits = (txs)=>{
|
|
1407
|
+
if (ending.signal.aborted || !txs.length) return;
|
|
1408
|
+
advance();
|
|
1409
|
+
let applied = txs.flat();
|
|
1410
|
+
for (let mine of held.values()){
|
|
1411
|
+
for (let sub of mine.values()){
|
|
1412
|
+
if (sub.loading && (!sub.reads || policyMoved(sub, applied) || applied.some((b)=>notices(sub, b)) || opts.invalidate?.(sub.query, applied))) sub.dirty = true;
|
|
1413
|
+
}
|
|
1414
|
+
}
|
|
1415
|
+
flushPeers();
|
|
1416
|
+
if (waiting) {
|
|
1417
|
+
waiting.txs.push(...txs);
|
|
1418
|
+
return waiting.done;
|
|
1419
|
+
}
|
|
1420
|
+
if (!pendingWork) return ordered(()=>fanout(txs));
|
|
1421
|
+
let next = {
|
|
1422
|
+
txs: [
|
|
1423
|
+
...txs
|
|
1424
|
+
],
|
|
1425
|
+
done: Promise.resolve()
|
|
1426
|
+
};
|
|
1427
|
+
waiting = next;
|
|
1428
|
+
next.done = ordered(()=>{
|
|
1429
|
+
waiting = undefined;
|
|
1430
|
+
return fanout(next.txs);
|
|
1431
|
+
});
|
|
1432
|
+
return next.done;
|
|
1433
|
+
};
|
|
1434
|
+
let commit = (applied)=>commits([
|
|
1435
|
+
applied
|
|
1436
|
+
]);
|
|
1437
|
+
// What other hosts committed comes as one pass. When some of it never
|
|
1438
|
+
// arrived, every answer is read again first: the patches that did arrive
|
|
1439
|
+
// cannot say what the missing ones moved.
|
|
1440
|
+
let hear = ({ applied, missed })=>{
|
|
1441
|
+
let reset = missed ? (flushPeers(), ordered(resync)) : undefined;
|
|
1442
|
+
let pass = commits(applied);
|
|
1443
|
+
return isPromise(reset) || isPromise(pass) ? Promise.all([
|
|
1444
|
+
reset,
|
|
1445
|
+
pass
|
|
1446
|
+
]).then(()=>{}) : undefined;
|
|
1447
|
+
};
|
|
1448
|
+
// Each caller context routes only mandatory-policy-filtered canonical rows.
|
|
1449
|
+
// Its network and shared answers never contain another person's aspects.
|
|
1450
|
+
let routeAll = (rows, touched, peer, joinsOnly = false)=>{
|
|
1451
|
+
let groups = new Map();
|
|
1452
|
+
for (let sub of [
|
|
1453
|
+
...all(),
|
|
1454
|
+
...kept.values()
|
|
1455
|
+
]){
|
|
1456
|
+
if (sub.routed && sub.peer === peer) groups.set(network(sub), sub.opts);
|
|
1457
|
+
}
|
|
1458
|
+
let out = new Map();
|
|
1459
|
+
return after(over([
|
|
1460
|
+
...groups
|
|
1461
|
+
], ([routing, readOpts])=>after(graph.answer(rows, {
|
|
1462
|
+
...readOpts,
|
|
1463
|
+
native: true
|
|
1464
|
+
}), (visible)=>{
|
|
1465
|
+
for (let [sub, moved] of route(visible, touched, routing, joinsOnly)){
|
|
1466
|
+
out.set(sub, moved);
|
|
1467
|
+
}
|
|
1468
|
+
})), ()=>out);
|
|
1469
|
+
};
|
|
1470
|
+
// The entities a transaction changed, read whole, each moved through the
|
|
1471
|
+
// network once: what joined and what left every routed subscription.
|
|
1472
|
+
let route = (now, touched, routing, joinsOnly = false)=>{
|
|
1473
|
+
let out = new Map();
|
|
1474
|
+
let of = (s)=>{
|
|
1475
|
+
let m = out.get(s);
|
|
1476
|
+
if (!m) out.set(s, m = {
|
|
1477
|
+
bundles: [],
|
|
1478
|
+
joined: [],
|
|
1479
|
+
gone: []
|
|
1480
|
+
});
|
|
1481
|
+
return m;
|
|
1482
|
+
};
|
|
1483
|
+
let seen = new Set();
|
|
1484
|
+
for (let b of now){
|
|
1485
|
+
let eid = b.entity.eid;
|
|
1486
|
+
seen.add(eid);
|
|
1487
|
+
let { into, out: left } = routing.move(b);
|
|
1488
|
+
for (let s of left){
|
|
1489
|
+
s.members.delete(eid);
|
|
1490
|
+
of(s).gone.push(eid);
|
|
1491
|
+
}
|
|
1492
|
+
for (let s of into){
|
|
1493
|
+
let joined = !s.members.has(eid);
|
|
1494
|
+
if (joined) of(s).joined.push(eid);
|
|
1495
|
+
s.members.add(eid);
|
|
1496
|
+
// Peer relays change routing but not stored data. Existing members
|
|
1497
|
+
// already heard the patch from cast(); only new members need a
|
|
1498
|
+
// full stored bundle and the held relay from hail().
|
|
1499
|
+
if (joined || !joinsOnly) {
|
|
1500
|
+
of(s).bundles.push(s.cut(s.peer ? stored(b) : b));
|
|
1501
|
+
}
|
|
1502
|
+
}
|
|
1503
|
+
}
|
|
1504
|
+
// An entity storage no longer holds at all has left every set too.
|
|
1505
|
+
for (let eid of touched){
|
|
1506
|
+
if (seen.has(eid)) continue;
|
|
1507
|
+
for (let s of routing.forget(eid)){
|
|
1508
|
+
s.members.delete(eid);
|
|
1509
|
+
of(s).gone.push(eid);
|
|
1510
|
+
}
|
|
1511
|
+
}
|
|
1512
|
+
return out;
|
|
1513
|
+
};
|
|
1514
|
+
// Deliver a relay patch to the old members before that patch changes a
|
|
1515
|
+
// peer-aware query's membership. A raw feed receives every patch.
|
|
1516
|
+
let cast = (bundles, except)=>{
|
|
1517
|
+
for (let [sink, mine] of held){
|
|
1518
|
+
if (sink === except) continue;
|
|
1519
|
+
for (let sub of mine.values()){
|
|
1520
|
+
// Views refresh after the canonical movement; their whole answer
|
|
1521
|
+
// includes the reconstructed relay value in the caller's vocabulary.
|
|
1522
|
+
if (sub.view) continue;
|
|
1523
|
+
let seen = sub.raw ? bundles : bundles.filter((b)=>sub.members.has(b.entity.eid));
|
|
1524
|
+
if (seen.length) sub.send({
|
|
1525
|
+
id: sub.id,
|
|
1526
|
+
relay: seen
|
|
1527
|
+
});
|
|
1528
|
+
}
|
|
1529
|
+
}
|
|
1530
|
+
};
|
|
1531
|
+
let peers = relaying(graph.vocab, (b)=>{
|
|
1532
|
+
flushPeers();
|
|
1533
|
+
let out = ordered(()=>peerChange(b));
|
|
1534
|
+
if (isPromise(out)) out.catch((err)=>fault(err, 'peer expiry'));
|
|
1535
|
+
}, opts.timer);
|
|
1536
|
+
// A peer query is answered from durable candidates and the relay's current
|
|
1537
|
+
// values together. The query plan keeps unrelated durable rows out of this
|
|
1538
|
+
// read, even when an OR has both stored and peer branches.
|
|
1539
|
+
let overlay = (rows, values)=>{
|
|
1540
|
+
let out = new Map(rows.map((b)=>[
|
|
1541
|
+
b.entity.eid,
|
|
1542
|
+
b
|
|
1543
|
+
]));
|
|
1544
|
+
for (let peer of values){
|
|
1545
|
+
let was = out.get(peer.entity.eid) ?? {
|
|
1546
|
+
entity: peer.entity
|
|
1547
|
+
};
|
|
1548
|
+
out.set(peer.entity.eid, {
|
|
1549
|
+
...was,
|
|
1550
|
+
...peer
|
|
1551
|
+
});
|
|
1552
|
+
}
|
|
1553
|
+
return [
|
|
1554
|
+
...out.values()
|
|
1555
|
+
];
|
|
1556
|
+
};
|
|
1557
|
+
let stored = (b)=>{
|
|
1558
|
+
let out = {
|
|
1559
|
+
entity: b.entity
|
|
1560
|
+
};
|
|
1561
|
+
for (let [name, patch] of comps(b)){
|
|
1562
|
+
if (!peerComp(name)) out[name] = patch;
|
|
1563
|
+
}
|
|
1564
|
+
return out;
|
|
1565
|
+
};
|
|
1566
|
+
let durableRows = (ids)=>{
|
|
1567
|
+
let missing = ids.filter((eid)=>!peerRows.has(eid));
|
|
1568
|
+
let collect = ()=>ids.flatMap((eid)=>peerRows.get(eid) ?? []);
|
|
1569
|
+
if (!missing.length) return collect();
|
|
1570
|
+
return after(graph.get(missing, undefined, {
|
|
1571
|
+
native: true
|
|
1572
|
+
}), (rows)=>{
|
|
1573
|
+
let found = new Map(rows.map((b)=>[
|
|
1574
|
+
b.entity.eid,
|
|
1575
|
+
stored(b)
|
|
1576
|
+
]));
|
|
1577
|
+
for (let eid of missing)peerRows.set(eid, found.get(eid) ?? null);
|
|
1578
|
+
return collect();
|
|
1579
|
+
});
|
|
1580
|
+
};
|
|
1581
|
+
let candidatesOf = (sub)=>{
|
|
1582
|
+
let targets = peerTargets(sub.ast ?? parse(sub.query));
|
|
1583
|
+
if (!sub.durable) {
|
|
1584
|
+
return targets.length ? after(graph.get(targets, undefined, {
|
|
1585
|
+
...reading(sub),
|
|
1586
|
+
native: true
|
|
1587
|
+
}), (roots)=>roots.map(stored)) : [];
|
|
1588
|
+
}
|
|
1589
|
+
let read = ()=>after(graph.read(sub.durable, {
|
|
1590
|
+
...reading(sub),
|
|
1591
|
+
durable: true,
|
|
1592
|
+
native: !!sub.ast || graph.rewrites(sub.opts) || sub.opts?.native
|
|
1593
|
+
}), (rows)=>{
|
|
1594
|
+
return targets.length ? after(graph.get(targets, undefined, {
|
|
1595
|
+
...reading(sub),
|
|
1596
|
+
native: true
|
|
1597
|
+
}), (roots)=>[
|
|
1598
|
+
...rows,
|
|
1599
|
+
...roots
|
|
1600
|
+
].map(stored)) : rows.map(stored);
|
|
1601
|
+
});
|
|
1602
|
+
if (sub.opts?.now != null || moving(sub.durable)) return read();
|
|
1603
|
+
let key = JSON.stringify([
|
|
1604
|
+
sub.durable,
|
|
1605
|
+
sub.opts ?? null,
|
|
1606
|
+
targets
|
|
1607
|
+
]);
|
|
1608
|
+
let found = durableCandidates.get(key);
|
|
1609
|
+
if (found && (!isPromise(found.rows) || found.sub.abort === sub.abort)) return found.rows;
|
|
1610
|
+
if (found) return read();
|
|
1611
|
+
let held = {
|
|
1612
|
+
sub: {
|
|
1613
|
+
...sub,
|
|
1614
|
+
reads: dependencies(interest(sub.durable, graph.vocab, graph.worn), sub.opts),
|
|
1615
|
+
members: new Set(),
|
|
1616
|
+
want: null
|
|
1617
|
+
},
|
|
1618
|
+
rows: []
|
|
1619
|
+
};
|
|
1620
|
+
durableCandidates.set(key, held);
|
|
1621
|
+
let release = ()=>{
|
|
1622
|
+
if (durableCandidates.get(key) === held) durableCandidates.delete(key);
|
|
1623
|
+
};
|
|
1624
|
+
try {
|
|
1625
|
+
held.rows = after(read(), (rows)=>{
|
|
1626
|
+
held.sub.members = new Set(rows.map((b)=>b.entity.eid));
|
|
1627
|
+
if (rows.length > HELD) release();
|
|
1628
|
+
let n = 0;
|
|
1629
|
+
for (let [key, entry] of [
|
|
1630
|
+
...durableCandidates
|
|
1631
|
+
].reverse()){
|
|
1632
|
+
n += entry.sub.members.size;
|
|
1633
|
+
if (n > KEPT) durableCandidates.delete(key);
|
|
1634
|
+
}
|
|
1635
|
+
return rows;
|
|
1636
|
+
});
|
|
1637
|
+
if (isPromise(held.rows)) {
|
|
1638
|
+
held.rows = held.rows.catch((err)=>{
|
|
1639
|
+
release();
|
|
1640
|
+
throw err;
|
|
1641
|
+
});
|
|
1642
|
+
}
|
|
1643
|
+
} catch (err) {
|
|
1644
|
+
release();
|
|
1645
|
+
throw err;
|
|
1646
|
+
}
|
|
1647
|
+
while(durableCandidates.size > HELD){
|
|
1648
|
+
durableCandidates.delete(durableCandidates.keys().next().value);
|
|
1649
|
+
}
|
|
1650
|
+
return held.rows;
|
|
1651
|
+
};
|
|
1652
|
+
let gathered = (sub, scope, scoped)=>{
|
|
1653
|
+
let targets = peerTargets(sub.ast ?? parse(sub.query));
|
|
1654
|
+
let candidates = scope ? after(scoped ?? after(graph.get([
|
|
1655
|
+
...scope
|
|
1656
|
+
], undefined, {
|
|
1657
|
+
native: true
|
|
1658
|
+
}), (rows)=>rows.map(stored)), (rows)=>targets.length ? after(graph.get(targets, undefined, {
|
|
1659
|
+
...reading(sub),
|
|
1660
|
+
native: true
|
|
1661
|
+
}), (roots)=>[
|
|
1662
|
+
...rows,
|
|
1663
|
+
...roots
|
|
1664
|
+
].map(stored)) : rows) : sub.candidates ?? (sub.candidates = candidatesOf(sub));
|
|
1665
|
+
if (scope) {
|
|
1666
|
+
return after(candidates, (rows)=>{
|
|
1667
|
+
let ids = new Set(scope);
|
|
1668
|
+
if (sub.ref) {
|
|
1669
|
+
for (let row of overlay(rows, peers.values(scope))){
|
|
1670
|
+
let eid = row[sub.ref.comp]?.[sub.ref.prop];
|
|
1671
|
+
if (typeof eid == 'string') ids.add(eid);
|
|
1672
|
+
}
|
|
1673
|
+
}
|
|
1674
|
+
let values = peers.values(ids);
|
|
1675
|
+
return after(durableRows(values.map((b)=>b.entity.eid)), (held)=>overlay([
|
|
1676
|
+
...rows,
|
|
1677
|
+
...held
|
|
1678
|
+
], values));
|
|
1679
|
+
});
|
|
1680
|
+
}
|
|
1681
|
+
let values = peers.values();
|
|
1682
|
+
let ids = [
|
|
1683
|
+
...new Set(values.map((b)=>b.entity.eid))
|
|
1684
|
+
];
|
|
1685
|
+
let refs = sub.ref && !scope && ids.length ? linked(sub.ref, ids) : [];
|
|
1686
|
+
return after(candidates, (rows)=>after(refs, (linked)=>after(durableRows(ids), (held)=>overlay([
|
|
1687
|
+
...rows,
|
|
1688
|
+
...linked,
|
|
1689
|
+
...held
|
|
1690
|
+
], values))));
|
|
1691
|
+
};
|
|
1692
|
+
let source = (sub, scope, scoped)=>after(gathered(sub, scope, scoped), (rows)=>graph.answer(rows, {
|
|
1693
|
+
...reading(sub),
|
|
1694
|
+
native: true
|
|
1695
|
+
}));
|
|
1696
|
+
let read = (sub, scope, prepared)=>{
|
|
1697
|
+
if (sub.view) return after(viewed(sub.view, reading(sub)), flat);
|
|
1698
|
+
if (!sub.peer) {
|
|
1699
|
+
return graph.read(queryOf(sub), {
|
|
1700
|
+
...reading(sub),
|
|
1701
|
+
durable: true,
|
|
1702
|
+
native: !!sub.ast || graph.rewrites(sub.opts) || sub.opts?.native
|
|
1703
|
+
});
|
|
1704
|
+
}
|
|
1705
|
+
return after(after(prepared ?? gathered(sub, scope), (rows)=>graph.answer(rows, {
|
|
1706
|
+
...reading(sub),
|
|
1707
|
+
native: true
|
|
1708
|
+
})), (bundles)=>{
|
|
1709
|
+
let chosen = matcher(queryOf(sub), graph.vocab, sub.opts)(bundles);
|
|
1710
|
+
return chosen.filter((b)=>!scope || scope.has(b.entity.eid)).map((b)=>sub.cut(stored(b)));
|
|
1711
|
+
});
|
|
1712
|
+
};
|
|
1713
|
+
let snapshot = (line, readOpts = {}, nested = false, reduce = true)=>{
|
|
1714
|
+
let ast = typeof line == 'string' ? parse(line) : line;
|
|
1715
|
+
let op = reduce ? aggregate(ast) : null;
|
|
1716
|
+
if (readOpts.durable) {
|
|
1717
|
+
return op ? after(graph.rows(line, readOpts), (rows)=>reduced(op, rows)) : graph.read(line, readOpts);
|
|
1718
|
+
}
|
|
1719
|
+
let plan = peerPlan(ast, graph.vocab);
|
|
1720
|
+
let p = op ? null : projection(graph.vocab, ast);
|
|
1721
|
+
let cut = p?.cut ?? only(named(graph.vocab, line, nested || !!readOpts.native));
|
|
1722
|
+
if (!plan.peers) {
|
|
1723
|
+
if (op) {
|
|
1724
|
+
return after(graph.rows(ast, {
|
|
1725
|
+
...readOpts,
|
|
1726
|
+
durable: true,
|
|
1727
|
+
native: nested || readOpts.native || graph.rewrites(readOpts)
|
|
1728
|
+
}), (rows)=>reduced(op, rows));
|
|
1729
|
+
}
|
|
1730
|
+
// A projection answers what is stored, the entities it reaches too.
|
|
1731
|
+
if (p) {
|
|
1732
|
+
return graph.read(ast, {
|
|
1733
|
+
...readOpts,
|
|
1734
|
+
durable: true,
|
|
1735
|
+
native: nested || readOpts.native || graph.rewrites(readOpts)
|
|
1736
|
+
});
|
|
1737
|
+
}
|
|
1738
|
+
return after(graph.read(ast, {
|
|
1739
|
+
...readOpts,
|
|
1740
|
+
durable: true,
|
|
1741
|
+
native: nested || readOpts.native || graph.rewrites(readOpts)
|
|
1742
|
+
}), (rows)=>rows.map((row)=>cut(overlay([
|
|
1743
|
+
row
|
|
1744
|
+
], peers.values([
|
|
1745
|
+
row.entity.eid
|
|
1746
|
+
]))[0])));
|
|
1747
|
+
}
|
|
1748
|
+
if (p?.reaches.length) {
|
|
1749
|
+
throw new Refused('a projection through a reference is not read over relayed values');
|
|
1750
|
+
}
|
|
1751
|
+
let sub = {
|
|
1752
|
+
id: '',
|
|
1753
|
+
sink: ()=>{},
|
|
1754
|
+
send: ()=>{},
|
|
1755
|
+
raw: false,
|
|
1756
|
+
query: typeof line == 'string' ? line : '',
|
|
1757
|
+
ast: typeof line == 'string' ? undefined : line,
|
|
1758
|
+
opts: {
|
|
1759
|
+
...readOpts,
|
|
1760
|
+
native: nested || readOpts.native
|
|
1761
|
+
},
|
|
1762
|
+
members: new Set(),
|
|
1763
|
+
fields: new Map(),
|
|
1764
|
+
targets: new Map(),
|
|
1765
|
+
routed: false,
|
|
1766
|
+
peer: true,
|
|
1767
|
+
durable: plan.durable,
|
|
1768
|
+
ref: plan.ref,
|
|
1769
|
+
cut,
|
|
1770
|
+
riders: new Set()
|
|
1771
|
+
};
|
|
1772
|
+
return after(source(sub), (bundles)=>op ? reduced(op, matchRows(line, graph.vocab, readOpts)(bundles)) : matcher(line, graph.vocab, readOpts)(bundles).map(cut));
|
|
1773
|
+
};
|
|
1774
|
+
// A durable watch kept by the page may still carry relays for its rows.
|
|
1775
|
+
// Recover only the touched owners' membership, never its retained catalog.
|
|
1776
|
+
let recoverMembership = (bundles)=>{
|
|
1777
|
+
let deferred = all().filter((s)=>s.deferred);
|
|
1778
|
+
if (!deferred.length || !bundles.length) return;
|
|
1779
|
+
let ids = [
|
|
1780
|
+
...new Set(bundles.map((b)=>b.entity.eid))
|
|
1781
|
+
];
|
|
1782
|
+
return after(graph.get(ids, undefined, {
|
|
1783
|
+
native: true
|
|
1784
|
+
}), (rows)=>after(over(deferred, (s)=>{
|
|
1785
|
+
let i = s.reads;
|
|
1786
|
+
let candidates = rows.filter((row)=>i.own.every((c)=>c in row));
|
|
1787
|
+
if (!candidates.length) return;
|
|
1788
|
+
if (i.whole || i.far.size || i.via.size || s.plan?.reaches.length) return open(s.sink, s.id, s.query, s.opts);
|
|
1789
|
+
for (let row of matcher(queryOf(s), graph.vocab, s.opts)(candidates))s.members.add(row.entity.eid);
|
|
1790
|
+
}), ()=>{}));
|
|
1791
|
+
};
|
|
1792
|
+
let peerChange = (bundles, except, casted = false)=>{
|
|
1793
|
+
if (ending.signal.aborted || !bundles.length) return;
|
|
1794
|
+
for (let mine of held.values()){
|
|
1795
|
+
for (let sub of mine.values()){
|
|
1796
|
+
if (sub.loading && (!sub.reads || sub.peer)) sub.dirty = true;
|
|
1797
|
+
}
|
|
1798
|
+
}
|
|
1799
|
+
if (!casted) {
|
|
1800
|
+
return after(recoverMembership(bundles), ()=>{
|
|
1801
|
+
cast(bundles, except);
|
|
1802
|
+
return peerChange(bundles, except, true);
|
|
1803
|
+
});
|
|
1804
|
+
}
|
|
1805
|
+
notify(bundles);
|
|
1806
|
+
// Old members hear the patch that moved a row out; new members receive
|
|
1807
|
+
// its full held value in the membership frame below.
|
|
1808
|
+
let touched = [
|
|
1809
|
+
...new Set(bundles.map((b)=>b.entity.eid))
|
|
1810
|
+
];
|
|
1811
|
+
let values = peers.values(touched);
|
|
1812
|
+
let active = new Set(values.map((b)=>b.entity.eid));
|
|
1813
|
+
let release = ()=>{
|
|
1814
|
+
for (let eid of touched){
|
|
1815
|
+
if (active.has(eid)) continue;
|
|
1816
|
+
peerRows.delete(eid);
|
|
1817
|
+
for (let [key, value] of backlinks){
|
|
1818
|
+
if (value.peer == eid) backlinks.delete(key);
|
|
1819
|
+
}
|
|
1820
|
+
}
|
|
1821
|
+
};
|
|
1822
|
+
if (!all().some((s)=>s.peer)) {
|
|
1823
|
+
release();
|
|
1824
|
+
return;
|
|
1825
|
+
}
|
|
1826
|
+
// Every watch that follows the same reference sees the same rows for
|
|
1827
|
+
// this movement. The cross-movement cache avoids SQL; this batch map
|
|
1828
|
+
// avoids rebuilding the combined answer for every watch.
|
|
1829
|
+
let batchLinks = new Map();
|
|
1830
|
+
let batchSources = new Map();
|
|
1831
|
+
return after(durableRows(touched), (rows)=>{
|
|
1832
|
+
return after(routeAll(overlay(rows, values), touched, true, true), (routing)=>{
|
|
1833
|
+
release();
|
|
1834
|
+
return after(over(all().filter((s)=>s.peer), (s)=>attempt(s, ()=>{
|
|
1835
|
+
if (s.view) return push(s);
|
|
1836
|
+
if (s.routed) return send(s, routing.get(s));
|
|
1837
|
+
if (!s.ref || s.window) return push(s);
|
|
1838
|
+
let roots = new Set(bundles.filter((b)=>s.ref.comp in b).map((b)=>b.entity.eid));
|
|
1839
|
+
let moved = bundles.filter((b)=>Object.keys(b).some((c)=>s.ref.far.has(c)));
|
|
1840
|
+
if (!moved.length && !roots.size) return;
|
|
1841
|
+
let ids = [
|
|
1842
|
+
...new Set(moved.map((b)=>b.entity.eid))
|
|
1843
|
+
];
|
|
1844
|
+
let key = JSON.stringify([
|
|
1845
|
+
s.ref.comp,
|
|
1846
|
+
s.ref.prop,
|
|
1847
|
+
ids
|
|
1848
|
+
]);
|
|
1849
|
+
let found = batchLinks.get(key);
|
|
1850
|
+
if (!found) {
|
|
1851
|
+
found = ids.length ? linked(s.ref, ids) : [];
|
|
1852
|
+
batchLinks.set(key, found);
|
|
1853
|
+
}
|
|
1854
|
+
return after(found, (refs)=>{
|
|
1855
|
+
let scope = new Set([
|
|
1856
|
+
...roots,
|
|
1857
|
+
...refs.map((b)=>b.entity.eid)
|
|
1858
|
+
]);
|
|
1859
|
+
if (!scope.size) return;
|
|
1860
|
+
let own = rows.filter((b)=>roots.has(b.entity.eid));
|
|
1861
|
+
let sourceKey = JSON.stringify([
|
|
1862
|
+
key,
|
|
1863
|
+
[
|
|
1864
|
+
...roots
|
|
1865
|
+
]
|
|
1866
|
+
]);
|
|
1867
|
+
let prepared = batchSources.get(sourceKey);
|
|
1868
|
+
if (!prepared) {
|
|
1869
|
+
prepared = source(s, scope, [
|
|
1870
|
+
...refs,
|
|
1871
|
+
...own
|
|
1872
|
+
]);
|
|
1873
|
+
batchSources.set(sourceKey, prepared);
|
|
1874
|
+
}
|
|
1875
|
+
return push(s, scope, undefined, prepared, true);
|
|
1876
|
+
});
|
|
1877
|
+
})), ()=>undefined);
|
|
1878
|
+
});
|
|
1879
|
+
});
|
|
1880
|
+
};
|
|
1881
|
+
const live = transient(base);
|
|
1882
|
+
const pending = new Map();
|
|
1883
|
+
let scheduled = false;
|
|
1884
|
+
const flush = ()=>{
|
|
1885
|
+
scheduled = false;
|
|
1886
|
+
const batch = [
|
|
1887
|
+
...pending
|
|
1888
|
+
];
|
|
1889
|
+
pending.clear();
|
|
1890
|
+
for (const [sink, ids] of batch){
|
|
1891
|
+
for (const [id, frames] of ids){
|
|
1892
|
+
held.get(sink)?.get(id)?.send({
|
|
1893
|
+
id,
|
|
1894
|
+
transient: frames
|
|
1895
|
+
});
|
|
1896
|
+
}
|
|
1897
|
+
}
|
|
1898
|
+
};
|
|
1899
|
+
let stopLive = live.subscribe((frame)=>{
|
|
1900
|
+
if (syncOf(graph.vocab, frame.component) == 'none') return;
|
|
1901
|
+
for (const mine of held.values()){
|
|
1902
|
+
for (const sub of mine.values()){
|
|
1903
|
+
if (!sub.raw && !visible(sub, frame)) continue;
|
|
1904
|
+
let ids = pending.get(sub.sink);
|
|
1905
|
+
if (!ids) pending.set(sub.sink, ids = new Map());
|
|
1906
|
+
const frames = ids.get(sub.id) ?? [];
|
|
1907
|
+
frames.push(frame);
|
|
1908
|
+
ids.set(sub.id, frames);
|
|
1909
|
+
}
|
|
1910
|
+
}
|
|
1911
|
+
if (!scheduled) {
|
|
1912
|
+
scheduled = true;
|
|
1913
|
+
queueMicrotask(flush);
|
|
1914
|
+
}
|
|
1915
|
+
});
|
|
1916
|
+
// The writer is answered at its commit, and the pass it starts goes on
|
|
1917
|
+
// without it. A pass that fails is said here, where nobody waits on it.
|
|
1918
|
+
graph.use({
|
|
1919
|
+
name: '@yaks/api',
|
|
1920
|
+
// This hook follows its registry's reading. A namespace view supplies
|
|
1921
|
+
// that namespace at registration.
|
|
1922
|
+
namespace: null,
|
|
1923
|
+
hooks: {
|
|
1924
|
+
effect: (bundles, _tx, _err, context)=>{
|
|
1925
|
+
if (ending.signal.aborted) return bundles;
|
|
1926
|
+
let target = opts.activity ?? base;
|
|
1927
|
+
if (context?.parent && peek(target)) {
|
|
1928
|
+
link(target, bundles, context.parent);
|
|
1929
|
+
}
|
|
1930
|
+
let pass = commit(bundles);
|
|
1931
|
+
if (isPromise(pass)) {
|
|
1932
|
+
pass.finally(()=>unlink(target, bundles)).catch(()=>{});
|
|
1933
|
+
} else unlink(target, bundles);
|
|
1934
|
+
if (isPromise(pass)) pass.catch((err)=>fault(err, 'subscriptions'));
|
|
1935
|
+
return bundles;
|
|
1936
|
+
}
|
|
1937
|
+
}
|
|
1938
|
+
});
|
|
1939
|
+
let batch;
|
|
1940
|
+
let flushPeers = ()=>{
|
|
1941
|
+
let next = batch;
|
|
1942
|
+
if (!next) return;
|
|
1943
|
+
batch = undefined;
|
|
1944
|
+
next.cancel();
|
|
1945
|
+
let run = ()=>{
|
|
1946
|
+
let writes = new Map();
|
|
1947
|
+
for (let { sink, row } of next.writes.values()){
|
|
1948
|
+
let rows = writes.get(sink) ?? [];
|
|
1949
|
+
rows.push(row);
|
|
1950
|
+
writes.set(sink, rows);
|
|
1951
|
+
}
|
|
1952
|
+
let bundles = coalesced([
|
|
1953
|
+
...next.writes.values()
|
|
1954
|
+
].map((w)=>w.row));
|
|
1955
|
+
return after(recoverMembership(bundles), ()=>{
|
|
1956
|
+
for (let [sink, rows] of writes)cast(coalesced(rows), sink);
|
|
1957
|
+
return peerChange(bundles, undefined, true);
|
|
1958
|
+
});
|
|
1959
|
+
};
|
|
1960
|
+
try {
|
|
1961
|
+
let out = ordered(run);
|
|
1962
|
+
if (isPromise(out)) out.then(next.resolve, next.reject);
|
|
1963
|
+
else next.resolve();
|
|
1964
|
+
} catch (err) {
|
|
1965
|
+
next.reject(err);
|
|
1966
|
+
}
|
|
1967
|
+
};
|
|
1968
|
+
let enqueue = (sink, bundles, writer)=>{
|
|
1969
|
+
bundles = formed(bundles);
|
|
1970
|
+
let stage = (accepted)=>{
|
|
1971
|
+
if (ending.signal.aborted) return;
|
|
1972
|
+
let rows = peers.write(sink, accepted);
|
|
1973
|
+
if (!rows.length) return;
|
|
1974
|
+
if (!batch) {
|
|
1975
|
+
let resolve, reject;
|
|
1976
|
+
let done = new Promise((yes, no)=>{
|
|
1977
|
+
resolve = yes;
|
|
1978
|
+
reject = no;
|
|
1979
|
+
});
|
|
1980
|
+
let cancel = (opts.timer ?? ((fn, ms)=>{
|
|
1981
|
+
let t = setTimeout(fn, ms);
|
|
1982
|
+
return ()=>clearTimeout(t);
|
|
1983
|
+
}))(flushPeers, 16);
|
|
1984
|
+
batch = {
|
|
1985
|
+
writes: new Map(),
|
|
1986
|
+
done,
|
|
1987
|
+
resolve,
|
|
1988
|
+
reject,
|
|
1989
|
+
cancel
|
|
1990
|
+
};
|
|
1991
|
+
}
|
|
1992
|
+
for (let row of rows){
|
|
1993
|
+
for (let [comp, patch] of comps(row)){
|
|
1994
|
+
let key = JSON.stringify([
|
|
1995
|
+
row.entity.eid,
|
|
1996
|
+
comp
|
|
1997
|
+
]);
|
|
1998
|
+
let was = batch.writes.get(key)?.row[comp];
|
|
1999
|
+
let previous = was && typeof was == 'object' ? was : {};
|
|
2000
|
+
batch.writes.set(key, {
|
|
2001
|
+
sink,
|
|
2002
|
+
row: {
|
|
2003
|
+
entity: row.entity,
|
|
2004
|
+
[comp]: patch == null ? null : {
|
|
2005
|
+
...previous,
|
|
2006
|
+
...patch
|
|
2007
|
+
}
|
|
2008
|
+
}
|
|
2009
|
+
});
|
|
2010
|
+
}
|
|
2011
|
+
}
|
|
2012
|
+
return batch.done;
|
|
2013
|
+
};
|
|
2014
|
+
// The fan-out promise must not lock the registry while its batch is
|
|
2015
|
+
// gathering more inputs. Flush orders delivery after storage work.
|
|
2016
|
+
let delivery;
|
|
2017
|
+
let out = ordered(()=>after(saves.write(sink, bundles, writer, peers.values(bundles.map((b)=>b.entity.eid))), (accepted)=>{
|
|
2018
|
+
delivery = stage(accepted);
|
|
2019
|
+
}));
|
|
2020
|
+
return after(out, ()=>delivery);
|
|
2021
|
+
};
|
|
2022
|
+
// Views supply canonical candidates first; peer values are overlaid before
|
|
2023
|
+
// reconstructing and selecting the caller's components.
|
|
2024
|
+
let viewValues = (view, readOpts = {})=>after(graph.read(view.query, {
|
|
2025
|
+
...readOpts,
|
|
2026
|
+
native: true,
|
|
2027
|
+
durable: true
|
|
2028
|
+
}), (rows)=>after(view.expand ? view.expand(rows) : rows, (expanded)=>{
|
|
2029
|
+
// Canonical live reads strip even positions reconstructed from an
|
|
2030
|
+
// unmoved saved source. A durable caller component keeps its saved
|
|
2031
|
+
// baseline; held values replace that baseline under active holders.
|
|
2032
|
+
let baseline = view.saved || readOpts.durable ? expanded : expanded.map(stored);
|
|
2033
|
+
return view.answer(overlay(baseline, peers.values()));
|
|
2034
|
+
}));
|
|
2035
|
+
let viewed = (view, readOpts = {})=>after(viewValues(view, readOpts), (bundles)=>{
|
|
2036
|
+
let p = projection(view.vocab, view.original);
|
|
2037
|
+
if (p) return p.fold(matchRows(p.query, view.vocab, readOpts)(bundles));
|
|
2038
|
+
return answered(matcher(view.original, view.vocab, readOpts)(bundles).map(only(named(view.vocab, view.original))));
|
|
2039
|
+
});
|
|
2040
|
+
let answering = (query, readOpts = {}, reduce = true)=>{
|
|
2041
|
+
if (readOpts.durable) return snapshot(query, readOpts, false, reduce);
|
|
2042
|
+
return after(graph.view(query, readOpts), (view)=>{
|
|
2043
|
+
if (view) {
|
|
2044
|
+
let op = reduce ? aggregate(view.original) : null;
|
|
2045
|
+
return op ? after(viewValues(view, readOpts), (bundles)=>reduced(op, matchRows(view.original, view.vocab, readOpts)(bundles))) : after(viewed(view, readOpts), flat);
|
|
2046
|
+
}
|
|
2047
|
+
return after(graph.ask(query, readOpts), (q)=>after(snapshot(q, readOpts, q !== query, reduce), (out)=>Array.isArray(out) ? graph.answer(out, readOpts) : out));
|
|
2048
|
+
});
|
|
2049
|
+
};
|
|
2050
|
+
// A commit feed gives precise patches; a revision proves whether anything
|
|
2051
|
+
// changed even when no feed is installed. Such a change has no write set,
|
|
2052
|
+
// and neither have commits a feed missed, so every query gets an
|
|
2053
|
+
// authoritative reset and retained answers let go.
|
|
2054
|
+
let check = ()=>{
|
|
2055
|
+
if (ending.signal.aborted || !opts.revision) return;
|
|
2056
|
+
return after(knownRevision, ()=>after(opts.revision(), (next)=>{
|
|
2057
|
+
if (Object.is(next, revision)) return;
|
|
2058
|
+
revision = next;
|
|
2059
|
+
return resync();
|
|
2060
|
+
}));
|
|
2061
|
+
};
|
|
2062
|
+
let resync = ()=>{
|
|
2063
|
+
if (ending.signal.aborted) return;
|
|
2064
|
+
advance();
|
|
2065
|
+
for (let mine of held.values()){
|
|
2066
|
+
for (let sub of mine.values())if (sub.loading) sub.dirty = true;
|
|
2067
|
+
}
|
|
2068
|
+
for (let share of shared.values())unshare(share);
|
|
2069
|
+
peerRows.clear();
|
|
2070
|
+
backlinks.clear();
|
|
2071
|
+
durableCandidates.clear();
|
|
2072
|
+
let answers = new Map();
|
|
2073
|
+
return after(parallel(all().filter((s)=>!s.raw), (s)=>open(s.sink, s.id, s.query, s.opts, answers, false, true)), ()=>{});
|
|
2074
|
+
};
|
|
2075
|
+
let stopVocabulary = base.onVocabulary?.((previous)=>{
|
|
2076
|
+
let out = ordered(()=>{
|
|
2077
|
+
let watched = [
|
|
2078
|
+
...held.values()
|
|
2079
|
+
].flatMap((mine)=>[
|
|
2080
|
+
...mine.values()
|
|
2081
|
+
]);
|
|
2082
|
+
let queries = watched.filter((s)=>!s.raw);
|
|
2083
|
+
for (let sub of queries){
|
|
2084
|
+
let mine = held.get(sub.sink);
|
|
2085
|
+
mine.delete(sub.id);
|
|
2086
|
+
forget(sub, false);
|
|
2087
|
+
}
|
|
2088
|
+
for (let share of shared.values())unshare(share);
|
|
2089
|
+
scopedNets.clear();
|
|
2090
|
+
durableNet = net(graph.vocab);
|
|
2091
|
+
peerNet = net(graph.vocab);
|
|
2092
|
+
peerRows.clear();
|
|
2093
|
+
backlinks.clear();
|
|
2094
|
+
durableCandidates.clear();
|
|
2095
|
+
advance();
|
|
2096
|
+
let names = namesBetween(previous, graph.vocab);
|
|
2097
|
+
return parallel(queries, (sub)=>{
|
|
2098
|
+
let mine = held.get(sub.sink);
|
|
2099
|
+
mine.set(sub.id, sub);
|
|
2100
|
+
try {
|
|
2101
|
+
let query = names.query(sub.query);
|
|
2102
|
+
return open(sub.sink, sub.id, query, names.opts(sub.opts), undefined, false, true);
|
|
2103
|
+
} catch (error) {
|
|
2104
|
+
cut(sub, error);
|
|
2105
|
+
}
|
|
2106
|
+
});
|
|
2107
|
+
});
|
|
2108
|
+
if (isPromise(out)) {
|
|
2109
|
+
out.catch((error)=>fault(error, 'subscription vocabulary'));
|
|
2110
|
+
}
|
|
2111
|
+
});
|
|
2112
|
+
let checking = ()=>{
|
|
2113
|
+
let out = check();
|
|
2114
|
+
if (isPromise(out)) out.catch((err)=>fault(err, 'subscription revision'));
|
|
2115
|
+
};
|
|
2116
|
+
let reopen = (sub)=>{
|
|
2117
|
+
if (sub.deferred && held.get(sub.sink)?.get(sub.id) === sub) {
|
|
2118
|
+
return open(sub.sink, sub.id, sub.query, sub.opts);
|
|
2119
|
+
}
|
|
2120
|
+
};
|
|
2121
|
+
let resuming;
|
|
2122
|
+
let resume = ()=>{
|
|
2123
|
+
if (resuming) return resuming;
|
|
2124
|
+
let deferred = all().filter((sub)=>sub.deferred);
|
|
2125
|
+
let turn = opts.yieldRecovery;
|
|
2126
|
+
if (!turn || !deferred.length) return parallel(deferred, (s)=>reopen(s));
|
|
2127
|
+
return resuming = (async ()=>{
|
|
2128
|
+
// Restore equal queries under the same reader together, so each
|
|
2129
|
+
// after the first is answered from the one it read (`opening`).
|
|
2130
|
+
let groups = groupBy(deferred, keyOf);
|
|
2131
|
+
for (let group of groups.values()){
|
|
2132
|
+
for (let sub of group){
|
|
2133
|
+
await turn();
|
|
2134
|
+
await reopen(sub);
|
|
2135
|
+
}
|
|
2136
|
+
}
|
|
2137
|
+
})().finally(()=>resuming = undefined);
|
|
2138
|
+
};
|
|
2139
|
+
return {
|
|
2140
|
+
stop: ()=>{
|
|
2141
|
+
if (ending.signal.aborted) return;
|
|
2142
|
+
ending.abort(new Refused('subscription registry stopped'));
|
|
2143
|
+
let subs = [
|
|
2144
|
+
...held.values()
|
|
2145
|
+
].flatMap((mine)=>[
|
|
2146
|
+
...mine.values()
|
|
2147
|
+
]);
|
|
2148
|
+
held.clear();
|
|
2149
|
+
for (let sub of subs)forget(sub, false);
|
|
2150
|
+
for (let share of shared.values())unshare(share);
|
|
2151
|
+
stopVocabulary?.();
|
|
2152
|
+
stopLive();
|
|
2153
|
+
peers.close();
|
|
2154
|
+
pending.clear();
|
|
2155
|
+
observers.clear();
|
|
2156
|
+
batch?.cancel();
|
|
2157
|
+
batch?.resolve();
|
|
2158
|
+
batch = undefined;
|
|
2159
|
+
},
|
|
2160
|
+
get vocab () {
|
|
2161
|
+
return graph.vocab;
|
|
2162
|
+
},
|
|
2163
|
+
activity: opts.activity ?? base,
|
|
2164
|
+
// A host can read while its own graph apply is saving a relay. This read
|
|
2165
|
+
// takes the currently admitted held values without entering ordered work.
|
|
2166
|
+
read: (query, readOpts)=>after(answering(query, readOpts, false), (out)=>out),
|
|
2167
|
+
observe: (fn)=>{
|
|
2168
|
+
observers.add(fn);
|
|
2169
|
+
return ()=>void observers.delete(fn);
|
|
2170
|
+
},
|
|
2171
|
+
snapshot: (query, readOpts)=>{
|
|
2172
|
+
flushPeers();
|
|
2173
|
+
checking();
|
|
2174
|
+
return after(answering(query, readOpts), (out)=>Array.isArray(out) ? published(graph.vocab, out) : out);
|
|
2175
|
+
},
|
|
2176
|
+
enqueue,
|
|
2177
|
+
open: (sink, id, query, readOpts)=>{
|
|
2178
|
+
ending.signal.throwIfAborted();
|
|
2179
|
+
flushPeers();
|
|
2180
|
+
checking();
|
|
2181
|
+
return open(sink, id, query, readOpts);
|
|
2182
|
+
},
|
|
2183
|
+
restore: (openings, deferStatic = false)=>{
|
|
2184
|
+
ending.signal.throwIfAborted();
|
|
2185
|
+
flushPeers();
|
|
2186
|
+
checking();
|
|
2187
|
+
let answers = new Map();
|
|
2188
|
+
return parallel(openings, ({ sink, id, query, opts })=>open(sink, id, query, opts, answers, deferStatic));
|
|
2189
|
+
},
|
|
2190
|
+
resume,
|
|
2191
|
+
close: (sink, id)=>{
|
|
2192
|
+
flushPeers();
|
|
2193
|
+
pending.get(sink)?.delete(id);
|
|
2194
|
+
let sub = held.get(sink)?.get(id);
|
|
2195
|
+
held.get(sink)?.delete(id);
|
|
2196
|
+
forget(sub);
|
|
2197
|
+
},
|
|
2198
|
+
drop: (sink)=>{
|
|
2199
|
+
flushPeers();
|
|
2200
|
+
pending.delete(sink);
|
|
2201
|
+
let mine = held.get(sink);
|
|
2202
|
+
held.delete(sink);
|
|
2203
|
+
for (let sub of mine?.values() ?? [])forget(sub);
|
|
2204
|
+
if (ending.signal.aborted) return saves.drop(sink);
|
|
2205
|
+
return ordered(()=>{
|
|
2206
|
+
// Connection-lifetime values leave with their writer. Timed values
|
|
2207
|
+
// remain available for another writer until their own expiry.
|
|
2208
|
+
return after(saves.drop(sink), ()=>{
|
|
2209
|
+
let off = peers.drop(sink);
|
|
2210
|
+
if (off.length) return peerChange(off);
|
|
2211
|
+
});
|
|
2212
|
+
});
|
|
2213
|
+
},
|
|
2214
|
+
commit,
|
|
2215
|
+
hear,
|
|
2216
|
+
check: ()=>{
|
|
2217
|
+
flushPeers();
|
|
2218
|
+
return ordered(check);
|
|
2219
|
+
},
|
|
2220
|
+
relay: (sink, bundles, writer)=>{
|
|
2221
|
+
bundles = formed(bundles);
|
|
2222
|
+
flushPeers();
|
|
2223
|
+
// Admit before queueing, so even a patch superseded while waiting still
|
|
2224
|
+
// gets the same refusal as one sent without a backlog.
|
|
2225
|
+
if (saved(bundles)) {
|
|
2226
|
+
return ordered(()=>after(saves.write(sink, bundles, writer, peers.values(bundles.map((b)=>b.entity.eid))), (accepted)=>{
|
|
2227
|
+
if (ending.signal.aborted) return;
|
|
2228
|
+
let out = peers.write(sink, accepted);
|
|
2229
|
+
return peerChange(out, sink);
|
|
2230
|
+
}));
|
|
2231
|
+
}
|
|
2232
|
+
return after(saves.write(sink, bundles, writer, peers.values(bundles.map((b)=>b.entity.eid))), (accepted)=>{
|
|
2233
|
+
if (ending.signal.aborted) return;
|
|
2234
|
+
let waiting = relays.get(sink);
|
|
2235
|
+
if (waiting) {
|
|
2236
|
+
waiting.bundles = coalesced([
|
|
2237
|
+
...waiting.bundles,
|
|
2238
|
+
...accepted
|
|
2239
|
+
]);
|
|
2240
|
+
return waiting.done;
|
|
2241
|
+
}
|
|
2242
|
+
let run = (rows)=>ending.signal.aborted ? undefined : peerChange(peers.write(sink, rows), sink);
|
|
2243
|
+
if (!pendingWork) return ordered(()=>run(accepted));
|
|
2244
|
+
let next = {
|
|
2245
|
+
bundles: accepted,
|
|
2246
|
+
done: Promise.resolve()
|
|
2247
|
+
};
|
|
2248
|
+
relays.set(sink, next);
|
|
2249
|
+
next.done = ordered(()=>{
|
|
2250
|
+
relays.delete(sink);
|
|
2251
|
+
return run(next.bundles);
|
|
2252
|
+
});
|
|
2253
|
+
return next.done;
|
|
2254
|
+
});
|
|
2255
|
+
},
|
|
2256
|
+
relaying: (sink)=>peers.holds(sink),
|
|
2257
|
+
relayed: (sink, keys)=>ordered(()=>{
|
|
2258
|
+
let before = new Set(peers.holds(sink));
|
|
2259
|
+
peers.adopt(sink, keys);
|
|
2260
|
+
let restored = peers.holds(sink).filter((key)=>{
|
|
2261
|
+
let comp = key.slice(key.indexOf(' ') + 1);
|
|
2262
|
+
return !before.has(key) && saveOf(graph.vocab, comp) != null;
|
|
2263
|
+
});
|
|
2264
|
+
if (!restored.length) return;
|
|
2265
|
+
let ids = [
|
|
2266
|
+
...new Set(restored.map((k)=>k.slice(0, k.indexOf(' '))))
|
|
2267
|
+
];
|
|
2268
|
+
return after(graph.get(ids, undefined, {
|
|
2269
|
+
native: true,
|
|
2270
|
+
durable: true
|
|
2271
|
+
}), (rows)=>{
|
|
2272
|
+
let held = new Set(restored);
|
|
2273
|
+
let values = rows.flatMap((row)=>{
|
|
2274
|
+
let value = {
|
|
2275
|
+
entity: row.entity
|
|
2276
|
+
};
|
|
2277
|
+
for (let [comp, patch] of comps(row)){
|
|
2278
|
+
if (held.has(row.entity.eid + ' ' + comp) && patch != null) {
|
|
2279
|
+
value[comp] = patch;
|
|
2280
|
+
}
|
|
2281
|
+
}
|
|
2282
|
+
return comps(value).length ? [
|
|
2283
|
+
value
|
|
2284
|
+
] : [];
|
|
2285
|
+
});
|
|
2286
|
+
return peerChange(peers.write(sink, values), sink);
|
|
2287
|
+
});
|
|
2288
|
+
}),
|
|
2289
|
+
pace: (comp)=>peerComp(comp) ? paceOf(graph.vocab, comp) : null,
|
|
2290
|
+
save: (comp)=>saveOf(graph.vocab, comp),
|
|
2291
|
+
now: opts.now,
|
|
2292
|
+
timer: opts.timer
|
|
2293
|
+
};
|
|
2294
|
+
};
|
|
2295
|
+
//# sourceMappingURL=subs.js.map
|