@fictjs/runtime 0.4.0 → 0.5.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.
Files changed (72) hide show
  1. package/dist/advanced.cjs +10 -8
  2. package/dist/advanced.cjs.map +1 -1
  3. package/dist/advanced.d.cts +4 -3
  4. package/dist/advanced.d.ts +4 -3
  5. package/dist/advanced.js +10 -8
  6. package/dist/advanced.js.map +1 -1
  7. package/dist/{chunk-L4DIV3RC.cjs → chunk-4ZPZM5IG.cjs} +9 -7
  8. package/dist/chunk-4ZPZM5IG.cjs.map +1 -0
  9. package/dist/{chunk-XLIZJMMJ.js → chunk-5OYBRKE4.js} +8 -6
  10. package/dist/{chunk-XLIZJMMJ.js.map → chunk-5OYBRKE4.js.map} +1 -1
  11. package/dist/chunk-6RCEIWZL.cjs +2380 -0
  12. package/dist/chunk-6RCEIWZL.cjs.map +1 -0
  13. package/dist/chunk-7BO6P2KP.js +2380 -0
  14. package/dist/chunk-7BO6P2KP.js.map +1 -0
  15. package/dist/{chunk-TWELIZRY.js → chunk-AR6NSCZM.js} +5 -3
  16. package/dist/{chunk-TWELIZRY.js.map → chunk-AR6NSCZM.js.map} +1 -1
  17. package/dist/{chunk-M2TSXZ4C.cjs → chunk-LFMXNQZC.cjs} +18 -16
  18. package/dist/chunk-LFMXNQZC.cjs.map +1 -0
  19. package/dist/{chunk-SO6X7G5S.js → chunk-RY5CY4CI.js} +501 -1880
  20. package/dist/chunk-RY5CY4CI.js.map +1 -0
  21. package/dist/chunk-WJHXPF7M.cjs +2259 -0
  22. package/dist/chunk-WJHXPF7M.cjs.map +1 -0
  23. package/dist/{context-B25xyQrJ.d.cts → context-CTBE00S_.d.cts} +1 -1
  24. package/dist/{context-CGdP7_Jb.d.ts → context-lkLhbkFJ.d.ts} +1 -1
  25. package/dist/{effect-D6kaLM2-.d.cts → effect-BpSNEJJz.d.cts} +7 -67
  26. package/dist/{effect-D6kaLM2-.d.ts → effect-BpSNEJJz.d.ts} +7 -67
  27. package/dist/index.cjs +40 -38
  28. package/dist/index.cjs.map +1 -1
  29. package/dist/index.d.cts +5 -4
  30. package/dist/index.d.ts +5 -4
  31. package/dist/index.dev.js +125 -22
  32. package/dist/index.dev.js.map +1 -1
  33. package/dist/index.js +19 -17
  34. package/dist/index.js.map +1 -1
  35. package/dist/internal.cjs +202 -203
  36. package/dist/internal.cjs.map +1 -1
  37. package/dist/internal.d.cts +13 -23
  38. package/dist/internal.d.ts +13 -23
  39. package/dist/internal.js +207 -208
  40. package/dist/internal.js.map +1 -1
  41. package/dist/loader.cjs +280 -0
  42. package/dist/loader.cjs.map +1 -0
  43. package/dist/loader.d.cts +57 -0
  44. package/dist/loader.d.ts +57 -0
  45. package/dist/loader.js +280 -0
  46. package/dist/loader.js.map +1 -0
  47. package/dist/{props-BIfromL0.d.cts → props-XTHYD19o.d.cts} +13 -2
  48. package/dist/{props-BEgIVMRx.d.ts → props-x-HbI-jX.d.ts} +13 -2
  49. package/dist/resume-BrAkmSTY.d.cts +79 -0
  50. package/dist/resume-Dx8_l72o.d.ts +79 -0
  51. package/dist/{scope-CzNkn587.d.ts → scope-CdbGmsFf.d.ts} +1 -1
  52. package/dist/{scope-Cx_3CjIZ.d.cts → scope-DfcP9I-A.d.cts} +1 -1
  53. package/dist/signal-C4ISF17w.d.cts +66 -0
  54. package/dist/signal-C4ISF17w.d.ts +66 -0
  55. package/package.json +6 -1
  56. package/src/binding.ts +254 -5
  57. package/src/cycle-guard.ts +1 -1
  58. package/src/dom.ts +103 -5
  59. package/src/hooks.ts +15 -2
  60. package/src/hydration.ts +75 -0
  61. package/src/internal.ts +34 -2
  62. package/src/list-helpers.ts +127 -11
  63. package/src/loader.ts +437 -0
  64. package/src/node-ops.ts +65 -0
  65. package/src/resume.ts +517 -0
  66. package/src/signal.ts +47 -22
  67. package/src/store.ts +8 -0
  68. package/dist/chunk-ID3WBWNO.cjs +0 -3638
  69. package/dist/chunk-ID3WBWNO.cjs.map +0 -1
  70. package/dist/chunk-L4DIV3RC.cjs.map +0 -1
  71. package/dist/chunk-M2TSXZ4C.cjs.map +0 -1
  72. package/dist/chunk-SO6X7G5S.js.map +0 -1
