@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.js CHANGED
@@ -230,6 +230,7 @@ var import_react3 = __toESM(require("react"));
230
230
  var import_core3 = require("@xmachines/json-render-core");
231
231
  var import_jsx_runtime3 = require("react/jsx-runtime");
232
232
  var ValidationContext = (0, import_react3.createContext)(null);
233
+ var EMPTY_VALIDATION_FUNCTIONS = {};
233
234
  function dynamicArgsEqual(a, b) {
234
235
  if (a === b) return true;
235
236
  if (!a || !b) return false;
@@ -265,7 +266,7 @@ function validationConfigEqual(a, b) {
265
266
  return true;
266
267
  }
267
268
  function ValidationProvider({
268
- customFunctions = {},
269
+ customFunctions = EMPTY_VALIDATION_FUNCTIONS,
269
270
  children
270
271
  }) {
271
272
  const { state, getSnapshot } = useStateStore();
@@ -913,7 +914,7 @@ var import_core8 = require("@xmachines/json-render-core");
913
914
  var import_react6 = __toESM(require("react"));
914
915
  var import_core6 = require("@xmachines/json-render-core");
915
916
  var import_jsx_runtime6 = require("react/jsx-runtime");
916
- var registryMetadata = /* @__PURE__ */ new WeakMap();
917
+ var EMPTY_ELEMENT_PROPS = {};
917
918
  function withRenderErrorHandler(registry, onRenderError) {
918
919
  const next = { ...registry };
919
920
  Object.defineProperty(next, "onRenderError", {
@@ -921,10 +922,20 @@ function withRenderErrorHandler(registry, onRenderError) {
921
922
  enumerable: false,
922
923
  configurable: true
923
924
  });
924
- const metadata = registryMetadata.get(registry);
925
- if (metadata) registryMetadata.set(next, metadata);
925
+ (0, import_core6.copySlotMetadata)(registry, next);
926
926
  return next;
927
927
  }
928
+ function useRenderErrorRegistry(registry, onRenderError) {
929
+ const handlerRef = (0, import_react6.useRef)(onRenderError);
930
+ handlerRef.current = onRenderError;
931
+ const hasHandler = onRenderError !== void 0;
932
+ return (0, import_react6.useMemo)(
933
+ () => hasHandler ? withRenderErrorHandler(registry, (error, elementType) => {
934
+ handlerRef.current?.(error, elementType);
935
+ }) : registry,
936
+ [registry, hasHandler]
937
+ );
938
+ }
928
939
  function reportRenderError(registry, elementType, error, componentStack) {
929
940
  const onRenderError = registry?.onRenderError;
930
941
  if (typeof onRenderError === "function") {
@@ -953,6 +964,11 @@ var ElementErrorBoundary = class extends import_react6.default.Component {
953
964
  info.componentStack
954
965
  );
955
966
  }
967
+ componentDidUpdate(previous) {
968
+ if (this.state.hasError && previous.resetKey !== this.props.resetKey) {
969
+ this.setState({ hasError: false });
970
+ }
971
+ }
956
972
  render() {
957
973
  if (this.state.hasError) {
958
974
  return null;
@@ -971,6 +987,171 @@ var DirectivesContext = import_react6.default.createContext(
971
987
  function useDirectives() {
972
988
  return import_react6.default.useContext(DirectivesContext);
973
989
  }
990
+ function stabilizeRecord(value, ref) {
991
+ const previous = ref.current;
992
+ if (previous === value) return previous;
993
+ if (previous && value) {
994
+ const keys = Object.keys(value);
995
+ if (keys.length === Object.keys(previous).length && keys.every(
996
+ (key) => Object.prototype.hasOwnProperty.call(previous, key) && previous[key] === value[key]
997
+ )) {
998
+ return previous;
999
+ }
1000
+ }
1001
+ ref.current = value;
1002
+ return value;
1003
+ }
1004
+ function isPlainRecord(value) {
1005
+ if (value === null || typeof value !== "object") return false;
1006
+ const prototype = Object.getPrototypeOf(value);
1007
+ return prototype === Object.prototype || prototype === null;
1008
+ }
1009
+ function structurallyEqual(previous, next) {
1010
+ if (Object.is(previous, next)) return true;
1011
+ const previousIsArray = Array.isArray(previous);
1012
+ if (previousIsArray !== Array.isArray(next)) return false;
1013
+ if (!previousIsArray && (previous === null || next === null || typeof previous !== "object" || typeof next !== "object")) {
1014
+ return false;
1015
+ }
1016
+ const previousRecord = previous;
1017
+ const nextRecord = next;
1018
+ const keys = Object.keys(nextRecord);
1019
+ if (previousIsArray && previous.length !== next.length || keys.length !== Object.keys(previousRecord).length) {
1020
+ return false;
1021
+ }
1022
+ return keys.every(
1023
+ (key) => Object.prototype.hasOwnProperty.call(previousRecord, key) && structurallyEqual(previousRecord[key], nextRecord[key])
1024
+ );
1025
+ }
1026
+ function snapshotStructuralValue(value) {
1027
+ if (value === null || typeof value !== "object") return value;
1028
+ const snapshot = Array.isArray(value) ? new Array(value.length) : {};
1029
+ for (const key of Object.keys(value)) {
1030
+ snapshot[key] = snapshotStructuralValue(
1031
+ value[key]
1032
+ );
1033
+ }
1034
+ return snapshot;
1035
+ }
1036
+ function shareResolvedValue(previous, next) {
1037
+ if (Object.is(previous, next)) return previous;
1038
+ const previousIsArray = Array.isArray(previous);
1039
+ if (previousIsArray !== Array.isArray(next)) return next;
1040
+ if (!previousIsArray && (!isPlainRecord(previous) || !isPlainRecord(next))) {
1041
+ return next;
1042
+ }
1043
+ const previousRecord = previous;
1044
+ const nextRecord = next;
1045
+ const keys = Object.keys(nextRecord);
1046
+ const shared = previousIsArray ? new Array(next.length) : {};
1047
+ let unchanged = (!previousIsArray || previous.length === next.length) && keys.length === Object.keys(previousRecord).length;
1048
+ for (const key of keys) {
1049
+ let value = nextRecord[key];
1050
+ if (Object.prototype.hasOwnProperty.call(previousRecord, key)) {
1051
+ value = shareResolvedValue(previousRecord[key], value);
1052
+ if (!Object.is(previousRecord[key], value)) unchanged = false;
1053
+ } else {
1054
+ unchanged = false;
1055
+ }
1056
+ shared[key] = value;
1057
+ }
1058
+ return unchanged ? previous : shared;
1059
+ }
1060
+ function useElementSignatures(spec) {
1061
+ const entriesRef = (0, import_react6.useRef)({});
1062
+ const versionRef = (0, import_react6.useRef)(0);
1063
+ if (!spec) return {};
1064
+ const previous = entriesRef.current;
1065
+ const next = {};
1066
+ const signatures = {};
1067
+ const visiting = /* @__PURE__ */ new Set();
1068
+ for (const key of Object.keys(spec.elements)) {
1069
+ if (signatures[key] !== void 0) continue;
1070
+ const element = spec.elements[key];
1071
+ if (!element) continue;
1072
+ visiting.add(key);
1073
+ const stack = [
1074
+ {
1075
+ key,
1076
+ element,
1077
+ children: [
1078
+ ...element.children ?? [],
1079
+ ...Object.values(element.slots ?? {}).flat()
1080
+ ],
1081
+ childIndex: 0,
1082
+ childVersions: []
1083
+ }
1084
+ ];
1085
+ while (stack.length > 0) {
1086
+ const frame = stack[stack.length - 1];
1087
+ const childKey = frame.children[frame.childIndex];
1088
+ if (childKey !== void 0) {
1089
+ const childVersion = signatures[childKey];
1090
+ if (childVersion !== void 0) {
1091
+ frame.childVersions.push([childKey, childVersion]);
1092
+ frame.childIndex += 1;
1093
+ continue;
1094
+ }
1095
+ const childElement = spec.elements[childKey];
1096
+ if (!childElement || visiting.has(childKey)) {
1097
+ frame.childVersions.push([childKey, -1]);
1098
+ frame.childIndex += 1;
1099
+ continue;
1100
+ }
1101
+ visiting.add(childKey);
1102
+ stack.push({
1103
+ key: childKey,
1104
+ element: childElement,
1105
+ children: [
1106
+ ...childElement.children ?? [],
1107
+ ...Object.values(childElement.slots ?? {}).flat()
1108
+ ],
1109
+ childIndex: 0,
1110
+ childVersions: []
1111
+ });
1112
+ continue;
1113
+ }
1114
+ const prior = previous[frame.key];
1115
+ const version = prior && structurallyEqual(prior.own, frame.element) && structurallyEqual(prior.children, frame.childVersions) ? prior.version : ++versionRef.current;
1116
+ visiting.delete(frame.key);
1117
+ next[frame.key] = {
1118
+ own: snapshotStructuralValue(frame.element),
1119
+ children: frame.childVersions,
1120
+ version
1121
+ };
1122
+ signatures[frame.key] = version;
1123
+ stack.pop();
1124
+ }
1125
+ }
1126
+ entriesRef.current = next;
1127
+ return signatures;
1128
+ }
1129
+ var CatalogComponentBoundary = import_react6.default.memo(
1130
+ function CatalogComponentBoundary2({
1131
+ Component,
1132
+ element,
1133
+ slots,
1134
+ emit,
1135
+ on,
1136
+ bindings,
1137
+ loading,
1138
+ children
1139
+ }) {
1140
+ return /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(
1141
+ Component,
1142
+ {
1143
+ element,
1144
+ slots,
1145
+ emit,
1146
+ on,
1147
+ bindings,
1148
+ loading,
1149
+ children
1150
+ }
1151
+ );
1152
+ },
1153
+ (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
1154
+ );
974
1155
  function useDevtoolsActive() {
975
1156
  return import_react6.default.useSyncExternalStore(
976
1157
  import_core6.subscribeDevtoolsActive,
@@ -978,13 +1159,14 @@ function useDevtoolsActive() {
978
1159
  () => false
979
1160
  );
980
1161
  }
981
- var ElementRenderer = import_react6.default.memo(function ElementRenderer2({
1162
+ function ReactiveElementRenderer({
982
1163
  element,
983
1164
  elementKey,
984
1165
  spec,
985
1166
  registry,
986
1167
  loading,
987
- fallback
1168
+ fallback,
1169
+ signatures
988
1170
  }) {
989
1171
  const devtoolsActive = useDevtoolsActive();
990
1172
  const repeatScope = useRepeatScope();
@@ -1051,6 +1233,10 @@ var ElementRenderer = import_react6.default.memo(function ElementRenderer2({
1051
1233
  const watchConfig = element.watch;
1052
1234
  const prevWatchValues = (0, import_react6.useRef)(null);
1053
1235
  const stableWatchRef = (0, import_react6.useRef)(void 0);
1236
+ const stableBindingsRef = (0, import_react6.useRef)(
1237
+ void 0
1238
+ );
1239
+ const stableResolvedPropsRef = (0, import_react6.useRef)({});
1054
1240
  const watchedValues = (0, import_react6.useMemo)(() => {
1055
1241
  if (!watchConfig) return void 0;
1056
1242
  const values = {};
@@ -1110,30 +1296,23 @@ var ElementRenderer = import_react6.default.memo(function ElementRenderer2({
1110
1296
  if (!isVisible) {
1111
1297
  return null;
1112
1298
  }
1113
- const rawProps = element.props;
1114
- const elementBindings = (0, import_core6.resolveBindings)(rawProps, fullCtx);
1115
- const resolvedProps = (0, import_core6.resolveElementProps)(rawProps, fullCtx);
1299
+ const rawProps = element.props ?? EMPTY_ELEMENT_PROPS;
1300
+ const elementBindings = stabilizeRecord(
1301
+ (0, import_core6.resolveBindings)(rawProps, fullCtx),
1302
+ stableBindingsRef
1303
+ );
1304
+ const resolvedProps = shareResolvedValue(
1305
+ stableResolvedPropsRef.current,
1306
+ (0, import_core6.resolveElementProps)(rawProps, fullCtx)
1307
+ );
1308
+ stableResolvedPropsRef.current = resolvedProps;
1116
1309
  const resolvedElement = resolvedProps !== element.props ? { ...element, props: resolvedProps } : element;
1117
1310
  const Component = registry[resolvedElement.type] ?? fallback;
1118
1311
  if (!Component) {
1119
1312
  console.warn(`No renderer for component type: ${resolvedElement.type}`);
1120
1313
  return null;
1121
1314
  }
1122
- const metadata = registryMetadata.get(registry)?.[resolvedElement.type];
1123
- if (resolvedElement.slots && metadata?.slots) {
1124
- const availableSlots = new Set(metadata.slots);
1125
- for (const slotName of Object.keys(resolvedElement.slots)) {
1126
- if (slotName === "default") {
1127
- console.warn(
1128
- `[json-render] Component "${resolvedElement.type}" uses slots.default. Use "children" for default slot content.`
1129
- );
1130
- } else if (!availableSlots.has(slotName)) {
1131
- console.warn(
1132
- `[json-render] Unknown slot "${slotName}" on component "${resolvedElement.type}". Available slots: ${metadata.slots.join(", ")}`
1133
- );
1134
- }
1135
- }
1136
- }
1315
+ (0, import_core6.warnElementSlots)(registry, resolvedElement.type, resolvedElement.slots);
1137
1316
  const renderChildKeys = (childKeys, slotName) => childKeys.map((childKey) => {
1138
1317
  const childElement = spec.elements[childKey];
1139
1318
  if (!childElement) {
@@ -1146,14 +1325,15 @@ var ElementRenderer = import_react6.default.memo(function ElementRenderer2({
1146
1325
  return null;
1147
1326
  }
1148
1327
  return /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(
1149
- ElementRenderer2,
1328
+ ElementRenderer,
1150
1329
  {
1151
1330
  element: childElement,
1152
1331
  elementKey: childKey,
1153
1332
  spec,
1154
1333
  registry,
1155
1334
  loading,
1156
- fallback
1335
+ fallback,
1336
+ signatures
1157
1337
  },
1158
1338
  childKey
1159
1339
  );
@@ -1166,7 +1346,8 @@ var ElementRenderer = import_react6.default.memo(function ElementRenderer2({
1166
1346
  registry,
1167
1347
  loading,
1168
1348
  fallback,
1169
- itemFilter: repeatItemFilter
1349
+ itemFilter: repeatItemFilter,
1350
+ signatures
1170
1351
  }
1171
1352
  ) : resolvedElement.children ? renderChildKeys(resolvedElement.children) : void 0;
1172
1353
  const slots = resolvedElement.slots ? Object.fromEntries(
@@ -1176,8 +1357,14 @@ var ElementRenderer = import_react6.default.memo(function ElementRenderer2({
1176
1357
  ])
1177
1358
  ) : void 0;
1178
1359
  const rendered = /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(
1179
- Component,
1360
+ CatalogComponentBoundary,
1180
1361
  {
1362
+ Component,
1363
+ signature: signatures[elementKey ?? ""],
1364
+ execute,
1365
+ actionContext: repeatScope,
1366
+ functions,
1367
+ directives,
1181
1368
  element: resolvedElement,
1182
1369
  slots,
1183
1370
  emit,
@@ -1193,16 +1380,24 @@ var ElementRenderer = import_react6.default.memo(function ElementRenderer2({
1193
1380
  {
1194
1381
  elementType: resolvedElement.type,
1195
1382
  registry,
1383
+ resetKey: signatures[elementKey ?? ""],
1196
1384
  children: tagged
1197
1385
  }
1198
1386
  );
1199
- });
1387
+ }
1388
+ var ElementRenderer = import_react6.default.memo(
1389
+ function ElementRenderer2(props) {
1390
+ return /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(ReactiveElementRenderer, { ...props });
1391
+ },
1392
+ (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
1393
+ );
1200
1394
  function RepeatChildren({
1201
1395
  element,
1202
1396
  spec,
1203
1397
  registry,
1204
1398
  loading,
1205
1399
  fallback,
1400
+ signatures,
1206
1401
  itemFilter
1207
1402
  }) {
1208
1403
  const { state } = useStateStore();
@@ -1255,7 +1450,8 @@ function RepeatChildren({
1255
1450
  spec,
1256
1451
  registry,
1257
1452
  loading,
1258
- fallback
1453
+ fallback,
1454
+ signatures
1259
1455
  },
1260
1456
  childKey
1261
1457
  );
@@ -1272,10 +1468,8 @@ function Renderer({
1272
1468
  fallback,
1273
1469
  onRenderError
1274
1470
  }) {
1275
- const rendererRegistry = (0, import_react6.useMemo)(
1276
- () => onRenderError ? withRenderErrorHandler(registry, onRenderError) : registry,
1277
- [registry, onRenderError]
1278
- );
1471
+ const rendererRegistry = useRenderErrorRegistry(registry, onRenderError);
1472
+ const signatures = useElementSignatures(spec);
1279
1473
  if (!spec || !spec.root) {
1280
1474
  return null;
1281
1475
  }
@@ -1291,7 +1485,8 @@ function Renderer({
1291
1485
  spec,
1292
1486
  registry: rendererRegistry,
1293
1487
  loading,
1294
- fallback
1488
+ fallback,
1489
+ signatures
1295
1490
  }
1296
1491
  );
1297
1492
  }
@@ -1363,10 +1558,7 @@ function defineRegistry(catalog, options) {
1363
1558
  };
1364
1559
  }
1365
1560
  }
1366
- const catalogComponents = catalog.data.components;
1367
- if (catalogComponents) {
1368
- registryMetadata.set(registry, catalogComponents);
1369
- }
1561
+ (0, import_core6.setCatalogSlotMetadata)(registry, catalog);
1370
1562
  const actionMap = options.actions ? Object.entries(options.actions) : [];
1371
1563
  const handlers = (getSetState, getState) => {
1372
1564
  const result = {};
@@ -1400,10 +1592,7 @@ function defineRegistry(catalog, options) {
1400
1592
  }
1401
1593
  function createRenderer(catalog, components) {
1402
1594
  const registry = components;
1403
- const catalogComponents = catalog.data.components;
1404
- if (catalogComponents) {
1405
- registryMetadata.set(registry, catalogComponents);
1406
- }
1595
+ (0, import_core6.setCatalogSlotMetadata)(registry, catalog);
1407
1596
  return function CatalogRenderer({
1408
1597
  spec,
1409
1598
  store,
@@ -1420,10 +1609,7 @@ function createRenderer(catalog, components) {
1420
1609
  () => directives ? (0, import_core6.createDirectiveRegistry)(directives) : void 0,
1421
1610
  [directives]
1422
1611
  );
1423
- const rendererRegistry = (0, import_react6.useMemo)(
1424
- () => onRenderError ? withRenderErrorHandler(registry, onRenderError) : registry,
1425
- [onRenderError]
1426
- );
1612
+ const rendererRegistry = useRenderErrorRegistry(registry, onRenderError);
1427
1613
  const actionHandlers = onAction ? new Proxy(
1428
1614
  {},
1429
1615
  {