@xmachines/json-render-react 0.21.0-xm.1 → 0.21.0-xm.3
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.d.mts +10 -4
- package/dist/index.d.ts +10 -4
- package/dist/index.js +127 -141
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +142 -142
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -2
package/dist/index.mjs
CHANGED
|
@@ -188,7 +188,9 @@ import {
|
|
|
188
188
|
} from "react";
|
|
189
189
|
import {
|
|
190
190
|
resolveAction,
|
|
191
|
+
mergeActionHandlers,
|
|
191
192
|
executeAction,
|
|
193
|
+
isBuiltInAction,
|
|
192
194
|
nextActionDispatchId,
|
|
193
195
|
notifyActionDispatch,
|
|
194
196
|
notifyActionSettle
|
|
@@ -456,19 +458,32 @@ function deepResolveValue(value, get) {
|
|
|
456
458
|
}
|
|
457
459
|
var ActionContext = createContext4(null);
|
|
458
460
|
function ActionProvider({
|
|
459
|
-
handlers:
|
|
461
|
+
handlers: handlersProp = {},
|
|
460
462
|
navigate,
|
|
461
463
|
children
|
|
462
464
|
}) {
|
|
463
465
|
const { get, set, getSnapshot } = useStateStore();
|
|
464
466
|
const validation = useOptionalValidation();
|
|
465
|
-
const
|
|
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
|
+
);
|
|
466
481
|
const [loadingActions, setLoadingActions] = useState2(/* @__PURE__ */ new Set());
|
|
467
482
|
const [pendingConfirmation, setPendingConfirmation] = useState2(null);
|
|
468
483
|
const pendingRef = useRef3(null);
|
|
469
484
|
const registerHandler = useCallback3(
|
|
470
485
|
(name, handler) => {
|
|
471
|
-
|
|
486
|
+
setRegistered((prev) => ({ ...prev, [name]: handler }));
|
|
472
487
|
},
|
|
473
488
|
[]
|
|
474
489
|
);
|
|
@@ -488,6 +503,39 @@ function ActionProvider({
|
|
|
488
503
|
let __cancelled = false;
|
|
489
504
|
let __confirmMs = void 0;
|
|
490
505
|
try {
|
|
506
|
+
const handler = handlers[resolved.action];
|
|
507
|
+
if (!handler && !isBuiltInAction(resolved.action)) {
|
|
508
|
+
console.warn(`No handler registered for action: ${resolved.action}`);
|
|
509
|
+
return;
|
|
510
|
+
}
|
|
511
|
+
if (resolved.confirm) {
|
|
512
|
+
pendingRef.current?.reject();
|
|
513
|
+
const confirmOpenedAt = Date.now();
|
|
514
|
+
const confirmed = await new Promise((res) => {
|
|
515
|
+
const entry = {
|
|
516
|
+
action: resolved,
|
|
517
|
+
handler,
|
|
518
|
+
resolve: () => {
|
|
519
|
+
pendingRef.current = null;
|
|
520
|
+
setPendingConfirmation(null);
|
|
521
|
+
res(true);
|
|
522
|
+
},
|
|
523
|
+
reject: () => {
|
|
524
|
+
pendingRef.current = null;
|
|
525
|
+
setPendingConfirmation(null);
|
|
526
|
+
res(false);
|
|
527
|
+
}
|
|
528
|
+
};
|
|
529
|
+
pendingRef.current = entry;
|
|
530
|
+
setPendingConfirmation(entry);
|
|
531
|
+
});
|
|
532
|
+
__confirmMs = Date.now() - confirmOpenedAt;
|
|
533
|
+
if (!confirmed) {
|
|
534
|
+
__ok = false;
|
|
535
|
+
__cancelled = true;
|
|
536
|
+
return;
|
|
537
|
+
}
|
|
538
|
+
}
|
|
491
539
|
if (resolved.action === "setState" && resolved.params) {
|
|
492
540
|
const statePath = resolved.params.statePath;
|
|
493
541
|
const value2 = resolved.params.value;
|
|
@@ -568,39 +616,7 @@ function ActionProvider({
|
|
|
568
616
|
set(statePath, { valid, errors });
|
|
569
617
|
return;
|
|
570
618
|
}
|
|
571
|
-
|
|
572
|
-
if (!handler) {
|
|
573
|
-
console.warn(`No handler registered for action: ${resolved.action}`);
|
|
574
|
-
return;
|
|
575
|
-
}
|
|
576
|
-
if (resolved.confirm) {
|
|
577
|
-
pendingRef.current?.reject();
|
|
578
|
-
const confirmOpenedAt = Date.now();
|
|
579
|
-
const confirmed = await new Promise((res) => {
|
|
580
|
-
const entry = {
|
|
581
|
-
action: resolved,
|
|
582
|
-
handler,
|
|
583
|
-
resolve: () => {
|
|
584
|
-
pendingRef.current = null;
|
|
585
|
-
setPendingConfirmation(null);
|
|
586
|
-
res(true);
|
|
587
|
-
},
|
|
588
|
-
reject: () => {
|
|
589
|
-
pendingRef.current = null;
|
|
590
|
-
setPendingConfirmation(null);
|
|
591
|
-
res(false);
|
|
592
|
-
}
|
|
593
|
-
};
|
|
594
|
-
pendingRef.current = entry;
|
|
595
|
-
setPendingConfirmation(entry);
|
|
596
|
-
});
|
|
597
|
-
__confirmMs = Date.now() - confirmOpenedAt;
|
|
598
|
-
if (!confirmed) {
|
|
599
|
-
__ok = false;
|
|
600
|
-
__cancelled = true;
|
|
601
|
-
return;
|
|
602
|
-
}
|
|
603
|
-
}
|
|
619
|
+
if (!handler) return;
|
|
604
620
|
setLoadingActions((prev) => new Set(prev).add(resolved.action));
|
|
605
621
|
try {
|
|
606
622
|
await executeAction({
|
|
@@ -810,30 +826,30 @@ import React5, {
|
|
|
810
826
|
import {
|
|
811
827
|
resolveElementProps,
|
|
812
828
|
resolveBindings,
|
|
813
|
-
|
|
829
|
+
resolveBindingParams,
|
|
814
830
|
resolveRepeatItemStatePath,
|
|
815
831
|
resolveRepeatStatePath,
|
|
816
832
|
splitRepeatVisibility,
|
|
817
833
|
evaluateVisibility as evaluateVisibility2,
|
|
818
834
|
getByPath as getByPath2,
|
|
835
|
+
readWatchedValues,
|
|
836
|
+
changedWatchPaths,
|
|
837
|
+
runWatchBindings,
|
|
819
838
|
isDevtoolsActive,
|
|
820
839
|
subscribeDevtoolsActive,
|
|
821
|
-
createDirectiveRegistry
|
|
840
|
+
createDirectiveRegistry,
|
|
841
|
+
withRenderErrorHandler,
|
|
842
|
+
setRenderErrorHandler,
|
|
843
|
+
createComponentRegistry,
|
|
844
|
+
getRegistryComponent,
|
|
845
|
+
warnUnusedRenderErrorHandler,
|
|
846
|
+
getRenderErrorHandler,
|
|
847
|
+
reportActionError,
|
|
848
|
+
setCatalogSlotMetadata,
|
|
849
|
+
warnElementSlots
|
|
822
850
|
} from "@xmachines/json-render-core";
|
|
823
851
|
import { Fragment, jsx as jsx6, jsxs as jsxs2 } from "react/jsx-runtime";
|
|
824
|
-
var registryMetadata = /* @__PURE__ */ new WeakMap();
|
|
825
852
|
var EMPTY_ELEMENT_PROPS = {};
|
|
826
|
-
function withRenderErrorHandler(registry, onRenderError) {
|
|
827
|
-
const next = { ...registry };
|
|
828
|
-
Object.defineProperty(next, "onRenderError", {
|
|
829
|
-
value: onRenderError,
|
|
830
|
-
enumerable: false,
|
|
831
|
-
configurable: true
|
|
832
|
-
});
|
|
833
|
-
const metadata = registryMetadata.get(registry);
|
|
834
|
-
if (metadata) registryMetadata.set(next, metadata);
|
|
835
|
-
return next;
|
|
836
|
-
}
|
|
837
853
|
function useRenderErrorRegistry(registry, onRenderError) {
|
|
838
854
|
const handlerRef = useRef4(onRenderError);
|
|
839
855
|
handlerRef.current = onRenderError;
|
|
@@ -846,11 +862,12 @@ function useRenderErrorRegistry(registry, onRenderError) {
|
|
|
846
862
|
);
|
|
847
863
|
}
|
|
848
864
|
function reportRenderError(registry, elementType, error, componentStack) {
|
|
849
|
-
const onRenderError = registry
|
|
850
|
-
if (
|
|
865
|
+
const onRenderError = getRenderErrorHandler(registry);
|
|
866
|
+
if (onRenderError !== void 0) {
|
|
851
867
|
onRenderError(error, elementType);
|
|
852
868
|
return;
|
|
853
869
|
}
|
|
870
|
+
warnUnusedRenderErrorHandler(registry);
|
|
854
871
|
console.error(
|
|
855
872
|
`[json-render] Rendering error in <${elementType}>:`,
|
|
856
873
|
error,
|
|
@@ -1104,23 +1121,27 @@ function ReactiveElementRenderer({
|
|
|
1104
1121
|
const binding = onBindings?.[eventName];
|
|
1105
1122
|
if (!binding) return;
|
|
1106
1123
|
const actionBindings = Array.isArray(binding) ? binding : [binding];
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
|
|
1117
|
-
for (const [key, val] of Object.entries(b.params)) {
|
|
1118
|
-
resolved[key] = resolveActionParam(val, liveCtx);
|
|
1124
|
+
let action = "";
|
|
1125
|
+
try {
|
|
1126
|
+
for (const b of actionBindings) {
|
|
1127
|
+
action = b.action;
|
|
1128
|
+
await execute(
|
|
1129
|
+
resolveBindingParams(b, {
|
|
1130
|
+
...fullCtx,
|
|
1131
|
+
stateModel: getSnapshot()
|
|
1132
|
+
})
|
|
1133
|
+
);
|
|
1119
1134
|
}
|
|
1120
|
-
|
|
1135
|
+
} catch (err) {
|
|
1136
|
+
reportActionError(
|
|
1137
|
+
getRenderErrorHandler(registry),
|
|
1138
|
+
err,
|
|
1139
|
+
action,
|
|
1140
|
+
"event"
|
|
1141
|
+
);
|
|
1121
1142
|
}
|
|
1122
1143
|
},
|
|
1123
|
-
[onBindings, execute, fullCtx, getSnapshot]
|
|
1144
|
+
[onBindings, execute, fullCtx, getSnapshot, registry]
|
|
1124
1145
|
);
|
|
1125
1146
|
const on = useCallback4(
|
|
1126
1147
|
(eventName) => {
|
|
@@ -1140,6 +1161,20 @@ function ReactiveElementRenderer({
|
|
|
1140
1161
|
[onBindings, emit]
|
|
1141
1162
|
);
|
|
1142
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
|
+
};
|
|
1143
1178
|
const prevWatchValues = useRef4(null);
|
|
1144
1179
|
const stableWatchRef = useRef4(void 0);
|
|
1145
1180
|
const stableBindingsRef = useRef4(
|
|
@@ -1147,61 +1182,48 @@ function ReactiveElementRenderer({
|
|
|
1147
1182
|
);
|
|
1148
1183
|
const stableResolvedPropsRef = useRef4({});
|
|
1149
1184
|
const watchedValues = useMemo5(() => {
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
|
|
1153
|
-
|
|
1154
|
-
|
|
1155
|
-
const prev = stableWatchRef.current;
|
|
1156
|
-
if (prev) {
|
|
1157
|
-
const keys = Object.keys(values);
|
|
1158
|
-
if (keys.length === Object.keys(prev).length && keys.every((k) => values[k] === prev[k])) {
|
|
1159
|
-
return prev;
|
|
1160
|
-
}
|
|
1161
|
-
}
|
|
1185
|
+
const values = readWatchedValues(
|
|
1186
|
+
watchConfig,
|
|
1187
|
+
watchState,
|
|
1188
|
+
stableWatchRef.current
|
|
1189
|
+
);
|
|
1162
1190
|
stableWatchRef.current = values;
|
|
1163
1191
|
return values;
|
|
1164
1192
|
}, [watchConfig, watchState]);
|
|
1165
1193
|
useEffect2(() => {
|
|
1166
|
-
if (!watchConfig || !watchedValues) return;
|
|
1167
|
-
const paths = Object.keys(watchConfig);
|
|
1168
|
-
if (paths.length === 0) return;
|
|
1169
1194
|
const prev = prevWatchValues.current;
|
|
1170
|
-
prevWatchValues.current = watchedValues;
|
|
1171
|
-
|
|
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;
|
|
1172
1199
|
let cancelled = false;
|
|
1173
|
-
|
|
1174
|
-
|
|
1175
|
-
|
|
1176
|
-
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
|
|
1191
|
-
|
|
1192
|
-
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
|
|
1196
|
-
if (cancelled) break;
|
|
1197
|
-
}
|
|
1198
|
-
}
|
|
1199
|
-
}
|
|
1200
|
-
})().catch(console.error);
|
|
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
|
+
});
|
|
1201
1223
|
return () => {
|
|
1202
1224
|
cancelled = true;
|
|
1203
1225
|
};
|
|
1204
|
-
}, [
|
|
1226
|
+
}, [watchedValues]);
|
|
1205
1227
|
if (!isVisible) {
|
|
1206
1228
|
return null;
|
|
1207
1229
|
}
|
|
@@ -1216,26 +1238,12 @@ function ReactiveElementRenderer({
|
|
|
1216
1238
|
);
|
|
1217
1239
|
stableResolvedPropsRef.current = resolvedProps;
|
|
1218
1240
|
const resolvedElement = resolvedProps !== element.props ? { ...element, props: resolvedProps } : element;
|
|
1219
|
-
const Component = registry
|
|
1241
|
+
const Component = getRegistryComponent(registry, resolvedElement.type) ?? fallback;
|
|
1220
1242
|
if (!Component) {
|
|
1221
1243
|
console.warn(`No renderer for component type: ${resolvedElement.type}`);
|
|
1222
1244
|
return null;
|
|
1223
1245
|
}
|
|
1224
|
-
|
|
1225
|
-
if (resolvedElement.slots && metadata?.slots) {
|
|
1226
|
-
const availableSlots = new Set(metadata.slots);
|
|
1227
|
-
for (const slotName of Object.keys(resolvedElement.slots)) {
|
|
1228
|
-
if (slotName === "default") {
|
|
1229
|
-
console.warn(
|
|
1230
|
-
`[json-render] Component "${resolvedElement.type}" uses slots.default. Use "children" for default slot content.`
|
|
1231
|
-
);
|
|
1232
|
-
} else if (!availableSlots.has(slotName)) {
|
|
1233
|
-
console.warn(
|
|
1234
|
-
`[json-render] Unknown slot "${slotName}" on component "${resolvedElement.type}". Available slots: ${metadata.slots.join(", ")}`
|
|
1235
|
-
);
|
|
1236
|
-
}
|
|
1237
|
-
}
|
|
1238
|
-
}
|
|
1246
|
+
warnElementSlots(registry, resolvedElement.type, resolvedElement.slots);
|
|
1239
1247
|
const renderChildKeys = (childKeys, slotName) => childKeys.map((childKey) => {
|
|
1240
1248
|
const childElement = spec.elements[childKey];
|
|
1241
1249
|
if (!childElement) {
|
|
@@ -1457,7 +1465,9 @@ function ConfirmationDialogManager() {
|
|
|
1457
1465
|
);
|
|
1458
1466
|
}
|
|
1459
1467
|
function defineRegistry(catalog, options) {
|
|
1460
|
-
const registry =
|
|
1468
|
+
const registry = createComponentRegistry(
|
|
1469
|
+
options.components
|
|
1470
|
+
);
|
|
1461
1471
|
if (options.components) {
|
|
1462
1472
|
for (const [name, componentFn] of Object.entries(options.components)) {
|
|
1463
1473
|
registry[name] = ({
|
|
@@ -1481,10 +1491,7 @@ function defineRegistry(catalog, options) {
|
|
|
1481
1491
|
};
|
|
1482
1492
|
}
|
|
1483
1493
|
}
|
|
1484
|
-
|
|
1485
|
-
if (catalogComponents) {
|
|
1486
|
-
registryMetadata.set(registry, catalogComponents);
|
|
1487
|
-
}
|
|
1494
|
+
setCatalogSlotMetadata(registry, catalog);
|
|
1488
1495
|
const actionMap = options.actions ? Object.entries(options.actions) : [];
|
|
1489
1496
|
const handlers = (getSetState, getState) => {
|
|
1490
1497
|
const result = {};
|
|
@@ -1508,20 +1515,13 @@ function defineRegistry(catalog, options) {
|
|
|
1508
1515
|
}
|
|
1509
1516
|
};
|
|
1510
1517
|
if (options.onRenderError) {
|
|
1511
|
-
|
|
1512
|
-
value: options.onRenderError,
|
|
1513
|
-
enumerable: false,
|
|
1514
|
-
configurable: true
|
|
1515
|
-
});
|
|
1518
|
+
setRenderErrorHandler(registry, options.onRenderError);
|
|
1516
1519
|
}
|
|
1517
1520
|
return { registry, handlers, executeAction: executeAction2 };
|
|
1518
1521
|
}
|
|
1519
1522
|
function createRenderer(catalog, components) {
|
|
1520
1523
|
const registry = components;
|
|
1521
|
-
|
|
1522
|
-
if (catalogComponents) {
|
|
1523
|
-
registryMetadata.set(registry, catalogComponents);
|
|
1524
|
-
}
|
|
1524
|
+
setCatalogSlotMetadata(registry, catalog);
|
|
1525
1525
|
return function CatalogRenderer({
|
|
1526
1526
|
spec,
|
|
1527
1527
|
store,
|