@xmachines/json-render-react 0.20.0-xm.3 → 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.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();
@@ -821,6 +822,7 @@ import {
821
822
  } from "@xmachines/json-render-core";
822
823
  import { Fragment, jsx as jsx6, jsxs as jsxs2 } from "react/jsx-runtime";
823
824
  var registryMetadata = /* @__PURE__ */ new WeakMap();
825
+ var EMPTY_ELEMENT_PROPS = {};
824
826
  function withRenderErrorHandler(registry, onRenderError) {
825
827
  const next = { ...registry };
826
828
  Object.defineProperty(next, "onRenderError", {
@@ -832,6 +834,17 @@ function withRenderErrorHandler(registry, onRenderError) {
832
834
  if (metadata) registryMetadata.set(next, metadata);
833
835
  return next;
834
836
  }
837
+ function useRenderErrorRegistry(registry, onRenderError) {
838
+ const handlerRef = useRef4(onRenderError);
839
+ handlerRef.current = onRenderError;
840
+ const hasHandler = onRenderError !== void 0;
841
+ return useMemo5(
842
+ () => hasHandler ? withRenderErrorHandler(registry, (error, elementType) => {
843
+ handlerRef.current?.(error, elementType);
844
+ }) : registry,
845
+ [registry, hasHandler]
846
+ );
847
+ }
835
848
  function reportRenderError(registry, elementType, error, componentStack) {
836
849
  const onRenderError = registry?.onRenderError;
837
850
  if (typeof onRenderError === "function") {
@@ -860,6 +873,11 @@ var ElementErrorBoundary = class extends React5.Component {
860
873
  info.componentStack
861
874
  );
862
875
  }
876
+ componentDidUpdate(previous) {
877
+ if (this.state.hasError && previous.resetKey !== this.props.resetKey) {
878
+ this.setState({ hasError: false });
879
+ }
880
+ }
863
881
  render() {
864
882
  if (this.state.hasError) {
865
883
  return null;
@@ -878,6 +896,171 @@ var DirectivesContext = React5.createContext(
878
896
  function useDirectives() {
879
897
  return React5.useContext(DirectivesContext);
880
898
  }
899
+ function stabilizeRecord(value, ref) {
900
+ const previous = ref.current;
901
+ if (previous === value) return previous;
902
+ if (previous && value) {
903
+ const keys = Object.keys(value);
904
+ if (keys.length === Object.keys(previous).length && keys.every(
905
+ (key) => Object.prototype.hasOwnProperty.call(previous, key) && previous[key] === value[key]
906
+ )) {
907
+ return previous;
908
+ }
909
+ }
910
+ ref.current = value;
911
+ return value;
912
+ }
913
+ function isPlainRecord(value) {
914
+ if (value === null || typeof value !== "object") return false;
915
+ const prototype = Object.getPrototypeOf(value);
916
+ return prototype === Object.prototype || prototype === null;
917
+ }
918
+ function structurallyEqual(previous, next) {
919
+ if (Object.is(previous, next)) return true;
920
+ const previousIsArray = Array.isArray(previous);
921
+ if (previousIsArray !== Array.isArray(next)) return false;
922
+ if (!previousIsArray && (previous === null || next === null || typeof previous !== "object" || typeof next !== "object")) {
923
+ return false;
924
+ }
925
+ const previousRecord = previous;
926
+ const nextRecord = next;
927
+ const keys = Object.keys(nextRecord);
928
+ if (previousIsArray && previous.length !== next.length || keys.length !== Object.keys(previousRecord).length) {
929
+ return false;
930
+ }
931
+ return keys.every(
932
+ (key) => Object.prototype.hasOwnProperty.call(previousRecord, key) && structurallyEqual(previousRecord[key], nextRecord[key])
933
+ );
934
+ }
935
+ function snapshotStructuralValue(value) {
936
+ if (value === null || typeof value !== "object") return value;
937
+ const snapshot = Array.isArray(value) ? new Array(value.length) : {};
938
+ for (const key of Object.keys(value)) {
939
+ snapshot[key] = snapshotStructuralValue(
940
+ value[key]
941
+ );
942
+ }
943
+ return snapshot;
944
+ }
945
+ function shareResolvedValue(previous, next) {
946
+ if (Object.is(previous, next)) return previous;
947
+ const previousIsArray = Array.isArray(previous);
948
+ if (previousIsArray !== Array.isArray(next)) return next;
949
+ if (!previousIsArray && (!isPlainRecord(previous) || !isPlainRecord(next))) {
950
+ return next;
951
+ }
952
+ const previousRecord = previous;
953
+ const nextRecord = next;
954
+ const keys = Object.keys(nextRecord);
955
+ const shared = previousIsArray ? new Array(next.length) : {};
956
+ let unchanged = (!previousIsArray || previous.length === next.length) && keys.length === Object.keys(previousRecord).length;
957
+ for (const key of keys) {
958
+ let value = nextRecord[key];
959
+ if (Object.prototype.hasOwnProperty.call(previousRecord, key)) {
960
+ value = shareResolvedValue(previousRecord[key], value);
961
+ if (!Object.is(previousRecord[key], value)) unchanged = false;
962
+ } else {
963
+ unchanged = false;
964
+ }
965
+ shared[key] = value;
966
+ }
967
+ return unchanged ? previous : shared;
968
+ }
969
+ function useElementSignatures(spec) {
970
+ const entriesRef = useRef4({});
971
+ const versionRef = useRef4(0);
972
+ if (!spec) return {};
973
+ const previous = entriesRef.current;
974
+ const next = {};
975
+ const signatures = {};
976
+ const visiting = /* @__PURE__ */ new Set();
977
+ for (const key of Object.keys(spec.elements)) {
978
+ if (signatures[key] !== void 0) continue;
979
+ const element = spec.elements[key];
980
+ if (!element) continue;
981
+ visiting.add(key);
982
+ const stack = [
983
+ {
984
+ key,
985
+ element,
986
+ children: [
987
+ ...element.children ?? [],
988
+ ...Object.values(element.slots ?? {}).flat()
989
+ ],
990
+ childIndex: 0,
991
+ childVersions: []
992
+ }
993
+ ];
994
+ while (stack.length > 0) {
995
+ const frame = stack[stack.length - 1];
996
+ const childKey = frame.children[frame.childIndex];
997
+ if (childKey !== void 0) {
998
+ const childVersion = signatures[childKey];
999
+ if (childVersion !== void 0) {
1000
+ frame.childVersions.push([childKey, childVersion]);
1001
+ frame.childIndex += 1;
1002
+ continue;
1003
+ }
1004
+ const childElement = spec.elements[childKey];
1005
+ if (!childElement || visiting.has(childKey)) {
1006
+ frame.childVersions.push([childKey, -1]);
1007
+ frame.childIndex += 1;
1008
+ continue;
1009
+ }
1010
+ visiting.add(childKey);
1011
+ stack.push({
1012
+ key: childKey,
1013
+ element: childElement,
1014
+ children: [
1015
+ ...childElement.children ?? [],
1016
+ ...Object.values(childElement.slots ?? {}).flat()
1017
+ ],
1018
+ childIndex: 0,
1019
+ childVersions: []
1020
+ });
1021
+ continue;
1022
+ }
1023
+ const prior = previous[frame.key];
1024
+ const version = prior && structurallyEqual(prior.own, frame.element) && structurallyEqual(prior.children, frame.childVersions) ? prior.version : ++versionRef.current;
1025
+ visiting.delete(frame.key);
1026
+ next[frame.key] = {
1027
+ own: snapshotStructuralValue(frame.element),
1028
+ children: frame.childVersions,
1029
+ version
1030
+ };
1031
+ signatures[frame.key] = version;
1032
+ stack.pop();
1033
+ }
1034
+ }
1035
+ entriesRef.current = next;
1036
+ return signatures;
1037
+ }
1038
+ var CatalogComponentBoundary = React5.memo(
1039
+ function CatalogComponentBoundary2({
1040
+ Component,
1041
+ element,
1042
+ slots,
1043
+ emit,
1044
+ on,
1045
+ bindings,
1046
+ loading,
1047
+ children
1048
+ }) {
1049
+ return /* @__PURE__ */ jsx6(
1050
+ Component,
1051
+ {
1052
+ element,
1053
+ slots,
1054
+ emit,
1055
+ on,
1056
+ bindings,
1057
+ loading,
1058
+ children
1059
+ }
1060
+ );
1061
+ },
1062
+ (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
1063
+ );
881
1064
  function useDevtoolsActive() {
882
1065
  return React5.useSyncExternalStore(
883
1066
  subscribeDevtoolsActive,
@@ -885,13 +1068,14 @@ function useDevtoolsActive() {
885
1068
  () => false
886
1069
  );
887
1070
  }
888
- var ElementRenderer = React5.memo(function ElementRenderer2({
1071
+ function ReactiveElementRenderer({
889
1072
  element,
890
1073
  elementKey,
891
1074
  spec,
892
1075
  registry,
893
1076
  loading,
894
- fallback
1077
+ fallback,
1078
+ signatures
895
1079
  }) {
896
1080
  const devtoolsActive = useDevtoolsActive();
897
1081
  const repeatScope = useRepeatScope();
@@ -958,6 +1142,10 @@ var ElementRenderer = React5.memo(function ElementRenderer2({
958
1142
  const watchConfig = element.watch;
959
1143
  const prevWatchValues = useRef4(null);
960
1144
  const stableWatchRef = useRef4(void 0);
1145
+ const stableBindingsRef = useRef4(
1146
+ void 0
1147
+ );
1148
+ const stableResolvedPropsRef = useRef4({});
961
1149
  const watchedValues = useMemo5(() => {
962
1150
  if (!watchConfig) return void 0;
963
1151
  const values = {};
@@ -1017,9 +1205,16 @@ var ElementRenderer = React5.memo(function ElementRenderer2({
1017
1205
  if (!isVisible) {
1018
1206
  return null;
1019
1207
  }
1020
- const rawProps = element.props;
1021
- const elementBindings = resolveBindings(rawProps, fullCtx);
1022
- const resolvedProps = resolveElementProps(rawProps, fullCtx);
1208
+ const rawProps = element.props ?? EMPTY_ELEMENT_PROPS;
1209
+ const elementBindings = stabilizeRecord(
1210
+ resolveBindings(rawProps, fullCtx),
1211
+ stableBindingsRef
1212
+ );
1213
+ const resolvedProps = shareResolvedValue(
1214
+ stableResolvedPropsRef.current,
1215
+ resolveElementProps(rawProps, fullCtx)
1216
+ );
1217
+ stableResolvedPropsRef.current = resolvedProps;
1023
1218
  const resolvedElement = resolvedProps !== element.props ? { ...element, props: resolvedProps } : element;
1024
1219
  const Component = registry[resolvedElement.type] ?? fallback;
1025
1220
  if (!Component) {
@@ -1053,14 +1248,15 @@ var ElementRenderer = React5.memo(function ElementRenderer2({
1053
1248
  return null;
1054
1249
  }
1055
1250
  return /* @__PURE__ */ jsx6(
1056
- ElementRenderer2,
1251
+ ElementRenderer,
1057
1252
  {
1058
1253
  element: childElement,
1059
1254
  elementKey: childKey,
1060
1255
  spec,
1061
1256
  registry,
1062
1257
  loading,
1063
- fallback
1258
+ fallback,
1259
+ signatures
1064
1260
  },
1065
1261
  childKey
1066
1262
  );
@@ -1073,7 +1269,8 @@ var ElementRenderer = React5.memo(function ElementRenderer2({
1073
1269
  registry,
1074
1270
  loading,
1075
1271
  fallback,
1076
- itemFilter: repeatItemFilter
1272
+ itemFilter: repeatItemFilter,
1273
+ signatures
1077
1274
  }
1078
1275
  ) : resolvedElement.children ? renderChildKeys(resolvedElement.children) : void 0;
1079
1276
  const slots = resolvedElement.slots ? Object.fromEntries(
@@ -1083,8 +1280,14 @@ var ElementRenderer = React5.memo(function ElementRenderer2({
1083
1280
  ])
1084
1281
  ) : void 0;
1085
1282
  const rendered = /* @__PURE__ */ jsx6(
1086
- Component,
1283
+ CatalogComponentBoundary,
1087
1284
  {
1285
+ Component,
1286
+ signature: signatures[elementKey ?? ""],
1287
+ execute,
1288
+ actionContext: repeatScope,
1289
+ functions,
1290
+ directives,
1088
1291
  element: resolvedElement,
1089
1292
  slots,
1090
1293
  emit,
@@ -1100,16 +1303,24 @@ var ElementRenderer = React5.memo(function ElementRenderer2({
1100
1303
  {
1101
1304
  elementType: resolvedElement.type,
1102
1305
  registry,
1306
+ resetKey: signatures[elementKey ?? ""],
1103
1307
  children: tagged
1104
1308
  }
1105
1309
  );
1106
- });
1310
+ }
1311
+ var ElementRenderer = React5.memo(
1312
+ function ElementRenderer2(props) {
1313
+ return /* @__PURE__ */ jsx6(ReactiveElementRenderer, { ...props });
1314
+ },
1315
+ (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
1316
+ );
1107
1317
  function RepeatChildren({
1108
1318
  element,
1109
1319
  spec,
1110
1320
  registry,
1111
1321
  loading,
1112
1322
  fallback,
1323
+ signatures,
1113
1324
  itemFilter
1114
1325
  }) {
1115
1326
  const { state } = useStateStore();
@@ -1162,7 +1373,8 @@ function RepeatChildren({
1162
1373
  spec,
1163
1374
  registry,
1164
1375
  loading,
1165
- fallback
1376
+ fallback,
1377
+ signatures
1166
1378
  },
1167
1379
  childKey
1168
1380
  );
@@ -1179,10 +1391,8 @@ function Renderer({
1179
1391
  fallback,
1180
1392
  onRenderError
1181
1393
  }) {
1182
- const rendererRegistry = useMemo5(
1183
- () => onRenderError ? withRenderErrorHandler(registry, onRenderError) : registry,
1184
- [registry, onRenderError]
1185
- );
1394
+ const rendererRegistry = useRenderErrorRegistry(registry, onRenderError);
1395
+ const signatures = useElementSignatures(spec);
1186
1396
  if (!spec || !spec.root) {
1187
1397
  return null;
1188
1398
  }
@@ -1198,7 +1408,8 @@ function Renderer({
1198
1408
  spec,
1199
1409
  registry: rendererRegistry,
1200
1410
  loading,
1201
- fallback
1411
+ fallback,
1412
+ signatures
1202
1413
  }
1203
1414
  );
1204
1415
  }
@@ -1327,10 +1538,7 @@ function createRenderer(catalog, components) {
1327
1538
  () => directives ? createDirectiveRegistry(directives) : void 0,
1328
1539
  [directives]
1329
1540
  );
1330
- const rendererRegistry = useMemo5(
1331
- () => onRenderError ? withRenderErrorHandler(registry, onRenderError) : registry,
1332
- [onRenderError]
1333
- );
1541
+ const rendererRegistry = useRenderErrorRegistry(registry, onRenderError);
1334
1542
  const actionHandlers = onAction ? new Proxy(
1335
1543
  {},
1336
1544
  {