@yaks/render 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 (53) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +396 -2
  3. package/_dist/archetype.d.ts +3 -0
  4. package/_dist/archetype.d.ts.map +1 -0
  5. package/_dist/component.d.ts +15 -0
  6. package/_dist/component.d.ts.map +1 -0
  7. package/_dist/edit.d.ts +17 -0
  8. package/_dist/edit.d.ts.map +1 -0
  9. package/_dist/host.d.ts +59 -0
  10. package/_dist/host.d.ts.map +1 -0
  11. package/_dist/mod.d.ts +72 -0
  12. package/_dist/mod.d.ts.map +1 -0
  13. package/_dist/namespace.d.ts +12 -0
  14. package/_dist/namespace.d.ts.map +1 -0
  15. package/_dist/prepare.d.ts +11 -0
  16. package/_dist/prepare.d.ts.map +1 -0
  17. package/_dist/types.d.ts +66 -0
  18. package/_dist/types.d.ts.map +1 -0
  19. package/_dist/views.d.ts +88 -0
  20. package/_dist/views.d.ts.map +1 -0
  21. package/archetype.js +56 -0
  22. package/archetype.js.map +1 -0
  23. package/archetype.ts +76 -0
  24. package/component.js +39 -0
  25. package/component.js.map +1 -0
  26. package/component.ts +84 -0
  27. package/deno.json +1 -0
  28. package/edit.js +88 -0
  29. package/edit.js.map +1 -0
  30. package/edit.ts +113 -0
  31. package/host.js +191 -0
  32. package/host.js.map +1 -0
  33. package/host.ts +277 -0
  34. package/mod.js +158 -0
  35. package/mod.js.map +1 -0
  36. package/mod.ts +328 -0
  37. package/namespace.js +90 -0
  38. package/namespace.js.map +1 -0
  39. package/namespace.ts +121 -0
  40. package/package.json +31 -4
  41. package/prepare.js +32 -0
  42. package/prepare.js.map +1 -0
  43. package/prepare.ts +55 -0
  44. package/prop.js +37 -0
  45. package/prop.js.map +1 -0
  46. package/prop.ts +46 -0
  47. package/types.js +7 -0
  48. package/types.js.map +1 -0
  49. package/types.ts +116 -0
  50. package/views.js +333 -0
  51. package/views.js.map +1 -0
  52. package/views.ts +389 -0
  53. package/vocab.json +29 -0
