@xmachines/json-render-react 0.20.0-xm.1 → 0.20.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.mjs CHANGED
@@ -183,7 +183,8 @@ import {
183
183
  useContext as useContext4,
184
184
  useState as useState2,
185
185
  useCallback as useCallback3,
186
- useMemo as useMemo4
186
+ useMemo as useMemo4,
187
+ useRef as useRef3
187
188
  } from "react";
188
189
  import {
189
190
  resolveAction,
@@ -376,6 +377,7 @@ function useValidation() {
376
377
  function useOptionalValidation() {
377
378
  return useContext3(ValidationContext);
378
379
  }
380
+ var useIsomorphicLayoutEffect = typeof document !== "undefined" ? React2.useLayoutEffect : React2.useEffect;
379
381
  function useFieldValidation(path, config) {
380
382
  const {
381
383
  fieldStates,
@@ -388,7 +390,7 @@ function useFieldValidation(path, config) {
388
390
  const configKey = config === void 0 ? void 0 : JSON.stringify(config);
389
391
  const configRef = React2.useRef(config);
390
392
  configRef.current = config;
391
- React2.useEffect(() => {
393
+ useIsomorphicLayoutEffect(() => {
392
394
  const current = configRef.current;
393
395
  if (path && current) {
394
396
  registerField(path, current);
@@ -462,6 +464,7 @@ function ActionProvider({
462
464
  const [handlers, setHandlers] = useState2(initialHandlers);
463
465
  const [loadingActions, setLoadingActions] = useState2(/* @__PURE__ */ new Set());
464
466
  const [pendingConfirmation, setPendingConfirmation] = useState2(null);
467
+ const pendingRef = useRef3(null);
465
468
  const registerHandler = useCallback3(
466
469
  (name, handler) => {
467
470
  setHandlers((prev) => ({ ...prev, [name]: handler }));
@@ -481,6 +484,8 @@ function ActionProvider({
481
484
  });
482
485
  let __ok = true;
483
486
  let __error = void 0;
487
+ let __cancelled = false;
488
+ let __confirmMs = void 0;
484
489
  try {
485
490
  if (resolved.action === "setState" && resolved.params) {
486
491
  const statePath = resolved.params.statePath;
@@ -568,39 +573,32 @@ function ActionProvider({
568
573
  return;
569
574
  }
570
575
  if (resolved.confirm) {
571
- return new Promise((resolve, reject) => {
572
- setPendingConfirmation({
576
+ pendingRef.current?.reject();
577
+ const confirmOpenedAt = Date.now();
578
+ const confirmed = await new Promise((res) => {
579
+ const entry = {
573
580
  action: resolved,
574
581
  handler,
575
582
  resolve: () => {
583
+ pendingRef.current = null;
576
584
  setPendingConfirmation(null);
577
- resolve();
585
+ res(true);
578
586
  },
579
587
  reject: () => {
588
+ pendingRef.current = null;
580
589
  setPendingConfirmation(null);
581
- reject(new Error("Action cancelled"));
590
+ res(false);
582
591
  }
583
- });
584
- }).then(async () => {
585
- setLoadingActions((prev) => new Set(prev).add(resolved.action));
586
- try {
587
- await executeAction({
588
- action: resolved,
589
- handler,
590
- setState: set,
591
- navigate,
592
- executeAction: async (binding2) => {
593
- await execute(binding2);
594
- }
595
- });
596
- } finally {
597
- setLoadingActions((prev) => {
598
- const next = new Set(prev);
599
- next.delete(resolved.action);
600
- return next;
601
- });
602
- }
592
+ };
593
+ pendingRef.current = entry;
594
+ setPendingConfirmation(entry);
603
595
  });
596
+ __confirmMs = Date.now() - confirmOpenedAt;
597
+ if (!confirmed) {
598
+ __ok = false;
599
+ __cancelled = true;
600
+ return;
601
+ }
604
602
  }
605
603
  setLoadingActions((prev) => new Set(prev).add(resolved.action));
606
604
  try {
@@ -632,6 +630,8 @@ function ActionProvider({
632
630
  ok: __ok,
633
631
  at: now,
634
632
  durationMs: now - dispatchedAt,
633
+ cancelled: __cancelled || void 0,
634
+ confirmMs: __confirmMs,
635
635
  error: __error
636
636
  });
637
637
  }
@@ -639,11 +639,11 @@ function ActionProvider({
639
639
  [handlers, get, set, getSnapshot, navigate, validation]
640
640
  );
641
641
  const confirm = useCallback3(() => {
642
- pendingConfirmation?.resolve();
643
- }, [pendingConfirmation]);
642
+ pendingRef.current?.resolve();
643
+ }, []);
644
644
  const cancel = useCallback3(() => {
645
- pendingConfirmation?.reject();
646
- }, [pendingConfirmation]);
645
+ pendingRef.current?.reject();
646
+ }, []);
647
647
  const value = useMemo4(
648
648
  () => ({
649
649
  handlers,
@@ -804,7 +804,7 @@ import React5, {
804
804
  useCallback as useCallback4,
805
805
  useEffect as useEffect2,
806
806
  useMemo as useMemo5,
807
- useRef as useRef3
807
+ useRef as useRef4
808
808
  } from "react";
809
809
  import {
810
810
  resolveElementProps,
@@ -956,8 +956,8 @@ var ElementRenderer = React5.memo(function ElementRenderer2({
956
956
  [onBindings, emit]
957
957
  );
958
958
  const watchConfig = element.watch;
959
- const prevWatchValues = useRef3(null);
960
- const stableWatchRef = useRef3(void 0);
959
+ const prevWatchValues = useRef4(null);
960
+ const stableWatchRef = useRef4(void 0);
961
961
  const watchedValues = useMemo5(() => {
962
962
  if (!watchConfig) return void 0;
963
963
  const values = {};
@@ -1364,7 +1364,7 @@ function createRenderer(catalog, components) {
1364
1364
  }
1365
1365
 
1366
1366
  // src/hooks.ts
1367
- import { useState as useState3, useCallback as useCallback5, useRef as useRef4, useEffect as useEffect3 } from "react";
1367
+ import { useState as useState3, useCallback as useCallback5, useRef as useRef5, useEffect as useEffect3 } from "react";
1368
1368
  import {
1369
1369
  setByPath,
1370
1370
  getByPath as getByPath3,
@@ -1517,10 +1517,10 @@ function useUIStream({
1517
1517
  const [error, setError] = useState3(null);
1518
1518
  const [usage, setUsage] = useState3(null);
1519
1519
  const [rawLines, setRawLines] = useState3([]);
1520
- const abortControllerRef = useRef4(null);
1521
- const onCompleteRef = useRef4(onComplete);
1520
+ const abortControllerRef = useRef5(null);
1521
+ const onCompleteRef = useRef5(onComplete);
1522
1522
  onCompleteRef.current = onComplete;
1523
- const onErrorRef = useRef4(onError);
1523
+ const onErrorRef = useRef5(onError);
1524
1524
  onErrorRef.current = onError;
1525
1525
  const clear = useCallback5(() => {
1526
1526
  setSpec(null);
@@ -1706,8 +1706,8 @@ function getTextFromParts(parts) {
1706
1706
  ).map((p) => p.text.trim()).filter(Boolean).join("\n\n");
1707
1707
  }
1708
1708
  function useJsonRenderMessage(parts) {
1709
- const prevPartsRef = useRef4([]);
1710
- const prevResultRef = useRef4({
1709
+ const prevPartsRef = useRef5([]);
1710
+ const prevResultRef = useRef5({
1711
1711
  spec: null,
1712
1712
  text: ""
1713
1713
  });
@@ -1739,12 +1739,12 @@ function useChatUI({
1739
1739
  const [messages, setMessages] = useState3([]);
1740
1740
  const [isStreaming, setIsStreaming] = useState3(false);
1741
1741
  const [error, setError] = useState3(null);
1742
- const abortControllerRef = useRef4(null);
1743
- const messagesRef = useRef4(messages);
1742
+ const abortControllerRef = useRef5(null);
1743
+ const messagesRef = useRef5(messages);
1744
1744
  messagesRef.current = messages;
1745
- const onCompleteRef = useRef4(onComplete);
1745
+ const onCompleteRef = useRef5(onComplete);
1746
1746
  onCompleteRef.current = onComplete;
1747
- const onErrorRef = useRef4(onError);
1747
+ const onErrorRef = useRef5(onError);
1748
1748
  onErrorRef.current = onError;
1749
1749
  const clear = useCallback5(() => {
1750
1750
  setMessages([]);