@xmachines/json-render-core 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.mjs CHANGED
@@ -431,6 +431,14 @@ function resolveActionParam(value, ctx) {
431
431
  }
432
432
  return resolvePropValue(value, ctx);
433
433
  }
434
+ function resolveBindingParams(binding, ctx) {
435
+ if (!binding.params) return binding;
436
+ const params = {};
437
+ for (const [key, value] of Object.entries(binding.params)) {
438
+ params[key] = resolveActionParam(value, ctx);
439
+ }
440
+ return { ...binding, params };
441
+ }
434
442
 
435
443
  // src/action-observer.ts
436
444
  var observers = /* @__PURE__ */ new Set();
@@ -543,6 +551,43 @@ var ActionBindingSchema = z2.object({
543
551
  preventDefault: z2.boolean().optional()
544
552
  });
545
553
  var ActionSchema = ActionBindingSchema;
554
+ function mergeActionHandlers(prop, registered) {
555
+ const current = typeof prop === "function" ? prop : () => prop;
556
+ if (typeof prop !== "function" && Object.keys(registered).length === 0) {
557
+ return prop;
558
+ }
559
+ const own = (name) => typeof name === "string" && Object.hasOwn(registered, name);
560
+ const inherited = (name) => name in Object.prototype && !Object.hasOwn(current(), name);
561
+ const read = (name) => {
562
+ if (typeof name !== "string") return void 0;
563
+ if (own(name)) return registered[name];
564
+ return inherited(name) ? void 0 : Reflect.get(current(), name);
565
+ };
566
+ const has = (name) => typeof name === "string" && (own(name) || !inherited(name) && Reflect.has(current(), name));
567
+ const names = (map) => Reflect.ownKeys(map).filter((key) => typeof key === "string");
568
+ return new Proxy({}, {
569
+ get: (_target, name) => read(name),
570
+ has: (_target, name) => has(name),
571
+ ownKeys: () => [.../* @__PURE__ */ new Set([...names(current()), ...names(registered)])],
572
+ getOwnPropertyDescriptor: (_target, name) => has(name) ? {
573
+ value: read(name),
574
+ enumerable: true,
575
+ configurable: true,
576
+ writable: false
577
+ } : void 0
578
+ });
579
+ }
580
+ var BUILT_IN_ACTIONS = /* @__PURE__ */ new Set([
581
+ "setState",
582
+ "pushState",
583
+ "removeState",
584
+ "push",
585
+ "pop",
586
+ "validateForm"
587
+ ]);
588
+ function isBuiltInAction(name) {
589
+ return BUILT_IN_ACTIONS.has(name);
590
+ }
546
591
  function resolveAction(binding, stateModel) {
547
592
  const resolvedParams = {};
548
593
  if (binding.params) {
@@ -573,16 +618,21 @@ function interpolateString(template, stateModel) {
573
618
  });
574
619
  }