package/mod.js ADDED
@@ -0,0 +1,158 @@
1
+ import { Unknown } from "@yaks/vocab/runtime";
2
+ import { archetypeMatch, drawable } from "./archetype.js";
3
+ import { matchIn } from "./namespace.js";
4
+ export { contributed, needsIn, viewIn } from "./namespace.js";
5
+ import { projection, propVocab } from "./prop.js";
6
+ export { edit } from "./edit.js";
7
+ export function define(renderers, options = {}) {
8
+ return {
9
+ ...options,
10
+ renderers: [
11
+ ...renderers
12
+ ]
13
+ };
14
+ }
15
+ /** Prepend an overlay of renderers to this registry; earlier rows win equal
16
+ * scores. */ export let extend = (registry, renderers)=>{
17
+ registry.renderers = [
18
+ ...renderers,
19
+ ...registry.renderers
20
+ ];
21
+ };
22
+ // Selection asks the same registered queries again on every resolve, so
23
+ // compiling one per test made matching, not rendering, the cost of a view — a
24
+ // property row resolves its editor on each paint. The AST and the vocabulary
25
+ // are stable objects, so key the compiled predicate on the pair: each compiles
26
+ // once and a registry that is replaced drops its entries with it.
27
+ let cache = new WeakMap();
28
+ // A refused word stays unavailable for this query and immutable vocabulary.
29
+ let unavailable = new WeakMap();
30
+ let predicate = (match, vocab)=>{
31
+ let byVocab = cache.get(match);
32
+ if (!byVocab) cache.set(match, byVocab = new WeakMap());
33
+ let f = byVocab.get(vocab);
34
+ if (!f) byVocab.set(vocab, f = drawable(match, vocab));
35
+ return f;
36
+ };
37
+ let matches = (match, bundle, vocab, archetypes)=>{
38
+ if (match === true) return true;
39
+ if (unavailable.get(match)?.has(vocab)) return false;
40
+ try {
41
+ return archetypeMatch(match, bundle, vocab, archetypes) ?? predicate(match, vocab)(bundle);
42
+ } catch (error) {
43
+ // Registrations travel between hosts. A host not speaking a view's words
44
+ // cannot select it; even a bare absent component may be a property name
45
+ // here. Query doors still refuse such a question, but selection skips it.
46
+ if (error instanceof Unknown) {
47
+ let absent = unavailable.get(match);
48
+ if (!absent) unavailable.set(match, absent = new WeakSet());
49
+ absent.add(vocab);
50
+ return false;
51
+ }
52
+ throw error;
53
+ }
54
+ };
55
+ let best = (pool, bundle, vocab, archetypes)=>{
56
+ let top;
57
+ let max = -Infinity;
58
+ for (let r of pool){
59
+ if (!matches(matchIn(r, vocab), bundle, vocab, archetypes)) continue;
60
+ // Shift the whole query tier so an empty conjunction beats the catch-all
61
+ // while still ranking below every query with clauses.
62
+ let score = r.match === true ? 0.5 : 1 + r.match.clauses.length;
63
+ if (score > max) {
64
+ max = score;
65
+ top = r;
66
+ }
67
+ }
68
+ return top;
69
+ };
70
+ // Only registration metadata is indexed. A new array (extend, preparation or
71
+ // host replacement) gets new pools; every selection still tests its bundle.
72
+ let pools = new WeakMap();
73
+ let byView = (renderers)=>{
74
+ let found = pools.get(renderers);
75
+ if (!found) {
76
+ let grouped = new Map();
77
+ for (let r of renderers){
78
+ let rows = grouped.get(r.view);
79
+ if (rows) rows.push(r);
80
+ else grouped.set(r.view, [
81
+ r
82
+ ]);
83
+ }
84
+ pools.set(renderers, found = grouped);
85
+ }
86
+ return found;
87
+ };
88
+ let walk = (registry, bundle, view, vocab)=>{
89
+ let { renderers, views, archetypes } = registry;
90
+ let named = byView(renderers);
91
+ let pick = (name)=>best(named.get(name) ?? [], bundle, vocab, archetypes);
92
+ if (!view) {
93
+ return best(renderers.filter((r)=>!views || views.includes(r.view)), bundle, vocab, archetypes) ?? pick('JSON');
94
+ }
95
+ for(let v = view; v; v = v.replace(/^[^.]+\.?/, '')){
96
+ let found = pick(v);
97
+ if (found) return found;
98
+ }
99
+ return pick('JSON');
100
+ };
101
+ let memos = new WeakMap();
102
+ let memo = (registry, vocab)=>{
103
+ let m = memos.get(registry);
104
+ let { renderers, views } = registry;
105
+ if (!m || m.renderers !== renderers || m.views !== views || m.vocab !== vocab) {
106
+ memos.set(registry, m = {
107
+ renderers,
108
+ views,
109
+ vocab,
110
+ picks: new Map()
111
+ });
112
+ }
113
+ return m.picks;
114
+ };
115
+ /**
116
+ * Resolve the closest view, then its most specific renderer: the walk strips
117
+ * the leftmost role at each step, so a short registration serves every longer
118
+ * ask.
119
+ */ export let resolve = (registry, bundle, view, vocab, ctx = {})=>{
120
+ if (ctx.prop == null) return walk(registry, bundle, view, vocab);
121
+ let picks = memo(registry, vocab);
122
+ let key = `${view ?? ''}|${ctx.comp}|${ctx.prop}`;
123
+ if (picks.has(key)) return picks.get(key);
124
+ // An undeclared property throws out of projection(); nothing is remembered.
125
+ let found = walk(registry, projection(vocab, ctx), view, propVocab);
126
+ picks.set(key, found);
127
+ return found;
128
+ };
129
+ /** Exact, matching tab views in configured order, without JSON fallback. */ export let applicable = (registry, bundle, vocab, ctx = {})=>{
130
+ let archetypes = ctx.prop == null ? registry.archetypes : undefined;
131
+ if (ctx.prop != null) {
132
+ bundle = projection(vocab, ctx);
133
+ vocab = propVocab;
134
+ }
135
+ let named = byView(registry.renderers);
136
+ let views = registry.views ?? [
137
+ ...named.keys()
138
+ ];
139
+ return views.filter((view)=>named.get(view)?.some((r)=>matches(r.match, bundle, vocab, archetypes)));
140
+ };
141
+ export function actions(registry, bundle, vocab = registry.vocab, source) {
142
+ let test = (match)=>{
143
+ if (match === true) return true;
144
+ if (!vocab) throw new Error('action conditions need a vocabulary');
145
+ return matches(match, bundle, vocab, registry.archetypes);
146
+ };
147
+ let offered;
148
+ if (Array.isArray(registry.actions)) {
149
+ // The three-argument overload accepts Bundle sources only.
150
+ let input = source === undefined ? bundle : source;
151
+ offered = registry.actions.filter((c)=>test(c.match)).flatMap((c)=>c.acts(input));
152
+ } else {
153
+ offered = Object.entries(registry.actions ?? {}).flatMap(([comp, acts])=>!comp.startsWith('$') && bundle[comp] && typeof bundle[comp] == 'object' ? acts : []);
154
+ }
155
+ return offered.filter((action)=>!action.when || test(action.when));
156
+ }
157
+ export { prepared, rendering } from "./prepare.js";
158
+ //# sourceMappingURL=mod.js.map
package/mod.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mod.js","sources":["./mod.ts"],"names":[],"mappings":"AAAA,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C/C,OAAO,MAAM,MAAM,CAAC,CAAC,OACnB,MAAM,GAAG,CAAC,OAAO,KAAK,CAAC,IAAM,QAAQ,EAAE,KAAK,EAAE,QAAQ,OAAO;AAEjE,EAAC;AAoBD,OAAO,SAAS,QACd,QAA2B,EAC3B,MAAc,EACd,QAA2B,SAAS,KAAK,EACzC,MAAU;EAEV,IAAI,OAAO,CAAC;IACV,IAAI,UAAU,MAAM,OAAO;IAC3B,IAAI,CAAC,OAAO,MAAM,IAAI,MAAM;IAC5B,OAAO,QAAQ,OAAO,QAAQ,OAAO,SAAS,UAAU;EAC1D;EACA,IAAI;EACJ,IAAI,MAAM,OAAO,CAAC,SAAS,OAAO,GAAG;IACnC,2DAA2D;IAC3D,IAAI,QAAS,WAAW,YAAY,SAAS;IAC7C,UAAU,AAAC,SAAS,OAAO,CACxB,MAAM,CAAC,CAAC,IAAM,KAAK,EAAE,KAAK,GAAG,OAAO,CAAC,CAAC,IAAM,EAAE,IAAI,CAAC;EACxD,OAAO;IACL,UAAU,OAAO,OAAO,CAAC,SAAS,OAAO,IAAI,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,MAAM,KAAK,GACpE,CAAC,KAAK,UAAU,CAAC,QAAQ,MAAM,CAAC,KAAK,IAAI,OAAO,MAAM,CAAC,KAAK,IAAI,WAC5D,OACA,EAAE;EAEV;EACA,OAAO,QAAQ,MAAM,CAAC,CAAC,SAAW,CAAC,OAAO,IAAI,IAAI,KAAK,OAAO,IAAI;AACpE;AAEA,SAAwB,QAAQ,EAAE,SAAS,uBAAsB"}
package/mod.ts ADDED
@@ -0,0 +1,328 @@
1
+ import { Unknown } from '@yaks/vocab/runtime'
2
+ /**
3
+ * @yaks/render selects which renderer draws a bundle, and lists the actions a
4
+ * bundle offers. A view name is tried from the right (Board.List.Tile →
5
+ * List.Tile → Tile), applying any alias at each step; within one step, the
6
+ * registration whose query has the most clauses wins, and registration order
7
+ * breaks ties. A catch-all `true` scores 0.5. No rendering backend is imported
8
+ * here.
9
+ *
10
+ * Editors use this same registry. Pass {comp, prop} to resolve and it matches
11
+ * against a bundle-shaped projection of that property's declared schema rather
12
+ * than against the entity: parse('.prop.type=string') selects a string
13
+ * editor. The renderer still receives the original bundle and that context, so
14
+ * it knows both the value and where a patch belongs. Property queries and
15
+ * entity queries describe different subjects; register them under separate
16
+ * view names. See prop.ts for the four queryable schema fields; @yaks/ux
17
+ * registers its `Edit` controls this way.
18
+ *
19
+ * A missing view returns undefined unless a matching JSON view is registered.
20
+ * An unnamed request considers the configured views (all by default).
21
+ *
22
+ * ```ts
23
+ * import { assertEquals } from '@std/assert'
24
+ * import { parse } from '@yaks/query'
25
+ * import { loadVocab } from '@yaks/vocab/runtime'
26
+ * import { define, resolve, type H } from '@yaks/render'
27
+ *
28
+ * let vocab = loadVocab([{ $defs: { doc: {
29
+ * component: true, properties: { title: { type: 'string' } },
30
+ * } } }])
31
+ * let bundle = { entity: { eid: 'a' }, doc: { title: 'A page' } }
32
+ * let registry = define([{
33
+ * view: 'Tile', match: parse('.doc'),
34
+ * render: (b, h) => h('h2', null, String((b.doc as {title: string}).title)),
35
+ * }])
36
+ * let calls: unknown[] = []
37
+ * let h: H<number> = (tag, props, ...children) =>
38
+ * calls.push({ tag, props, children })
39
+ * let renderer = resolve(registry, bundle, 'Board.Tile', vocab)!
40
+ * assertEquals(renderer.render(bundle, h, {}), 1)
41
+ * assertEquals(calls, [{ tag: 'h2', props: null, children: ['A page'] }])
42
+ * ```
43
+ *
44
+ * @module
45
+ */
46
+
47
+ import type { Bundle, Filter } from 'jsr:@yaks/match@0.2.36'
48
+ import type { Query } from 'jsr:@yaks/query@0.2.36'
49
+ import type { Vocab } from '@yaks/vocab/runtime'
50
+ import { type ArchetypeLookup, archetypeMatch, drawable } from './archetype.js'
51
+ export type { ArchetypeLookup } from './archetype.js'
52
+ import { matchIn } from './namespace.js'
53
+ export { contributed, needsIn, viewIn } from './namespace.js'
54
+ import { projection, propVocab } from './prop.js'
55
+ import type {
56
+ Action,
57
+ Context,
58
+ Contributor,
59
+ Options,
60
+ Registration,
61
+ Registry,
62
+ Renderer,
63
+ Selection,
64
+ } from './types.ts'
65
+
66
+ export { edit, type EditOptions } from './edit.js'
67
+
68
+ export type { Bundle } from 'jsr:@yaks/match@0.2.36'
69
+ export type { Query } from 'jsr:@yaks/query@0.2.36'
70
+ export type {
71
+ Action,
72
+ Child,
73
+ Context,
74
+ Contributor,
75
+ H,
76
+ Options,
77
+ Patch,
78
+ Registration,
79
+ Registry,
80
+ RenderContext,
81
+ Renderer,
82
+ Rendering,
83
+ Selection,
84
+ } from './types.ts'
85
+
86
+ /** Build an independent registry; earlier registrations win equal scores. */
87
+ export function define<R extends Renderer = Renderer, A = Action, E = Bundle>(
88
+ renderers: readonly R[],
89
+ options?: Options<NoInfer<A>, E>,
90
+ ): Registry<R & Renderer, A, E>
91
+ export function define<R extends Registration, A = Action, E = Bundle>(
92
+ renderers: readonly R[],
93
+ options?: Options<NoInfer<A>, E>,
94
+ ): Registry<R, A, E>
95
+ export function define<R extends Registration, A = Action, E = Bundle>(
96
+ renderers: readonly R[],
97
+ options: Options<NoInfer<A>, E> = {},
98
+ ): Registry<R, A, E> {
99
+ return { ...options, renderers: [...renderers] }
100
+ }
101
+
102
+ /** Prepend an overlay of renderers to this registry; earlier rows win equal
103
+ * scores. */
104
+ export let extend = <R extends Registration>(
105
+ registry: Selection<R>,
106
+ renderers: readonly R[],
107
+ ): void => {
108
+ registry.renderers = [...renderers, ...registry.renderers]
109
+ }
110
+
111
+ // Selection asks the same registered queries again on every resolve, so
112
+ // compiling one per test made matching, not rendering, the cost of a view — a
113
+ // property row resolves its editor on each paint. The AST and the vocabulary
114
+ // are stable objects, so key the compiled predicate on the pair: each compiles
115
+ // once and a registry that is replaced drops its entries with it.
116
+ let cache = new WeakMap<Query, WeakMap<Vocab, Filter>>()
117
+ // A refused word stays unavailable for this query and immutable vocabulary.
118
+ let unavailable = new WeakMap<Query, WeakSet<Vocab>>()
119
+ let predicate = (match: Query, vocab: Vocab): Filter => {
120
+ let byVocab = cache.get(match)
121
+ if (!byVocab) cache.set(match, byVocab = new WeakMap())
122
+ let f = byVocab.get(vocab)
123
+ if (!f) byVocab.set(vocab, f = drawable(match, vocab))
124
+ return f
125
+ }
126
+
127
+ let matches = (
128
+ match: Registration['match'],
129
+ bundle: Bundle,
130
+ vocab: Vocab,
131
+ archetypes?: ArchetypeLookup,
132
+ ): boolean => {
133
+ if (match === true) return true
134
+ if (unavailable.get(match)?.has(vocab)) return false
135
+ try {
136
+ return archetypeMatch(match, bundle, vocab, archetypes) ??
137
+ predicate(match, vocab)(bundle)
138
+ } catch (error) {
139
+ // Registrations travel between hosts. A host not speaking a view's words
140
+ // cannot select it; even a bare absent component may be a property name
141
+ // here. Query doors still refuse such a question, but selection skips it.
142
+ if (error instanceof Unknown) {
143
+ let absent = unavailable.get(match)
144
+ if (!absent) unavailable.set(match, absent = new WeakSet())
145
+ absent.add(vocab)
146
+ return false
147
+ }
148
+ throw error
149
+ }
150
+ }
151
+
152
+ let best = <R extends Registration>(
153
+ pool: readonly R[],
154
+ bundle: Bundle,
155
+ vocab: Vocab,
156
+ archetypes?: ArchetypeLookup,
157
+ ): R | undefined => {
158
+ let top: R | undefined
159
+ let max = -Infinity
160
+ for (let r of pool) {
161
+ if (!matches(matchIn(r, vocab), bundle, vocab, archetypes)) continue
162
+ // Shift the whole query tier so an empty conjunction beats the catch-all
163
+ // while still ranking below every query with clauses.
164
+ let score = r.match === true ? 0.5 : 1 + r.match.clauses.length
165
+ if (score > max) {
166
+ max = score
167
+ top = r
168
+ }
169
+ }
170
+ return top
171
+ }
172
+
173
+ // Only registration metadata is indexed. A new array (extend, preparation or
174
+ // host replacement) gets new pools; every selection still tests its bundle.
175
+ let pools = new WeakMap<
176
+ readonly Registration[],
177
+ Map<string, readonly Registration[]>
178
+ >()
179
+ let byView = <R extends Registration>(renderers: readonly R[]) => {
180
+ let found = pools.get(renderers)
181
+ if (!found) {
182
+ let grouped = new Map<string, R[]>()
183
+ for (let r of renderers) {
184
+ let rows = grouped.get(r.view)
185
+ if (rows) rows.push(r)
186
+ else grouped.set(r.view, [r])
187
+ }
188
+ pools.set(renderers, found = grouped)
189
+ }
190
+ return found as Map<string, readonly R[]>
191
+ }
192
+
193
+ let walk = <R extends Registration>(
194
+ registry: Selection<R>,
195
+ bundle: Bundle,
196
+ view: string | undefined,
197
+ vocab: Vocab,
198
+ ): R | undefined => {
199
+ let { renderers, views, archetypes } = registry
200
+ let named = byView(renderers)
201
+ let pick = (name: string) =>
202
+ best(named.get(name) ?? [], bundle, vocab, archetypes)
203
+ if (!view) {
204
+ return best(
205
+ renderers.filter((r) => !views || views.includes(r.view)),
206
+ bundle,
207
+ vocab,
208
+ archetypes,
209
+ ) ?? pick('JSON')
210
+ }
211
+ for (let v = view; v; v = v.replace(/^[^.]+\.?/, '')) {
212
+ let found = pick(v)
213
+ if (found) return found
214
+ }
215
+ return pick('JSON')
216
+ }
217
+
218
+ // A property ask throws the entity away — the projection is the declaration, so
219
+ // the chosen control depends only on the registry, the vocabulary and the
220
+ // property's address, while a property row asks again on every paint. Remember
221
+ // the pick, and drop the whole memo when any of those three moves: define and
222
+ // extend assign the lists rather than splice them, so a re-registration (or a
223
+ // second vocabulary through the same registry) is honoured at once.
224
+ type Memo = {
225
+ renderers: unknown
226
+ views: unknown
227
+ vocab: Vocab
228
+ picks: Map<string, Registration | undefined>
229
+ }
230
+ let memos = new WeakMap<object, Memo>()
231
+ let memo = <R extends Registration>(registry: Selection<R>, vocab: Vocab) => {
232
+ let m = memos.get(registry)
233
+ let { renderers, views } = registry
234
+ if (
235
+ !m || m.renderers !== renderers || m.views !== views || m.vocab !== vocab
236
+ ) {
237
+ memos.set(registry, m = { renderers, views, vocab, picks: new Map() })
238
+ }
239
+ return m.picks as Map<string, R | undefined>
240
+ }
241
+
242
+ /**
243
+ * Resolve the closest view, then its most specific renderer: the walk strips
244
+ * the leftmost role at each step, so a short registration serves every longer
245
+ * ask.
246
+ */
247
+ export let resolve = <R extends Registration>(
248
+ registry: Selection<R>,
249
+ bundle: Bundle,
250
+ view: string | undefined,
251
+ vocab: Vocab,
252
+ ctx: Context = {},
253
+ ): R | undefined => {
254
+ if (ctx.prop == null) return walk(registry, bundle, view, vocab)
255
+ let picks = memo(registry, vocab)
256
+ let key = `${view ?? ''}|${ctx.comp}|${ctx.prop}`
257
+ if (picks.has(key)) return picks.get(key)
258
+ // An undeclared property throws out of projection(); nothing is remembered.
259
+ let found = walk(registry, projection(vocab, ctx), view, propVocab)
260
+ picks.set(key, found)
261
+ return found
262
+ }
263
+
264
+ /** Exact, matching tab views in configured order, without JSON fallback. */
265
+ export let applicable = <R extends Registration>(
266
+ registry: Selection<R>,
267
+ bundle: Bundle,
268
+ vocab: Vocab,
269
+ ctx: Context = {},
270
+ ): string[] => {
271
+ let archetypes = ctx.prop == null ? registry.archetypes : undefined
272
+ if (ctx.prop != null) {
273
+ bundle = projection(vocab, ctx)
274
+ vocab = propVocab
275
+ }
276
+ let named = byView(registry.renderers)
277
+ let views = registry.views ?? [...named.keys()]
278
+ return views.filter((view) =>
279
+ named.get(view)?.some((r) => matches(r.match, bundle, vocab, archetypes))
280
+ )
281
+ }
282
+
283
+ /**
284
+ * All matching contributions, preserving duplicates and registration order.
285
+ * Static offerings are keyed by the component an entity carries; dynamic
286
+ * contributors receive
287
+ * the source supplied by the caller, while their queries read the bundle.
288
+ * Conditions use the supplied vocabulary or define's default; run is never called.
289
+ */
290
+ export function actions<R extends Registration, A>(
291
+ registry: Registry<R, A>,
292
+ bundle: Bundle,
293
+ vocab?: Vocab,
294
+ ): A[]
295
+ export function actions<R extends Registration, A, E>(
296
+ registry: Registry<R, A, E>,
297
+ bundle: Bundle,
298
+ vocab: Vocab | undefined,
299
+ source: E,
300
+ ): A[]
301
+ export function actions<R extends Registration, A, E>(
302
+ registry: Registry<R, A, E>,
303
+ bundle: Bundle,
304
+ vocab: Vocab | undefined = registry.vocab,
305
+ source?: E,
306
+ ): A[] {
307
+ let test = (match: Registration['match']) => {
308
+ if (match === true) return true
309
+ if (!vocab) throw new Error('action conditions need a vocabulary')
310
+ return matches(match, bundle, vocab, registry.archetypes)
311
+ }
312
+ let offered: readonly (A & { when?: Query })[]
313
+ if (Array.isArray(registry.actions)) {
314
+ // The three-argument overload accepts Bundle sources only.
315
+ let input = (source === undefined ? bundle : source) as E
316
+ offered = (registry.actions as readonly Contributor<A, E>[])
317
+ .filter((c) => test(c.match)).flatMap((c) => c.acts(input))
318
+ } else {
319
+ offered = Object.entries(registry.actions ?? {}).flatMap(([comp, acts]) =>
320
+ !comp.startsWith('$') && bundle[comp] && typeof bundle[comp] == 'object'
321
+ ? acts
322
+ : []
323
+ )
324
+ }
325
+ return offered.filter((action) => !action.when || test(action.when))
326
+ }
327
+
328
+ export { type Prepared, prepared, rendering } from './prepare.js'
package/namespace.js ADDED
@@ -0,0 +1,90 @@
1
+ import { namesBetween } from "@yaks/graph";
2
+ import { reworded } from "@yaks/query";
3
+ /** Attribute renderers from one package so a match reads the package's words. */ export let contributed = (label, entries)=>entries.map((entry)=>({
4
+ ...entry,
5
+ namespace: entry.namespace ?? {
6
+ label
7
+ }
8
+ }));
9
+ let planned = new WeakMap();
10
+ export let matchIn = (entry, vocab)=>{
11
+ if (entry.match === true || !entry.namespace) return entry.match;
12
+ let local = vocab.in(entry.namespace);
13
+ if (local === vocab) return entry.match;
14
+ let plans = planned.get(entry);
15
+ if (!plans) planned.set(entry, plans = new WeakMap());
16
+ let match = plans.get(vocab);
17
+ if (!match) plans.set(vocab, match = reworded(entry.match, local, vocab));
18
+ return match;
19
+ };
20
+ export let viewIn = (entry, vocab)=>{
21
+ let local = entry.namespace ? vocab.in(entry.namespace) : vocab;
22
+ if (local === vocab) return;
23
+ return {
24
+ local,
25
+ into: namesBetween(vocab, local),
26
+ out: namesBetween(local, vocab)
27
+ };
28
+ };
29
+ export let needsIn = (entry, bundle, vocab)=>entry.needs?.(viewIn(entry, vocab)?.into.read([
30
+ bundle
31
+ ])[0] ?? bundle) ?? [];
32
+ export let renderingIn = (entry, render)=>(bundle, h, ctx)=>{
33
+ let words = ctx.vocab && viewIn(entry, ctx.vocab);
34
+ if (!words) return render(bundle, h, ctx);
35
+ let { into, out, local } = words;
36
+ let keys = (context, names)=>({
37
+ ...context,
38
+ comp: context.comp && names.comp(context.comp),
39
+ prop: context.comp && context.prop ? names.prop(context.comp, context.prop) : context.prop
40
+ });
41
+ let get = ctx.get;
42
+ let query = ctx.query;
43
+ let related = ctx.related;
44
+ let node = into.read([
45
+ bundle
46
+ ])[0];
47
+ return render(node, (tag, props, ...children)=>{
48
+ let change = props?.onChange;
49
+ if (change && typeof change == 'object' && 'run' in change && typeof change.run == 'function') {
50
+ let run = change.run;
51
+ props = {
52
+ ...props,
53
+ onChange: {
54
+ ...change,
55
+ run: (row, input)=>out.write([
56
+ run(into.read([
57
+ row
58
+ ])[0], input)
59
+ ])[0]
60
+ }
61
+ };
62
+ }
63
+ return h(tag, props, ...children);
64
+ }, {
65
+ ...ctx,
66
+ ...keys(ctx, into),
67
+ vocab: local,
68
+ ...get ? {
69
+ get: (eid)=>{
70
+ let found = get(eid);
71
+ return found && into.read([
72
+ found
73
+ ])[0];
74
+ }
75
+ } : {},
76
+ ...query ? {
77
+ query: (q)=>{
78
+ let found = query(out.query(q));
79
+ return found && into.read(found);
80
+ }
81
+ } : {},
82
+ ...related ? {
83
+ related: (eid, relation)=>into.read(related(eid, out.comp(relation)))
84
+ } : {},
85
+ ...ctx.render ? {
86
+ render: (name, opts)=>ctx.render(name, opts && keys(opts, out))
87
+ } : {}
88
+ });
89
+ };
90
+ //# sourceMappingURL=namespace.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"namespace.js","sources":["./namespace.ts"],"names":[],"mappings":"AAAA,SAAS,YAAY,2BAAgC;AACrD,SAAS,QAAQ,2BAAgC;AAKjD,+EAA+E,GAC/E,OAAO,IAAI,cAAc,CACvB,OACA,UAEA,QAAQ,GAAG,CAAC,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