@xmachines/json-render-react 0.21.0-xm.2 → 0.21.0-xm.4
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 +124 -116
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +136 -117
- package/dist/index.mjs.map +1 -1
- package/package.json +4 -4
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,31 +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
840
|
createDirectiveRegistry,
|
|
822
|
-
|
|
841
|
+
withRenderErrorHandler,
|
|
842
|
+
setRenderErrorHandler,
|
|
843
|
+
createComponentRegistry,
|
|
844
|
+
getRegistryComponent,
|
|
845
|
+
warnUnusedRenderErrorHandler,
|
|
846
|
+
getRenderErrorHandler,
|
|
847
|
+
reportActionError,
|
|
823
848
|
setCatalogSlotMetadata,
|
|
824
849
|
warnElementSlots
|
|
825
850
|
} from "@xmachines/json-render-core";
|
|
826
851
|
import { Fragment, jsx as jsx6, jsxs as jsxs2 } from "react/jsx-runtime";
|
|
827
852
|
var EMPTY_ELEMENT_PROPS = {};
|
|
828
|
-
function withRenderErrorHandler(registry, onRenderError) {
|
|
829
|
-
const next = { ...registry };
|
|
830
|
-
Object.defineProperty(next, "onRenderError", {
|
|
831
|
-
value: onRenderError,
|
|
832
|
-
enumerable: false,
|
|
833
|
-
configurable: true
|
|
834
|
-
});
|
|
835
|
-
copySlotMetadata(registry, next);
|
|
836
|
-
return next;
|
|
837
|
-
}
|
|
838
853
|
function useRenderErrorRegistry(registry, onRenderError) {
|
|
839
854
|
const handlerRef = useRef4(onRenderError);
|
|
840
855
|
handlerRef.current = onRenderError;
|
|
@@ -847,11 +862,12 @@ function useRenderErrorRegistry(registry, onRenderError) {
|
|
|
847
862
|
);
|
|
848
863
|
}
|
|
849
864
|
function reportRenderError(registry, elementType, error, componentStack) {
|
|
850
|
-
const onRenderError = registry
|
|
851
|
-
if (
|
|
865
|
+
const onRenderError = getRenderErrorHandler(registry);
|
|
866
|
+
if (onRenderError !== void 0) {
|
|
852
867
|
onRenderError(error, elementType);
|
|
853
868
|
return;
|
|
854
869
|
}
|
|
870
|
+
warnUnusedRenderErrorHandler(registry);
|
|
855
871
|
console.error(
|
|
856
872
|
`[json-render] Rendering error in <${elementType}>:`,
|
|
857
873
|
error,
|
|
@@ -1105,23 +1121,27 @@ function ReactiveElementRenderer({
|
|
|
1105
1121
|
const binding = onBindings?.[eventName];
|
|
1106
1122
|
if (!binding) return;
|
|
1107
1123
|
const actionBindings = Array.isArray(binding) ? binding : [binding];
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
for (const [key, val] of Object.entries(b.params)) {
|
|
1119
|
-
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
|
+
);
|
|
1120
1134
|
}
|
|
1121
|
-
|
|
1135
|
+
} catch (err) {
|
|
1136
|
+
reportActionError(
|
|
1137
|
+
getRenderErrorHandler(registry),
|
|
1138
|
+
err,
|
|
1139
|
+
action,
|
|
1140
|
+
"event"
|
|
1141
|
+
);
|
|
1122
1142
|
}
|
|
1123
1143
|
},
|
|
1124
|
-
[onBindings, execute, fullCtx, getSnapshot]
|
|
1144
|
+
[onBindings, execute, fullCtx, getSnapshot, registry]
|
|
1125
1145
|
);
|
|
1126
1146
|
const on = useCallback4(
|
|
1127
1147
|
(eventName) => {
|
|
@@ -1141,6 +1161,20 @@ function ReactiveElementRenderer({
|
|
|
1141
1161
|
[onBindings, emit]
|
|
1142
1162
|
);
|
|
1143
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
|
+
};
|
|
1144
1178
|
const prevWatchValues = useRef4(null);
|
|
1145
1179
|
const stableWatchRef = useRef4(void 0);
|
|
1146
1180
|
const stableBindingsRef = useRef4(
|
|
@@ -1148,61 +1182,48 @@ function ReactiveElementRenderer({
|
|
|
1148
1182
|
);
|
|
1149
1183
|
const stableResolvedPropsRef = useRef4({});
|
|
1150
1184
|
const watchedValues = useMemo5(() => {
|
|
1151
|
-
|
|
1152
|
-
|
|
1153
|
-
|
|
1154
|
-
|
|
1155
|
-
|
|
1156
|
-
const prev = stableWatchRef.current;
|
|
1157
|
-
if (prev) {
|
|
1158
|
-
const keys = Object.keys(values);
|
|
1159
|
-
if (keys.length === Object.keys(prev).length && keys.every((k) => values[k] === prev[k])) {
|
|
1160
|
-
return prev;
|
|
1161
|
-
}
|
|
1162
|
-
}
|
|
1185
|
+
const values = readWatchedValues(
|
|
1186
|
+
watchConfig,
|
|
1187
|
+
watchState,
|
|
1188
|
+
stableWatchRef.current
|
|
1189
|
+
);
|
|
1163
1190
|
stableWatchRef.current = values;
|
|
1164
1191
|
return values;
|
|
1165
1192
|
}, [watchConfig, watchState]);
|
|
1166
1193
|
useEffect2(() => {
|
|
1167
|
-
if (!watchConfig || !watchedValues) return;
|
|
1168
|
-
const paths = Object.keys(watchConfig);
|
|
1169
|
-
if (paths.length === 0) return;
|
|
1170
1194
|
const prev = prevWatchValues.current;
|
|
1171
|
-
prevWatchValues.current = watchedValues;
|
|
1172
|
-
|
|
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;
|
|
1173
1199
|
let cancelled = false;
|
|
1174
|
-
|
|
1175
|
-
|
|
1176
|
-
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
|
|
1191
|
-
|
|
1192
|
-
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
|
|
1196
|
-
|
|
1197
|
-
if (cancelled) break;
|
|
1198
|
-
}
|
|
1199
|
-
}
|
|
1200
|
-
}
|
|
1201
|
-
})().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
|
+
});
|
|
1202
1223
|
return () => {
|
|
1203
1224
|
cancelled = true;
|
|
1204
1225
|
};
|
|
1205
|
-
}, [
|
|
1226
|
+
}, [watchedValues]);
|
|
1206
1227
|
if (!isVisible) {
|
|
1207
1228
|
return null;
|
|
1208
1229
|
}
|
|
@@ -1217,7 +1238,7 @@ function ReactiveElementRenderer({
|
|
|
1217
1238
|
);
|
|
1218
1239
|
stableResolvedPropsRef.current = resolvedProps;
|
|
1219
1240
|
const resolvedElement = resolvedProps !== element.props ? { ...element, props: resolvedProps } : element;
|
|
1220
|
-
const Component = registry
|
|
1241
|
+
const Component = getRegistryComponent(registry, resolvedElement.type) ?? fallback;
|
|
1221
1242
|
if (!Component) {
|
|
1222
1243
|
console.warn(`No renderer for component type: ${resolvedElement.type}`);
|
|
1223
1244
|
return null;
|
|
@@ -1444,7 +1465,9 @@ function ConfirmationDialogManager() {
|
|
|
1444
1465
|
);
|
|
1445
1466
|
}
|
|
1446
1467
|
function defineRegistry(catalog, options) {
|
|
1447
|
-
const registry =
|
|
1468
|
+
const registry = createComponentRegistry(
|
|
1469
|
+
options.components
|
|
1470
|
+
);
|
|
1448
1471
|
if (options.components) {
|
|
1449
1472
|
for (const [name, componentFn] of Object.entries(options.components)) {
|
|
1450
1473
|
registry[name] = ({
|
|
@@ -1492,11 +1515,7 @@ function defineRegistry(catalog, options) {
|
|
|
1492
1515
|
}
|
|
1493
1516
|
};
|
|
1494
1517
|
if (options.onRenderError) {
|
|
1495
|
-
|
|
1496
|
-
value: options.onRenderError,
|
|
1497
|
-
enumerable: false,
|
|
1498
|
-
configurable: true
|
|
1499
|
-
});
|
|
1518
|
+
setRenderErrorHandler(registry, options.onRenderError);
|
|
1500
1519
|
}
|
|
1501
1520
|
return { registry, handlers, executeAction: executeAction2 };
|
|
1502
1521
|
}
|