@yaks/inspect 0.0.0-stage → 0.2.36

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (133) hide show
  1. package/Comp.js +469 -0
  2. package/Comp.js.map +1 -0
  3. package/Comp.ts +471 -0
  4. package/Effect.js +136 -0
  5. package/Effect.js.map +1 -0
  6. package/Effect.ts +142 -0
  7. package/Entity.js +112 -0
  8. package/Entity.js.map +1 -0
  9. package/Entity.ts +137 -0
  10. package/Facts.js +143 -0
  11. package/Facts.js.map +1 -0
  12. package/Facts.ts +150 -0
  13. package/Head.js +196 -0
  14. package/Head.js.map +1 -0
  15. package/Head.ts +195 -0
  16. package/Held.js +144 -0
  17. package/Held.js.map +1 -0
  18. package/Held.ts +172 -0
  19. package/History.js +157 -0
  20. package/History.js.map +1 -0
  21. package/History.ts +215 -0
  22. package/Home.js +482 -0
  23. package/Home.js.map +1 -0
  24. package/Home.ts +550 -0
  25. package/LICENSE +202 -0
  26. package/Links.js +197 -0
  27. package/Links.js.map +1 -0
  28. package/Links.ts +212 -0
  29. package/Package.js +195 -0
  30. package/Package.js.map +1 -0
  31. package/Package.ts +191 -0
  32. package/Past.js +163 -0
  33. package/Past.js.map +1 -0
  34. package/Past.ts +181 -0
  35. package/Prop.js +225 -0
  36. package/Prop.js.map +1 -0
  37. package/Prop.ts +208 -0
  38. package/Query.js +207 -0
  39. package/Query.js.map +1 -0
  40. package/Query.ts +229 -0
  41. package/README.md +141 -2
  42. package/Words.js +43 -0
  43. package/Words.js.map +1 -0
  44. package/Words.ts +70 -0
  45. package/_dist/Entity.d.ts +7 -0
  46. package/_dist/Entity.d.ts.map +1 -0
  47. package/_dist/Facts.d.ts +7 -0
  48. package/_dist/Facts.d.ts.map +1 -0
  49. package/_dist/Head.d.ts +22 -0
  50. package/_dist/Head.d.ts.map +1 -0
  51. package/_dist/Held.d.ts +37 -0
  52. package/_dist/Held.d.ts.map +1 -0
  53. package/_dist/Links.d.ts +9 -0
  54. package/_dist/Links.d.ts.map +1 -0
  55. package/_dist/Past.d.ts +65 -0
  56. package/_dist/Past.d.ts.map +1 -0
  57. package/_dist/Words.d.ts +27 -0
  58. package/_dist/Words.d.ts.map +1 -0
  59. package/_dist/census.d.ts +10 -0
  60. package/_dist/census.d.ts.map +1 -0
  61. package/_dist/door.d.ts +28 -0
  62. package/_dist/door.d.ts.map +1 -0
  63. package/_dist/front.d.ts +4 -0
  64. package/_dist/front.d.ts.map +1 -0
  65. package/_dist/host.d.ts +94 -0
  66. package/_dist/host.d.ts.map +1 -0
  67. package/_dist/mod.d.ts +24 -0
  68. package/_dist/mod.d.ts.map +1 -0
  69. package/_dist/notes.d.ts +38 -0
  70. package/_dist/notes.d.ts.map +1 -0
  71. package/_dist/read.d.ts +59 -0
  72. package/_dist/read.d.ts.map +1 -0
  73. package/_dist/reading.d.ts +21 -0
  74. package/_dist/reading.d.ts.map +1 -0
  75. package/_dist/registry.d.ts +12 -0
  76. package/_dist/registry.d.ts.map +1 -0
  77. package/_dist/schema.d.ts +4 -0
  78. package/_dist/schema.d.ts.map +1 -0
  79. package/_dist/state.d.ts +22 -0
  80. package/_dist/state.d.ts.map +1 -0
  81. package/_dist/value.d.ts +20 -0
  82. package/_dist/value.d.ts.map +1 -0
  83. package/_dist/views.d.ts +3 -0
  84. package/_dist/views.d.ts.map +1 -0
  85. package/census.js +50 -0
  86. package/census.js.map +1 -0
  87. package/census.ts +76 -0
  88. package/deno.json +1 -0
  89. package/door.js +54 -0
  90. package/door.js.map +1 -0
  91. package/door.ts +59 -0
  92. package/front.js +12 -0
  93. package/front.js.map +1 -0
  94. package/front.json +76 -0
  95. package/front.ts +13 -0
  96. package/grid.js +249 -0
  97. package/grid.js.map +1 -0
  98. package/grid.ts +395 -0
  99. package/host.js +20 -0
  100. package/host.js.map +1 -0
  101. package/host.ts +134 -0
  102. package/mod.js +20 -0
  103. package/mod.js.map +1 -0
  104. package/mod.ts +45 -0
  105. package/notes.js +184 -0
  106. package/notes.js.map +1 -0
  107. package/notes.ts +288 -0
  108. package/package.json +35 -4
  109. package/read.js +300 -0
  110. package/read.js.map +1 -0
  111. package/read.ts +386 -0
  112. package/reading.js +64 -0
  113. package/reading.js.map +1 -0
  114. package/reading.ts +73 -0
  115. package/registry.js +89 -0
  116. package/registry.js.map +1 -0
  117. package/registry.ts +89 -0
  118. package/rows.js +12 -0
  119. package/rows.js.map +1 -0
  120. package/rows.ts +26 -0
  121. package/schema.js +47 -0
  122. package/schema.js.map +1 -0
  123. package/schema.ts +70 -0
  124. package/state.js +85 -0
  125. package/state.js.map +1 -0
  126. package/state.ts +128 -0
  127. package/terminal-links.ts +16 -0
  128. package/value.js +67 -0
  129. package/value.js.map +1 -0
  130. package/value.ts +79 -0
  131. package/views.js +4 -0
  132. package/views.js.map +1 -0
  133. package/views.ts +6 -0
