@xmachines/json-render-react 0.21.0-xm.5 → 0.21.0-xm.7

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/dist/index.mjs CHANGED
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  elementTreeSchema,
3
3
  schema
4
- } from "./chunk-SDI5YI5X.mjs";
4
+ } from "./chunk-YOMYRECB.mjs";
5
5
 
6
6
  // ../react-state/dist/index.mjs
7
7
  import {
@@ -181,14 +181,13 @@ function useIsVisible(condition) {
181
181
  import {
182
182
  createContext as createContext4,
183
183
  useContext as useContext4,
184
- useState as useState2,
185
- useCallback as useCallback3,
186
- useMemo as useMemo4,
187
- useRef as useRef3
184
+ useState as useState4,
185
+ useCallback as useCallback5,
186
+ useMemo as useMemo5,
187
+ useRef as useRef5
188
188
  } from "react";
189
189
  import {
190
190
  resolveAction,
191
- mergeActionHandlers,
192
191
  executeAction,
193
192
  isBuiltInAction,
194
193
  nextActionDispatchId,
@@ -197,17 +196,68 @@ import {
197
196
  } from "@xmachines/json-render-core";
198
197
 
199
198
  // src/contexts/validation.tsx
200
- import React2, {
199
+ import {
201
200
  createContext as createContext3,
202
201
  useContext as useContext3,
203
- useRef as useRef2,
204
- useState,
205
- useCallback as useCallback2,
202
+ useRef as useRef3,
203
+ useState as useState2,
204
+ useCallback as useCallback3,
206
205
  useMemo as useMemo3
207
206
  } from "react";
208
207
  import {
209
208
  runValidation
210
209
  } from "@xmachines/json-render-core";
210
+
211
+ // src/xm/field-registration.ts
212
+ import React2, { useCallback as useCallback2, useRef as useRef2, useState } from "react";
213
+ var useIsomorphicLayoutEffect = typeof document !== "undefined" ? React2.useLayoutEffect : React2.useEffect;
214
+ function useFieldConfigs(fieldStatesRef, configEqual) {
215
+ const [fieldConfigs, setFieldConfigs] = useState({});
216
+ const fieldConfigsRef = useRef2({});
217
+ const registrationCounts = useRef2({});
218
+ const registerField = useCallback2(
219
+ (path, config) => {
220
+ registrationCounts.current[path] = (registrationCounts.current[path] ?? 0) + 1;
221
+ const existing = fieldConfigsRef.current[path];
222
+ if (existing && configEqual(existing, config)) return;
223
+ fieldConfigsRef.current = { ...fieldConfigsRef.current, [path]: config };
224
+ setFieldConfigs(fieldConfigsRef.current);
225
+ },
226
+ [configEqual]
227
+ );
228
+ const unregisterField = useCallback2(
229
+ (path) => {
230
+ const next = (registrationCounts.current[path] ?? 0) - 1;
231
+ if (next > 0) {
232
+ registrationCounts.current[path] = next;
233
+ return;
234
+ }
235
+ delete registrationCounts.current[path];
236
+ delete fieldStatesRef.current[path];
237
+ if (!(path in fieldConfigsRef.current)) return;
238
+ const { [path]: _removed, ...rest } = fieldConfigsRef.current;
239
+ fieldConfigsRef.current = rest;
240
+ setFieldConfigs(rest);
241
+ },
242
+ [fieldStatesRef]
243
+ );
244
+ return { fieldConfigs, fieldConfigsRef, registerField, unregisterField };
245
+ }
246
+ function useFieldRegistration(path, config, registerField, unregisterField) {
247
+ const configKey = config === void 0 ? void 0 : JSON.stringify(config);
248
+ const configRef = useRef2(config);
249
+ configRef.current = config;
250
+ useIsomorphicLayoutEffect(() => {
251
+ const current = configRef.current;
252
+ if (path && current) {
253
+ registerField(path, current);
254
+ return () => unregisterField(path);
255
+ }
256
+ return void 0;
257
+ }, [path, configKey, registerField, unregisterField]);
258
+ }
259
+
260
+ // src/contexts/validation.tsx
211
261
  import { jsx as jsx3 } from "react/jsx-runtime";
212
262
  var ValidationContext = createContext3(null);
213
263
  var EMPTY_VALIDATION_FUNCTIONS = {};
@@ -250,38 +300,10 @@ function ValidationProvider({
250
300
  children
251
301
  }) {
252
302
  const { state, getSnapshot } = useStateStore();
253
- const [fieldStates, setFieldStates] = useState({});
254
- const fieldStatesRef = useRef2({});
255
- const [fieldConfigs, setFieldConfigs] = useState({});
256
- const registrationCounts = useRef2({});
257
- const registerField = useCallback2(
258
- (path, config) => {
259
- registrationCounts.current[path] = (registrationCounts.current[path] ?? 0) + 1;
260
- setFieldConfigs((prev) => {
261
- const existing = prev[path];
262
- if (existing && validationConfigEqual(existing, config)) {
263
- return prev;
264
- }
265
- return { ...prev, [path]: config };
266
- });
267
- },
268
- []
269
- );
270
- const unregisterField = useCallback2((path) => {
271
- const next = (registrationCounts.current[path] ?? 0) - 1;
272
- if (next > 0) {
273
- registrationCounts.current[path] = next;
274
- return;
275
- }
276
- delete registrationCounts.current[path];
277
- delete fieldStatesRef.current[path];
278
- setFieldConfigs((prev) => {
279
- if (!(path in prev)) return prev;
280
- const { [path]: _removed, ...rest } = prev;
281
- return rest;
282
- });
283
- }, []);
284
- const validate = useCallback2(
303
+ const [fieldStates, setFieldStates] = useState2({});
304
+ const fieldStatesRef = useRef3({});
305
+ const { fieldConfigs, fieldConfigsRef, registerField, unregisterField } = useFieldConfigs(fieldStatesRef, validationConfigEqual);
306
+ const validate = useCallback3(
285
307
  (path, config) => {
286
308
  const currentState = getSnapshot();
287
309
  const segments = path.split("/").filter(Boolean);
@@ -313,7 +335,7 @@ function ValidationProvider({
313
335
  },
314
336
  [customFunctions, getSnapshot]
315
337
  );
316
- const touch = useCallback2((path) => {
338
+ const touch = useCallback3((path) => {
317
339
  fieldStatesRef.current = {
318
340
  ...fieldStatesRef.current,
319
341
  [path]: {
@@ -325,21 +347,21 @@ function ValidationProvider({
325
347
  };
326
348
  setFieldStates(fieldStatesRef.current);
327
349
  }, []);
328
- const clear = useCallback2((path) => {
350
+ const clear = useCallback3((path) => {
329
351
  const { [path]: _, ...rest } = fieldStatesRef.current;
330
352
  fieldStatesRef.current = rest;
331
353
  setFieldStates(rest);
332
354
  }, []);
333
- const validateAll = useCallback2(() => {
355
+ const validateAll = useCallback3(() => {
334
356
  let allValid = true;
335
- for (const [path, config] of Object.entries(fieldConfigs)) {
357
+ for (const [path, config] of Object.entries(fieldConfigsRef.current)) {
336
358
  const result = validate(path, config);
337
359
  if (!result.valid) {
338
360
  allValid = false;
339
361
  }
340
362
  }
341
363
  return allValid;
342
- }, [fieldConfigs, validate]);
364
+ }, [validate]);
343
365
  const value = useMemo3(
344
366
  () => ({
345
367
  customFunctions,
@@ -360,6 +382,7 @@ function ValidationProvider({
360
382
  // fieldStates (React state) stays in deps so the context value object
361
383
  // is recreated on re-render, triggering downstream consumers.
362
384
  fieldStates,
385
+ fieldConfigs,
363
386
  validate,
364
387
  touch,
365
388
  clear,
@@ -380,7 +403,6 @@ function useValidation() {
380
403
  function useOptionalValidation() {
381
404
  return useContext3(ValidationContext);
382
405
  }
383
- var useIsomorphicLayoutEffect = typeof document !== "undefined" ? React2.useLayoutEffect : React2.useEffect;
384
406
  function useFieldValidation(path, config) {
385
407
  const {
386
408
  fieldStates,
@@ -390,28 +412,18 @@ function useFieldValidation(path, config) {
390
412
  registerField,
391
413
  unregisterField
392
414
  } = useValidation();
393
- const configKey = config === void 0 ? void 0 : JSON.stringify(config);
394
- const configRef = React2.useRef(config);
395
- configRef.current = config;
396
- useIsomorphicLayoutEffect(() => {
397
- const current = configRef.current;
398
- if (path && current) {
399
- registerField(path, current);
400
- return () => unregisterField(path);
401
- }
402
- return void 0;
403
- }, [path, configKey, registerField, unregisterField]);
415
+ useFieldRegistration(path, config, registerField, unregisterField);
404
416
  const state = fieldStates[path] ?? {
405
417
  touched: false,
406
418
  validated: false,
407
419
  result: null
408
420
  };
409
- const validate = useCallback2(
421
+ const validate = useCallback3(
410
422
  () => validateField(path, config ?? { checks: [] }),
411
423
  [path, config, validateField]
412
424
  );
413
- const touch = useCallback2(() => touchField(path), [path, touchField]);
414
- const clear = useCallback2(() => clearField(path), [path, clearField]);
425
+ const touch = useCallback3(() => touchField(path), [path, touchField]);
426
+ const clear = useCallback3(() => clearField(path), [path, clearField]);
415
427
  return {
416
428
  state,
417
429
  validate,
@@ -422,6 +434,34 @@ function useFieldValidation(path, config) {
422
434
  };
423
435
  }
424
436
 
437
+ // src/xm/action-handlers.ts
438
+ import { useCallback as useCallback4, useMemo as useMemo4, useRef as useRef4, useState as useState3 } from "react";
439
+ import { mergeActionHandlers } from "@xmachines/json-render-core";
440
+ var ANY_NAME_PROBE = "\0json-render:any-name";
441
+ function useActionHandlers(handlersProp) {
442
+ const propRef = useRef4(handlersProp);
443
+ propRef.current = handlersProp;
444
+ const [registered, setRegistered] = useState3(
445
+ {}
446
+ );
447
+ const propNames = JSON.stringify([
448
+ Object.keys(handlersProp),
449
+ Reflect.has(handlersProp, ANY_NAME_PROBE)
450
+ ]);
451
+ const handlers = useMemo4(
452
+ () => mergeActionHandlers(() => propRef.current, registered),
453
+ // eslint-disable-next-line react-hooks/exhaustive-deps -- propNames is the change signal
454
+ [registered, propNames]
455
+ );
456
+ const registerHandler = useCallback4(
457
+ (name, handler) => {
458
+ setRegistered((prev) => ({ ...prev, [name]: handler }));
459
+ },
460
+ []
461
+ );
462
+ return { handlers, registerHandler };
463
+ }
464
+
425
465
  // src/contexts/actions.tsx
426
466
  import { jsx as jsx4, jsxs } from "react/jsx-runtime";
427
467
  var idCounter = 0;
@@ -464,30 +504,11 @@ function ActionProvider({
464
504
  }) {
465
505
  const { get, set, getSnapshot } = useStateStore();
466
506
  const validation = useOptionalValidation();
467
- const propRef = useRef3(handlersProp);
468
- propRef.current = handlersProp;
469
- const [registered, setRegistered] = useState2(
470
- {}
471
- );
472
- const propNames = JSON.stringify([
473
- Object.keys(handlersProp),
474
- Reflect.has(handlersProp, "\0json-render:any-name")
475
- ]);
476
- const handlers = useMemo4(
477
- () => mergeActionHandlers(() => propRef.current, registered),
478
- // eslint-disable-next-line react-hooks/exhaustive-deps -- propNames is the change signal
479
- [registered, propNames]
480
- );
481
- const [loadingActions, setLoadingActions] = useState2(/* @__PURE__ */ new Set());
482
- const [pendingConfirmation, setPendingConfirmation] = useState2(null);
483
- const pendingRef = useRef3(null);
484
- const registerHandler = useCallback3(
485
- (name, handler) => {
486
- setRegistered((prev) => ({ ...prev, [name]: handler }));
487
- },
488
- []
489
- );
490
- const execute = useCallback3(
507
+ const { handlers, registerHandler } = useActionHandlers(handlersProp);
508
+ const [loadingActions, setLoadingActions] = useState4(/* @__PURE__ */ new Set());
509
+ const [pendingConfirmation, setPendingConfirmation] = useState4(null);
510
+ const pendingRef = useRef5(null);
511
+ const execute = useCallback5(
491
512
  async (binding) => {
492
513
  const resolved = resolveAction(binding, getSnapshot());
493
514
  const dispatchId = nextActionDispatchId();
@@ -655,13 +676,13 @@ function ActionProvider({
655
676
  },
656
677
  [handlers, get, set, getSnapshot, navigate, validation]
657
678
  );
658
- const confirm = useCallback3(() => {
679
+ const confirm = useCallback5(() => {
659
680
  pendingRef.current?.resolve();
660
681
  }, []);
661
- const cancel = useCallback3(() => {
682
+ const cancel = useCallback5(() => {
662
683
  pendingRef.current?.reject();
663
684
  }, []);
664
- const value = useMemo4(
685
+ const value = useMemo5(
665
686
  () => ({
666
687
  handlers,
667
688
  loadingActions,
@@ -693,7 +714,7 @@ function useActions() {
693
714
  function useAction(binding) {
694
715
  const { execute, loadingActions } = useActions();
695
716
  const isLoading = loadingActions.has(binding.action);
696
- const executeAction2 = useCallback3(() => execute(binding), [execute, binding]);
717
+ const executeAction2 = useCallback5(() => execute(binding), [execute, binding]);
697
718
  return { execute: executeAction2, isLoading };
698
719
  }
699
720
  function ConfirmDialog({
@@ -817,11 +838,10 @@ function useRepeatScope() {
817
838
  import { createStateStore as createStateStore2 } from "@xmachines/json-render-core";
818
839
 
819
840
  // src/renderer.tsx
820
- import React5, {
821
- useCallback as useCallback4,
822
- useEffect as useEffect2,
823
- useMemo as useMemo5,
824
- useRef as useRef4
841
+ import React7, {
842
+ useCallback as useCallback6,
843
+ useMemo as useMemo8,
844
+ useRef as useRef8
825
845
  } from "react";
826
846
  import {
827
847
  resolveElementProps,
@@ -832,49 +852,93 @@ import {
832
852
  splitRepeatVisibility,
833
853
  evaluateVisibility as evaluateVisibility2,
834
854
  getByPath as getByPath2,
835
- readWatchedValues,
836
- changedWatchPaths,
837
- runWatchBindings,
838
855
  isDevtoolsActive,
839
856
  subscribeDevtoolsActive,
840
857
  createDirectiveRegistry,
841
- withRenderErrorHandler,
842
858
  setRenderErrorHandler,
843
859
  createComponentRegistry,
844
- getRegistryComponent,
845
- warnUnusedRenderErrorHandler,
846
- getRenderErrorHandler,
847
- reportActionError,
860
+ getRegistryComponent as getRegistryComponent2,
848
861
  setCatalogSlotMetadata,
849
862
  warnElementSlots
850
863
  } from "@xmachines/json-render-core";
851
- import { Fragment, jsx as jsx6, jsxs as jsxs2 } from "react/jsx-runtime";
852
- var EMPTY_ELEMENT_PROPS = {};
864
+
865
+ // src/xm/render-errors.tsx
866
+ import React6, { useEffect as useEffect2, useMemo as useMemo6, useRef as useRef6 } from "react";
867
+ import {
868
+ getRegistryComponent,
869
+ getRenderErrorHandler,
870
+ reportRenderError,
871
+ warnMissingRoot,
872
+ warnUnknownType,
873
+ warnUnusedRenderErrorHandler,
874
+ withRenderErrorHandler
875
+ } from "@xmachines/json-render-core";
853
876
  function useRenderErrorRegistry(registry, onRenderError) {
854
- const handlerRef = useRef4(onRenderError);
877
+ const handlerRef = useRef6(onRenderError);
855
878
  handlerRef.current = onRenderError;
856
879
  const hasHandler = onRenderError !== void 0;
857
- return useMemo5(
858
- () => hasHandler ? withRenderErrorHandler(registry, (error, elementType) => {
859
- handlerRef.current?.(error, elementType);
880
+ return useMemo6(
881
+ () => hasHandler ? withRenderErrorHandler(registry, (error, name, info) => {
882
+ handlerRef.current?.(error, name, info);
860
883
  }) : registry,
861
884
  [registry, hasHandler]
862
885
  );
863
886
  }
864
- function reportRenderError(registry, elementType, error, componentStack) {
865
- const onRenderError = getRenderErrorHandler(registry);
866
- if (onRenderError !== void 0) {
867
- onRenderError(error, elementType);
868
- return;
887
+ var objectTags = /* @__PURE__ */ new WeakMap();
888
+ var primitiveTag;
889
+ function isKeyable(value) {
890
+ return typeof value === "object" && value !== null || typeof value === "function";
891
+ }
892
+ function setTag(error, tag) {
893
+ if (isKeyable(error)) objectTags.set(error, tag);
894
+ else primitiveTag = { error, tag };
895
+ }
896
+ function takeTag(error) {
897
+ if (isKeyable(error)) {
898
+ const tag = objectTags.get(error);
899
+ objectTags.delete(error);
900
+ return tag;
869
901
  }
870
- warnUnusedRenderErrorHandler(registry);
871
- console.error(
872
- `[json-render] Rendering error in <${elementType}>:`,
873
- error,
874
- componentStack
902
+ if (primitiveTag !== void 0 && Object.is(primitiveTag.error, error)) {
903
+ const { tag } = primitiveTag;
904
+ primitiveTag = void 0;
905
+ return tag;
906
+ }
907
+ return void 0;
908
+ }
909
+ function failsAs(kind, key, type, resolve, ...args) {
910
+ try {
911
+ return resolve(...args);
912
+ } catch (error) {
913
+ setTag(error, { kind, key, type });
914
+ throw error;
915
+ }
916
+ }
917
+ function useElementFailures(registry, key, type) {
918
+ return useMemo6(
919
+ () => ({
920
+ failsAt: (kind, resolve, ...args) => failsAs(kind, key, type, resolve, ...args),
921
+ reportChain: (error, action, kind) => reportRenderError(getRenderErrorHandler(registry), error, action, {
922
+ kind,
923
+ action,
924
+ key,
925
+ type
926
+ })
927
+ }),
928
+ [registry, key, type]
875
929
  );
876
930
  }
877
- var ElementErrorBoundary = class extends React5.Component {
931
+ function caughtInfo(error, type, key) {
932
+ const tag = takeTag(error);
933
+ if (tag === void 0) return { kind: "element", key, type };
934
+ return { kind: tag.kind, key: tag.key ?? key, type: tag.type };
935
+ }
936
+ function reportElementFailure(registry, type, key, error) {
937
+ const handler = getRenderErrorHandler(registry);
938
+ if (handler === void 0) warnUnusedRenderErrorHandler(registry);
939
+ reportRenderError(handler, error, type, caughtInfo(error, type, key));
940
+ }
941
+ var RootErrorBoundary = class extends React6.Component {
878
942
  constructor(props) {
879
943
  super(props);
880
944
  this.state = { hasError: false };
@@ -882,16 +946,101 @@ var ElementErrorBoundary = class extends React5.Component {
882
946
  static getDerivedStateFromError() {
883
947
  return { hasError: true };
884
948
  }
885
- componentDidCatch(error, info) {
886
- reportRenderError(
949
+ componentDidCatch(error) {
950
+ const handler = getRenderErrorHandler(this.props.registry);
951
+ if (handler === void 0)
952
+ warnUnusedRenderErrorHandler(this.props.registry);
953
+ takeTag(error);
954
+ reportRenderError(handler, error, this.props.type, {
955
+ kind: "root",
956
+ key: this.props.rootKey,
957
+ type: this.props.type
958
+ });
959
+ }
960
+ componentDidUpdate(previous, previousState) {
961
+ if (this.state.hasError && previousState.hasError && previous.resetKey !== this.props.resetKey) {
962
+ this.setState({ hasError: false });
963
+ }
964
+ }
965
+ render() {
966
+ return this.state.hasError ? null : this.props.children;
967
+ }
968
+ };
969
+ function useMissingRootWarning(spec, loading) {
970
+ const missing = spec !== null && !loading && typeof spec.root === "string" && spec.root !== "" && spec.elements?.[spec.root] === void 0 ? spec.root : void 0;
971
+ useEffect2(() => {
972
+ if (missing !== void 0) warnMissingRoot(missing);
973
+ }, [missing]);
974
+ }
975
+ function useUnknownTypeWarning(registry, fallback, type, key) {
976
+ const unknown = type !== void 0 && (fallback === void 0 || fallback === null) && getRegistryComponent(registry, type) === void 0 ? type : void 0;
977
+ useEffect2(() => {
978
+ if (unknown !== void 0) warnUnknownType(unknown, key);
979
+ }, [unknown, key]);
980
+ }
981
+
982
+ // src/xm/element-watch.ts
983
+ import { useEffect as useEffect3, useMemo as useMemo7, useRef as useRef7 } from "react";
984
+ import {
985
+ changedWatchPaths,
986
+ readWatchedValues,
987
+ runWatchBindings
988
+ } from "@xmachines/json-render-core";
989
+ function useElementWatch(watch, state, run, report) {
990
+ const latest = { watch, run, report };
991
+ const latestRef = useRef7(latest);
992
+ latestRef.current = latest;
993
+ const stableRef = useRef7(void 0);
994
+ const previousRef = useRef7(null);
995
+ const watchedValues = useMemo7(() => {
996
+ const values = readWatchedValues(watch, state, stableRef.current);
997
+ stableRef.current = values;
998
+ return values;
999
+ }, [watch, state]);
1000
+ useEffect3(() => {
1001
+ const previous = previousRef.current;
1002
+ previousRef.current = watchedValues ?? null;
1003
+ const paths = changedWatchPaths(watchedValues, previous ?? void 0);
1004
+ const current = latestRef.current.watch;
1005
+ if (!current || paths.length === 0) return;
1006
+ let cancelled = false;
1007
+ let action = "";
1008
+ void runWatchBindings(
1009
+ current,
1010
+ paths,
1011
+ async (binding) => {
1012
+ action = binding.action;
1013
+ await latestRef.current.run(binding);
1014
+ },
1015
+ () => cancelled
1016
+ ).catch((error) => latestRef.current.report(error, action));
1017
+ return () => {
1018
+ cancelled = true;
1019
+ };
1020
+ }, [watchedValues]);
1021
+ }
1022
+
1023
+ // src/renderer.tsx
1024
+ import { Fragment, jsx as jsx6, jsxs as jsxs2 } from "react/jsx-runtime";
1025
+ var EMPTY_ELEMENT_PROPS = {};
1026
+ var ElementErrorBoundary = class extends React7.Component {
1027
+ constructor(props) {
1028
+ super(props);
1029
+ this.state = { hasError: false };
1030
+ }
1031
+ static getDerivedStateFromError() {
1032
+ return { hasError: true };
1033
+ }
1034
+ componentDidCatch(error) {
1035
+ reportElementFailure(
887
1036
  this.props.registry,
888
1037
  this.props.elementType,
889
- error,
890
- info.componentStack
1038
+ this.props.elementKey,
1039
+ error
891
1040
  );
892
1041
  }
893
- componentDidUpdate(previous) {
894
- if (this.state.hasError && previous.resetKey !== this.props.resetKey) {
1042
+ componentDidUpdate(previous, previousState) {
1043
+ if (this.state.hasError && previousState.hasError && previous.resetKey !== this.props.resetKey) {
895
1044
  this.setState({ hasError: false });
896
1045
  }
897
1046
  }
@@ -903,15 +1052,15 @@ var ElementErrorBoundary = class extends React5.Component {
903
1052
  }
904
1053
  };
905
1054
  var EMPTY_FUNCTIONS = {};
906
- var FunctionsContext = React5.createContext(EMPTY_FUNCTIONS);
1055
+ var FunctionsContext = React7.createContext(EMPTY_FUNCTIONS);
907
1056
  function useFunctions() {
908
- return React5.useContext(FunctionsContext);
1057
+ return React7.useContext(FunctionsContext);
909
1058
  }
910
- var DirectivesContext = React5.createContext(
1059
+ var DirectivesContext = React7.createContext(
911
1060
  void 0
912
1061
  );
913
1062
  function useDirectives() {
914
- return React5.useContext(DirectivesContext);
1063
+ return React7.useContext(DirectivesContext);
915
1064
  }
916
1065
  function stabilizeRecord(value, ref) {
917
1066
  const previous = ref.current;
@@ -942,11 +1091,12 @@ function structurallyEqual(previous, next) {
942
1091
  const previousRecord = previous;
943
1092
  const nextRecord = next;
944
1093
  const keys = Object.keys(nextRecord);
945
- if (previousIsArray && previous.length !== next.length || keys.length !== Object.keys(previousRecord).length) {
1094
+ const previousKeys = Object.keys(previousRecord);
1095
+ if (previousIsArray && previous.length !== next.length || keys.length !== previousKeys.length) {
946
1096
  return false;
947
1097
  }
948
1098
  return keys.every(
949
- (key) => Object.prototype.hasOwnProperty.call(previousRecord, key) && structurallyEqual(previousRecord[key], nextRecord[key])
1099
+ (key, index) => previousKeys[index] === key && structurallyEqual(previousRecord[key], nextRecord[key])
950
1100
  );
951
1101
  }
952
1102
  function snapshotStructuralValue(value) {
@@ -984,8 +1134,8 @@ function shareResolvedValue(previous, next) {
984
1134
  return unchanged ? previous : shared;
985
1135
  }
986
1136
  function useElementSignatures(spec) {
987
- const entriesRef = useRef4({});
988
- const versionRef = useRef4(0);
1137
+ const entriesRef = useRef8({});
1138
+ const versionRef = useRef8(0);
989
1139
  if (!spec) return {};
990
1140
  const previous = entriesRef.current;
991
1141
  const next = {};
@@ -1052,7 +1202,7 @@ function useElementSignatures(spec) {
1052
1202
  entriesRef.current = next;
1053
1203
  return signatures;
1054
1204
  }
1055
- var CatalogComponentBoundary = React5.memo(
1205
+ var CatalogComponentBoundary = React7.memo(
1056
1206
  function CatalogComponentBoundary2({
1057
1207
  Component,
1058
1208
  element,
@@ -1079,7 +1229,7 @@ var CatalogComponentBoundary = React5.memo(
1079
1229
  (previous, next) => previous.Component === next.Component && previous.signature === next.signature && previous.execute === next.execute && previous.actionContext?.item === next.actionContext?.item && previous.actionContext?.index === next.actionContext?.index && previous.actionContext?.basePath === next.actionContext?.basePath && previous.functions === next.functions && previous.directives === next.directives && previous.element.props === next.element.props && previous.bindings === next.bindings && previous.loading === next.loading
1080
1230
  );
1081
1231
  function useDevtoolsActive() {
1082
- return React5.useSyncExternalStore(
1232
+ return React7.useSyncExternalStore(
1083
1233
  subscribeDevtoolsActive,
1084
1234
  isDevtoolsActive,
1085
1235
  () => false
@@ -1092,7 +1242,8 @@ function ReactiveElementRenderer({
1092
1242
  registry,
1093
1243
  loading,
1094
1244
  fallback,
1095
- signatures
1245
+ signatures,
1246
+ isRoot
1096
1247
  }) {
1097
1248
  const devtoolsActive = useDevtoolsActive();
1098
1249
  const repeatScope = useRepeatScope();
@@ -1101,7 +1252,12 @@ function ReactiveElementRenderer({
1101
1252
  const { getSnapshot, state: watchState } = useStateStore();
1102
1253
  const functions = useFunctions();
1103
1254
  const directives = useDirectives();
1104
- const fullCtx = useMemo5(() => {
1255
+ const { failsAt, reportChain } = useElementFailures(
1256
+ registry,
1257
+ elementKey,
1258
+ element.type
1259
+ );
1260
+ const fullCtx = useMemo8(() => {
1105
1261
  const base = repeatScope ? {
1106
1262
  ...ctx,
1107
1263
  repeatItem: repeatScope.item,
@@ -1114,9 +1270,14 @@ function ReactiveElementRenderer({
1114
1270
  }, [ctx, repeatScope, functions, directives]);
1115
1271
  const repeatVisibility = element.repeat !== void 0 && repeatScope == null ? splitRepeatVisibility(element.visible) : { container: element.visible, itemFilter: void 0 };
1116
1272
  const repeatItemFilter = repeatVisibility.itemFilter;
1117
- const isVisible = repeatVisibility.container === void 0 ? true : evaluateVisibility2(repeatVisibility.container, fullCtx);
1273
+ const isVisible = repeatVisibility.container === void 0 ? true : failsAt(
1274
+ "visible",
1275
+ evaluateVisibility2,
1276
+ repeatVisibility.container,
1277
+ fullCtx
1278
+ );
1118
1279
  const onBindings = element.on;
1119
- const emit = useCallback4(
1280
+ const emit = useCallback6(
1120
1281
  async (eventName) => {
1121
1282
  const binding = onBindings?.[eventName];
1122
1283
  if (!binding) return;
@@ -1133,17 +1294,12 @@ function ReactiveElementRenderer({
1133
1294
  );
1134
1295
  }
1135
1296
  } catch (err) {
1136
- reportActionError(
1137
- getRenderErrorHandler(registry),
1138
- err,
1139
- action,
1140
- "event"
1141
- );
1297
+ reportChain(err, action, "action");
1142
1298
  }
1143
1299
  },
1144
- [onBindings, execute, fullCtx, getSnapshot, registry]
1300
+ [onBindings, execute, fullCtx, getSnapshot, reportChain]
1145
1301
  );
1146
- const on = useCallback4(
1302
+ const on = useCallback6(
1147
1303
  (eventName) => {
1148
1304
  const binding = onBindings?.[eventName];
1149
1305
  if (!binding) {
@@ -1160,87 +1316,40 @@ function ReactiveElementRenderer({
1160
1316
  },
1161
1317
  [onBindings, emit]
1162
1318
  );
1163
- const watchConfig = element.watch;
1164
- const watchDepsRef = useRef4({
1165
- watchConfig,
1166
- fullCtx,
1167
- execute,
1168
- getSnapshot,
1169
- registry
1170
- });
1171
- watchDepsRef.current = {
1172
- watchConfig,
1173
- fullCtx,
1174
- execute,
1175
- getSnapshot,
1176
- registry
1177
- };
1178
- const prevWatchValues = useRef4(null);
1179
- const stableWatchRef = useRef4(void 0);
1180
- const stableBindingsRef = useRef4(
1319
+ const stableBindingsRef = useRef8(
1181
1320
  void 0
1182
1321
  );
1183
- const stableResolvedPropsRef = useRef4({});
1184
- const watchedValues = useMemo5(() => {
1185
- const values = readWatchedValues(
1186
- watchConfig,
1187
- watchState,
1188
- stableWatchRef.current
1189
- );
1190
- stableWatchRef.current = values;
1191
- return values;
1192
- }, [watchConfig, watchState]);
1193
- useEffect2(() => {
1194
- const prev = prevWatchValues.current;
1195
- prevWatchValues.current = watchedValues ?? null;
1196
- const paths = changedWatchPaths(watchedValues, prev ?? void 0);
1197
- const watch = watchDepsRef.current.watchConfig;
1198
- if (!watch || paths.length === 0) return;
1199
- let cancelled = false;
1200
- let action = "";
1201
- void runWatchBindings(
1202
- watch,
1203
- paths,
1204
- async (b) => {
1205
- action = b.action;
1206
- const { execute: execute2, fullCtx: fullCtx2, getSnapshot: getSnapshot2 } = watchDepsRef.current;
1207
- await execute2(
1208
- resolveBindingParams(b, {
1209
- ...fullCtx2,
1210
- stateModel: getSnapshot2()
1211
- })
1212
- );
1213
- },
1214
- () => cancelled
1215
- ).catch((err) => {
1216
- reportActionError(
1217
- getRenderErrorHandler(watchDepsRef.current.registry),
1218
- err,
1219
- action,
1220
- "watch"
1221
- );
1222
- });
1223
- return () => {
1224
- cancelled = true;
1225
- };
1226
- }, [watchedValues]);
1322
+ const stableResolvedPropsRef = useRef8({});
1323
+ useElementWatch(
1324
+ element.watch,
1325
+ watchState,
1326
+ (b) => execute(
1327
+ resolveBindingParams(b, { ...fullCtx, stateModel: getSnapshot() })
1328
+ ),
1329
+ (error, action) => reportChain(error, action, "watch")
1330
+ );
1331
+ useUnknownTypeWarning(
1332
+ registry,
1333
+ fallback,
1334
+ isVisible ? element.type : void 0,
1335
+ elementKey
1336
+ );
1227
1337
  if (!isVisible) {
1228
1338
  return null;
1229
1339
  }
1230
1340
  const rawProps = element.props ?? EMPTY_ELEMENT_PROPS;
1231
1341
  const elementBindings = stabilizeRecord(
1232
- resolveBindings(rawProps, fullCtx),
1342
+ failsAt("props", resolveBindings, rawProps, fullCtx),
1233
1343
  stableBindingsRef
1234
1344
  );
1235
1345
  const resolvedProps = shareResolvedValue(
1236
1346
  stableResolvedPropsRef.current,
1237
- resolveElementProps(rawProps, fullCtx)
1347
+ failsAt("props", resolveElementProps, rawProps, fullCtx)
1238
1348
  );
1239
1349
  stableResolvedPropsRef.current = resolvedProps;
1240
1350
  const resolvedElement = resolvedProps !== element.props ? { ...element, props: resolvedProps } : element;
1241
- const Component = getRegistryComponent(registry, resolvedElement.type) ?? fallback;
1351
+ const Component = getRegistryComponent2(registry, resolvedElement.type) ?? fallback;
1242
1352
  if (!Component) {
1243
- console.warn(`No renderer for component type: ${resolvedElement.type}`);
1244
1353
  return null;
1245
1354
  }
1246
1355
  warnElementSlots(registry, resolvedElement.type, resolvedElement.slots);
@@ -1306,17 +1415,19 @@ function ReactiveElementRenderer({
1306
1415
  }
1307
1416
  );
1308
1417
  const tagged = devtoolsActive && elementKey ? /* @__PURE__ */ jsx6("span", { "data-jr-key": elementKey, style: { display: "contents" }, children: rendered }) : rendered;
1418
+ if (isRoot) return tagged;
1309
1419
  return /* @__PURE__ */ jsx6(
1310
1420
  ElementErrorBoundary,
1311
1421
  {
1312
1422
  elementType: resolvedElement.type,
1423
+ elementKey,
1313
1424
  registry,
1314
1425
  resetKey: signatures[elementKey ?? ""],
1315
1426
  children: tagged
1316
1427
  }
1317
1428
  );
1318
1429
  }
1319
- var ElementRenderer = React5.memo(
1430
+ var ElementRenderer = React7.memo(
1320
1431
  function ElementRenderer2(props) {
1321
1432
  return /* @__PURE__ */ jsx6(ReactiveElementRenderer, { ...props });
1322
1433
  },
@@ -1345,13 +1456,21 @@ function RepeatChildren({
1345
1456
  );
1346
1457
  return null;
1347
1458
  }
1348
- const items = getByPath2(state, statePath) ?? [];
1459
+ const raw = getByPath2(state, statePath);
1460
+ const items = Array.isArray(raw) ? raw : [];
1349
1461
  const entries = items.map((itemValue, index) => ({ itemValue, index })).filter(
1350
- ({ itemValue, index }) => itemFilter === void 0 || evaluateVisibility2(itemFilter, {
1351
- ...ctx,
1352
- repeatItem: itemValue,
1353
- repeatIndex: index
1354
- })
1462
+ ({ itemValue, index }) => itemFilter === void 0 || failsAs(
1463
+ "visible",
1464
+ void 0,
1465
+ element.type,
1466
+ evaluateVisibility2,
1467
+ itemFilter,
1468
+ {
1469
+ ...ctx,
1470
+ repeatItem: itemValue,
1471
+ repeatIndex: index
1472
+ }
1473
+ )
1355
1474
  );
1356
1475
  return /* @__PURE__ */ jsx6(Fragment, { children: entries.map(({ itemValue, index }) => {
1357
1476
  const key = repeat.key && typeof itemValue === "object" && itemValue !== null ? String(
@@ -1401,6 +1520,7 @@ function Renderer({
1401
1520
  }) {
1402
1521
  const rendererRegistry = useRenderErrorRegistry(registry, onRenderError);
1403
1522
  const signatures = useElementSignatures(spec);
1523
+ useMissingRootWarning(spec, loading);
1404
1524
  if (!spec || !spec.root) {
1405
1525
  return null;
1406
1526
  }
@@ -1409,15 +1529,25 @@ function Renderer({
1409
1529
  return null;
1410
1530
  }
1411
1531
  return /* @__PURE__ */ jsx6(
1412
- ElementRenderer,
1532
+ RootErrorBoundary,
1413
1533
  {
1414
- element: rootElement,
1415
- elementKey: spec.root,
1416
- spec,
1417
1534
  registry: rendererRegistry,
1418
- loading,
1419
- fallback,
1420
- signatures
1535
+ rootKey: spec.root,
1536
+ type: rootElement.type,
1537
+ resetKey: `${spec.root}:${signatures[spec.root]}`,
1538
+ children: /* @__PURE__ */ jsx6(
1539
+ ElementRenderer,
1540
+ {
1541
+ element: rootElement,
1542
+ elementKey: spec.root,
1543
+ spec,
1544
+ registry: rendererRegistry,
1545
+ loading,
1546
+ fallback,
1547
+ signatures,
1548
+ isRoot: true
1549
+ }
1550
+ )
1421
1551
  }
1422
1552
  );
1423
1553
  }
@@ -1433,7 +1563,7 @@ function JSONUIProvider({
1433
1563
  onStateChange,
1434
1564
  children
1435
1565
  }) {
1436
- const directiveRegistry = useMemo5(
1566
+ const directiveRegistry = useMemo8(
1437
1567
  () => directives ? createDirectiveRegistry(directives) : void 0,
1438
1568
  [directives]
1439
1569
  );
@@ -1534,7 +1664,7 @@ function createRenderer(catalog, components) {
1534
1664
  fallback,
1535
1665
  onRenderError
1536
1666
  }) {
1537
- const directiveRegistry = useMemo5(
1667
+ const directiveRegistry = useMemo8(
1538
1668
  () => directives ? createDirectiveRegistry(directives) : void 0,
1539
1669
  [directives]
1540
1670
  );
@@ -1572,7 +1702,7 @@ function createRenderer(catalog, components) {
1572
1702
  }
1573
1703
 
1574
1704
  // src/hooks.ts
1575
- import { useState as useState3, useCallback as useCallback5, useRef as useRef5, useEffect as useEffect3 } from "react";
1705
+ import { useState as useState5, useCallback as useCallback7, useRef as useRef9, useEffect as useEffect5 } from "react";
1576
1706
  import {
1577
1707
  setByPath,
1578
1708
  getByPath as getByPath3,
@@ -1720,21 +1850,21 @@ function useUIStream({
1720
1850
  onComplete,
1721
1851
  onError
1722
1852
  }) {
1723
- const [spec, setSpec] = useState3(null);
1724
- const [isStreaming, setIsStreaming] = useState3(false);
1725
- const [error, setError] = useState3(null);
1726
- const [usage, setUsage] = useState3(null);
1727
- const [rawLines, setRawLines] = useState3([]);
1728
- const abortControllerRef = useRef5(null);
1729
- const onCompleteRef = useRef5(onComplete);
1853
+ const [spec, setSpec] = useState5(null);
1854
+ const [isStreaming, setIsStreaming] = useState5(false);
1855
+ const [error, setError] = useState5(null);
1856
+ const [usage, setUsage] = useState5(null);
1857
+ const [rawLines, setRawLines] = useState5([]);
1858
+ const abortControllerRef = useRef9(null);
1859
+ const onCompleteRef = useRef9(onComplete);
1730
1860
  onCompleteRef.current = onComplete;
1731
- const onErrorRef = useRef5(onError);
1861
+ const onErrorRef = useRef9(onError);
1732
1862
  onErrorRef.current = onError;
1733
- const clear = useCallback5(() => {
1863
+ const clear = useCallback7(() => {
1734
1864
  setSpec(null);
1735
1865
  setError(null);
1736
1866
  }, []);
1737
- const send = useCallback5(
1867
+ const send = useCallback7(
1738
1868
  async (prompt, context) => {
1739
1869
  abortControllerRef.current?.abort();
1740
1870
  abortControllerRef.current = new AbortController();
@@ -1822,7 +1952,7 @@ function useUIStream({
1822
1952
  },
1823
1953
  [api]
1824
1954
  );
1825
- useEffect3(() => {
1955
+ useEffect5(() => {
1826
1956
  return () => {
1827
1957
  abortControllerRef.current?.abort();
1828
1958
  };
@@ -1865,7 +1995,7 @@ function flatToTree(elements) {
1865
1995
  }
1866
1996
  function useBoundProp(propValue, bindingPath) {
1867
1997
  const { set } = useStateStore();
1868
- const setValue = useCallback5(
1998
+ const setValue = useCallback7(
1869
1999
  (value) => {
1870
2000
  if (bindingPath) set(bindingPath, value);
1871
2001
  },
@@ -1914,8 +2044,8 @@ function getTextFromParts(parts) {
1914
2044
  ).map((p) => p.text.trim()).filter(Boolean).join("\n\n");
1915
2045
  }
1916
2046
  function useJsonRenderMessage(parts) {
1917
- const prevPartsRef = useRef5([]);
1918
- const prevResultRef = useRef5({
2047
+ const prevPartsRef = useRef9([]);
2048
+ const prevResultRef = useRef9({
1919
2049
  spec: null,
1920
2050
  text: ""
1921
2051
  });
@@ -1944,21 +2074,21 @@ function useChatUI({
1944
2074
  onComplete,
1945
2075
  onError
1946
2076
  }) {
1947
- const [messages, setMessages] = useState3([]);
1948
- const [isStreaming, setIsStreaming] = useState3(false);
1949
- const [error, setError] = useState3(null);
1950
- const abortControllerRef = useRef5(null);
1951
- const messagesRef = useRef5(messages);
2077
+ const [messages, setMessages] = useState5([]);
2078
+ const [isStreaming, setIsStreaming] = useState5(false);
2079
+ const [error, setError] = useState5(null);
2080
+ const abortControllerRef = useRef9(null);
2081
+ const messagesRef = useRef9(messages);
1952
2082
  messagesRef.current = messages;
1953
- const onCompleteRef = useRef5(onComplete);
2083
+ const onCompleteRef = useRef9(onComplete);
1954
2084
  onCompleteRef.current = onComplete;
1955
- const onErrorRef = useRef5(onError);
2085
+ const onErrorRef = useRef9(onError);
1956
2086
  onErrorRef.current = onError;
1957
- const clear = useCallback5(() => {
2087
+ const clear = useCallback7(() => {
1958
2088
  setMessages([]);
1959
2089
  setError(null);
1960
2090
  }, []);
1961
- const send = useCallback5(
2091
+ const send = useCallback7(
1962
2092
  async (text) => {
1963
2093
  if (!text.trim()) return;
1964
2094
  abortControllerRef.current?.abort();
@@ -2073,7 +2203,7 @@ function useChatUI({
2073
2203
  },
2074
2204
  [api]
2075
2205
  );
2076
- useEffect3(() => {
2206
+ useEffect5(() => {
2077
2207
  return () => {
2078
2208
  abortControllerRef.current?.abort();
2079
2209
  };