575
620
  async function executeAction(ctx) {
576
- const { action: action2, handler, setState, navigate, executeAction: executeAction2 } = ctx;
621
+ const { action: action2, handler, setState, updateState, navigate, executeAction: executeAction2 } = ctx;
622
+ const writeAll = (updates) => {
623
+ if (updateState) {
624
+ updateState(updates);
625
+ return;
626
+ }
627
+ for (const [path, value] of Object.entries(updates)) setState(path, value);
628
+ };
577
629
  try {
578
630
  await handler(action2.params);
579
631
  if (action2.onSuccess) {
580
632
  if ("navigate" in action2.onSuccess && navigate) {
581
633
  navigate(action2.onSuccess.navigate);
582
634
  } else if ("set" in action2.onSuccess) {
583
- for (const [path, value] of Object.entries(action2.onSuccess.set)) {
584
- setState(path, value);
585
- }
635
+ writeAll(action2.onSuccess.set);
586
636
  } else if ("action" in action2.onSuccess && executeAction2) {
587
637
  await executeAction2(action2.onSuccess);
588
638
  }
@@ -590,10 +640,11 @@ async function executeAction(ctx) {
590
640
  } catch (error) {
591
641
  if (action2.onError) {
592
642
  if ("set" in action2.onError) {
643
+ const updates = {};
593
644
  for (const [path, value] of Object.entries(action2.onError.set)) {
594
- const resolvedValue = typeof value === "string" && value === "$error.message" ? error.message : value;
595
- setState(path, resolvedValue);
645
+ updates[path] = typeof value === "string" && value === "$error.message" ? error.message : value;
596
646
  }
647
+ writeAll(updates);
597
648
  } else if ("action" in action2.onError && executeAction2) {
598
649
  await executeAction2(action2.onError);
599
650
  }
@@ -2484,6 +2535,181 @@ function warnElementSlots(registry, type, slots) {
2484
2535
  }
2485
2536
  }
2486
2537
  }
2538
+ function withRenderErrorHandler(registry, onRenderError) {
2539
+ const members = ownMemberDescriptors(registry);
2540
+ if ("onRenderError" in members) {
2541
+ warnReplacedComponent(registry);
2542
+ delete members["onRenderError"];
2543
+ }
2544
+ const next = Object.create(registry, members);
2545
+ defineRenderErrorHandler(next, onRenderError);
2546
+ copySources.set(next, copySources.get(registry) ?? registry);
2547
+ copySlotMetadata(registry, next);
2548
+ return next;
2549
+ }
2550
+ function setRenderErrorHandler(registry, onRenderError) {
2551
+ if (findDescriptor(registry, "onRenderError")?.enumerable === true) {
2552
+ warnReplacedComponent(registry);
2553
+ }
2554
+ defineRenderErrorHandler(registry, onRenderError);
2555
+ }
2556
+ var copySources = /* @__PURE__ */ new WeakMap();
2557
+ function defineRenderErrorHandler(registry, onRenderError) {
2558
+ const defined = Reflect.defineProperty(registry, "onRenderError", {
2559
+ value: onRenderError,
2560
+ enumerable: false,
2561
+ configurable: true
2562
+ });
2563
+ if (!defined) {
2564
+ console.warn(
2565
+ "[json-render] The registry takes no onRenderError handler, for example because it is frozen. Use withRenderErrorHandler, which gives a copy."
2566
+ );
2567
+ }
2568
+ }
2569
+ var warnedReplaced = /* @__PURE__ */ new WeakSet();
2570
+ var warnedUnused = /* @__PURE__ */ new WeakSet();
2571
+ function warnReplacedComponent(registry) {
2572
+ if (warnedReplaced.has(registry)) return;
2573
+ warnedReplaced.add(registry);
2574
+ console.warn(
2575
+ '[json-render] The registry has a component named "onRenderError". The onRenderError handler takes its place, and an element of that type renders the fallback.'
2576
+ );
2577
+ }
2578
+ function findDescriptor(target, key) {
2579
+ let current = target;
2580
+ while (current !== null && current !== Object.prototype) {
2581
+ const descriptor = Object.getOwnPropertyDescriptor(current, key);
2582
+ if (descriptor !== void 0) return descriptor;
2583
+ current = Object.getPrototypeOf(current);
2584
+ }
2585
+ return void 0;
2586
+ }
2587
+ function warnUnusedRenderErrorHandler(registry) {
2588
+ if (registry === void 0 || warnedUnused.has(registry)) return;
2589
+ const descriptor = findDescriptor(registry, "onRenderError");
2590
+ if (descriptor?.enumerable !== true) return;
2591
+ warnedUnused.add(registry);
2592
+ console.warn(
2593
+ '[json-render] The registry has an enumerable "onRenderError". It counts as a component of that name, and not as the error handler. Give the handler with defineRegistry(catalog, { onRenderError }) or with the onRenderError prop of the renderer.'
2594
+ );
2595
+ }
2596
+ function getRenderErrorHandler(registry) {
2597
+ if (registry === void 0) return void 0;
2598
+ const descriptor = findDescriptor(registry, "onRenderError");
2599
+ if (descriptor === void 0 || descriptor.enumerable) return void 0;
2600
+ const handler = "value" in descriptor ? descriptor.value : registry.onRenderError;
2601
+ return typeof handler === "function" ? handler : void 0;
2602
+ }
2603
+ function getOwnEntry(map, key) {
2604
+ if (key in Object.prototype && !Object.prototype.hasOwnProperty.call(map, key)) {
2605
+ return void 0;
2606
+ }
2607
+ return map[key];
2608
+ }
2609
+ function ownMemberDescriptors(source) {
2610
+ const members = /* @__PURE__ */ Object.create(null);
2611
+ for (const key of Reflect.ownKeys(source)) {
2612
+ const descriptor = Reflect.getOwnPropertyDescriptor(source, key);
2613
+ if (descriptor?.enumerable !== true) continue;
2614
+ members[key] = "get" in descriptor || "set" in descriptor ? {
2615
+ get: () => Reflect.get(source, key),
2616
+ enumerable: true,
2617
+ configurable: true
2618
+ } : {
2619
+ value: Reflect.get(source, key),
2620
+ enumerable: true,
2621
+ writable: true,
2622
+ configurable: true
2623
+ };
2624
+ }
2625
+ return members;
2626
+ }
2627
+ function createComponentRegistry(components) {
2628
+ const registry = {};
2629
+ if (components !== void 0 && Object.prototype.hasOwnProperty.call(components, "__proto__")) {
2630
+ Object.defineProperty(registry, "__proto__", {
2631
+ value: void 0,
2632
+ enumerable: true,
2633
+ writable: true,
2634
+ configurable: true
2635
+ });
2636
+ }
2637
+ return registry;
2638
+ }
2639
+ function getRegistryComponent(registry, type) {
2640
+ if (type === "onRenderError" && findDescriptor(registry, type)?.enumerable === false) {
2641
+ return void 0;
2642
+ }
2643
+ const source = Object.prototype.hasOwnProperty.call(registry, type) ? registry : copySources.get(registry) ?? registry;
2644
+ const component = getOwnEntry(source, type);
2645
+ if (component === void 0 || component === null) return void 0;
2646
+ return component;
2647
+ }
2648
+ function reportActionError(onRenderError, error, action2, source) {
2649
+ if (onRenderError) {
2650
+ onRenderError(error, action2);
2651
+ return;
2652
+ }
2653
+ console.error(
2654
+ source === "watch" ? "[json-render] Unhandled action error in watch handler:" : "[json-render] Unhandled action error in handler:",
2655
+ action2,
2656
+ error
2657
+ );
2658
+ }
2659
+
2660
+ // src/subtree.ts
2661
+ function elementSubtreeJson(spec, key, element) {
2662
+ const seen = /* @__PURE__ */ new Set();
2663
+ const parts = [];
2664
+ const visit = (elementKey, current) => {
2665
+ if (elementKey !== void 0) {
2666
+ if (seen.has(elementKey)) return;
2667
+ seen.add(elementKey);
2668
+ }
2669
+ parts.push([elementKey ?? null, current ?? null]);
2670
+ if (!current) return;
2671
+ const lists = [current.children, ...Object.values(current.slots ?? {})];
2672
+ for (const list of lists) {
2673
+ if (!Array.isArray(list)) continue;
2674
+ for (const child of list) visit(child, spec?.elements[child]);
2675
+ }
2676
+ };
2677
+ visit(key, element);
2678
+ return JSON.stringify(parts);
2679
+ }
2680
+
2681
+ // src/watch.ts
2682
+ function readWatchedValues(watch, state, previous) {
2683
+ if (!watch) return void 0;
2684
+ const paths = Object.keys(watch);
2685
+ const values = {};
2686
+ for (const path of paths) {
2687
+ values[path] = getByPath(state, path);
2688
+ }
2689
+ const before = previous === void 0 ? [] : Object.keys(previous);
2690
+ if (previous !== void 0 && before.length === paths.length && paths.every(
2691
+ (path, index) => before[index] === path && Object.is(previous[path], values[path])
2692
+ )) {
2693
+ return previous;
2694
+ }
2695
+ return values;
2696
+ }
2697
+ function changedWatchPaths(current, previous) {
2698
+ if (current === void 0 || previous === void 0) return [];
2699
+ return Object.keys(current).filter(
2700
+ (path) => path in previous && !Object.is(current[path], previous[path])
2701
+ );
2702
+ }
2703
+ async function runWatchBindings(watch, paths, run, isCancelled) {
2704
+ for (const path of paths) {
2705
+ const raw = watch[path];
2706
+ if (!raw) continue;
2707
+ for (const binding of Array.isArray(raw) ? raw : [raw]) {
2708
+ if (isCancelled()) return;
2709
+ await run(binding);
2710
+ }
2711
+ }
2712
+ }
2487
2713
 
2488
2714
  // src/merge.ts
2489
2715
  function isPlainObject(value) {
@@ -3435,10 +3661,12 @@ export {
3435
3661
  buildEditUserPrompt,
3436
3662
  buildUserPrompt,
3437
3663
  builtInValidationFunctions,
3664
+ changedWatchPaths,
3438
3665
  check,
3439
3666
  compileSpecStream,
3440
3667
  conditionUsesItemScope,
3441
3668
  copySlotMetadata,
3669
+ createComponentRegistry,
3442
3670
  createDirectiveRegistry,
3443
3671
  createJsonRenderTransform,
3444
3672
  createMixedStreamParser,
@@ -3449,6 +3677,7 @@ export {
3449
3677
  defineDirective,
3450
3678
  defineSchema,
3451
3679
  diffToPatches,
3680
+ elementSubtreeJson,
3452
3681
  evaluateVisibility,
3453
3682
  executeAction,
3454
3683
  experimental_composeSpec,
@@ -3458,20 +3687,29 @@ export {
3458
3687
  formatSpecIssues,
3459
3688
  getByPath,
3460
3689
  getComponentSlotMetadata,
3690
+ getOwnEntry,
3691
+ getRegistryComponent,
3692
+ getRenderErrorHandler,
3461
3693
  interpolateString,
3694
+ isBuiltInAction,
3462
3695
  isDevtoolsActive,
3463
3696
  isNonEmptySpec,
3464
3697
  markDevtoolsActive,
3698
+ mergeActionHandlers,
3465
3699
  nestedToFlat,
3466
3700
  nextActionDispatchId,
3467
3701
  notifyActionDispatch,
3468
3702
  notifyActionSettle,
3703
+ ownMemberDescriptors,
3469
3704
  parseSpecStreamLine,
3470
3705
  pipeJsonRender,
3706
+ readWatchedValues,
3471
3707
  registerActionObserver,
3472
3708
  removeByPath,
3709
+ reportActionError,
3473
3710
  resolveAction,
3474
3711
  resolveActionParam,
3712
+ resolveBindingParams,
3475
3713
  resolveBindings,
3476
3714
  resolveDynamicValue,
3477
3715
  resolveElementProps,
@@ -3480,12 +3718,16 @@ export {
3480
3718
  resolveRepeatStatePath,
3481
3719
  runValidation,
3482
3720
  runValidationCheck,
3721
+ runWatchBindings,
3483
3722
  setByPath,
3484
3723
  setCatalogSlotMetadata,
3724
+ setRenderErrorHandler,
3485
3725
  splitRepeatVisibility,
3486
3726
  subscribeDevtoolsActive,
3487
3727
  validateSpec,
3488
3728
  visibility,
3489
- warnElementSlots
3729
+ warnElementSlots,
3730
+ warnUnusedRenderErrorHandler,
3731
+ withRenderErrorHandler
3490
3732
  };
3491
3733
  //# sourceMappingURL=index.mjs.map