@xmachines/json-render-svelte 0.21.0-xm.2 → 0.21.0-xm.4
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/README.md +20 -0
- package/dist/CatalogRenderer.svelte +11 -2
- package/dist/CatalogRenderer.svelte.d.ts +3 -1
- package/dist/CatalogRenderer.svelte.d.ts.map +1 -1
- package/dist/CreateRendererHost.test.svelte +16 -0
- package/dist/CreateRendererHost.test.svelte.d.ts +13 -0
- package/dist/CreateRendererHost.test.svelte.d.ts.map +1 -0
- package/dist/ElementRenderer.svelte +144 -20
- package/dist/ElementRenderer.svelte.d.ts.map +1 -1
- package/dist/FunctionsHost.test.svelte +20 -0
- package/dist/FunctionsHost.test.svelte.d.ts +13 -0
- package/dist/FunctionsHost.test.svelte.d.ts.map +1 -0
- package/dist/Renderer.svelte +26 -3
- package/dist/Renderer.svelte.d.ts +7 -1
- package/dist/Renderer.svelte.d.ts.map +1 -1
- package/dist/RendererWithProvider.test.svelte +4 -2
- package/dist/RendererWithProvider.test.svelte.d.ts +2 -1
- package/dist/RendererWithProvider.test.svelte.d.ts.map +1 -1
- package/dist/TestInit.svelte +12 -0
- package/dist/TestInit.svelte.d.ts +19 -0
- package/dist/TestInit.svelte.d.ts.map +1 -0
- package/dist/TestMaybeBoom.svelte +21 -0
- package/dist/TestMaybeBoom.svelte.d.ts +13 -0
- package/dist/TestMaybeBoom.svelte.d.ts.map +1 -0
- package/dist/boundary-reset.test.js +140 -0
- package/dist/contexts/ActionProvider.svelte +58 -45
- package/dist/contexts/ActionProvider.svelte.d.ts +2 -1
- package/dist/contexts/ActionProvider.svelte.d.ts.map +1 -1
- package/dist/contexts/actions.test.js +52 -0
- package/dist/event-rule.test.js +140 -0
- package/dist/handlers-prop.test.js +62 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -0
- package/dist/render-error.test.js +43 -1
- package/dist/renderer.d.ts.map +1 -1
- package/dist/renderer.js +9 -18
- package/dist/watch-rule.test.js +297 -0
- package/dist/watch.test.js +133 -0
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -110,6 +110,25 @@ Svelte 5 cannot bind a slot name to a snippet at run time. A component therefore
|
|
|
110
110
|
|
|
111
111
|
A slot renders only when the component calls `renderSlot` for it. Keep default content in `children`: the renderer warns about a `default` entry in `slots` and ignores it. It also warns about a slot name that the catalog does not declare, and it still renders that slot.
|
|
112
112
|
|
|
113
|
+
## State Watchers
|
|
114
|
+
|
|
115
|
+
An element can declare a `watch` field. It maps a state path to action bindings, and the bindings fire when the value at the path changes:
|
|
116
|
+
|
|
117
|
+
```json
|
|
118
|
+
{
|
|
119
|
+
"type": "Select",
|
|
120
|
+
"props": { "value": { "$bindState": "/form/country" } },
|
|
121
|
+
"watch": {
|
|
122
|
+
"/form/country": {
|
|
123
|
+
"action": "loadCities",
|
|
124
|
+
"params": { "country": { "$state": "/form/country" } }
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
`watch` is a top-level field of the element, beside `type` and `props`. The first render fires nothing. The bindings of one path run in order, and the renderer resolves their params against the live state.
|
|
131
|
+
|
|
113
132
|
## Providers
|
|
114
133
|
|
|
115
134
|
| Provider | Purpose |
|
|
@@ -160,6 +179,7 @@ const chat = createChatUI({ endpoint: "/api/chat" });
|
|
|
160
179
|
| `Renderer` | Render a spec using a registry |
|
|
161
180
|
| `CatalogRenderer` | Render with automatic provider wiring |
|
|
162
181
|
| `JsonUIProvider` | All-in-one provider |
|
|
182
|
+
| `createStateStore` | Create a framework-agnostic in-memory `StateStore` |
|
|
163
183
|
| `schema` | Element tree schema (includes built-in state actions) |
|
|
164
184
|
| `createUIStream` | Stream specs from an API endpoint |
|
|
165
185
|
| `createChatUI` | Chat-style streaming interface |
|
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
<script module lang="ts">
|
|
2
|
-
import type {
|
|
2
|
+
import type {
|
|
3
|
+
ComputedFunction,
|
|
4
|
+
DirectiveDefinition,
|
|
5
|
+
RenderErrorHandler,
|
|
6
|
+
Spec,
|
|
7
|
+
StateStore,
|
|
8
|
+
} from "@xmachines/json-render-core";
|
|
3
9
|
import type { ComponentRenderer, ComponentRegistry } from "./renderer.js";
|
|
4
10
|
|
|
5
11
|
export interface CatalogRendererProps {
|
|
@@ -14,6 +20,8 @@
|
|
|
14
20
|
directives?: DirectiveDefinition[];
|
|
15
21
|
loading?: boolean;
|
|
16
22
|
fallback?: ComponentRenderer;
|
|
23
|
+
/** Called when a catalog component throws during a render. */
|
|
24
|
+
onRenderError?: RenderErrorHandler;
|
|
17
25
|
}
|
|
18
26
|
</script>
|
|
19
27
|
|
|
@@ -33,6 +41,7 @@
|
|
|
33
41
|
directives,
|
|
34
42
|
loading = false,
|
|
35
43
|
fallback,
|
|
44
|
+
onRenderError,
|
|
36
45
|
}: CatalogRendererProps = $props();
|
|
37
46
|
|
|
38
47
|
let actionHandlers = $derived.by(() => {
|
|
@@ -55,5 +64,5 @@
|
|
|
55
64
|
{functions}
|
|
56
65
|
{directives}
|
|
57
66
|
{onStateChange}>
|
|
58
|
-
<Renderer {spec} {registry} {loading} {fallback} />
|
|
67
|
+
<Renderer {spec} {registry} {loading} {fallback} {onRenderError} />
|
|
59
68
|
</JsonUIProvider>
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { ComputedFunction, DirectiveDefinition, Spec, StateStore } from "@xmachines/json-render-core";
|
|
1
|
+
import type { ComputedFunction, DirectiveDefinition, RenderErrorHandler, Spec, StateStore } from "@xmachines/json-render-core";
|
|
2
2
|
import type { ComponentRenderer, ComponentRegistry } from "./renderer.js";
|
|
3
3
|
export interface CatalogRendererProps {
|
|
4
4
|
spec: Spec | null;
|
|
@@ -15,6 +15,8 @@ export interface CatalogRendererProps {
|
|
|
15
15
|
directives?: DirectiveDefinition[];
|
|
16
16
|
loading?: boolean;
|
|
17
17
|
fallback?: ComponentRenderer;
|
|
18
|
+
/** Called when a catalog component throws during a render. */
|
|
19
|
+
onRenderError?: RenderErrorHandler;
|
|
18
20
|
}
|
|
19
21
|
declare const CatalogRenderer: import("svelte").Component<CatalogRendererProps, {}, "">;
|
|
20
22
|
type CatalogRenderer = ReturnType<typeof CatalogRenderer>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"CatalogRenderer.svelte.d.ts","sourceRoot":"","sources":["../src/CatalogRenderer.svelte.ts"],"names":[],"mappings":"AAGE,OAAO,KAAK,
|
|
1
|
+
{"version":3,"file":"CatalogRenderer.svelte.d.ts","sourceRoot":"","sources":["../src/CatalogRenderer.svelte.ts"],"names":[],"mappings":"AAGE,OAAO,KAAK,EACV,gBAAgB,EAChB,mBAAmB,EACnB,kBAAkB,EAClB,IAAI,EACJ,UAAU,EACX,MAAM,mBAAmB,CAAC;AAC3B,OAAO,KAAK,EAAE,iBAAiB,EAAE,iBAAiB,EAAE,MAAM,eAAe,CAAC;AAE1E,MAAM,WAAW,oBAAoB;IACnC,IAAI,EAAE,IAAI,GAAG,IAAI,CAAC;IAClB,QAAQ,EAAE,iBAAiB,CAAC;IAC5B,KAAK,CAAC,EAAE,UAAU,CAAC;IACnB,KAAK,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAChC,QAAQ,CAAC,EAAE,CAAC,UAAU,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,KAAK,IAAI,CAAC;IAC1E,aAAa,CAAC,EAAE,CAAC,OAAO,EAAE,KAAK,CAAC;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,KAAK,EAAE,OAAO,CAAA;KAAE,CAAC,KAAK,IAAI,CAAC;IAC3E,SAAS,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,gBAAgB,CAAC,CAAC;IAC7C,qEAAqE;IACrE,UAAU,CAAC,EAAE,mBAAmB,EAAE,CAAC;IACnC,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB,QAAQ,CAAC,EAAE,iBAAiB,CAAC;IAC7B,8DAA8D;IAC9D,aAAa,CAAC,EAAE,kBAAkB,CAAC;CACpC;AA8CH,QAAA,MAAM,eAAe,0DAAwC,CAAC;AAC9D,KAAK,eAAe,GAAG,UAAU,CAAC,OAAO,eAAe,CAAC,CAAC;AAC1D,eAAe,eAAe,CAAC"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
/** Renders a component of createRenderer, so a test can change its props. */
|
|
3
|
+
import type { Component } from "svelte";
|
|
4
|
+
import type { RenderErrorHandler, Spec } from "@xmachines/json-render-core";
|
|
5
|
+
import type { CreateRendererProps } from "./renderer.js";
|
|
6
|
+
|
|
7
|
+
interface Props {
|
|
8
|
+
renderer: Component<CreateRendererProps>;
|
|
9
|
+
spec: Spec | null;
|
|
10
|
+
onRenderError?: RenderErrorHandler;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
let { renderer: CatalogRenderer, spec, onRenderError }: Props = $props();
|
|
14
|
+
</script>
|
|
15
|
+
|
|
16
|
+
<CatalogRenderer {spec} {onRenderError} />
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/** Renders a component of createRenderer, so a test can change its props. */
|
|
2
|
+
import type { Component } from "svelte";
|
|
3
|
+
import type { RenderErrorHandler, Spec } from "@xmachines/json-render-core";
|
|
4
|
+
import type { CreateRendererProps } from "./renderer.js";
|
|
5
|
+
interface Props {
|
|
6
|
+
renderer: Component<CreateRendererProps>;
|
|
7
|
+
spec: Spec | null;
|
|
8
|
+
onRenderError?: RenderErrorHandler;
|
|
9
|
+
}
|
|
10
|
+
declare const CreateRendererHost: Component<Props, {}, "">;
|
|
11
|
+
type CreateRendererHost = ReturnType<typeof CreateRendererHost>;
|
|
12
|
+
export default CreateRendererHost;
|
|
13
|
+
//# sourceMappingURL=CreateRendererHost.test.svelte.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"CreateRendererHost.test.svelte.d.ts","sourceRoot":"","sources":["../src/CreateRendererHost.test.svelte.ts"],"names":[],"mappings":"AAGA,6EAA6E;AAC7E,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,QAAQ,CAAC;AACxC,OAAO,KAAK,EAAE,kBAAkB,EAAE,IAAI,EAAE,MAAM,mBAAmB,CAAC;AAClE,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,eAAe,CAAC;AAGvD,UAAU,KAAK;IACb,QAAQ,EAAE,SAAS,CAAC,mBAAmB,CAAC,CAAC;IACzC,IAAI,EAAE,IAAI,GAAG,IAAI,CAAC;IAClB,aAAa,CAAC,EAAE,kBAAkB,CAAC;CACpC;AAeH,QAAA,MAAM,kBAAkB,0BAAwC,CAAC;AACjE,KAAK,kBAAkB,GAAG,UAAU,CAAC,OAAO,kBAAkB,CAAC,CAAC;AAChE,eAAe,kBAAkB,CAAC"}
|
|
@@ -1,10 +1,18 @@
|
|
|
1
1
|
<script lang="ts">
|
|
2
|
-
import type {
|
|
2
|
+
import type { Spec, UIElement } from "@xmachines/json-render-core";
|
|
3
3
|
import {
|
|
4
4
|
resolveElementProps,
|
|
5
5
|
resolveBindings,
|
|
6
|
-
|
|
6
|
+
resolveBindingParams,
|
|
7
7
|
evaluateVisibility,
|
|
8
|
+
elementSubtreeJson,
|
|
9
|
+
readWatchedValues,
|
|
10
|
+
changedWatchPaths,
|
|
11
|
+
runWatchBindings,
|
|
12
|
+
getRenderErrorHandler,
|
|
13
|
+
getRegistryComponent,
|
|
14
|
+
warnUnusedRenderErrorHandler,
|
|
15
|
+
reportActionError,
|
|
8
16
|
isDevtoolsActive,
|
|
9
17
|
subscribeDevtoolsActive,
|
|
10
18
|
warnElementSlots,
|
|
@@ -97,7 +105,9 @@
|
|
|
97
105
|
);
|
|
98
106
|
|
|
99
107
|
// Get the component renderer
|
|
100
|
-
let Component = $derived(
|
|
108
|
+
let Component = $derived(
|
|
109
|
+
getRegistryComponent(registry, resolvedElement.type) ?? fallback,
|
|
110
|
+
);
|
|
101
111
|
|
|
102
112
|
// Named slots. `slots.default` is left out: the warning below names
|
|
103
113
|
// `children` as its spelling. The slot children render at the scope of the
|
|
@@ -119,23 +129,133 @@
|
|
|
119
129
|
warnElementSlots(registry, element.type, element.slots);
|
|
120
130
|
});
|
|
121
131
|
|
|
132
|
+
// Watch effect: fire actions when watched state paths change. The values
|
|
133
|
+
// object keeps its reference while the watched values stay, so an unrelated
|
|
134
|
+
// state change neither runs the effect again nor cancels bindings that still
|
|
135
|
+
// run. `previousWatched` starts undefined, so the first render fires nothing.
|
|
136
|
+
let stableWatched: Record<string, unknown> | undefined;
|
|
137
|
+
let watchedValues = $derived.by(() => {
|
|
138
|
+
// An element without a watch reads no state, so a write does not run
|
|
139
|
+
// this derived again.
|
|
140
|
+
const cfg = element.watch;
|
|
141
|
+
stableWatched = cfg
|
|
142
|
+
? readWatchedValues(cfg, stateCtx.state, stableWatched)
|
|
143
|
+
: undefined;
|
|
144
|
+
return stableWatched;
|
|
145
|
+
});
|
|
146
|
+
// The values of the render are the baseline. A write between the render
|
|
147
|
+
// and the first run of the effect, such as the onMount of an earlier
|
|
148
|
+
// sibling, is a change, as in react.
|
|
149
|
+
let previousWatched: Record<string, unknown> | undefined = untrack(
|
|
150
|
+
() => watchedValues,
|
|
151
|
+
);
|
|
152
|
+
$effect(() => {
|
|
153
|
+
const current = watchedValues;
|
|
154
|
+
const prev = previousWatched;
|
|
155
|
+
previousWatched = current;
|
|
156
|
+
// A path that the element starts to watch fires nothing: only a change of
|
|
157
|
+
// a watched value does.
|
|
158
|
+
const paths = changedWatchPaths(current, prev);
|
|
159
|
+
const cfg = untrack(() => element.watch);
|
|
160
|
+
if (!cfg || paths.length === 0) return;
|
|
161
|
+
|
|
162
|
+
let cancelled = false;
|
|
163
|
+
// Start the chain untracked. `execute` reads the handlers, the pending
|
|
164
|
+
// confirmation, and whatever a handler reads before its first await. A
|
|
165
|
+
// change of any of them ran this effect again, and its cleanup cancelled
|
|
166
|
+
// the chain.
|
|
167
|
+
untrack(() => {
|
|
168
|
+
let action = "";
|
|
169
|
+
void runWatchBindings(
|
|
170
|
+
cfg,
|
|
171
|
+
paths,
|
|
172
|
+
async (b) => {
|
|
173
|
+
action = b.action;
|
|
174
|
+
// Resolve the params against the live state, as the other renderers do.
|
|
175
|
+
await actionCtx.execute(
|
|
176
|
+
resolveBindingParams(b, {
|
|
177
|
+
...fullCtx,
|
|
178
|
+
stateModel: stateCtx.getSnapshot(),
|
|
179
|
+
}),
|
|
180
|
+
);
|
|
181
|
+
},
|
|
182
|
+
() => cancelled,
|
|
183
|
+
).catch((err: unknown) => {
|
|
184
|
+
reportActionError(
|
|
185
|
+
getRenderErrorHandler(registry),
|
|
186
|
+
err,
|
|
187
|
+
action,
|
|
188
|
+
"watch",
|
|
189
|
+
);
|
|
190
|
+
});
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
return () => {
|
|
194
|
+
cancelled = true;
|
|
195
|
+
};
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
// The reset of the boundary of a failed element, and the content of that
|
|
199
|
+
// element with the elements below it. A new version of the element or of a
|
|
200
|
+
// child — a streamed chunk that fixes it, or a spec that the host renders
|
|
201
|
+
// again — renders it again, as react does. A child resolves its props
|
|
202
|
+
// outside its own boundary, so its failure lands here.
|
|
203
|
+
//
|
|
204
|
+
// The effect serializes the subtree only after a failure: a streamed spec
|
|
205
|
+
// changes every element often. `failedElement` is state, so the failure
|
|
206
|
+
// runs the effect, and the serialization then reads every field of the
|
|
207
|
+
// subtree, also of a spec that the host changes in place.
|
|
208
|
+
const subtree = () => elementSubtreeJson(spec, elementKey, element);
|
|
209
|
+
let resetFailed: (() => void) | undefined;
|
|
210
|
+
let failedElement = $state.raw<string | undefined>(undefined);
|
|
211
|
+
$effect(() => {
|
|
212
|
+
const failed = failedElement;
|
|
213
|
+
const reset = resetFailed;
|
|
214
|
+
// The boundary goes when the element hides, or when its component goes,
|
|
215
|
+
// and its reset goes with it: a spec that fixes and hides the element in
|
|
216
|
+
// one change must not reset a boundary that is gone. A later mount gets a
|
|
217
|
+
// new boundary.
|
|
218
|
+
if (!(isVisible && Component)) {
|
|
219
|
+
resetFailed = undefined;
|
|
220
|
+
failedElement = undefined;
|
|
221
|
+
return;
|
|
222
|
+
}
|
|
223
|
+
if (failed === undefined || reset === undefined) return;
|
|
224
|
+
if (subtree() === failed) return;
|
|
225
|
+
resetFailed = undefined;
|
|
226
|
+
failedElement = undefined;
|
|
227
|
+
reset();
|
|
228
|
+
});
|
|
229
|
+
|
|
122
230
|
// Create emit function
|
|
123
|
-
|
|
231
|
+
// Run the bindings of an event one after another, as the other renderers
|
|
232
|
+
// do: each one waits for the previous one, and each one resolves its params
|
|
233
|
+
// against the live state, so a later binding sees what an earlier one wrote.
|
|
234
|
+
async function emit(eventName: string): Promise<void> {
|
|
124
235
|
const binding = element.on?.[eventName];
|
|
125
236
|
if (!binding) return;
|
|
126
237
|
|
|
127
238
|
const actionBindings = Array.isArray(binding) ? binding : [binding];
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
239
|
+
let action = "";
|
|
240
|
+
try {
|
|
241
|
+
for (const b of actionBindings) {
|
|
242
|
+
action = b.action;
|
|
243
|
+
await actionCtx.execute(
|
|
244
|
+
resolveBindingParams(b, {
|
|
245
|
+
...fullCtx,
|
|
246
|
+
stateModel: stateCtx.getSnapshot(),
|
|
247
|
+
}),
|
|
248
|
+
);
|
|
137
249
|
}
|
|
138
|
-
|
|
250
|
+
} catch (err) {
|
|
251
|
+
// A binding that fails stops the chain. A component discards the
|
|
252
|
+
// promise of emit, so the renderer reports the failure.
|
|
253
|
+
reportActionError(
|
|
254
|
+
getRenderErrorHandler(registry),
|
|
255
|
+
err,
|
|
256
|
+
action,
|
|
257
|
+
"event",
|
|
258
|
+
);
|
|
139
259
|
}
|
|
140
260
|
}
|
|
141
261
|
|
|
@@ -148,7 +268,7 @@
|
|
|
148
268
|
const actionBindings = Array.isArray(binding) ? binding : [binding];
|
|
149
269
|
const shouldPreventDefault = actionBindings.some((b) => b.preventDefault);
|
|
150
270
|
return {
|
|
151
|
-
emit: () => emit(eventName),
|
|
271
|
+
emit: () => void emit(eventName),
|
|
152
272
|
shouldPreventDefault,
|
|
153
273
|
bound: true,
|
|
154
274
|
};
|
|
@@ -175,15 +295,19 @@
|
|
|
175
295
|
|
|
176
296
|
{#if isVisible && Component}
|
|
177
297
|
<svelte:boundary
|
|
178
|
-
onerror={(error) => {
|
|
298
|
+
onerror={(error, reset) => {
|
|
299
|
+
// A new version of the element renders it again: see the effect of
|
|
300
|
+
// `resetFailed` above.
|
|
301
|
+
resetFailed = reset;
|
|
302
|
+
failedElement = subtree();
|
|
179
303
|
// onRenderError is attached as a non-enumerable registry property
|
|
180
304
|
// (defineRegistry / createRenderer), not declared on ComponentRegistry.
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
)
|
|
184
|
-
if (typeof onRenderError === 'function') {
|
|
305
|
+
// A component named onRenderError is no handler.
|
|
306
|
+
const onRenderError = getRenderErrorHandler(registry);
|
|
307
|
+
if (onRenderError !== undefined) {
|
|
185
308
|
onRenderError(error, resolvedElement.type);
|
|
186
309
|
} else {
|
|
310
|
+
warnUnusedRenderErrorHandler(registry);
|
|
187
311
|
console.error(
|
|
188
312
|
`[json-render] Rendering error in <${resolvedElement.type}>:`,
|
|
189
313
|
error,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ElementRenderer.svelte.d.ts","sourceRoot":"","sources":["../src/ElementRenderer.svelte.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,
|
|
1
|
+
{"version":3,"file":"ElementRenderer.svelte.d.ts","sourceRoot":"","sources":["../src/ElementRenderer.svelte.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,IAAI,EAAE,SAAS,EAAE,MAAM,mBAAmB,CAAC;AAqBzD,OAAO,KAAK,EAAE,iBAAiB,EAAE,iBAAiB,EAAE,MAAM,eAAe,CAAC;AAWxE,UAAU,KAAK;IACb,OAAO,EAAE,SAAS,CAAC;IACnB,8DAA8D;IAC9D,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,IAAI,EAAE,IAAI,CAAC;IACX,QAAQ,EAAE,iBAAiB,CAAC;IAC5B,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB,QAAQ,CAAC,EAAE,iBAAiB,CAAC;CAC9B;AAqUH,QAAA,MAAM,eAAe,2CAAwC,CAAC;AAC9D,KAAK,eAAe,GAAG,UAAU,CAAC,OAAO,eAAe,CAAC,CAAC;AAC1D,eAAe,eAAe,CAAC"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
/** Renders a spec with `$computed` functions, so a test can change the spec. */
|
|
3
|
+
import type { ComputedFunction, Spec, StateStore } from "@xmachines/json-render-core";
|
|
4
|
+
import type { ComponentRegistry } from "./renderer.js";
|
|
5
|
+
import JsonUIProvider from "./JsonUIProvider.svelte";
|
|
6
|
+
import Renderer from "./Renderer.svelte";
|
|
7
|
+
|
|
8
|
+
interface Props {
|
|
9
|
+
spec: Spec | null;
|
|
10
|
+
registry: ComponentRegistry;
|
|
11
|
+
store: StateStore;
|
|
12
|
+
functions: Record<string, ComputedFunction>;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
let { spec, registry, store, functions }: Props = $props();
|
|
16
|
+
</script>
|
|
17
|
+
|
|
18
|
+
<JsonUIProvider {registry} {store} {functions}>
|
|
19
|
+
<Renderer {spec} {registry} />
|
|
20
|
+
</JsonUIProvider>
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/** Renders a spec with `$computed` functions, so a test can change the spec. */
|
|
2
|
+
import type { ComputedFunction, Spec, StateStore } from "@xmachines/json-render-core";
|
|
3
|
+
import type { ComponentRegistry } from "./renderer.js";
|
|
4
|
+
interface Props {
|
|
5
|
+
spec: Spec | null;
|
|
6
|
+
registry: ComponentRegistry;
|
|
7
|
+
store: StateStore;
|
|
8
|
+
functions: Record<string, ComputedFunction>;
|
|
9
|
+
}
|
|
10
|
+
declare const FunctionsHost: import("svelte").Component<Props, {}, "">;
|
|
11
|
+
type FunctionsHost = ReturnType<typeof FunctionsHost>;
|
|
12
|
+
export default FunctionsHost;
|
|
13
|
+
//# sourceMappingURL=FunctionsHost.test.svelte.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"FunctionsHost.test.svelte.d.ts","sourceRoot":"","sources":["../src/FunctionsHost.test.svelte.ts"],"names":[],"mappings":"AAGA,gFAAgF;AAChF,OAAO,KAAK,EAAE,gBAAgB,EAAE,IAAI,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAC;AAC5E,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,eAAe,CAAC;AAKrD,UAAU,KAAK;IACb,IAAI,EAAE,IAAI,GAAG,IAAI,CAAC;IAClB,QAAQ,EAAE,iBAAiB,CAAC;IAC5B,KAAK,EAAE,UAAU,CAAC;IAClB,SAAS,EAAE,MAAM,CAAC,MAAM,EAAE,gBAAgB,CAAC,CAAC;CAC7C;AAkBH,QAAA,MAAM,aAAa,2CAAwC,CAAC;AAC5D,KAAK,aAAa,GAAG,UAAU,CAAC,OAAO,aAAa,CAAC,CAAC;AACtD,eAAe,aAAa,CAAC"}
|
package/dist/Renderer.svelte
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
<script module lang="ts">
|
|
2
|
-
import type { Spec } from "@xmachines/json-render-core";
|
|
2
|
+
import type { RenderErrorHandler, Spec } from "@xmachines/json-render-core";
|
|
3
|
+
import { withRenderErrorHandler } from "@xmachines/json-render-core";
|
|
3
4
|
import ElementRenderer from "./ElementRenderer.svelte";
|
|
4
5
|
import type { ComponentRegistry, ComponentRenderer } from "./renderer.js";
|
|
5
6
|
|
|
@@ -15,11 +16,33 @@
|
|
|
15
16
|
loading?: boolean;
|
|
16
17
|
/** Fallback component for unknown types */
|
|
17
18
|
fallback?: ComponentRenderer;
|
|
19
|
+
/**
|
|
20
|
+
* Called when a catalog component throws during a render, in place of
|
|
21
|
+
* console.error. It replaces a handler of defineRegistry, as the prop of
|
|
22
|
+
* the react, vue and solid renderers does.
|
|
23
|
+
*/
|
|
24
|
+
onRenderError?: RenderErrorHandler;
|
|
18
25
|
}
|
|
19
26
|
</script>
|
|
20
27
|
|
|
21
28
|
<script lang="ts">
|
|
22
|
-
let {
|
|
29
|
+
let {
|
|
30
|
+
spec,
|
|
31
|
+
registry,
|
|
32
|
+
loading = false,
|
|
33
|
+
fallback,
|
|
34
|
+
onRenderError,
|
|
35
|
+
}: RendererProps = $props();
|
|
36
|
+
|
|
37
|
+
// One copy of the registry for each registry, while a handler is given. The
|
|
38
|
+
// copy calls the handler of now, so a new inline handler keeps the registry,
|
|
39
|
+
// and no element resolves again for it — the rule of the react renderer.
|
|
40
|
+
const callHandler: RenderErrorHandler = (error, elementType) =>
|
|
41
|
+
onRenderError?.(error, elementType);
|
|
42
|
+
let hasHandler = $derived(onRenderError !== undefined);
|
|
43
|
+
let activeRegistry = $derived(
|
|
44
|
+
hasHandler ? withRenderErrorHandler(registry, callHandler) : registry,
|
|
45
|
+
);
|
|
23
46
|
|
|
24
47
|
let rootElement = $derived(spec?.root ? spec.elements[spec.root] : undefined);
|
|
25
48
|
</script>
|
|
@@ -29,7 +52,7 @@
|
|
|
29
52
|
element={rootElement}
|
|
30
53
|
elementKey={spec.root}
|
|
31
54
|
{spec}
|
|
32
|
-
{
|
|
55
|
+
registry={activeRegistry}
|
|
33
56
|
{loading}
|
|
34
57
|
{fallback} />
|
|
35
58
|
{/if}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { Spec } from "@xmachines/json-render-core";
|
|
1
|
+
import type { RenderErrorHandler, Spec } from "@xmachines/json-render-core";
|
|
2
2
|
import type { ComponentRegistry, ComponentRenderer } from "./renderer.js";
|
|
3
3
|
/**
|
|
4
4
|
* Props for the Renderer component
|
|
@@ -12,6 +12,12 @@ export interface RendererProps {
|
|
|
12
12
|
loading?: boolean;
|
|
13
13
|
/** Fallback component for unknown types */
|
|
14
14
|
fallback?: ComponentRenderer;
|
|
15
|
+
/**
|
|
16
|
+
* Called when a catalog component throws during a render, in place of
|
|
17
|
+
* console.error. It replaces a handler of defineRegistry, as the prop of
|
|
18
|
+
* the react, vue and solid renderers does.
|
|
19
|
+
*/
|
|
20
|
+
onRenderError?: RenderErrorHandler;
|
|
15
21
|
}
|
|
16
22
|
declare const Renderer: import("svelte").Component<RendererProps, {}, "">;
|
|
17
23
|
type Renderer = ReturnType<typeof Renderer>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Renderer.svelte.d.ts","sourceRoot":"","sources":["../src/Renderer.svelte.ts"],"names":[],"mappings":"AAGE,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,mBAAmB,CAAC;
|
|
1
|
+
{"version":3,"file":"Renderer.svelte.d.ts","sourceRoot":"","sources":["../src/Renderer.svelte.ts"],"names":[],"mappings":"AAGE,OAAO,KAAK,EAAE,kBAAkB,EAAE,IAAI,EAAE,MAAM,mBAAmB,CAAC;AAGlE,OAAO,KAAK,EAAE,iBAAiB,EAAE,iBAAiB,EAAE,MAAM,eAAe,CAAC;AAE1E;;GAEG;AACH,MAAM,WAAW,aAAa;IAC5B,4BAA4B;IAC5B,IAAI,EAAE,IAAI,GAAG,IAAI,CAAC;IAClB,yBAAyB;IACzB,QAAQ,EAAE,iBAAiB,CAAC;IAC5B,sDAAsD;IACtD,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB,2CAA2C;IAC3C,QAAQ,CAAC,EAAE,iBAAiB,CAAC;IAC7B;;;;OAIG;IACH,aAAa,CAAC,EAAE,kBAAkB,CAAC;CACpC;AAgCH,QAAA,MAAM,QAAQ,mDAAwC,CAAC;AACvD,KAAK,QAAQ,GAAG,UAAU,CAAC,OAAO,QAAQ,CAAC,CAAC;AAC5C,eAAe,QAAQ,CAAC"}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
<script lang="ts">
|
|
2
|
-
import type { Spec, ActionHandler, StateStore } from "@xmachines/json-render-core";
|
|
2
|
+
import type { Spec, ActionHandler, RenderErrorHandler, StateStore } from "@xmachines/json-render-core";
|
|
3
3
|
import type { ComponentRegistry, ComponentRenderer } from "./renderer.js";
|
|
4
4
|
import StateProvider from "./contexts/StateProvider.svelte";
|
|
5
5
|
import VisibilityProvider from "./contexts/VisibilityProvider.svelte";
|
|
@@ -15,6 +15,7 @@
|
|
|
15
15
|
initialState?: Record<string, unknown>;
|
|
16
16
|
handlers?: Record<string, ActionHandler>;
|
|
17
17
|
store?: StateStore;
|
|
18
|
+
onRenderError?: RenderErrorHandler;
|
|
18
19
|
}
|
|
19
20
|
|
|
20
21
|
let {
|
|
@@ -25,6 +26,7 @@
|
|
|
25
26
|
initialState = {},
|
|
26
27
|
handlers = {},
|
|
27
28
|
store,
|
|
29
|
+
onRenderError,
|
|
28
30
|
}: Props = $props();
|
|
29
31
|
</script>
|
|
30
32
|
|
|
@@ -32,7 +34,7 @@
|
|
|
32
34
|
<VisibilityProvider>
|
|
33
35
|
<ValidationProvider>
|
|
34
36
|
<ActionProvider {handlers}>
|
|
35
|
-
<Renderer {spec} {registry} {loading} {fallback} />
|
|
37
|
+
<Renderer {spec} {registry} {loading} {fallback} {onRenderError} />
|
|
36
38
|
</ActionProvider>
|
|
37
39
|
</ValidationProvider>
|
|
38
40
|
</VisibilityProvider>
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { Spec, ActionHandler, StateStore } from "@xmachines/json-render-core";
|
|
1
|
+
import type { Spec, ActionHandler, RenderErrorHandler, StateStore } from "@xmachines/json-render-core";
|
|
2
2
|
import type { ComponentRegistry, ComponentRenderer } from "./renderer.js";
|
|
3
3
|
interface Props {
|
|
4
4
|
spec: Spec | null;
|
|
@@ -8,6 +8,7 @@ interface Props {
|
|
|
8
8
|
initialState?: Record<string, unknown>;
|
|
9
9
|
handlers?: Record<string, ActionHandler>;
|
|
10
10
|
store?: StateStore;
|
|
11
|
+
onRenderError?: RenderErrorHandler;
|
|
11
12
|
}
|
|
12
13
|
declare const RendererWithProvider: import("svelte").Component<Props, {}, "">;
|
|
13
14
|
type RendererWithProvider = ReturnType<typeof RendererWithProvider>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"RendererWithProvider.test.svelte.d.ts","sourceRoot":"","sources":["../src/RendererWithProvider.test.svelte.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,IAAI,EAAE,aAAa,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAC;
|
|
1
|
+
{"version":3,"file":"RendererWithProvider.test.svelte.d.ts","sourceRoot":"","sources":["../src/RendererWithProvider.test.svelte.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,IAAI,EAAE,aAAa,EAAE,kBAAkB,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAC;AAC7F,OAAO,KAAK,EAAE,iBAAiB,EAAE,iBAAiB,EAAE,MAAM,eAAe,CAAC;AAQxE,UAAU,KAAK;IACb,IAAI,EAAE,IAAI,GAAG,IAAI,CAAC;IAClB,QAAQ,EAAE,iBAAiB,CAAC;IAC5B,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB,QAAQ,CAAC,EAAE,iBAAiB,CAAC;IAC7B,YAAY,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IACvC,QAAQ,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,aAAa,CAAC,CAAC;IACzC,KAAK,CAAC,EAAE,UAAU,CAAC;IACnB,aAAa,CAAC,EAAE,kBAAkB,CAAC;CACpC;AAmCH,QAAA,MAAM,oBAAoB,2CAAwC,CAAC;AACnE,KAAK,oBAAoB,GAAG,UAAU,CAAC,OAAO,oBAAoB,CAAC,CAAC;AACpE,eAAe,oBAAoB,CAAC"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
/** Writes /x when it mounts, as a select that writes its default value. */
|
|
3
|
+
import { onMount } from "svelte";
|
|
4
|
+
import { getStateContext } from "./contexts/StateProvider.svelte";
|
|
5
|
+
|
|
6
|
+
const state = getStateContext();
|
|
7
|
+
onMount(() => {
|
|
8
|
+
state.set("/x", "US");
|
|
9
|
+
});
|
|
10
|
+
</script>
|
|
11
|
+
|
|
12
|
+
<i></i>
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
interface $$__sveltets_2_IsomorphicComponent<Props extends Record<string, any> = any, Events extends Record<string, any> = any, Slots extends Record<string, any> = any, Exports = {}, Bindings = string> {
|
|
2
|
+
new (options: import('svelte').ComponentConstructorOptions<Props>): import('svelte').SvelteComponent<Props, Events, Slots> & {
|
|
3
|
+
$$bindings?: Bindings;
|
|
4
|
+
} & Exports;
|
|
5
|
+
(internal: unknown, props: {
|
|
6
|
+
$$events?: Events;
|
|
7
|
+
$$slots?: Slots;
|
|
8
|
+
}): Exports & {
|
|
9
|
+
$set?: any;
|
|
10
|
+
$on?: any;
|
|
11
|
+
};
|
|
12
|
+
z_$$bindings?: Bindings;
|
|
13
|
+
}
|
|
14
|
+
declare const TestInit: $$__sveltets_2_IsomorphicComponent<Record<string, never>, {
|
|
15
|
+
[evt: string]: CustomEvent<any>;
|
|
16
|
+
}, {}, {}, string>;
|
|
17
|
+
type TestInit = InstanceType<typeof TestInit>;
|
|
18
|
+
export default TestInit;
|
|
19
|
+
//# sourceMappingURL=TestInit.svelte.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"TestInit.svelte.d.ts","sourceRoot":"","sources":["../src/TestInit.svelte.ts"],"names":[],"mappings":"AAsBA,UAAU,kCAAkC,CAAC,KAAK,SAAS,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GAAG,GAAG,EAAE,MAAM,SAAS,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GAAG,GAAG,EAAE,KAAK,SAAS,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GAAG,GAAG,EAAE,OAAO,GAAG,EAAE,EAAE,QAAQ,GAAG,MAAM;IACpM,KAAK,OAAO,EAAE,OAAO,QAAQ,EAAE,2BAA2B,CAAC,KAAK,CAAC,GAAG,OAAO,QAAQ,EAAE,eAAe,CAAC,KAAK,EAAE,MAAM,EAAE,KAAK,CAAC,GAAG;QAAE,UAAU,CAAC,EAAE,QAAQ,CAAA;KAAE,GAAG,OAAO,CAAC;IACjK,CAAC,QAAQ,EAAE,OAAO,EAAE,KAAK,EAAE;QAAC,QAAQ,CAAC,EAAE,MAAM,CAAC;QAAC,OAAO,CAAC,EAAE,KAAK,CAAA;KAAC,GAAG,OAAO,GAAG;QAAE,IAAI,CAAC,EAAE,GAAG,CAAC;QAAC,GAAG,CAAC,EAAE,GAAG,CAAA;KAAE,CAAC;IACtG,YAAY,CAAC,EAAE,QAAQ,CAAC;CAC3B;AAKD,QAAA,MAAM,QAAQ;;kBAA+E,CAAC;AAC5E,KAAK,QAAQ,GAAG,YAAY,CAAC,OAAO,QAAQ,CAAC,CAAC;AAChD,eAAe,QAAQ,CAAC"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
<script module lang="ts">
|
|
2
|
+
/** The instances that rendered "ok", for a test that counts mounts. */
|
|
3
|
+
export const mounts = { count: 0 };
|
|
4
|
+
</script>
|
|
5
|
+
|
|
6
|
+
<script lang="ts">
|
|
7
|
+
/** Throws while its `fail` prop is true, and renders "ok" otherwise. */
|
|
8
|
+
import { untrack } from "svelte";
|
|
9
|
+
import type { BaseComponentProps } from "./catalog-types.js";
|
|
10
|
+
|
|
11
|
+
interface Props extends BaseComponentProps<{ fail?: boolean }> {}
|
|
12
|
+
|
|
13
|
+
let { props }: Props = $props();
|
|
14
|
+
|
|
15
|
+
// The component throws while it initialises, and a new element mounts a new
|
|
16
|
+
// instance, so it reads its prop one time.
|
|
17
|
+
if (untrack(() => props.fail)) throw new Error("the component failed");
|
|
18
|
+
mounts.count += 1;
|
|
19
|
+
</script>
|
|
20
|
+
|
|
21
|
+
<span data-ok>ok</span>
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/** The instances that rendered "ok", for a test that counts mounts. */
|
|
2
|
+
export declare const mounts: {
|
|
3
|
+
count: number;
|
|
4
|
+
};
|
|
5
|
+
import type { BaseComponentProps } from "./catalog-types.js";
|
|
6
|
+
interface Props extends BaseComponentProps<{
|
|
7
|
+
fail?: boolean;
|
|
8
|
+
}> {
|
|
9
|
+
}
|
|
10
|
+
declare const TestMaybeBoom: import("svelte").Component<Props, {}, "">;
|
|
11
|
+
type TestMaybeBoom = ReturnType<typeof TestMaybeBoom>;
|
|
12
|
+
export default TestMaybeBoom;
|
|
13
|
+
//# sourceMappingURL=TestMaybeBoom.svelte.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"TestMaybeBoom.svelte.d.ts","sourceRoot":"","sources":["../src/TestMaybeBoom.svelte.ts"],"names":[],"mappings":"AAGE,uEAAuE;AACvE,eAAO,MAAM,MAAM;;CAAe,CAAC;AAKrC,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,oBAAoB,CAAC;AAG3D,UAAU,KAAM,SAAQ,kBAAkB,CAAC;IAAE,IAAI,CAAC,EAAE,OAAO,CAAA;CAAE,CAAC;CAAG;AAqBnE,QAAA,MAAM,aAAa,2CAAwC,CAAC;AAC5D,KAAK,aAAa,GAAG,UAAU,CAAC,OAAO,aAAa,CAAC,CAAC;AACtD,eAAe,aAAa,CAAC"}
|