@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/reached.ts
ADDED
|
@@ -0,0 +1,549 @@
|
|
|
1
|
+
// Subscriptions over graphs this host can ask but not watch: the members of a
|
|
2
|
+
// reach (@yaks/reach), several graphs answering as one. No member's commit
|
|
3
|
+
// passes through here as a transaction the registry could route, so a member
|
|
4
|
+
// feeding news says only that something moved: every line is asked again,
|
|
5
|
+
// whole, and its answer is sent when it moved. Like a tide table read again
|
|
6
|
+
// whenever any harbour reports, rather than a harbour's own log.
|
|
7
|
+
//
|
|
8
|
+
// A line is answered in the frames `subscriptions` speaks: whole, with
|
|
9
|
+
// `reset`, and again after each change that moved it. {@link beside} puts it
|
|
10
|
+
// next to a graph's own registry, which keeps the raw feed and relayed
|
|
11
|
+
// values, so one socket asks each line of whichever answers it.
|
|
12
|
+
|
|
13
|
+
import type { Bundle, Graph, ReadOpts, Reduced } from 'jsr:@yaks/graph@0.2.36'
|
|
14
|
+
import { hook } from '@yaks/graph/plugin'
|
|
15
|
+
import { after, type Clock, system } from '@yaks/fp'
|
|
16
|
+
import { fault, refusal, type Tags } from './refuse.js'
|
|
17
|
+
import type {
|
|
18
|
+
Ask,
|
|
19
|
+
Frame,
|
|
20
|
+
Opening,
|
|
21
|
+
RecoverableListening,
|
|
22
|
+
Sink,
|
|
23
|
+
Subs,
|
|
24
|
+
} from './subs.ts'
|
|
25
|
+
import type { PeerWriter } from './save.ts'
|
|
26
|
+
|
|
27
|
+
/** A registry over a reach, as {@link reached} makes one. */
|
|
28
|
+
export type Reached = {
|
|
29
|
+
/** Watch a line as `opts` and `writer` say who asks, and send its
|
|
30
|
+
* answer. */
|
|
31
|
+
open: (
|
|
32
|
+
sink: Sink,
|
|
33
|
+
id: string,
|
|
34
|
+
line: string,
|
|
35
|
+
opts?: ReadOpts,
|
|
36
|
+
writer?: PeerWriter,
|
|
37
|
+
) => void
|
|
38
|
+
/** Reopen saved watches. With `quiet`, each is held and not asked until
|
|
39
|
+
* {@link Reached.resume} or the next {@link Reached.hear}: a page that
|
|
40
|
+
* kept its answers through its host's sleep is not sent them again. */
|
|
41
|
+
restore: (openings: Opening[], quiet?: boolean) => void | Promise<void>
|
|
42
|
+
/** Ask every watch held quiet. */
|
|
43
|
+
resume: () => void | Promise<void>
|
|
44
|
+
close: (sink: Sink, id: string) => void
|
|
45
|
+
drop: (sink: Sink) => void
|
|
46
|
+
/** A member's graph moved: each watch selected by its line and read
|
|
47
|
+
* options is asked again. Omit `only` to refresh every watch. */
|
|
48
|
+
hear: (only?: (line: string, read?: ReadOpts) => boolean) => Promise<void>
|
|
49
|
+
/** Whether any line is watched. */
|
|
50
|
+
watching: () => boolean
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
type Watch = {
|
|
54
|
+
line: string
|
|
55
|
+
asked: number
|
|
56
|
+
opts?: ReadOpts
|
|
57
|
+
writer?: PeerWriter
|
|
58
|
+
said?: string
|
|
59
|
+
cancel?: () => void
|
|
60
|
+
revision?: number
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
type Read = (
|
|
64
|
+
line: string,
|
|
65
|
+
opts?: ReadOpts,
|
|
66
|
+
writer?: PeerWriter,
|
|
67
|
+
) => Promise<Bundle[] | Reduced>
|
|
68
|
+
|
|
69
|
+
type Reader = { read: Read; readBatch?: never } | {
|
|
70
|
+
read?: never
|
|
71
|
+
/** Open a reader for one set of watches, so their reads can share work. */
|
|
72
|
+
readBatch: () => Read
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Subscriptions answered by `read`, the reach's one answer to a line, and
|
|
77
|
+
* asked again on each {@link Reached.hear}. A refusal is sent under its id
|
|
78
|
+
* and the subscription stays open: the next change asks again, and a member
|
|
79
|
+
* that came back answers.
|
|
80
|
+
*
|
|
81
|
+
* ```ts
|
|
82
|
+
* import { reached } from '@yaks/api/reached'
|
|
83
|
+
* import { equal, until } from '@yaks/testing'
|
|
84
|
+
*
|
|
85
|
+
* let books = [{ entity: { eid: 'dune' }, book: {} }]
|
|
86
|
+
* let subs = reached({ read: () => Promise.resolve(books) })
|
|
87
|
+
* let frames: unknown[] = []
|
|
88
|
+
* subs.open((f) => frames.push(f), 'all', '.book')
|
|
89
|
+
* await until(() => frames.length == 1)
|
|
90
|
+
* books = [...books, { entity: { eid: 'emma' }, book: {} }]
|
|
91
|
+
* await subs.hear()
|
|
92
|
+
* equal(frames[1], { id: 'all', bundles: books, reset: true })
|
|
93
|
+
* ```
|
|
94
|
+
*/
|
|
95
|
+
export let reached = (
|
|
96
|
+
opts: Reader & {
|
|
97
|
+
/** told of every answer refused, with its line, for a host that knows
|
|
98
|
+
* whose line it was */
|
|
99
|
+
refused?: (error: unknown, line: string) => void
|
|
100
|
+
/** what a failure reported here is tagged with (./refuse.ts `fault`) */
|
|
101
|
+
tags?: Tags
|
|
102
|
+
/** When a time-dependent answer next changes without a write. */
|
|
103
|
+
renew?: (answer: Bundle[] | Reduced) => number | undefined
|
|
104
|
+
/** Schedule a renewal at an absolute time. Defaults to the clock's timer. */
|
|
105
|
+
renewAt?: (at: number, read: () => Promise<void>) => () => void
|
|
106
|
+
clock?: Clock
|
|
107
|
+
/** The local revision at which a derived read starts. */
|
|
108
|
+
revision?: () => number
|
|
109
|
+
/** Give other requests a turn between watches asked one after another:
|
|
110
|
+
* those recovered after wake, and those a change asks again. */
|
|
111
|
+
yieldRecovery?: () => Promise<void>
|
|
112
|
+
/** Watches whose rows signal an upstream change on every refresh. */
|
|
113
|
+
signals?: (line: string) => boolean
|
|
114
|
+
},
|
|
115
|
+
): Reached => {
|
|
116
|
+
let held = new Map<Sink, Map<string, Watch>>()
|
|
117
|
+
let clock = opts.clock ?? system
|
|
118
|
+
let reader = opts.readBatch ?? (() => opts.read!)
|
|
119
|
+
// The lines opened in one burst share a reader, as those a change asks
|
|
120
|
+
// again do: a restart's sockets all subscribe at once, and each tab's
|
|
121
|
+
// Updates, sidebar and Friends lines read the same person's seats
|
|
122
|
+
// (bug 73244693).
|
|
123
|
+
let burst: { read: Read; at: number } | undefined
|
|
124
|
+
let opening = () => {
|
|
125
|
+
let now = clock.now()
|
|
126
|
+
if (!burst || now - burst.at >= BURST) burst = { read: reader(), at: now }
|
|
127
|
+
return burst.read
|
|
128
|
+
}
|
|
129
|
+
// A newer ask, a replacement or a close makes an answer still on its way
|
|
130
|
+
// nobody's.
|
|
131
|
+
let ask = (
|
|
132
|
+
sink: Sink,
|
|
133
|
+
id: string,
|
|
134
|
+
watch: Watch,
|
|
135
|
+
read = reader(),
|
|
136
|
+
): Promise<void> => {
|
|
137
|
+
watch.cancel?.()
|
|
138
|
+
let asked = ++watch.asked
|
|
139
|
+
let revision = opts.revision?.()
|
|
140
|
+
let current = () =>
|
|
141
|
+
held.get(sink)?.get(id) === watch && watch.asked == asked
|
|
142
|
+
return read(watch.line, watch.opts, watch.writer).then((answer) => {
|
|
143
|
+
if (!current()) return
|
|
144
|
+
let next = opts.renew?.(answer)
|
|
145
|
+
if (next !== undefined) {
|
|
146
|
+
let read = () => ask(sink, id, watch)
|
|
147
|
+
let now = clock.now()
|
|
148
|
+
watch.cancel = opts.renewAt
|
|
149
|
+
? opts.renewAt(Math.max(now + 1, next), read)
|
|
150
|
+
: clock.after(Math.min(2 ** 31 - 1, Math.max(1, next - now)), read)
|
|
151
|
+
}
|
|
152
|
+
let frame: Frame = Array.isArray(answer)
|
|
153
|
+
? { id, bundles: answer, reset: true }
|
|
154
|
+
: { id, ...answer }
|
|
155
|
+
let said = JSON.stringify(frame)
|
|
156
|
+
if (said == watch.said && !opts.signals?.(watch.line)) return
|
|
157
|
+
watch.said = said
|
|
158
|
+
if (revision !== undefined) {
|
|
159
|
+
frame.revision = revision
|
|
160
|
+
frame.since = watch.revision
|
|
161
|
+
watch.revision = revision
|
|
162
|
+
}
|
|
163
|
+
sink(frame)
|
|
164
|
+
}, (error) => {
|
|
165
|
+
if (!current()) return
|
|
166
|
+
watch.said = undefined
|
|
167
|
+
fault(error, 'reached subscription', opts.tags)
|
|
168
|
+
opts.refused?.(error, watch.line)
|
|
169
|
+
sink({ id, refused: refusal(error) })
|
|
170
|
+
})
|
|
171
|
+
}
|
|
172
|
+
// Held, and asked unless `quiet`.
|
|
173
|
+
let open = (
|
|
174
|
+
sink: Sink,
|
|
175
|
+
id: string,
|
|
176
|
+
line: string,
|
|
177
|
+
read?: ReadOpts,
|
|
178
|
+
writer?: PeerWriter,
|
|
179
|
+
quiet = false,
|
|
180
|
+
batch?: Read,
|
|
181
|
+
) => {
|
|
182
|
+
let mine = held.get(sink) ?? new Map<string, Watch>()
|
|
183
|
+
held.set(sink, mine)
|
|
184
|
+
mine.get(id)?.cancel?.()
|
|
185
|
+
let watch: Watch = { line, asked: 0, opts: read, writer }
|
|
186
|
+
mine.set(id, watch)
|
|
187
|
+
if (!quiet) {
|
|
188
|
+
let asked = ask(sink, id, watch, batch)
|
|
189
|
+
if (opts.renewAt) return asked
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
let joined = (work: Promise<void>[]) =>
|
|
193
|
+
work.length ? Promise.all(work).then(() => {}) : undefined
|
|
194
|
+
let unasked = function* () {
|
|
195
|
+
for (let [sink, mine] of held) {
|
|
196
|
+
for (let [id, watch] of mine) {
|
|
197
|
+
if (!watch.asked) yield { sink, id, watch }
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
let resuming: Promise<void> | undefined
|
|
202
|
+
let resume = () => {
|
|
203
|
+
if (resuming) return resuming
|
|
204
|
+
let batch = reader()
|
|
205
|
+
let turn = opts.yieldRecovery
|
|
206
|
+
if (turn) {
|
|
207
|
+
let watches = [...unasked()]
|
|
208
|
+
if (!watches.length) return
|
|
209
|
+
return resuming = (async () => {
|
|
210
|
+
for (let { sink, id, watch } of watches) {
|
|
211
|
+
await turn()
|
|
212
|
+
if (!watch.asked && held.get(sink)?.get(id) === watch) {
|
|
213
|
+
await ask(sink, id, watch, batch)
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
})().finally(() => resuming = undefined)
|
|
217
|
+
}
|
|
218
|
+
let work: Promise<void>[] = []
|
|
219
|
+
for (let { sink, id, watch } of unasked()) {
|
|
220
|
+
let asked = ask(sink, id, watch, batch)
|
|
221
|
+
if (opts.renewAt) work.push(asked)
|
|
222
|
+
}
|
|
223
|
+
return joined(work)
|
|
224
|
+
}
|
|
225
|
+
return {
|
|
226
|
+
open: (sink, id, line, read, writer) => {
|
|
227
|
+
// The subscription is registered before its read settles, so a later
|
|
228
|
+
// unsubscribe can remove it from the socket attachment immediately.
|
|
229
|
+
void open(sink, id, line, read, writer, false, opening())
|
|
230
|
+
},
|
|
231
|
+
restore: (openings, quiet) => {
|
|
232
|
+
let batch = quiet ? undefined : reader()
|
|
233
|
+
let work: Promise<void>[] = []
|
|
234
|
+
for (let { sink, id, query, opts: read, writer } of openings) {
|
|
235
|
+
if (query === true) continue
|
|
236
|
+
let asked = open(sink, id, query, read, writer, quiet, batch)
|
|
237
|
+
if (asked) work.push(asked)
|
|
238
|
+
}
|
|
239
|
+
return joined(work)
|
|
240
|
+
},
|
|
241
|
+
resume,
|
|
242
|
+
watching: () => [...held.values()].some((mine) => mine.size > 0),
|
|
243
|
+
close: (sink, id) => {
|
|
244
|
+
let mine = held.get(sink)
|
|
245
|
+
mine?.get(id)?.cancel?.()
|
|
246
|
+
mine?.delete(id)
|
|
247
|
+
if (!mine?.size) held.delete(sink)
|
|
248
|
+
},
|
|
249
|
+
drop: (sink) => {
|
|
250
|
+
for (let watch of held.get(sink)?.values() ?? []) watch.cancel?.()
|
|
251
|
+
held.delete(sink)
|
|
252
|
+
},
|
|
253
|
+
hear: async (only) => {
|
|
254
|
+
let batch = reader()
|
|
255
|
+
let chosen = [...held].flatMap(([sink, mine]) =>
|
|
256
|
+
[...mine].flatMap(([id, watch]) =>
|
|
257
|
+
!only || only(watch.line, watch.opts) ? [{ sink, id, watch }] : []
|
|
258
|
+
)
|
|
259
|
+
)
|
|
260
|
+
let turn = opts.yieldRecovery
|
|
261
|
+
if (!turn) {
|
|
262
|
+
await Promise.all(
|
|
263
|
+
chosen.map(({ sink, id, watch }) => ask(sink, id, watch, batch)),
|
|
264
|
+
)
|
|
265
|
+
return
|
|
266
|
+
}
|
|
267
|
+
// Asked all at once, every watch's read ran before a request waiting
|
|
268
|
+
// on the host was let in: nineteen asks of a person's Updates held the
|
|
269
|
+
// directory 1.2 s (bug 73244693).
|
|
270
|
+
for (let { sink, id, watch } of chosen) {
|
|
271
|
+
await turn()
|
|
272
|
+
if (held.get(sink)?.get(id) === watch) await ask(sink, id, watch, batch)
|
|
273
|
+
}
|
|
274
|
+
},
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
// How long after its first open a burst's reader serves later ones. A
|
|
279
|
+
// reader keeps what it read only while the graph stands as it was
|
|
280
|
+
// (`readBatch`), so this bounds what it holds, never what it answers.
|
|
281
|
+
let BURST = 1_000
|
|
282
|
+
|
|
283
|
+
// A turn of the host's event loop: what it answers goes out first.
|
|
284
|
+
let yielded = () => new Promise<void>((done) => setTimeout(done, 0))
|
|
285
|
+
|
|
286
|
+
/** A derived read that changes with committed inputs in a local graph: each
|
|
287
|
+
* commit asks again the lines `affects` says it moves, after the write that
|
|
288
|
+
* made it has answered. An entity the commit deleted reaches `affects` as it
|
|
289
|
+
* stood before, beside its `$delete`, so a deletion is judged by what it took
|
|
290
|
+
* away; one deleted by another (a cascade) reaches it bare.
|
|
291
|
+
*
|
|
292
|
+
* ```ts
|
|
293
|
+
* import { assertEquals } from '@std/assert'
|
|
294
|
+
* import { graph } from '@yaks/graph'
|
|
295
|
+
* import { ram } from '@yaks/ram'
|
|
296
|
+
* import { loadVocab } from '@yaks/vocab'
|
|
297
|
+
* let vocab = loadVocab({ $defs: { book: { component: true } } })
|
|
298
|
+
* let g = graph({ vocab, storage: ram(vocab) })
|
|
299
|
+
* await g.apply([{ entity: { eid: 'a' }, book: {} }])
|
|
300
|
+
* let seen: unknown[] = []
|
|
301
|
+
* let subs = computed(g, {
|
|
302
|
+
* affects: (bundles) => (seen.push(...bundles), false),
|
|
303
|
+
* read: async () => [],
|
|
304
|
+
* })
|
|
305
|
+
* subs.open(() => {}, 'all', '.book')
|
|
306
|
+
* await g.apply([{ entity: { eid: 'a' }, $delete: true }])
|
|
307
|
+
* await new Promise((done) => setTimeout(done, 10))
|
|
308
|
+
* assertEquals(seen, [{ entity: { eid: 'a' }, book: {}, $delete: true }])
|
|
309
|
+
* ```
|
|
310
|
+
*/
|
|
311
|
+
export let computed = (
|
|
312
|
+
graph: Graph,
|
|
313
|
+
opts: Parameters<typeof reached>[0] & {
|
|
314
|
+
affects: (bundles: Bundle[]) => Affected | Promise<Affected>
|
|
315
|
+
hold?: (work: Promise<void>) => void
|
|
316
|
+
},
|
|
317
|
+
): Reached => {
|
|
318
|
+
let clock = opts.clock ?? system
|
|
319
|
+
let revision = clock.now()
|
|
320
|
+
let queries = reached({
|
|
321
|
+
...opts,
|
|
322
|
+
clock,
|
|
323
|
+
revision: () => revision = Math.max(revision + 1, clock.now()),
|
|
324
|
+
})
|
|
325
|
+
// The commits not yet asked about, and the asking under way. Commits that
|
|
326
|
+
// land while the watches are asked again are asked about together once it
|
|
327
|
+
// ends, so a burst of writes asks each line twice at most rather than once
|
|
328
|
+
// a write, each ask reading it again whole (bug 73244693).
|
|
329
|
+
let owed: Bundle[] = []
|
|
330
|
+
let asking: Promise<void> | undefined
|
|
331
|
+
// It yields before it looks, so it ends only after `asking` holds it: a
|
|
332
|
+
// commit of no bundles, ended at once, left `asking` held for good and
|
|
333
|
+
// every later commit owed, never asked about (bug d2f05876).
|
|
334
|
+
let drain = async () => {
|
|
335
|
+
try {
|
|
336
|
+
do {
|
|
337
|
+
await yielded()
|
|
338
|
+
let bundles = owed.splice(0)
|
|
339
|
+
try {
|
|
340
|
+
let only = await opts.affects(bundles)
|
|
341
|
+
if (only) {
|
|
342
|
+
await queries.hear(typeof only == 'function' ? only : undefined)
|
|
343
|
+
}
|
|
344
|
+
} catch (e) {
|
|
345
|
+
fault(e, 'computed subscription', opts.tags)
|
|
346
|
+
}
|
|
347
|
+
} while (owed.length)
|
|
348
|
+
} finally {
|
|
349
|
+
asking = undefined
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
// What each transaction's deletions held, read before they were written. A
|
|
353
|
+
// bare `$delete` says nothing of what it took, and a deleted row no line
|
|
354
|
+
// draws (a spent wake's) is no reason to read every line again.
|
|
355
|
+
let deleted = new WeakMap<object, Map<string, Bundle>>()
|
|
356
|
+
let stood = (b: Bundle, was?: Map<string, Bundle>) => {
|
|
357
|
+
let held = b.$delete ? was?.get(b.entity.eid) : undefined
|
|
358
|
+
return held ? { ...held, ...b } : b
|
|
359
|
+
}
|
|
360
|
+
graph.use({
|
|
361
|
+
name: '@yaks/api/computed',
|
|
362
|
+
// Its one check reads, and holds nothing past the transaction.
|
|
363
|
+
admission: () => true,
|
|
364
|
+
hooks: {
|
|
365
|
+
precondition: hook(['$delete'], (bundles, tx, _error, context) => {
|
|
366
|
+
let at = context?.transaction
|
|
367
|
+
let eids = bundles.filter((b) => b.$delete).map((b) => b.entity.eid)
|
|
368
|
+
if (!at || !eids.length || !queries.watching()) return bundles
|
|
369
|
+
return after(tx.get(eids), (rows) => {
|
|
370
|
+
deleted.set(at, new Map(rows.map((b) => [b.entity.eid, b])))
|
|
371
|
+
return bundles
|
|
372
|
+
})
|
|
373
|
+
}),
|
|
374
|
+
// What a commit moved is asked again once the write that made it has
|
|
375
|
+
// answered: the reads are the host's to run, and those run now would
|
|
376
|
+
// hold the write's answer until every watcher's line was read.
|
|
377
|
+
effect: (bundles, _tx, _error, context) => {
|
|
378
|
+
if (queries.watching()) {
|
|
379
|
+
let was = context?.transaction && deleted.get(context.transaction)
|
|
380
|
+
owed.push(...was ? bundles.map((b) => stood(b, was)) : bundles)
|
|
381
|
+
if (!asking) {
|
|
382
|
+
asking = drain()
|
|
383
|
+
opts.hold?.(asking)
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
return bundles
|
|
387
|
+
},
|
|
388
|
+
},
|
|
389
|
+
})
|
|
390
|
+
return queries
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
type Affected = boolean | NonNullable<Parameters<Reached['hear']>[0]>
|
|
394
|
+
|
|
395
|
+
/**
|
|
396
|
+
* One registry of two: the lines `borrows` names asked of `lent`, and every
|
|
397
|
+
* other subscription (its own lines, the raw feed) and every relayed value
|
|
398
|
+
* of `subs`, so a host's socket serves both as one ({@link Subs}). A watch
|
|
399
|
+
* replaced by a line the other answers moves to it.
|
|
400
|
+
*
|
|
401
|
+
* ```ts
|
|
402
|
+
* import { reached, beside } from '@yaks/api/reached'
|
|
403
|
+
* import { subscriptions } from '@yaks/api'
|
|
404
|
+
* import { graph } from '@yaks/graph'
|
|
405
|
+
* import { ram } from '@yaks/ram'
|
|
406
|
+
* import { loadVocab } from '@yaks/vocab'
|
|
407
|
+
* import { equal, until } from '@yaks/testing'
|
|
408
|
+
*
|
|
409
|
+
* let vocab = loadVocab({ $defs: { book: { component: true, type: 'object' } } })
|
|
410
|
+
* let own = subscriptions(graph({ vocab, storage: ram(vocab) }))
|
|
411
|
+
* let lent = reached({
|
|
412
|
+
* read: () => Promise.resolve([{ entity: { eid: 'loan' }, loan: {} }]),
|
|
413
|
+
* })
|
|
414
|
+
* let both = beside(own, lent, (line) => line.includes('loan'))
|
|
415
|
+
* let frames: unknown[] = []
|
|
416
|
+
* both.open((f) => frames.push(f), 'loans', '.loan')
|
|
417
|
+
* await until(() => frames.length == 1)
|
|
418
|
+
* equal(lent.watching(), true)
|
|
419
|
+
* ```
|
|
420
|
+
*/
|
|
421
|
+
export let beside = (
|
|
422
|
+
subs: Subs,
|
|
423
|
+
lent: Reached,
|
|
424
|
+
borrows: (line: string) => boolean,
|
|
425
|
+
): Subs => {
|
|
426
|
+
let theirs = (query: Ask): query is string => query !== true && borrows(query)
|
|
427
|
+
// Which of a sink's watches the lent registry holds, so a close goes to
|
|
428
|
+
// the one holding it and a replacement leaves the other.
|
|
429
|
+
let away = new Map<Sink, Set<string>>()
|
|
430
|
+
let lend = (sink: Sink, id: string, yes: boolean) => {
|
|
431
|
+
let ids = away.get(sink) ?? new Set<string>()
|
|
432
|
+
away.set(sink, ids)
|
|
433
|
+
let was = ids.has(id)
|
|
434
|
+
if (yes) ids.add(id)
|
|
435
|
+
else ids.delete(id)
|
|
436
|
+
return was
|
|
437
|
+
}
|
|
438
|
+
return {
|
|
439
|
+
...subs,
|
|
440
|
+
open: (sink, id, query, opts, writer) => {
|
|
441
|
+
let was = lend(sink, id, theirs(query))
|
|
442
|
+
if (theirs(query)) {
|
|
443
|
+
return after(
|
|
444
|
+
was ? undefined : subs.close(sink, id),
|
|
445
|
+
() => lent.open(sink, id, query, opts, writer),
|
|
446
|
+
)
|
|
447
|
+
}
|
|
448
|
+
if (was) lent.close(sink, id)
|
|
449
|
+
return subs.open(sink, id, query, opts, writer)
|
|
450
|
+
},
|
|
451
|
+
restore: (openings, deferStatic) => {
|
|
452
|
+
for (let o of openings) lend(o.sink, o.id, theirs(o.query))
|
|
453
|
+
return after(
|
|
454
|
+
lent.restore(openings.filter((o) => theirs(o.query)), deferStatic),
|
|
455
|
+
() =>
|
|
456
|
+
subs.restore(openings.filter((o) => !theirs(o.query)), deferStatic),
|
|
457
|
+
)
|
|
458
|
+
},
|
|
459
|
+
resume: () => after(lent.resume(), () => subs.resume?.()),
|
|
460
|
+
close: (sink, id) => {
|
|
461
|
+
if (lend(sink, id, false)) return lent.close(sink, id)
|
|
462
|
+
return subs.close(sink, id)
|
|
463
|
+
},
|
|
464
|
+
drop: (sink) => {
|
|
465
|
+
away.delete(sink)
|
|
466
|
+
lent.drop(sink)
|
|
467
|
+
return subs.drop(sink)
|
|
468
|
+
},
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
/** A frame a kept watch sends, before it is said under a subscription's id:
|
|
473
|
+
* its `refused` is the error itself. */
|
|
474
|
+
export type Kept = Omit<Frame, 'id' | 'refused'> & { refused?: unknown }
|
|
475
|
+
|
|
476
|
+
/**
|
|
477
|
+
* Subscriptions whose every line is a live watch another keeps, such as a
|
|
478
|
+
* reach's (@yaks/reach `watch`): what it sends goes out under the
|
|
479
|
+
* subscription's id, and closing the subscription stops it. It answers lines
|
|
480
|
+
* only, so the raw feed is refused and relayed values go nowhere.
|
|
481
|
+
*
|
|
482
|
+
* ```ts
|
|
483
|
+
* import { watched } from '@yaks/api/reached'
|
|
484
|
+
* import { equal } from '@yaks/testing'
|
|
485
|
+
*
|
|
486
|
+
* let stopped = 0
|
|
487
|
+
* let subs = watched({
|
|
488
|
+
* watch: (line, sink) => {
|
|
489
|
+
* sink({ bundles: [{ entity: { eid: line } }], reset: true })
|
|
490
|
+
* return () => stopped++
|
|
491
|
+
* },
|
|
492
|
+
* })
|
|
493
|
+
* let frames: unknown[] = []
|
|
494
|
+
* let to = (f: unknown) => void frames.push(f)
|
|
495
|
+
* subs.open(to, 'a', 'dune')
|
|
496
|
+
* subs.drop(to)
|
|
497
|
+
* equal(frames, [{ id: 'a', bundles: [{ entity: { eid: 'dune' } }], reset: true }])
|
|
498
|
+
* equal(stopped, 1)
|
|
499
|
+
* ```
|
|
500
|
+
*/
|
|
501
|
+
export let watched = (opts: {
|
|
502
|
+
/** watch a line as `read` says who asks; returns what stops it */
|
|
503
|
+
watch: (
|
|
504
|
+
line: string,
|
|
505
|
+
sink: (frame: Kept) => void,
|
|
506
|
+
read?: ReadOpts,
|
|
507
|
+
) => () => void
|
|
508
|
+
/** what a failure reported here is tagged with (./refuse.ts `fault`) */
|
|
509
|
+
tags?: Tags
|
|
510
|
+
}): RecoverableListening => {
|
|
511
|
+
let held = new Map<Sink, Map<string, () => void>>()
|
|
512
|
+
let close = (sink: Sink, id: string) => {
|
|
513
|
+
held.get(sink)?.get(id)?.()
|
|
514
|
+
held.get(sink)?.delete(id)
|
|
515
|
+
}
|
|
516
|
+
let listener: RecoverableListening = {
|
|
517
|
+
open: (sink, id, query, read) => {
|
|
518
|
+
if (query === true) throw new Error('a watch answers lines, not a feed')
|
|
519
|
+
close(sink, id)
|
|
520
|
+
let mine = held.get(sink) ?? new Map<string, () => void>()
|
|
521
|
+
held.set(sink, mine)
|
|
522
|
+
let live = true
|
|
523
|
+
let stop = opts.watch(query, (frame) => {
|
|
524
|
+
if (!live) return
|
|
525
|
+
if (!('refused' in frame)) return sink({ id, ...frame } as Frame)
|
|
526
|
+
fault(frame.refused, 'watched subscription', opts.tags)
|
|
527
|
+
sink({ id, refused: refusal(frame.refused) })
|
|
528
|
+
}, read)
|
|
529
|
+
mine.set(id, () => {
|
|
530
|
+
live = false
|
|
531
|
+
stop()
|
|
532
|
+
})
|
|
533
|
+
},
|
|
534
|
+
close,
|
|
535
|
+
drop: (sink) => {
|
|
536
|
+
for (let id of [...held.get(sink)?.keys() ?? []]) close(sink, id)
|
|
537
|
+
held.delete(sink)
|
|
538
|
+
},
|
|
539
|
+
relay: () => {},
|
|
540
|
+
relaying: () => [],
|
|
541
|
+
relayed: () => {},
|
|
542
|
+
restore: (openings) => {
|
|
543
|
+
for (let { sink, id, query, opts, writer } of openings) {
|
|
544
|
+
listener.open(sink, id, query, opts, writer)
|
|
545
|
+
}
|
|
546
|
+
},
|
|
547
|
+
}
|
|
548
|
+
return listener
|
|
549
|
+
}
|