@xmachines/json-render-react 0.20.0-xm.4 → 0.21.0-xm.2

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
@@ -208,6 +208,7 @@ import {
208
208
  } from "@xmachines/json-render-core";
209
209
  import { jsx as jsx3 } from "react/jsx-runtime";
210
210
  var ValidationContext = createContext3(null);
211
+ var EMPTY_VALIDATION_FUNCTIONS = {};
211
212
  function dynamicArgsEqual(a, b) {
212
213
  if (a === b) return true;
213
214
  if (!a || !b) return false;
@@ -243,7 +244,7 @@ function validationConfigEqual(a, b) {
243
244
  return true;
244
245
  }
245
246
  function ValidationProvider({
246
- customFunctions = {},
247
+ customFunctions = EMPTY_VALIDATION_FUNCTIONS,
247
248
  children
248
249
  }) {
249
250
  const { state, getSnapshot } = useStateStore();
@@ -817,10 +818,13 @@ import {
817
818
  getByPath as getByPath2,
818
819
  isDevtoolsActive,
819
820
  subscribeDevtoolsActive,
820
- createDirectiveRegistry
821
+ createDirectiveRegistry,
822
+ copySlotMetadata,
823
+ setCatalogSlotMetadata,
824
+ warnElementSlots
821
825
  } from "@xmachines/json-render-core";
822
826
  import { Fragment, jsx as jsx6, jsxs as jsxs2 } from "react/jsx-runtime";
823
- var registryMetadata = /* @__PURE__ */ new WeakMap();
827
+ var EMPTY_ELEMENT_PROPS = {};
824
828
  function withRenderErrorHandler(registry, onRenderError) {
825
829
  const next = { ...registry };
826
830
  Object.defineProperty(next, "onRenderError", {
@@ -828,10 +832,20 @@ function withRenderErrorHandler(registry, onRenderError) {
828
832
  enumerable: false,
829
833
  configurable: true
830
834
  });
831
- const metadata = registryMetadata.get(registry);
832
- if (metadata) registryMetadata.set(next, metadata);
835
+ copySlotMetadata(registry, next);
833
836
  return next;
834
837
  }
838
+ function useRenderErrorRegistry(registry, onRenderError) {
839
+ const handlerRef = useRef4(onRenderError);
840
+ handlerRef.current = onRenderError;
841
+ const hasHandler = onRenderError !== void 0;
842
+ return useMemo5(
843
+ () => hasHandler ? withRenderErrorHandler(registry, (error, elementType) => {
844
+ handlerRef.current?.(error, elementType);
845
+ }) : registry,
846
+ [registry, hasHandler]
847
+ );
848
+ }
835
849
  function reportRenderError(registry, elementType, error, componentStack) {
836
850
  const onRenderError = registry?.onRenderError;
837
851
  if (typeof onRenderError === "function") {
@@ -860,6 +874,11 @@ var ElementErrorBoundary = class extends React5.Component {
860
874
  info.componentStack
861
875
  );
862
876
  }
877
+ componentDidUpdate(previous) {
878
+ if (this.state.hasError && previous.resetKey !== this.props.resetKey) {
879
+ this.setState({ hasError: false });
880
+ }
881
+ }
863
882
  render() {
864
883
  if (this.state.hasError) {
865
884
  return null;
@@ -878,6 +897,171 @@ var DirectivesContext = React5.createContext(
878
897
  function useDirectives() {
879
898
  return React5.useContext(DirectivesContext);
880
899
  }
900
+ function stabilizeRecord(value, ref) {
901
+ const previous = ref.current;
902
+ if (previous === value) return previous;
903
+ if (previous && value) {
904
+ const keys = Object.keys(value);
905
+ if (keys.length === Object.keys(previous).length && keys.every(
906
+ (key) => Object.prototype.hasOwnProperty.call(previous, key) && previous[key] === value[key]
907
+ )) {
908
+ return previous;
909
+ }
910
+ }
911
+ ref.current = value;
912
+ return value;
913
+ }
914
+ function isPlainRecord(value) {
915
+ if (value === null || typeof value !== "object") return false;
916
+ const prototype = Object.getPrototypeOf(value);
917
+ return prototype === Object.prototype || prototype === null;
918
+ }
919
+ function structurallyEqual(previous, next) {
920
+ if (Object.is(previous, next)) return true;
921
+ const previousIsArray = Array.isArray(previous);
922
+ if (previousIsArray !== Array.isArray(next)) return false;
923
+ if (!previousIsArray && (previous === null || next === null || typeof previous !== "object" || typeof next !== "object")) {
924
+ return false;
925
+ }
926
+ const previousRecord = previous;
927
+ const nextRecord = next;
928
+ const keys = Object.keys(nextRecord);
929
+ if (previousIsArray && previous.length !== next.length || keys.length !== Object.keys(previousRecord).length) {
930
+ return false;
931
+ }
932
+ return keys.every(
933
+ (key) => Object.prototype.hasOwnProperty.call(previousRecord, key) && structurallyEqual(previousRecord[key], nextRecord[key])
934
+ );
935
+ }
936
+ function snapshotStructuralValue(value) {
937
+ if (value === null || typeof value !== "object") return value;
938
+ const snapshot = Array.isArray(value) ? new Array(value.length) : {};
939
+ for (const key of Object.keys(value)) {
940
+ snapshot[key] = snapshotStructuralValue(
941
+ value[key]
942
+ );
943
+ }
944
+ return snapshot;
945
+ }
946
+ function shareResolvedValue(previous, next) {
947
+ if (Object.is(previous, next)) return previous;
948
+ const previousIsArray = Array.isArray(previous);
949
+ if (previousIsArray !== Array.isArray(next)) return next;
950
+ if (!previousIsArray && (!isPlainRecord(previous) || !isPlainRecord(next))) {
951
+ return next;
952
+ }
953
+ const previousRecord = previous;
954
+ const nextRecord = next;
955
+ const keys = Object.keys(nextRecord);
956
+ const shared = previousIsArray ? new Array(next.length) : {};
957
+ let unchanged = (!previousIsArray || previous.length === next.length) && keys.length === Object.keys(previousRecord).length;
958
+ for (const key of keys) {
959
+ let value = nextRecord[key];
960
+ if (Object.prototype.hasOwnProperty.call(previousRecord, key)) {
961
+ value = shareResolvedValue(previousRecord[key], value);
962
+ if (!Object.is(previousRecord[key], value)) unchanged = false;
963
+ } else {
964
+ unchanged = false;
965
+ }
966
+ shared[key] = value;
967
+ }
968
+ return unchanged ? previous : shared;
969
+ }
970
+ function useElementSignatures(spec) {
971
+ const entriesRef = useRef4({});
972
+ const versionRef = useRef4(0);
973
+ if (!spec) return {};
974
+ const previous = entriesRef.current;
975
+ const next = {};
976
+ const signatures = {};
977
+ const visiting = /* @__PURE__ */ new Set();
978
+ for (const key of Object.keys(spec.elements)) {
979
+ if (signatures[key] !== void 0) continue;
980
+ const element = spec.elements[key];
981
+ if (!element) continue;
982
+ visiting.add(key);
983
+ const stack = [
984
+ {
985
+ key,
986
+ element,
987
+ children: [
988
+ ...element.children ?? [],
989
+ ...Object.values(element.slots ?? {}).flat()
990
+ ],
991
+ childIndex: 0,
992
+ childVersions: []
993
+ }
994
+ ];
995
+ while (stack.length > 0) {
996
+ const frame = stack[stack.length - 1];
997
+ const childKey = frame.children[frame.childIndex];
998
+ if (childKey !== void 0) {
999
+ const childVersion = signatures[childKey];
1000
+ if (childVersion !== void 0) {
1001
+ frame.childVersions.push([childKey, childVersion]);
1002
+ frame.childIndex += 1;
1003
+ continue;
1004
+ }
1005
+ const childElement = spec.elements[childKey];
1006
+ if (!childElement || visiting.has(childKey)) {
1007
+ frame.childVersions.push([childKey, -1]);
1008
+ frame.childIndex += 1;
1009
+ continue;
1010
+ }
1011
+ visiting.add(childKey);
1012
+ stack.push({
1013
+ key: childKey,
1014
+ element: childElement,
1015
+ children: [
1016
+ ...childElement.children ?? [],
1017
+ ...Object.values(childElement.slots ?? {}).flat()
1018
+ ],
1019
+ childIndex: 0,
1020
+ childVersions: []
1021
+ });
1022
+ continue;
1023
+ }
1024
+ const prior = previous[frame.key];
1025
+ const version = prior && structurallyEqual(prior.own, frame.element) && structurallyEqual(prior.children, frame.childVersions) ? prior.version : ++versionRef.current;
1026
+ visiting.delete(frame.key);
1027
+ next[frame.key] = {
1028
+ own: snapshotStructuralValue(frame.element),
1029
+ children: frame.childVersions,
1030
+ version
1031
+ };
1032
+ signatures[frame.key] = version;
1033
+ stack.pop();
1034
+ }
1035
+ }
1036
+ entriesRef.current = next;
1037
+ return signatures;
1038
+ }
1039
+ var CatalogComponentBoundary = React5.memo(
1040
+ function CatalogComponentBoundary2({
1041
+ Component,
1042
+ element,
1043
+ slots,
1044
+ emit,
1045
+ on,
1046
+ bindings,
1047
+ loading,
1048
+ children
1049
+ }) {
1050
+ return /* @__PURE__ */ jsx6(
1051
+ Component,
1052
+ {
1053
+ element,
1054
+ slots,
1055
+ emit,
1056
+ on,
1057
+ bindings,
1058
+ loading,
1059
+ children
1060
+ }
1061
+ );
1062
+ },
1063
+ (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
1064
+ );
881
1065
  function useDevtoolsActive() {
882
1066
  return React5.useSyncExternalStore(
883
1067
  subscribeDevtoolsActive,
@@ -885,13 +1069,14 @@ function useDevtoolsActive() {
885
1069
  () => false
886
1070
  );
887
1071
  }
888
- var ElementRenderer = React5.memo(function ElementRenderer2({
1072
+ function ReactiveElementRenderer({
889
1073
  element,
890
1074
  elementKey,
891
1075
  spec,
892
1076
  registry,
893
1077
  loading,
894
- fallback
1078
+ fallback,
1079
+ signatures
895
1080
  }) {
896
1081
  const devtoolsActive = useDevtoolsActive();
897
1082
  const repeatScope = useRepeatScope();
@@ -958,6 +1143,10 @@ var ElementRenderer = React5.memo(function ElementRenderer2({
958
1143
  const watchConfig = element.watch;
959
1144
  const prevWatchValues = useRef4(null);
960
1145
  const stableWatchRef = useRef4(void 0);
1146
+ const stableBindingsRef = useRef4(
1147
+ void 0
1148
+ );
1149
+ const stableResolvedPropsRef = useRef4({});
961
1150
  const watchedValues = useMemo5(() => {
962
1151
  if (!watchConfig) return void 0;
963
1152
  const values = {};
@@ -1017,30 +1206,23 @@ var ElementRenderer = React5.memo(function ElementRenderer2({
1017
1206
  if (!isVisible) {
1018
1207
  return null;
1019
1208
  }
1020
- const rawProps = element.props;
1021
- const elementBindings = resolveBindings(rawProps, fullCtx);
1022
- const resolvedProps = resolveElementProps(rawProps, fullCtx);
1209
+ const rawProps = element.props ?? EMPTY_ELEMENT_PROPS;
1210
+ const elementBindings = stabilizeRecord(
1211
+ resolveBindings(rawProps, fullCtx),
1212
+ stableBindingsRef
1213
+ );
1214
+ const resolvedProps = shareResolvedValue(
1215
+ stableResolvedPropsRef.current,
1216
+ resolveElementProps(rawProps, fullCtx)
1217
+ );
1218
+ stableResolvedPropsRef.current = resolvedProps;
1023
1219
  const resolvedElement = resolvedProps !== element.props ? { ...element, props: resolvedProps } : element;
1024
1220
  const Component = registry[resolvedElement.type] ?? fallback;
1025
1221
  if (!Component) {
1026
1222
  console.warn(`No renderer for component type: ${resolvedElement.type}`);
1027
1223
  return null;
1028
1224
  }
1029
- const metadata = registryMetadata.get(registry)?.[resolvedElement.type];
1030
- if (resolvedElement.slots && metadata?.slots) {
1031
- const availableSlots = new Set(metadata.slots);
1032
- for (const slotName of Object.keys(resolvedElement.slots)) {
1033
- if (slotName === "default") {
1034
- console.warn(
1035
- `[json-render] Component "${resolvedElement.type}" uses slots.default. Use "children" for default slot content.`
1036
- );
1037
- } else if (!availableSlots.has(slotName)) {
1038
- console.warn(
1039
- `[json-render] Unknown slot "${slotName}" on component "${resolvedElement.type}". Available slots: ${metadata.slots.join(", ")}`
1040
- );
1041
- }
1042
- }
1043
- }
1225
+ warnElementSlots(registry, resolvedElement.type, resolvedElement.slots);
1044
1226
  const renderChildKeys = (childKeys, slotName) => childKeys.map((childKey) => {
1045
1227
  const childElement = spec.elements[childKey];
1046
1228
  if (!childElement) {
@@ -1053,14 +1235,15 @@ var ElementRenderer = React5.memo(function ElementRenderer2({
1053
1235
  return null;
1054
1236
  }
1055
1237
  return /* @__PURE__ */ jsx6(
1056
- ElementRenderer2,
1238
+ ElementRenderer,
1057
1239
  {
1058
1240
  element: childElement,
1059
1241
  elementKey: childKey,
1060
1242
  spec,
1061
1243
  registry,
1062
1244
  loading,
1063
- fallback
1245
+ fallback,
1246
+ signatures
1064
1247
  },
1065
1248
  childKey
1066
1249
  );
@@ -1073,7 +1256,8 @@ var ElementRenderer = React5.memo(function ElementRenderer2({
1073
1256
  registry,
1074
1257
  loading,
1075
1258
  fallback,
1076
- itemFilter: repeatItemFilter
1259
+ itemFilter: repeatItemFilter,
1260
+ signatures
1077
1261
  }
1078
1262
  ) : resolvedElement.children ? renderChildKeys(resolvedElement.children) : void 0;
1079
1263
  const slots = resolvedElement.slots ? Object.fromEntries(
@@ -1083,8 +1267,14 @@ var ElementRenderer = React5.memo(function ElementRenderer2({
1083
1267
  ])
1084
1268
  ) : void 0;
1085
1269
  const rendered = /* @__PURE__ */ jsx6(
1086
- Component,
1270
+ CatalogComponentBoundary,
1087
1271
  {
1272
+ Component,
1273
+ signature: signatures[elementKey ?? ""],
1274
+ execute,
1275
+ actionContext: repeatScope,
1276
+ functions,
1277
+ directives,
1088
1278
  element: resolvedElement,
1089
1279
  slots,
1090
1280
  emit,
@@ -1100,16 +1290,24 @@ var ElementRenderer = React5.memo(function ElementRenderer2({
1100
1290
  {
1101
1291
  elementType: resolvedElement.type,
1102
1292
  registry,
1293
+ resetKey: signatures[elementKey ?? ""],
1103
1294
  children: tagged
1104
1295
  }
1105
1296
  );
1106
- });
1297
+ }
1298
+ var ElementRenderer = React5.memo(
1299
+ function ElementRenderer2(props) {
1300
+ return /* @__PURE__ */ jsx6(ReactiveElementRenderer, { ...props });
1301
+ },
1302
+ (previous, next) => previous.elementKey === next.elementKey && previous.signatures[previous.elementKey ?? ""] === next.signatures[next.elementKey ?? ""] && previous.registry === next.registry && previous.loading === next.loading && previous.fallback === next.fallback
1303
+ );
1107
1304
  function RepeatChildren({
1108
1305
  element,
1109
1306
  spec,
1110
1307
  registry,
1111
1308
  loading,
1112
1309
  fallback,
1310
+ signatures,
1113
1311
  itemFilter
1114
1312
  }) {
1115
1313
  const { state } = useStateStore();
@@ -1162,7 +1360,8 @@ function RepeatChildren({
1162
1360
  spec,
1163
1361
  registry,
1164
1362
  loading,
1165
- fallback
1363
+ fallback,
1364
+ signatures
1166
1365
  },
1167
1366
  childKey
1168
1367
  );
@@ -1179,10 +1378,8 @@ function Renderer({
1179
1378
  fallback,
1180
1379
  onRenderError
1181
1380
  }) {
1182
- const rendererRegistry = useMemo5(
1183
- () => onRenderError ? withRenderErrorHandler(registry, onRenderError) : registry,
1184
- [registry, onRenderError]
1185
- );
1381
+ const rendererRegistry = useRenderErrorRegistry(registry, onRenderError);
1382
+ const signatures = useElementSignatures(spec);
1186
1383
  if (!spec || !spec.root) {
1187
1384
  return null;
1188
1385
  }
@@ -1198,7 +1395,8 @@ function Renderer({
1198
1395
  spec,
1199
1396
  registry: rendererRegistry,
1200
1397
  loading,
1201
- fallback
1398
+ fallback,
1399
+ signatures
1202
1400
  }
1203
1401
  );
1204
1402
  }
@@ -1270,10 +1468,7 @@ function defineRegistry(catalog, options) {
1270
1468
  };
1271
1469
  }
1272
1470
  }
1273
- const catalogComponents = catalog.data.components;
1274
- if (catalogComponents) {
1275
- registryMetadata.set(registry, catalogComponents);
1276
- }
1471
+ setCatalogSlotMetadata(registry, catalog);
1277
1472
  const actionMap = options.actions ? Object.entries(options.actions) : [];
1278
1473
  const handlers = (getSetState, getState) => {
1279
1474
  const result = {};
@@ -1307,10 +1502,7 @@ function defineRegistry(catalog, options) {
1307
1502
  }
1308
1503
  function createRenderer(catalog, components) {
1309
1504
  const registry = components;
1310
- const catalogComponents = catalog.data.components;
1311
- if (catalogComponents) {
1312
- registryMetadata.set(registry, catalogComponents);
1313
- }
1505
+ setCatalogSlotMetadata(registry, catalog);
1314
1506
  return function CatalogRenderer({
1315
1507
  spec,
1316
1508
  store,
@@ -1327,10 +1519,7 @@ function createRenderer(catalog, components) {
1327
1519
  () => directives ? createDirectiveRegistry(directives) : void 0,
1328
1520
  [directives]
1329
1521
  );
1330
- const rendererRegistry = useMemo5(
1331
- () => onRenderError ? withRenderErrorHandler(registry, onRenderError) : registry,
1332
- [onRenderError]
1333
- );
1522
+ const rendererRegistry = useRenderErrorRegistry(registry, onRenderError);
1334
1523
  const actionHandlers = onAction ? new Proxy(
1335
1524
  {},
1336
1525
  {