package/read.ts ADDED
@@ -0,0 +1,386 @@
1
+ /**
2
+ * What the inspector reads off a bundle or an answer: pure functions, the
3
+ * vocabulary of every view. How a value is shaped, which hue a name wears, the
4
+ * components an entity carries, the tables an archetype names, how many
5
+ * entities carry each component, and what a client may write.
6
+ *
7
+ * @module
8
+ */
9
+
10
+ import { nameIn, type Vocab } from '@yaks/vocab'
11
+ import type { Bundle, Io } from './host.ts'
12
+
13
+ /** One component, as a bundle carries it. */
14
+ export type Comp = Record<string, unknown>
15
+
16
+ let EID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/
17
+ let HEX = /^[0-9a-f]{32,40}$/
18
+ let AT = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}/
19
+
20
+ /**
21
+ * The hue a component's name wears (@yaks/ui `Chip-0` … `Chip-5`). Provenance
22
+ * rides nearly every entity, so it keeps the warm end; every other name
23
+ * hashes into the other four, the same on every page.
24
+ *
25
+ * ```ts
26
+ * import { tone } from './read.ts'
27
+ * tone('created') // '4'
28
+ * tone('task') == tone('task') // true
29
+ * ```
30
+ */
31
+ export let tone = (name: string): string => {
32
+ if (name == 'created') return '4'
33
+ if (name == 'updated') return '5'
34
+ let hash = 2166136261
35
+ for (let ch of name) hash = Math.imul(hash ^ ch.charCodeAt(0), 16777619)
36
+ return String((hash >>> 0) % 4)
37
+ }
38
+
39
+ /**
40
+ * A stored value's shape (@yaks/ui `Value`): nothing, a number, a flag, an
41
+ * id, a moment, structure, or text.
42
+ *
43
+ * ```ts
44
+ * import { shape } from './read.ts'
45
+ * shape(null) // 'nil'
46
+ * shape('') // 'nil'
47
+ * shape(3) // 'num'
48
+ * shape('2026-09-29T19:12:21Z') // 'time'
49
+ * shape({ a: 1 }) // 'json'
50
+ * ```
51
+ */
52
+ export let shape = (v: unknown): string =>
53
+ v == null || v === ''
54
+ ? 'nil'
55
+ : typeof v == 'number'
56
+ ? 'num'
57
+ : typeof v == 'boolean'
58
+ ? 'bool'
59
+ : typeof v == 'object'
60
+ ? 'json'
61
+ : EID.test(String(v)) || HEX.test(String(v))
62
+ ? 'id'
63
+ : AT.test(String(v))
64
+ ? 'time'
65
+ : 'text'
66
+
67
+ /**
68
+ * A stored value, as text: absence says which absence it is.
69
+ *
70
+ * ```ts
71
+ * import { face } from './read.ts'
72
+ * face(null) // 'null'
73
+ * face('') // '""'
74
+ * face({ a: 1 }) // '{"a":1}'
75
+ * ```
76
+ */
77
+ export let face = (v: unknown): string =>
78
+ v === ''
79
+ ? '""'
80
+ : v == null
81
+ ? 'null'
82
+ : typeof v == 'object'
83
+ ? JSON.stringify(v)
84
+ : String(v)
85
+
86
+ /**
87
+ * A stored value as its leaves, each with the keys that lead to it: an object
88
+ * opens into its keys, a list of structures into its places, and a scalar or
89
+ * a list of scalars is a leaf. So a structure reads as rows, never as one
90
+ * line of JSON or `[object Object]`.
91
+ *
92
+ * ```ts
93
+ * import { leaves } from './read.ts'
94
+ * leaves(3) // [[[], 3]]
95
+ * leaves({ a: { b: 1 }, c: [1, 2] }) // [[['a', 'b'], 1], [['c'], [1, 2]]]
96
+ * leaves([{ a: 1 }]) // [[['0', 'a'], 1]]
97
+ * leaves({}) // [[[], {}]]
98
+ * ```
99
+ */
100
+ export let leaves = (v: unknown, at: string[] = []): [string[], unknown][] => {
101
+ let open = !!v && typeof v == 'object' &&
102
+ !(Array.isArray(v) && v.every((x) => x == null || typeof x != 'object'))
103
+ let kids = open ? Object.entries(v as object) : []
104
+ return kids.length
105
+ ? kids.flatMap(([k, x]) => leaves(x, [...at, k]))
106
+ : [[at, v]]
107
+ }
108
+
109
+ /** Whether a client may write this component, or this property of it,
110
+ * here: never where the host's controls take no input (a terminal), nor a
111
+ * component that is not on the wire or is computed, nor a property that is
112
+ * stamped or computed. */
113
+ export let writable = (
114
+ io: Pick<Io, 'vocab' | 'edits'>,
115
+ name: string,
116
+ prop?: string,
117
+ ): boolean => {
118
+ let c = io.vocab.comp(name)
119
+ if (!io.edits || !c?.wire || c.computed) return false
120
+ let p = prop == null ? undefined : io.vocab.prop(name, prop)
121
+ return prop == null || (!!p && !p.stamped && !p.computed)
122
+ }
123
+
124
+ /** Whether a value names an entity by its eid. */
125
+ export let named = (v: unknown): v is string =>
126
+ typeof v == 'string' && (EID.test(v) || HEX.test(v))
127
+
128
+ /**
129
+ * The components a bundle carries, by name, in the order it carries them:
130
+ * everything but its `entity` spine and its `$` requests.
131
+ *
132
+ * ```ts
133
+ * import { comps } from './read.ts'
134
+ * comps({ entity: { eid: 'a' }, doc: { title: 'x' }, $num: true })
135
+ * // [['doc', { title: 'x' }]]
136
+ * ```
137
+ */
138
+ export let comps = (b: Bundle): [string, Comp][] =>
139
+ Object.entries(b).filter(([k, v]) =>
140
+ k != 'entity' && !k.startsWith('$') && !!v && typeof v == 'object'
141
+ ) as [string, Comp][]
142
+
143
+ /** One component of a bundle, or an empty one. */
144
+ export let comp = (b: Bundle | undefined, name: string): Comp =>
145
+ (b?.[name] ?? {}) as Comp
146
+
147
+ /** A string property of a component, or ''. */
148
+ export let str = (
149
+ b: Bundle | undefined,
150
+ name: string,
151
+ prop: string,
152
+ ): string => {
153
+ let v = comp(b, name)[prop]
154
+ return typeof v == 'string' ? v : ''
155
+ }
156
+
157
+ /**
158
+ * The components an archetype holds (@yaks/archetype), as `vocab` names them,
159
+ * from the descriptor it stores: their identities, or their labels for words
160
+ * that have none.
161
+ *
162
+ * ```ts
163
+ * import { tables } from './read.ts'
164
+ * tables({ entity: { eid: 'a' }, archetype: { tables: '["doc","task"]' } })
165
+ * // ['doc', 'task']
166
+ * ```
167
+ */
168
+ export let tables = (
169
+ b: Bundle,
170
+ vocab?: Pick<Vocab, 'word'>,
171
+ ): string[] => {
172
+ try {
173
+ let t = JSON.parse(str(b, 'archetype', 'tables') || '[]')
174
+ if (!Array.isArray(t)) return []
175
+ return t.map((id) => vocab ? nameIn(vocab, String(id)) : String(id))
176
+ } catch {
177
+ return []
178
+ }
179
+ }
180
+
181
+ /**
182
+ * How many entities carry each component: each archetype's count of entities
183
+ * (a `.tally=entity.archetype`), added to every table it names.
184
+ *
185
+ * ```ts
186
+ * import { census } from './read.ts'
187
+ * let sets = [
188
+ * { entity: { eid: 'x' }, archetype: { tables: '["doc","task"]' } },
189
+ * { entity: { eid: 'y' }, archetype: { tables: '["doc"]' } },
190
+ * ]
191
+ * census(sets, { x: 3, y: 2 }) // { doc: 5, task: 3 }
192
+ * ```
193
+ */
194
+ export let census = (
195
+ sets: Bundle[],
196
+ tally: Record<string, number>,
197
+ vocab?: Pick<Vocab, 'word'>,
198
+ ): Record<string, number> => {
199
+ let out: Record<string, number> = {}
200
+ for (let s of sets) {
201
+ let n = tally[s.entity.eid] ?? 0
202
+ for (let t of tables(s, vocab)) out[t] = (out[t] ?? 0) + n
203
+ }
204
+ return out
205
+ }
206
+
207
+ /** The first line of a text, cut to `n` characters. */
208
+ export let line = (text: unknown, n = 120): string => {
209
+ let first = String(text ?? '').split('\n').map((l) => l.trim())
210
+ .find(Boolean) ?? ''
211
+ return first.length > n ? first.slice(0, n - 1) + '…' : first
212
+ }
213
+
214
+ /**
215
+ * The text an entity holds for reading: a doc's body, else what a message
216
+ * says (`content.body`, a transcript entry's).
217
+ *
218
+ * ```ts
219
+ * import { bodyOf } from './read.ts'
220
+ * bodyOf({ entity: { eid: 'e' }, content: { body: 'hi' } }) // 'hi'
221
+ * ```
222
+ */
223
+ export let bodyOf = (b: Bundle): string =>
224
+ str(b, 'doc', 'body') || str(b, 'content', 'body')
225
+
226
+ /**
227
+ * Some words, cut where they run past `n` characters.
228
+ *
229
+ * ```ts
230
+ * import { cut } from './read.ts'
231
+ * cut('abcdef', 4) // 'abc…'
232
+ * cut('abc', 4) // 'abc'
233
+ * ```
234
+ */
235
+ export let cut = (text: string, n: number): string =>
236
+ text.length > n ? text.slice(0, n - 1).trimEnd() + '…' : text
237
+
238
+ /**
239
+ * What an entity is called, in words: its title; else the `label` its kind
240
+ * gives it (a model's "GPT-6.2 Sol"), or the `name` (a topic's, a
241
+ * component's); else its id where it is numbered
242
+ * (`S-12`); else the first line of what it says (a transcript entry); else
243
+ * its kind and its handle (`process #1320a8ee61`). `id` is the id a person
244
+ * reads, and `kind` the kind it shows as.
245
+ *
246
+ * ```ts
247
+ * import { called } from './read.ts'
248
+ * let e = (b: object) => ({ entity: { eid: 'e1' }, ...b })
249
+ * called(e({ doc: { title: 'Fix it' } }), 'T-9', 'task') // 'Fix it'
250
+ * called(e({ topic: { name: 'naming' } }), '#e1', 'topic') // 'naming'
251
+ * called(e({ model: { name: 'gpt-6.2-sol', label: 'GPT-6.2 Sol' } }), 'O-50', 'model') // 'GPT-6.2 Sol'
252
+ * called(e({ content: { body: '\nuse grams' } }), '#e1', 'entry') // 'use grams'
253
+ * called(e({ process: { pid: 1 } }), '#e1', 'process') // 'process #e1'
254
+ * called(e({ file: { path: 'a/b/c.jsonl' } }), '#e1', 'file') // 'c.jsonl'
255
+ * ```
256
+ */
257
+ export let called = (b: Bundle, id: string, kind: string): string =>
258
+ str(b, 'doc', 'title') || str(b, kind, 'label') || str(b, kind, 'name') ||
259
+ str(b, kind, 'path').split('/').at(-1) ||
260
+ (b.entity.num != null ? id : '') || line(bodyOf(b), 80) ||
261
+ (kind && kind != 'entity' ? `${kind} ${id}` : id)
262
+
263
+ /** Where an entity came from, said once in a quiet line rather than stated
264
+ * as a fact: who made and changed it and when, what built it, where it was
265
+ * imported from, when it was checked. */
266
+ export let PROVENANCE = ['created', 'updated', 'built', 'lines', 'verified']
267
+
268
+ /** What a page says in its head and its body rather than as facts. */
269
+ let SAID = new Set(['entity', 'doc', 'content', ...PROVENANCE])
270
+
271
+ /**
272
+ * The components a page states as facts, in the order they mean: the kinds
273
+ * it is, most specific first (the vocabulary's kind order); then what else it
274
+ * carries, by name; then the marks of what happened to it. What the head and
275
+ * the body say (its title, its text, where it came from) is left out, and so
276
+ * is anything in `skip`.
277
+ *
278
+ * ```ts
279
+ * import { loadVocab } from '@yaks/vocab'
280
+ * import { facts } from './read.ts'
281
+ * let c = (o = {}) => ({ component: true, properties: {}, ...o })
282
+ * let at = { type: 'string', stamped: true }
283
+ * let vocab = loadVocab([{ $defs: {
284
+ * doc: c(), task: c({ kind: true }), filed: c(), claim: c(),
285
+ * completed: c({ properties: { at, by: at } }),
286
+ * created: c({ properties: { at, by: at } }),
287
+ * } }])
288
+ * facts(vocab, {
289
+ * entity: { eid: 'e' }, completed: {}, created: {}, filed: {}, doc: {},
290
+ * task: {}, claim: {},
291
+ * }) // ['task', 'claim', 'filed', 'completed']
292
+ * ```
293
+ */
294
+ export let facts = (
295
+ vocab: Pick<Io['vocab'], 'kinds' | 'comp'>,
296
+ b: Bundle,
297
+ skip: string[] = [],
298
+ ): string[] => {
299
+ let has = comps(b).map(([n]) => n)
300
+ .filter((n) => !SAID.has(n) && !skip.includes(n))
301
+ let kinds = vocab.kinds.filter((k) => has.includes(k))
302
+ let rest = has.filter((n) => !kinds.includes(n)).toSorted()
303
+ let marks = rest.filter((n) => vocab.comp(n)?.mark)
304
+ return [...kinds, ...rest.filter((n) => !marks.includes(n)), ...marks]
305
+ }
306
+
307
+ /**
308
+ * A long hash, as far as a reader needs it: its first ten characters.
309
+ *
310
+ * ```ts
311
+ * import { brief } from './read.ts'
312
+ * brief('f07941748e70ecf0a7272d9a05dba89f0b98cb1cde9709b12a6e1e711ef3621b')
313
+ * // 'f07941748e…'
314
+ * brief('open') // 'open'
315
+ * ```
316
+ */
317
+ export let brief = (v: string): string =>
318
+ /^[0-9a-f]{41,}$/.test(v) ? v.slice(0, 10) + '…' : v
319
+
320
+ /**
321
+ * The relation an edge's entity states: the component beside `edge` that the
322
+ * vocabulary marks `edge` (@yaks/edge). `relations` is those components'
323
+ * names.
324
+ *
325
+ * ```ts
326
+ * import { relation } from './read.ts'
327
+ * relation(
328
+ * { entity: { eid: 'e' }, edge: { from: 'a', to: 'b' }, requires: {} },
329
+ * ['requires', 'contains'],
330
+ * ) // 'requires'
331
+ * ```
332
+ */
333
+ export let relation = (b: Bundle, relations: string[]): string | undefined =>
334
+ comps(b).map(([name]) => name).find((n) => relations.includes(n))
335
+
336
+ // Counts need only decimal grouping. Keep Intl's rounding and non-finite
337
+ // behavior for other numbers without paying its constructor during module load.
338
+ let grouped: Intl.NumberFormat | undefined
339
+
340
+ /** A number, grouped for reading: 12345 reads 12,345. */
341
+ export let count = (n: number): string =>
342
+ Number.isSafeInteger(n) && !Object.is(n, -0)
343
+ ? String(n).replace(/\B(?=(\d{3})+(?!\d))/g, ',')
344
+ : (grouped ??= new Intl.NumberFormat('en-US')).format(n)
345
+
346
+ /**
347
+ * A property's type as it reads, off its `_prop` row.
348
+ *
349
+ * ```ts
350
+ * import { typed } from './read.ts'
351
+ * typed({ type: 'string', enum: ['open', 'done'] }) // 'string enum'
352
+ * typed({ type: 'string', ref: 'session' }) // 'string → session'
353
+ * ```
354
+ */
355
+ export let typed = (prop: Record<string, unknown>): string =>
356
+ [
357
+ [prop.type].flat().filter(Boolean).join('|'),
358
+ prop.format,
359
+ prop.enum ? 'enum' : null,
360
+ prop.ref ? `→ ${prop.ref}` : null,
361
+ ].filter(Boolean).join(' ')
362
+
363
+ /**
364
+ * What is special about a property, off its `_prop` row: who owns it,
365
+ * whether it is stored, how it is found.
366
+ *
367
+ * ```ts
368
+ * import { flags } from './read.ts'
369
+ * flags({ stamped: true, search: true, unique: null }) // ['stamped', 'search']
370
+ * ```
371
+ */
372
+ export let flags = (prop: Record<string, unknown>): string[] =>
373
+ ['stamped', 'computed', 'required', 'search', 'unique', 'identity']
374
+ .filter((k) => prop[k] === true)
375
+
376
+ /**
377
+ * Some values' distinct members, in the order first met, blanks left out.
378
+ *
379
+ * ```ts
380
+ * import { unique } from './read.ts'
381
+ * unique(['a', '', 'b', 'a', undefined]) // ['a', 'b']
382
+ * ```
383
+ */
384
+ export let unique = (xs: (string | undefined | null)[]): string[] => [
385
+ ...new Set(xs.filter((x): x is string => !!x)),
386
+ ]
package/reading.js ADDED
@@ -0,0 +1,64 @@
1
+ /**
2
+ * Code written for a storage, run in a view over what the view holds. A
3
+ * function that reads a storage (`read` a query, `get` some entities), such as
4
+ * a package's composer, is handed a {@link Reader} whose every read is one of
5
+ * the view's asks: answered from what the host holds and kept live while the
6
+ * view is mounted. A read not answered yet reads as nothing, so the function
7
+ * returns what it could read; as each answer lands it runs again and reads on
8
+ * from there, and when a write moves anything it read, it runs again with
9
+ * that. The server's code and the page's agree because they are the same
10
+ * code.
11
+ *
12
+ * A function that reads a storage asynchronously is still handed answers
13
+ * synchronously here: it must stay synchronous over synchronous input, as
14
+ * @yaks/fp's `after` and `each` do.
15
+ *
16
+ * @module
17
+ */ /* @ts-types="npm:@types/react@19.3.0" */ import { useLayoutEffect, useState } from "react";
18
+ import { stringify } from "@yaks/query";
19
+ /** The line `get` asks for some entities, each whole. */ /// whole(['a', 'b']) -> '.entity.eid=a,b&*'
20
+ export let whole = (eids)=>`.entity.eid=${eids.join(',')}&*`;
21
+ /**
22
+ * Run `read` over a reader whose reads are this view's asks: a hook, for a
23
+ * view drawn by `io`.
24
+ *
25
+ * ```ts ignore
26
+ * let { value, ready } = useReading(io, (r) => wear(r, io.vocab)(eid))
27
+ * ```
28
+ */ export let useReading = (io, read)=>{
29
+ let [asked, ask] = useState([]);
30
+ let got = io.ask(Object.fromEntries(asked.map((line)=>[
31
+ line,
32
+ line
33
+ ])));
34
+ let reads = new Set();
35
+ let ready = true;
36
+ let answer = (line)=>{
37
+ reads.add(line);
38
+ let a = got[line];
39
+ if (!a?.ready || a.error) ready = false;
40
+ return a?.rows ?? [];
41
+ };
42
+ let value = read({
43
+ read: (query)=>answer(stringify(query)),
44
+ get: (eids)=>eids.length ? answer(whole(eids)) : []
45
+ });
46
+ let now = [
47
+ ...reads
48
+ ].sort();
49
+ let key = now.join('\n');
50
+ let held = asked.join('\n');
51
+ // What it read that the view does not hold yet is asked for, and what it
52
+ // no longer reads let go; the answers run it again.
53
+ useLayoutEffect(()=>{
54
+ if (key != held) ask(now);
55
+ }, [
56
+ key,
57
+ held
58
+ ]);
59
+ return {
60
+ value,
61
+ ready: ready && key == held
62
+ };
63
+ };
64
+ //# sourceMappingURL=reading.js.map
package/reading.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"reading.js","sources":["./reading.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;CAgBC,GAED,uCAAuC,GAAG,SAAS,eAAe,EAAE,QAAQ,gBAA0B;AACtG,SAAS,SAAS,2BAAgC;AAclD,uDAAuD,GACvD,4CAA4C;AAC5C,OAAO,IAAI,QAAQ,CAAC,OAA2B,CAAC,YAAY,EAAE,KAAK,IAAI,CAAC,KAAK,EAAE,CAAC,CAAA;AAEhF;;;;;;;CAOC,GACD,OAAO,IAAI,aAAa,CACtB,IACA;EAEA,IAAI,CAAC,OAAO,IAAI,GAAG,SAAmB,EAAE;EACxC,IAAI,MAAM,GAAG,GAAG,CAAC,OAAO,WAAW,CAAC,MAAM,GAAG,CAAC,CAAC,OAAS;MAAC;MAAM;KAAK;EACpE,IAAI,QAAQ,IAAI;EAChB,IAAI,QAAQ;EACZ,IAAI,SAAS,CAAC;IACZ,MAAM,GAAG,CAAC;IACV,IAAI,IAAI,GAAG,CAAC,KAAK;IACjB,IAAI,CAAC,GAAG,SAAS,EAAE,KAAK,EAAE,QAAQ;IAClC,OAAO,GAAG,QAAQ,EAAE;EACtB;EACA,IAAI,QAAQ,KAAK;IACf,MAAM,CAAC,QAAU,OAAO,UAAU;IAClC,KAAK,CAAC,OAAS,KAAK,MAAM,GAAG,OAAO,MAAM,SAAS,EAAE;EACvD;EACA,IAAI,MAAM;OAAI;GAAM,CAAC,IAAI;EACzB,IAAI,MAAM,IAAI,IAAI,CAAC;EACnB,IAAI,OAAO,MAAM,IAAI,CAAC;EACtB,yEAAyE;EACzE,oDAAoD;EACpD,gBAAgB;IACd,IAAI,OAAO,MAAM,IAAI;EACvB,GAAG;IAAC;IAAK;GAAK;EACd,OAAO;IAAE;IAAO,OAAO,SAAS,OAAO;EAAK;AAC9C,EAAC"}
package/reading.ts ADDED
@@ -0,0 +1,73 @@
1
+ /**
2
+ * Code written for a storage, run in a view over what the view holds. A
3
+ * function that reads a storage (`read` a query, `get` some entities), such as
4
+ * a package's composer, is handed a {@link Reader} whose every read is one of
5
+ * the view's asks: answered from what the host holds and kept live while the
6
+ * view is mounted. A read not answered yet reads as nothing, so the function
7
+ * returns what it could read; as each answer lands it runs again and reads on
8
+ * from there, and when a write moves anything it read, it runs again with
9
+ * that. The server's code and the page's agree because they are the same
10
+ * code.
11
+ *
12
+ * A function that reads a storage asynchronously is still handed answers
13
+ * synchronously here: it must stay synchronous over synchronous input, as
14
+ * @yaks/fp's `after` and `each` do.
15
+ *
16
+ * @module
17
+ */
18
+
19
+ /* @ts-types="npm:@types/react@19.3.0" */ import { useLayoutEffect, useState } from 'react'
20
+ import { stringify } from '@yaks/query'
21
+ import type { Query } from 'jsr:@yaks/graph@0.2.36'
22
+ import type { Bundle, Io } from './host.ts'
23
+
24
+ /** A storage that only reads: what code written for one needs of it. */
25
+ export type Reader = {
26
+ read: (query: Query) => Bundle[]
27
+ get: (eids: string[]) => Bundle[]
28
+ }
29
+
30
+ /** What a reading came to: the function's value over what was answered, and
31
+ * whether every read it made was answered. */
32
+ export type Reading<T> = { value: T; ready: boolean }
33
+
34
+ /** The line `get` asks for some entities, each whole. */
35
+ /// whole(['a', 'b']) -> '.entity.eid=a,b&*'
36
+ export let whole = (eids: string[]): string => `.entity.eid=${eids.join(',')}&*`
37
+
38
+ /**
39
+ * Run `read` over a reader whose reads are this view's asks: a hook, for a
40
+ * view drawn by `io`.
41
+ *
42
+ * ```ts ignore
43
+ * let { value, ready } = useReading(io, (r) => wear(r, io.vocab)(eid))
44
+ * ```
45
+ */
46
+ export let useReading = <T>(
47
+ io: Io,
48
+ read: (reader: Reader) => T,
49
+ ): Reading<T> => {
50
+ let [asked, ask] = useState<string[]>([])
51
+ let got = io.ask(Object.fromEntries(asked.map((line) => [line, line])))
52
+ let reads = new Set<string>()
53
+ let ready = true
54
+ let answer = (line: string): Bundle[] => {
55
+ reads.add(line)
56
+ let a = got[line]
57
+ if (!a?.ready || a.error) ready = false
58
+ return a?.rows ?? []
59
+ }
60
+ let value = read({
61
+ read: (query) => answer(stringify(query)),
62
+ get: (eids) => eids.length ? answer(whole(eids)) : [],
63
+ })
64
+ let now = [...reads].sort()
65
+ let key = now.join('\n')
66
+ let held = asked.join('\n')
67
+ // What it read that the view does not hold yet is asked for, and what it
68
+ // no longer reads let go; the answers run it again.
69
+ useLayoutEffect(() => {
70
+ if (key != held) ask(now)
71
+ }, [key, held])
72
+ return { value, ready: ready && key == held }
73
+ }
package/registry.js ADDED
@@ -0,0 +1,89 @@
1
+ /**
2
+ * Inspect registrations for vocabulary entities, entity readings, query rows
3
+ * and the map. A host merges them into the same registry as its domain views.
4
+ * @module
5
+ */ import { define } from "@yaks/render";
6
+ import { and, parse } from "@yaks/query";
7
+ /* @ts-types="npm:@types/react@19.3.0" */ import { createElement as h } from "react";
8
+ import { Tile } from "@yaks/ui";
9
+ import { str } from "./read.js";
10
+ import { HomePage } from "./Home.js";
11
+ import { QueryPage } from "./Query.js";
12
+ import { Note } from "./notes.js";
13
+ import { compViews } from "./Comp.js";
14
+ import { effectViews } from "./Effect.js";
15
+ import { entityViews } from "./Entity.js";
16
+ import { packageViews } from "./Package.js";
17
+ import { propViews } from "./Prop.js";
18
+ // Schema names belong to inspect; every consumer asks for this renderer,
19
+ // including the query table and any inline reference.
20
+ let schemaTiles = [
21
+ '_namespace',
22
+ '_comp',
23
+ '_kind',
24
+ '_prop',
25
+ '_effect'
26
+ ].flatMap((name)=>[
27
+ {
28
+ view: 'Tile',
29
+ match: parse(`.${name}`),
30
+ Render: ({ e, io })=>/*#__PURE__*/ h(Tile, {
31
+ href: io.link(e.entity.eid)
32
+ }, /*#__PURE__*/ h(Tile.Title, {}, str(e, name, 'name')), /*#__PURE__*/ h(Tile.Kind, {}, name.slice(1)))
33
+ },
34
+ {
35
+ view: 'Inline',
36
+ match: parse(`.${name}`),
37
+ Render: ({ e, io })=>/*#__PURE__*/ h('a', {
38
+ href: io.link(e.entity.eid)
39
+ }, str(e, name, 'name'))
40
+ }
41
+ ]);
42
+ /** The pages for vocabulary entities. */ export let schemaPages = [
43
+ ...compViews,
44
+ ...propViews,
45
+ ...packageViews,
46
+ ...effectViews
47
+ ];
48
+ /** Every inspector view. */ export let all = [
49
+ {
50
+ view: 'Inspect.Note',
51
+ match: parse('.comment'),
52
+ Render: Note
53
+ },
54
+ ...schemaTiles,
55
+ ...compViews,
56
+ ...propViews,
57
+ ...packageViews,
58
+ ...effectViews,
59
+ ...entityViews,
60
+ {
61
+ view: 'Inspect.Reference.Inline',
62
+ match: and(),
63
+ Render: ({ e, io })=>/*#__PURE__*/ h('a', {
64
+ href: io.link(e.entity.eid)
65
+ }, io.name(e.entity.eid))
66
+ },
67
+ {
68
+ view: 'Inspect.Map',
69
+ match: and(),
70
+ Render: ({ io })=>/*#__PURE__*/ h(HomePage, {
71
+ io
72
+ })
73
+ },
74
+ {
75
+ view: 'Inspect.Query',
76
+ match: and(),
77
+ Render: ({ io, ctx })=>/*#__PURE__*/ h(QueryPage, {
78
+ io,
79
+ text: String(ctx.query ?? '')
80
+ })
81
+ }
82
+ ];
83
+ /** The inspector's views, as a registry. */ export let views = define(all);
84
+ /** The inspector's views with `more` ahead of them, as a registry of its
85
+ * own. */ export let composed = (more)=>more.length ? define([
86
+ ...more,
87
+ ...all
88
+ ]) : views;
89
+ //# sourceMappingURL=registry.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"registry.js","sources":["./registry.ts"],"names":[],"mappings":"AAAA;;;;CAIC,GAED,SAAS,MAAM,4BAAgD;AAE/D,SAAS,GAAG,EAAE,KAAK,2BAAgC;AACnD,uCAAuC,GAAG,SAAS,iBAAiB,CAAC,gBAA0B;AAC/F,SAAS,IAAI,wBAA6B;AAC1C,SAAS,GAAG,oBAAmB;AAC/B,SAAS,QAAQ,oBAAmB;AACpC,SAAS,SAAS,qBAAoB;AACtC,SAAS,IAAI,qBAAoB;AACjC,SAAS,SAAS,oBAAmB;AACrC,SAAS,WAAW,sBAAqB;AACzC,SAAS,WAAW,sBAAqB;AACzC,SAAS,YAAY,uBAAsB;AAC3C,SAAS,SAAS,oBAAmB;AAErC,yEAAyE;AACzE,sDAAsD;AACtD,IAAI,cAAsB;EAAC;EAAc;EAAS;EAAS;EAAS;CAAU,CAC3E,OAAO,CAAC,CACP,OACG;IACH;MACE,MAAM;MACN,OAAO,MAAM,CAAC,CAAC,EAAE,MAAM;MACvB,QAAQ,CAAC,EAAE,CAAC,EAAE,EAAE,EAAE,iBAChB,EACE,MACA;UAAE,MAAM,GAAG,IAAI,CAAC,EAAE,MAAM,CAAC,GAAG;QAAE,iBAC9B,EAAE,KAAK,KAAK,EAAE,CAAC,GAAG,IAAI,GAAG,MAAM,wBAC/B,EAAE,KAAK,IAAI,EAAE,CAAC,GAAG,KAAK,KAAK,CAAC;IAElC;IACA;MACE,MAAM;MACN,OAAO,MAAM,CAAC,CAAC,EAAE,MAAM;MACvB,QAAQ,CAAC,EAAE,CAAC,EAAE,EAAE,EAAE,iBAChB,EAAE,KAAK;UAAE,MAAM,GAAG,IAAI,CAAC,EAAE,MAAM,CAAC,GAAG;QAAE,GAAG,IAAI,GAAG,MAAM;IACzD;GACD;AAEH,uCAAuC,GACvC,OAAO,IAAI,cAAsB;KAC5B;KACA;KACA;KACA;CACJ,CAAA;AAED,0BAA0B,GAC1B,OAAO,IAAI,MAAc;EACvB;IAAE,MAAM;IAAgB,OAAO,MAAM;IAAa,QAAQ;EAAK;KAC5D;KACA;KACA;KACA;KACA;KACA;EACH;IACE,MAAM;IACN,OAAO;IACP,QAAQ,CAAC,EAAE,CAAC,EAAE,EAAE,EAAE,iBAChB,EAAE,KAAK;QAAE,MAAM,GAAG,IAAI,CAAC,EAAE,MAAM,CAAC,GAAG;MAAE,GAAG,GAAG,IAAI,CAAC,EAAE,MAAM,CAAC,GAAG;EAChE;EACA;IACE,MAAM;IACN,OAAO;IACP,QAAQ,CAAC,EAAE,EAAE,EAAE,iBAAK,EAAE,UAAU;QAAE;MAAG;EACvC;EACA;IACE,MAAM;IACN,OAAO;IACP,QAAQ,CAAC,EAAE,EAAE,EAAE,GAAG,EAAE,iBAClB,EAAE,WAAW;QAAE;QAAI,MAAM,OAAO,IAAI,KAAK,IAAI;MAAI;EACrD;CACD,CAAA;AAED,0CAA0C,GAC1C,OAAO,IAAI,QAAwB,OAAO,KAAI;AAE9C;QACQ,GACR,OAAO,IAAI,WAAW,CAAC,OACrB,KAAK,MAAM,GAAG,OAAO;OAAI;OAAS;GAAI,IAAI,MAAK"}