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

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();
@@ -914,6 +915,7 @@ 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
917
  var registryMetadata = /* @__PURE__ */ new WeakMap();
918
+ var EMPTY_ELEMENT_PROPS = {};
917
919
  function withRenderErrorHandler(registry, onRenderError) {
918
920
  const next = { ...registry };
919
921
  Object.defineProperty(next, "onRenderError", {
@@ -925,6 +927,17 @@ function withRenderErrorHandler(registry, onRenderError) {
925
927
  if (metadata) registryMetadata.set(next, metadata);
926
928
  return next;
927
929
  }
930
+ function useRenderErrorRegistry(registry, onRenderError) {
931
+ const handlerRef = (0, import_react6.useRef)(onRenderError);
932
+ handlerRef.current = onRenderError;
933
+ const hasHandler = onRenderError !== void 0;
934
+ return (0, import_react6.useMemo)(
935
+ () => hasHandler ? withRenderErrorHandler(registry, (error, elementType) => {
936
+ handlerRef.current?.(error, elementType);
937
+ }) : registry,
938
+ [registry, hasHandler]
939
+ );
940
+ }
928
941
  function reportRenderError(registry, elementType, error, componentStack) {
929
942
  const onRenderError = registry?.onRenderError;
930
943
  if (typeof onRenderError === "function") {
@@ -953,6 +966,11 @@ var ElementErrorBoundary = class extends import_react6.default.Component {
953
966
  info.componentStack
954
967
  );
955
968
  }
969
+ componentDidUpdate(previous) {
970
+ if (this.state.hasError && previous.resetKey !== this.props.resetKey) {
971
+ this.setState({ hasError: false });
972
+ }
973
+ }
956
974
  render() {
957
975
  if (this.state.hasError) {
958
976
  return null;
@@ -971,6 +989,171 @@ var DirectivesContext = import_react6.default.createContext(
971
989
  function useDirectives() {
972
990
  return import_react6.default.useContext(DirectivesContext);
973
991
  }
992
+ function stabilizeRecord(value, ref) {
993
+ const previous = ref.current;
994
+ if (previous === value) return previous;
995
+ if (previous && value) {
996
+ const keys = Object.keys(value);
997
+ if (keys.length === Object.keys(previous).length && keys.every(
998
+ (key) => Object.prototype.hasOwnProperty.call(previous, key) && previous[key] === value[key]
999
+ )) {
1000
+ return previous;
1001
+ }
1002
+ }
1003
+ ref.current = value;
1004
+ return value;
1005
+ }
1006
+ function isPlainRecord(value) {
1007
+ if (value === null || typeof value !== "object") return false;
1008
+ const prototype = Object.getPrototypeOf(value);
1009
+ return prototype === Object.prototype || prototype === null;
1010
+ }
1011
+ function structurallyEqual(previous, next) {
1012
+ if (Object.is(previous, next)) return true;
1013
+ const previousIsArray = Array.isArray(previous);
1014
+ if (previousIsArray !== Array.isArray(next)) return false;
1015
+ if (!previousIsArray && (previous === null || next === null || typeof previous !== "object" || typeof next !== "object")) {
1016
+ return false;
1017
+ }
1018
+ const previousRecord = previous;
1019
+ const nextRecord = next;
1020
+ const keys = Object.keys(nextRecord);
1021
+ if (previousIsArray && previous.length !== next.length || keys.length !== Object.keys(previousRecord).length) {
1022
+ return false;
1023
+ }
1024
+ return keys.every(
1025
+ (key) => Object.prototype.hasOwnProperty.call(previousRecord, key) && structurallyEqual(previousRecord[key], nextRecord[key])
1026
+ );
1027
+ }
1028
+ function snapshotStructuralValue(value) {
1029
+ if (value === null || typeof value !== "object") return value;
1030
+ const snapshot = Array.isArray(value) ? new Array(value.length) : {};
1031
+ for (const key of Object.keys(value)) {
1032
+ snapshot[key] = snapshotStructuralValue(
1033
+ value[key]
1034
+ );
1035
+ }
1036
+ return snapshot;
1037
+ }
1038
+ function shareResolvedValue(previous, next) {
1039
+ if (Object.is(previous, next)) return previous;
1040
+ const previousIsArray = Array.isArray(previous);
1041
+ if (previousIsArray !== Array.isArray(next)) return next;
1042
+ if (!previousIsArray && (!isPlainRecord(previous) || !isPlainRecord(next))) {
1043
+ return next;
1044
+ }
1045
+ const previousRecord = previous;
1046
+ const nextRecord = next;
1047
+ const keys = Object.keys(nextRecord);
1048
+ const shared = previousIsArray ? new Array(next.length) : {};
1049
+ let unchanged = (!previousIsArray || previous.length === next.length) && keys.length === Object.keys(previousRecord).length;
1050
+ for (const key of keys) {
1051
+ let value = nextRecord[key];
1052
+ if (Object.prototype.hasOwnProperty.call(previousRecord, key)) {
1053
+ value = shareResolvedValue(previousRecord[key], value);
1054
+ if (!Object.is(previousRecord[key], value)) unchanged = false;
1055
+ } else {
1056
+ unchanged = false;
1057
+ }
1058
+ shared[key] = value;
1059
+ }
1060
+ return unchanged ? previous : shared;
1061
+ }
1062
+ function useElementSignatures(spec) {
1063
+ const entriesRef = (0, import_react6.useRef)({});
1064
+ const versionRef = (0, import_react6.useRef)(0);
1065
+ if (!spec) return {};
1066
+ const previous = entriesRef.current;
1067
+ const next = {};
1068
+ const signatures = {};
1069
+ const visiting = /* @__PURE__ */ new Set();
1070
+ for (const key of Object.keys(spec.elements)) {
1071
+ if (signatures[key] !== void 0) continue;
1072
+ const element = spec.elements[key];
1073
+ if (!element) continue;
1074
+ visiting.add(key);
1075
+ const stack = [
1076
+ {
1077
+ key,
1078
+ element,
1079
+ children: [
1080
+ ...element.children ?? [],
1081
+ ...Object.values(element.slots ?? {}).flat()
1082
+ ],
1083
+ childIndex: 0,
1084
+ childVersions: []
1085
+ }
1086
+ ];
1087
+ while (stack.length > 0) {
1088
+ const frame = stack[stack.length - 1];
1089
+ const childKey = frame.children[frame.childIndex];
1090
+ if (childKey !== void 0) {
1091
+ const childVersion = signatures[childKey];
1092
+ if (childVersion !== void 0) {
1093
+ frame.childVersions.push([childKey, childVersion]);
1094
+ frame.childIndex += 1;
1095
+ continue;
1096
+ }
1097
+ const childElement = spec.elements[childKey];
1098
+ if (!childElement || visiting.has(childKey)) {
1099
+ frame.childVersions.push([childKey, -1]);
1100
+ frame.childIndex += 1;
1101
+ continue;
1102
+ }
1103
+ visiting.add(childKey);
1104
+ stack.push({
1105
+ key: childKey,
1106
+ element: childElement,
1107
+ children: [
1108
+ ...childElement.children ?? [],
1109
+ ...Object.values(childElement.slots ?? {}).flat()
1110
+ ],
1111
+ childIndex: 0,
1112
+ childVersions: []
1113
+ });
1114
+ continue;
1115
+ }
1116
+ const prior = previous[frame.key];
1117
+ const version = prior && structurallyEqual(prior.own, frame.element) && structurallyEqual(prior.children, frame.childVersions) ? prior.version : ++versionRef.current;
1118
+ visiting.delete(frame.key);
1119
+ next[frame.key] = {
1120
+ own: snapshotStructuralValue(frame.element),
1121
+ children: frame.childVersions,
1122
+ version
1123
+ };
1124
+ signatures[frame.key] = version;
1125
+ stack.pop();
1126
+ }
1127
+ }
1128
+ entriesRef.current = next;
1129
+ return signatures;
1130
+ }
1131
+ var CatalogComponentBoundary = import_react6.default.memo(
1132
+ function CatalogComponentBoundary2({
1133
+ Component,
1134
+ element,
1135
+ slots,
1136
+ emit,
1137
+ on,
1138
+ bindings,
1139
+ loading,
1140
+ children
1141
+ }) {
1142
+ return /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(
1143
+ Component,
1144
+ {
1145
+ element,
1146
+ slots,
1147
+ emit,
1148
+ on,
1149
+ bindings,
1150
+ loading,
1151
+ children
1152
+ }
1153
+ );
1154
+ },
1155
+ (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
1156
+ );
974
1157
  function useDevtoolsActive() {
975
1158
  return import_react6.default.useSyncExternalStore(
976
1159
  import_core6.subscribeDevtoolsActive,
@@ -978,13 +1161,14 @@ function useDevtoolsActive() {
978
1161
  () => false
979
1162
  );
980
1163
  }
981
- var ElementRenderer = import_react6.default.memo(function ElementRenderer2({
1164
+ function ReactiveElementRenderer({
982
1165
  element,
983
1166
  elementKey,
984
1167
  spec,
985
1168
  registry,
986
1169
  loading,
987
- fallback
1170
+ fallback,
1171
+ signatures
988
1172
  }) {
989
1173
  const devtoolsActive = useDevtoolsActive();
990
1174
  const repeatScope = useRepeatScope();
@@ -1051,6 +1235,10 @@ var ElementRenderer = import_react6.default.memo(function ElementRenderer2({
1051
1235
  const watchConfig = element.watch;
1052
1236
  const prevWatchValues = (0, import_react6.useRef)(null);
1053
1237
  const stableWatchRef = (0, import_react6.useRef)(void 0);
1238
+ const stableBindingsRef = (0, import_react6.useRef)(
1239
+ void 0
1240
+ );
1241
+ const stableResolvedPropsRef = (0, import_react6.useRef)({});
1054
1242
  const watchedValues = (0, import_react6.useMemo)(() => {
1055
1243
  if (!watchConfig) return void 0;
1056
1244
  const values = {};
@@ -1110,9 +1298,16 @@ var ElementRenderer = import_react6.default.memo(function ElementRenderer2({
1110
1298
  if (!isVisible) {
1111
1299
  return null;
1112
1300
  }
1113
- const rawProps = element.props;
1114
- const elementBindings = (0, import_core6.resolveBindings)(rawProps, fullCtx);
1115
- const resolvedProps = (0, import_core6.resolveElementProps)(rawProps, fullCtx);
1301
+ const rawProps = element.props ?? EMPTY_ELEMENT_PROPS;
1302
+ const elementBindings = stabilizeRecord(
1303
+ (0, import_core6.resolveBindings)(rawProps, fullCtx),
1304
+ stableBindingsRef
1305
+ );
1306
+ const resolvedProps = shareResolvedValue(
1307
+ stableResolvedPropsRef.current,
1308
+ (0, import_core6.resolveElementProps)(rawProps, fullCtx)
1309
+ );
1310
+ stableResolvedPropsRef.current = resolvedProps;
1116
1311
  const resolvedElement = resolvedProps !== element.props ? { ...element, props: resolvedProps } : element;
1117
1312
  const Component = registry[resolvedElement.type] ?? fallback;
1118
1313
  if (!Component) {
@@ -1146,14 +1341,15 @@ var ElementRenderer = import_react6.default.memo(function ElementRenderer2({
1146
1341
  return null;
1147
1342
  }
1148
1343
  return /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(
1149
- ElementRenderer2,
1344
+ ElementRenderer,
1150
1345
  {
1151
1346
  element: childElement,
1152
1347
  elementKey: childKey,
1153
1348
  spec,
1154
1349
  registry,
1155
1350
  loading,
1156
- fallback
1351
+ fallback,
1352
+ signatures
1157
1353
  },
1158
1354
  childKey
1159
1355
  );
@@ -1166,7 +1362,8 @@ var ElementRenderer = import_react6.default.memo(function ElementRenderer2({
1166
1362
  registry,
1167
1363
  loading,
1168
1364
  fallback,
1169
- itemFilter: repeatItemFilter
1365
+ itemFilter: repeatItemFilter,
1366
+ signatures
1170
1367
  }
1171
1368
  ) : resolvedElement.children ? renderChildKeys(resolvedElement.children) : void 0;
1172
1369
  const slots = resolvedElement.slots ? Object.fromEntries(
@@ -1176,8 +1373,14 @@ var ElementRenderer = import_react6.default.memo(function ElementRenderer2({
1176
1373
  ])
1177
1374
  ) : void 0;
1178
1375
  const rendered = /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(
1179
- Component,
1376
+ CatalogComponentBoundary,
1180
1377
  {
1378
+ Component,
1379
+ signature: signatures[elementKey ?? ""],
1380
+ execute,
1381
+ actionContext: repeatScope,
1382
+ functions,
1383
+ directives,
1181
1384
  element: resolvedElement,
1182
1385
  slots,
1183
1386
  emit,
@@ -1193,16 +1396,24 @@ var ElementRenderer = import_react6.default.memo(function ElementRenderer2({
1193
1396
  {
1194
1397
  elementType: resolvedElement.type,
1195
1398
  registry,
1399
+ resetKey: signatures[elementKey ?? ""],
1196
1400
  children: tagged
1197
1401
  }
1198
1402
  );
1199
- });
1403
+ }
1404
+ var ElementRenderer = import_react6.default.memo(
1405
+ function ElementRenderer2(props) {
1406
+ return /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(ReactiveElementRenderer, { ...props });
1407
+ },
1408
+ (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
1409
+ );
1200
1410
  function RepeatChildren({
1201
1411
  element,
1202
1412
  spec,
1203
1413
  registry,
1204
1414
  loading,
1205
1415
  fallback,
1416
+ signatures,
1206
1417
  itemFilter
1207
1418
  }) {
1208
1419
  const { state } = useStateStore();
@@ -1255,7 +1466,8 @@ function RepeatChildren({
1255
1466
  spec,
1256
1467
  registry,
1257
1468
  loading,
1258
- fallback
1469
+ fallback,
1470
+ signatures
1259
1471
  },
1260
1472
  childKey
1261
1473
  );
@@ -1272,10 +1484,8 @@ function Renderer({
1272
1484
  fallback,
1273
1485
  onRenderError
1274
1486
  }) {
1275
- const rendererRegistry = (0, import_react6.useMemo)(
1276
- () => onRenderError ? withRenderErrorHandler(registry, onRenderError) : registry,
1277
- [registry, onRenderError]
1278
- );
1487
+ const rendererRegistry = useRenderErrorRegistry(registry, onRenderError);
1488
+ const signatures = useElementSignatures(spec);
1279
1489
  if (!spec || !spec.root) {
1280
1490
  return null;
1281
1491
  }
@@ -1291,7 +1501,8 @@ function Renderer({
1291
1501
  spec,
1292
1502
  registry: rendererRegistry,
1293
1503
  loading,
1294
- fallback
1504
+ fallback,
1505
+ signatures
1295
1506
  }
1296
1507
  );
1297
1508
  }
@@ -1420,10 +1631,7 @@ function createRenderer(catalog, components) {
1420
1631
  () => directives ? (0, import_core6.createDirectiveRegistry)(directives) : void 0,
1421
1632
  [directives]
1422
1633
  );
1423
- const rendererRegistry = (0, import_react6.useMemo)(
1424
- () => onRenderError ? withRenderErrorHandler(registry, onRenderError) : registry,
1425
- [onRenderError]
1426
- );
1634
+ const rendererRegistry = useRenderErrorRegistry(registry, onRenderError);
1427
1635
  const actionHandlers = onAction ? new Proxy(
1428
1636
  {},
1429
1637
  {