package/dist/index.dev.js CHANGED
@@ -22,7 +22,7 @@ var enterRootGuard = () => true;
22
22
  var exitRootGuard = () => {
23
23
  };
24
24
  var defaultOptions = {
25
- enabled: true,
25
+ enabled: isDev,
26
26
  maxFlushCyclesPerMicrotask: 1e4,
27
27
  maxEffectRunsPerFlush: 2e4,
28
28
  windowSize: 5,
@@ -495,8 +495,10 @@ function createRenderEffect(fn) {
495
495
  // src/hooks.ts
496
496
  var isDev3 = true ? true : typeof process === "undefined" || process.env?.NODE_ENV !== "production";
497
497
  var ctxStack = [];
498
+ var preparedContext = null;
498
499
  function __fictPushContext() {
499
- const ctx = { slots: [], cursor: 0 };
500
+ const ctx = preparedContext ?? { slots: [], cursor: 0 };
501
+ preparedContext = null;
500
502
  ctxStack.push(ctx);
501
503
  return ctx;
502
504
  }
@@ -524,6 +526,8 @@ var cycle = 0;
524
526
  var batchDepth = 0;
525
527
  var activeSub;
526
528
  var flushScheduled = false;
529
+ var currentFlushId = 0;
530
+ var activeCleanupFlushId = 0;
527
531
  var highPriorityQueue = [];
528
532
  var lowPriorityQueue = [];
529
533
  var isInTransition = false;
@@ -769,6 +773,8 @@ function updateSignal(s) {
769
773
  const current = s.currentValue;
770
774
  const pending = s.pendingValue;
771
775
  if (valuesDiffer(s, current, pending)) {
776
+ s.prevValue = current;
777
+ s.prevFlushId = currentFlushId;
772
778
  s.currentValue = pending;
773
779
  return true;
774
780
  }
@@ -787,6 +793,8 @@ function updateComputed(c) {
787
793
  c.flags &= ~Running;
788
794
  purgeDeps(c);
789
795
  if (valuesDiffer(c, oldValue, newValue)) {
796
+ c.prevValue = oldValue;
797
+ c.prevFlushId = currentFlushId;
790
798
  c.value = newValue;
791
799
  if (isDev4) updateComputedDevtools(c, newValue);
792
800
  return true;
@@ -800,15 +808,19 @@ function updateComputed(c) {
800
808
  }
801
809
  function runEffect(e) {
802
810
  const flags = e.flags;
803
- if (flags & Dirty) {
804
- if (e.runCleanup) {
805
- inCleanup = true;
806
- try {
807
- e.runCleanup();
808
- } finally {
809
- inCleanup = false;
810
- }
811
+ const runCleanup = () => {
812
+ if (!e.runCleanup) return;
813
+ inCleanup = true;
814
+ activeCleanupFlushId = currentFlushId;
815
+ try {
816
+ e.runCleanup();
817
+ } finally {
818
+ activeCleanupFlushId = 0;
819
+ inCleanup = false;
811
820
  }
821
+ };
822
+ if (flags & Dirty) {
823
+ runCleanup();
812
824
  ++cycle;
813
825
  if (isDev4) effectRunDevtools(e);
814
826
  e.depsTail = void 0;
@@ -826,14 +838,6 @@ function runEffect(e) {
826
838
  throw err;
827
839
  }
828
840
  } else if (flags & Pending && e.deps) {
829
- if (e.runCleanup) {
830
- inCleanup = true;
831
- try {
832
- e.runCleanup();
833
- } finally {
834
- inCleanup = false;
835
- }
836
- }
837
841
  let isDirty = false;
838
842
  try {
839
843
  isDirty = checkDirty(e.deps, e);
@@ -853,6 +857,7 @@ function runEffect(e) {
853
857
  throw err;
854
858
  }
855
859
  if (isDirty) {
860
+ runCleanup();
856
861
  ++cycle;
857
862
  if (isDev4) effectRunDevtools(e);
858
863
  e.depsTail = void 0;
@@ -898,6 +903,7 @@ function flush() {
898
903
  endFlushGuard();
899
904
  return;
900
905
  }
906
+ currentFlushId++;
901
907
  flushScheduled = false;
902
908
  let highIndex = 0;
903
909
  while (highIndex < highPriorityQueue.length) {
@@ -1002,6 +1008,12 @@ function signalOper(value) {
1002
1008
  if (subs !== void 0) shallowPropagate(subs);
1003
1009
  }
1004
1010
  }
1011
+ if (inCleanup) {
1012
+ if (this.prevFlushId === activeCleanupFlushId) {
1013
+ return this.prevValue;
1014
+ }
1015
+ return this.currentValue;
1016
+ }
1005
1017
  let sub = activeSub;
1006
1018
  while (sub !== void 0) {
1007
1019
  if (sub.flags & 3) {
@@ -1035,7 +1047,12 @@ function computed(getter, options2) {
1035
1047
  return bound;
1036
1048
  }
1037
1049
  function computedOper() {
1038
- if (inCleanup) return this.value;
1050
+ if (inCleanup) {
1051
+ if (this.prevFlushId === activeCleanupFlushId) {
1052
+ return this.prevValue;
1053
+ }
1054
+ return this.value;
1055
+ }
1039
1056
  const flags = this.flags;
1040
1057
  if (flags & Dirty) {
1041
1058
  if (updateComputed(this)) {
@@ -1673,6 +1690,22 @@ var unitlessList = isDev5 ? [
1673
1690
  ] : ["opacity", "zIndex"];
1674
1691
  var UnitlessStyles = new Set(unitlessList);
1675
1692
 
1693
+ // src/hydration.ts
1694
+ var hydrationStack = [];
1695
+ function withHydration(root, fn) {
1696
+ const owner = root.ownerDocument ?? document;
1697
+ hydrationStack.push({
1698
+ cursor: root.firstChild,
1699
+ boundary: null,
1700
+ owner
1701
+ });
1702
+ try {
1703
+ fn();
1704
+ } finally {
1705
+ hydrationStack.pop();
1706
+ }
1707
+ }
1708
+
1676
1709
  // src/node-ops.ts
1677
1710
  function toNodeArray(node) {
1678
1711
  try {
@@ -1816,6 +1849,40 @@ function removeNodes(nodes) {
1816
1849
  }
1817
1850
  }
1818
1851
 
1852
+ // src/resume.ts
1853
+ var ssrEnabled = false;
1854
+ var resumableEnabled = false;
1855
+ var hydrating = false;
1856
+ var scopeCounter = 0;
1857
+ var scopeRegistry = /* @__PURE__ */ new Map();
1858
+ function __fictIsResumable() {
1859
+ return ssrEnabled || resumableEnabled;
1860
+ }
1861
+ function __fictIsHydrating() {
1862
+ return hydrating;
1863
+ }
1864
+ function __fictRegisterScope(ctx, host, type, props) {
1865
+ if (!__fictIsResumable()) return "";
1866
+ const id = `s${++scopeCounter}`;
1867
+ ctx.scopeId = id;
1868
+ if (type !== void 0) {
1869
+ ctx.scopeType = type;
1870
+ }
1871
+ host.setAttribute("data-fict-s", id);
1872
+ if (type) {
1873
+ host.setAttribute("data-fict-t", type);
1874
+ }
1875
+ const record = { id, ctx, host };
1876
+ if (type !== void 0) {
1877
+ record.type = type;
1878
+ }
1879
+ if (props !== void 0) {
1880
+ record.props = props;
1881
+ }
1882
+ scopeRegistry.set(id, record);
1883
+ return id;
1884
+ }
1885
+
1819
1886
  // src/binding.ts
1820
1887
  var isDev6 = true ? true : typeof process === "undefined" || process.env?.NODE_ENV !== "production";
1821
1888
  function isReactive(value) {
@@ -2400,14 +2467,22 @@ var nextComponentId = 1;
2400
2467
  function render(view, container) {
2401
2468
  const root = createRootContext();
2402
2469
  const prev = pushRoot(root);
2403
- let dom;
2470
+ let dom = void 0;
2404
2471
  try {
2405
2472
  const output = view();
2406
- dom = createElement(output);
2473
+ if (__fictIsHydrating()) {
2474
+ withHydration(container, () => {
2475
+ dom = createElement(output);
2476
+ });
2477
+ } else {
2478
+ dom = createElement(output);
2479
+ }
2407
2480
  } finally {
2408
2481
  popRoot(prev);
2409
2482
  }
2410
- container.replaceChildren(dom);
2483
+ if (!__fictIsHydrating()) {
2484
+ container.replaceChildren(dom);
2485
+ }
2411
2486
  container.setAttribute("data-fict-fine-grained", "1");
2412
2487
  flushOnMount(root);
2413
2488
  const teardown = () => {
@@ -2434,6 +2509,13 @@ function createElementWithContext(node, namespace) {
2434
2509
  if (node === null || node === void 0 || node === false) {
2435
2510
  return document.createTextNode("");
2436
2511
  }
2512
+ if (isReactive(node)) {
2513
+ const resolved = node();
2514
+ if (resolved === node) {
2515
+ return document.createTextNode("");
2516
+ }
2517
+ return createElementWithContext(resolved, namespace);
2518
+ }
2437
2519
  if (typeof node === "object" && node !== null && !(node instanceof Node)) {
2438
2520
  if ("marker" in node) {
2439
2521
  const handle = node;
@@ -2510,6 +2592,27 @@ function createElementWithContext(node, namespace) {
2510
2592
  });
2511
2593
  onCleanup(() => hook.componentUnmount?.(componentId));
2512
2594
  }
2595
+ if (__fictIsResumable() && !__fictIsHydrating()) {
2596
+ const content = createElementWithContext(rendered, namespace);
2597
+ const host = namespace === "svg" ? document.createElementNS(SVG_NS, "fict-host") : namespace === "mathml" ? document.createElementNS(MATHML_NS, "fict-host") : document.createElement("fict-host");
2598
+ host.setAttribute("data-fict-host", "");
2599
+ if (namespace === null && host.style) {
2600
+ ;
2601
+ host.style.display = "contents";
2602
+ }
2603
+ const meta = vnode.type.__fictMeta;
2604
+ const typeKey = (meta?.id ?? vnode.type.name) || "Anonymous";
2605
+ __fictRegisterScope(ctx, host, typeKey, rawProps);
2606
+ if (meta?.resume) {
2607
+ host.setAttribute("data-fict-h", meta.resume);
2608
+ }
2609
+ if (content instanceof DocumentFragment) {
2610
+ host.append(...Array.from(content.childNodes));
2611
+ } else {
2612
+ host.appendChild(content);
2613
+ }
2614
+ return host;
2615
+ }
2513
2616
  return createElementWithContext(rendered, namespace);
2514
2617
  } catch (err) {
2515
2618
  if (handleSuspend(err)) {