@fictjs/runtime 0.0.7 → 0.0.9

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.js CHANGED
@@ -887,6 +887,7 @@ function createSelector(source, equalityFn = (a, b) => a === b) {
887
887
  // src/store.ts
888
888
  var PROXY = Symbol("fict:store-proxy");
889
889
  var TARGET = Symbol("fict:store-target");
890
+ var ITERATE_KEY = Symbol("fict:iterate");
890
891
  function createStore(initialValue) {
891
892
  const unwrapped = unwrap(initialValue);
892
893
  const wrapped = wrap(unwrapped);
@@ -914,20 +915,41 @@ function wrap(value) {
914
915
  track(target, prop);
915
916
  return wrap(value2);
916
917
  },
918
+ has(target, prop) {
919
+ const result = Reflect.has(target, prop);
920
+ track(target, prop);
921
+ return result;
922
+ },
923
+ ownKeys(target) {
924
+ track(target, ITERATE_KEY);
925
+ return Reflect.ownKeys(target);
926
+ },
927
+ getOwnPropertyDescriptor(target, prop) {
928
+ track(target, prop);
929
+ return Reflect.getOwnPropertyDescriptor(target, prop);
930
+ },
917
931
  set(target, prop, value2, receiver) {
918
932
  if (prop === PROXY || prop === TARGET) return false;
933
+ const hadKey = Object.prototype.hasOwnProperty.call(target, prop);
919
934
  const oldValue = Reflect.get(target, prop, receiver);
920
935
  if (oldValue === value2) return true;
921
936
  const result = Reflect.set(target, prop, value2, receiver);
922
937
  if (result) {
923
938
  trigger(target, prop);
939
+ if (!hadKey) {
940
+ trigger(target, ITERATE_KEY);
941
+ }
924
942
  }
925
943
  return result;
926
944
  },
927
945
  deleteProperty(target, prop) {
946
+ const hadKey = Object.prototype.hasOwnProperty.call(target, prop);
928
947
  const result = Reflect.deleteProperty(target, prop);
929
948
  if (result) {
930
949
  trigger(target, prop);
950
+ if (hadKey) {
951
+ trigger(target, ITERATE_KEY);
952
+ }
931
953
  }
932
954
  return result;
933
955
  }
@@ -950,7 +972,8 @@ function track(target, prop) {
950
972
  }
951
973
  let s = signals.get(prop);
952
974
  if (!s) {
953
- s = signal(getLastValue(target, prop));
975
+ const initial = prop === ITERATE_KEY ? Reflect.ownKeys(target).length : getLastValue(target, prop);
976
+ s = signal(initial);
954
977
  signals.set(prop, s);
955
978
  }
956
979
  s();
@@ -960,7 +983,11 @@ function trigger(target, prop) {
960
983
  if (signals) {
961
984
  const s = signals.get(prop);
962
985
  if (s) {
963
- s(getLastValue(target, prop));
986
+ if (prop === ITERATE_KEY) {
987
+ s(Reflect.ownKeys(target).length);
988
+ } else {
989
+ s(getLastValue(target, prop));
990
+ }
964
991
  }
965
992
  }
966
993
  }
@@ -1568,2614 +1595,2545 @@ function removeNodes(nodes) {
1568
1595
  }
1569
1596
  }
1570
1597
 
1571
- // src/binding.ts
1572
- function isReactive(value) {
1573
- return typeof value === "function" && value.length === 0;
1598
+ // src/hooks.ts
1599
+ var ctxStack = [];
1600
+ function __fictUseContext() {
1601
+ if (ctxStack.length === 0) {
1602
+ const ctx2 = { slots: [], cursor: 0 };
1603
+ ctxStack.push(ctx2);
1604
+ return ctx2;
1605
+ }
1606
+ const ctx = ctxStack[ctxStack.length - 1];
1607
+ ctx.cursor = 0;
1608
+ return ctx;
1574
1609
  }
1575
- function unwrap2(value) {
1576
- return isReactive(value) ? value() : value;
1610
+ function __fictPushContext() {
1611
+ const ctx = { slots: [], cursor: 0 };
1612
+ ctxStack.push(ctx);
1613
+ return ctx;
1577
1614
  }
1578
- function callEventHandler(handler, event, node, data) {
1579
- if (!handler) return;
1580
- const context = node ?? event.currentTarget ?? void 0;
1581
- const invoke = (fn) => {
1582
- if (typeof fn === "function") {
1583
- const result = data === void 0 ? fn.call(context, event) : fn.call(context, data, event);
1584
- if (typeof result === "function" && result !== fn) {
1585
- if (data === void 0) {
1586
- result.call(context, event);
1587
- } else {
1588
- result.call(context, data, event);
1589
- }
1590
- } else if (result && typeof result.handleEvent === "function") {
1591
- result.handleEvent.call(result, event);
1615
+ function __fictPopContext() {
1616
+ ctxStack.pop();
1617
+ }
1618
+ function __fictResetContext() {
1619
+ ctxStack.length = 0;
1620
+ }
1621
+ function __fictUseSignal(ctx, initial, slot) {
1622
+ const index = slot ?? ctx.cursor++;
1623
+ if (!ctx.slots[index]) {
1624
+ ctx.slots[index] = signal(initial);
1625
+ }
1626
+ return ctx.slots[index];
1627
+ }
1628
+ function __fictUseMemo(ctx, fn, slot) {
1629
+ const index = slot ?? ctx.cursor++;
1630
+ if (!ctx.slots[index]) {
1631
+ ctx.slots[index] = createMemo(fn);
1632
+ }
1633
+ return ctx.slots[index];
1634
+ }
1635
+ function __fictUseEffect(ctx, fn, slot) {
1636
+ const index = slot ?? ctx.cursor++;
1637
+ if (!ctx.slots[index]) {
1638
+ ctx.slots[index] = createEffect(fn);
1639
+ }
1640
+ }
1641
+ function __fictRender(ctx, fn) {
1642
+ ctxStack.push(ctx);
1643
+ ctx.cursor = 0;
1644
+ try {
1645
+ return fn();
1646
+ } finally {
1647
+ ctxStack.pop();
1648
+ }
1649
+ }
1650
+
1651
+ // src/props.ts
1652
+ var propGetters = /* @__PURE__ */ new WeakSet();
1653
+ var rawToProxy = /* @__PURE__ */ new WeakMap();
1654
+ var proxyToRaw = /* @__PURE__ */ new WeakMap();
1655
+ function __fictProp(getter) {
1656
+ if (typeof getter === "function" && getter.length === 0) {
1657
+ propGetters.add(getter);
1658
+ }
1659
+ return getter;
1660
+ }
1661
+ function isPropGetter(value) {
1662
+ return typeof value === "function" && propGetters.has(value);
1663
+ }
1664
+ function createPropsProxy(props) {
1665
+ if (!props || typeof props !== "object") {
1666
+ return props;
1667
+ }
1668
+ if (proxyToRaw.has(props)) {
1669
+ return props;
1670
+ }
1671
+ const cached = rawToProxy.get(props);
1672
+ if (cached) {
1673
+ return cached;
1674
+ }
1675
+ const proxy = new Proxy(props, {
1676
+ get(target, prop, receiver) {
1677
+ const value = Reflect.get(target, prop, receiver);
1678
+ if (isPropGetter(value)) {
1679
+ return value();
1592
1680
  }
1593
- } else if (fn && typeof fn.handleEvent === "function") {
1594
- fn.handleEvent.call(fn, event);
1681
+ return value;
1682
+ },
1683
+ set(target, prop, value, receiver) {
1684
+ return Reflect.set(target, prop, value, receiver);
1685
+ },
1686
+ has(target, prop) {
1687
+ return prop in target;
1688
+ },
1689
+ ownKeys(target) {
1690
+ return Reflect.ownKeys(target);
1691
+ },
1692
+ getOwnPropertyDescriptor(target, prop) {
1693
+ return Object.getOwnPropertyDescriptor(target, prop);
1595
1694
  }
1596
- };
1597
- invoke(handler);
1695
+ });
1696
+ rawToProxy.set(props, proxy);
1697
+ proxyToRaw.set(proxy, props);
1698
+ return proxy;
1598
1699
  }
1599
- var PRIMITIVE_PROXY = Symbol("fict:primitive-proxy");
1600
- var PRIMITIVE_PROXY_RAW_VALUE = Symbol("fict:primitive-proxy:raw-value");
1601
- function unwrapPrimitive(value) {
1602
- if (value && typeof value === "object" && PRIMITIVE_PROXY in value) {
1603
- const getRawValue = value[PRIMITIVE_PROXY_RAW_VALUE];
1604
- if (typeof getRawValue === "function") {
1605
- return getRawValue();
1606
- }
1700
+ function unwrapProps(props) {
1701
+ if (!props || typeof props !== "object") {
1702
+ return props;
1607
1703
  }
1608
- return value;
1704
+ return proxyToRaw.get(props) ?? props;
1609
1705
  }
1610
- function createValueProxy(read) {
1611
- const getPrimitivePrototype = (value) => {
1612
- switch (typeof value) {
1613
- case "string":
1614
- return String.prototype;
1615
- case "number":
1616
- return Number.prototype;
1617
- case "boolean":
1618
- return Boolean.prototype;
1619
- case "bigint":
1620
- return BigInt.prototype;
1621
- case "symbol":
1622
- return Symbol.prototype;
1623
- default:
1624
- return void 0;
1625
- }
1706
+ function __fictPropsRest(props, exclude) {
1707
+ const raw = unwrapProps(props);
1708
+ const out = {};
1709
+ const excludeSet = new Set(exclude);
1710
+ for (const key of Reflect.ownKeys(raw)) {
1711
+ if (excludeSet.has(key)) continue;
1712
+ out[key] = raw[key];
1713
+ }
1714
+ return createPropsProxy(out);
1715
+ }
1716
+ function mergeProps(...sources) {
1717
+ const validSources = sources.filter(
1718
+ (s) => s != null && (typeof s === "object" || typeof s === "function")
1719
+ );
1720
+ if (validSources.length === 0) {
1721
+ return {};
1722
+ }
1723
+ if (validSources.length === 1 && typeof validSources[0] === "object") {
1724
+ return validSources[0];
1725
+ }
1726
+ const resolveSource = (src) => {
1727
+ const value = typeof src === "function" ? src() : src;
1728
+ if (!value || typeof value !== "object") return void 0;
1729
+ return unwrapProps(value);
1626
1730
  };
1627
- const target = {};
1628
- const handler = {
1629
- get(_target, prop, receiver) {
1630
- if (prop === PRIMITIVE_PROXY) {
1631
- return true;
1632
- }
1633
- if (prop === PRIMITIVE_PROXY_RAW_VALUE) {
1634
- return () => read();
1635
- }
1636
- if (prop === Symbol.toPrimitive) {
1637
- return (hint) => {
1638
- const value2 = read();
1639
- if (value2 != null && (typeof value2 === "object" || typeof value2 === "function")) {
1640
- const toPrimitive = value2[Symbol.toPrimitive];
1641
- if (typeof toPrimitive === "function") {
1642
- return toPrimitive.call(value2, hint);
1643
- }
1644
- if (hint === "string") return value2.toString?.() ?? "[object Object]";
1645
- if (hint === "number") return value2.valueOf?.() ?? value2;
1646
- return value2.valueOf?.() ?? value2;
1647
- }
1648
- return value2;
1649
- };
1650
- }
1651
- if (prop === "valueOf") {
1652
- return () => {
1653
- const value2 = read();
1654
- if (value2 != null && (typeof value2 === "object" || typeof value2 === "function")) {
1655
- return typeof value2.valueOf === "function" ? value2.valueOf() : value2;
1656
- }
1657
- return value2;
1658
- };
1659
- }
1660
- if (prop === "toString") {
1661
- return () => String(read());
1662
- }
1663
- const value = read();
1664
- if (value != null && (typeof value === "object" || typeof value === "function")) {
1665
- return Reflect.get(value, prop, receiver === _target ? value : receiver);
1731
+ return new Proxy({}, {
1732
+ get(_, prop) {
1733
+ if (typeof prop === "symbol") {
1734
+ return void 0;
1666
1735
  }
1667
- const proto = getPrimitivePrototype(value);
1668
- if (proto && prop in proto) {
1669
- const descriptor = Reflect.get(proto, prop, value);
1670
- return typeof descriptor === "function" ? descriptor.bind(value) : descriptor;
1736
+ for (let i = validSources.length - 1; i >= 0; i--) {
1737
+ const src = validSources[i];
1738
+ const raw = resolveSource(src);
1739
+ if (!raw || !(prop in raw)) continue;
1740
+ const value = raw[prop];
1741
+ if (typeof src === "function" && !isPropGetter(value)) {
1742
+ return __fictProp(() => {
1743
+ const latest = resolveSource(src);
1744
+ if (!latest || !(prop in latest)) return void 0;
1745
+ return latest[prop];
1746
+ });
1747
+ }
1748
+ return value;
1671
1749
  }
1672
1750
  return void 0;
1673
1751
  },
1674
- set(_target, prop, newValue, receiver) {
1675
- const value = read();
1676
- if (value != null && (typeof value === "object" || typeof value === "function")) {
1677
- return Reflect.set(value, prop, newValue, receiver === _target ? value : receiver);
1752
+ has(_, prop) {
1753
+ for (const src of validSources) {
1754
+ const raw = resolveSource(src);
1755
+ if (raw && prop in raw) {
1756
+ return true;
1757
+ }
1678
1758
  }
1679
1759
  return false;
1680
1760
  },
1681
- has(_target, prop) {
1682
- if (prop === PRIMITIVE_PROXY || prop === PRIMITIVE_PROXY_RAW_VALUE) {
1683
- return true;
1684
- }
1685
- const value = read();
1686
- if (value != null && (typeof value === "object" || typeof value === "function")) {
1687
- return prop in value;
1688
- }
1689
- const proto = getPrimitivePrototype(value);
1690
- return proto ? prop in proto : false;
1691
- },
1692
1761
  ownKeys() {
1693
- const value = read();
1694
- if (value != null && (typeof value === "object" || typeof value === "function")) {
1695
- return Reflect.ownKeys(value);
1762
+ const keys = /* @__PURE__ */ new Set();
1763
+ for (const src of validSources) {
1764
+ const raw = resolveSource(src);
1765
+ if (raw) {
1766
+ for (const key of Reflect.ownKeys(raw)) {
1767
+ keys.add(key);
1768
+ }
1769
+ }
1696
1770
  }
1697
- const proto = getPrimitivePrototype(value);
1698
- return proto ? Reflect.ownKeys(proto) : [];
1771
+ return Array.from(keys);
1699
1772
  },
1700
- getOwnPropertyDescriptor(_target, prop) {
1701
- const value = read();
1702
- if (value != null && (typeof value === "object" || typeof value === "function")) {
1703
- return Object.getOwnPropertyDescriptor(value, prop);
1704
- }
1705
- const proto = getPrimitivePrototype(value);
1706
- return proto ? Object.getOwnPropertyDescriptor(proto, prop) || void 0 : void 0;
1707
- }
1708
- };
1709
- return new Proxy(target, handler);
1710
- }
1711
- function createTextBinding(value) {
1712
- const text = document.createTextNode("");
1713
- if (isReactive(value)) {
1714
- createRenderEffect(() => {
1715
- const v = value();
1716
- const fmt = formatTextValue(v);
1717
- if (text.data !== fmt) {
1718
- text.data = fmt;
1773
+ getOwnPropertyDescriptor(_, prop) {
1774
+ for (let i = validSources.length - 1; i >= 0; i--) {
1775
+ const raw = resolveSource(validSources[i]);
1776
+ if (raw && prop in raw) {
1777
+ return {
1778
+ enumerable: true,
1779
+ configurable: true,
1780
+ get: () => {
1781
+ const value = raw[prop];
1782
+ return value;
1783
+ }
1784
+ };
1785
+ }
1719
1786
  }
1720
- });
1721
- } else {
1722
- text.data = formatTextValue(value);
1723
- }
1724
- return text;
1725
- }
1726
- function bindText(textNode, getValue) {
1727
- return createRenderEffect(() => {
1728
- const value = formatTextValue(getValue());
1729
- if (textNode.data !== value) {
1730
- textNode.data = value;
1787
+ return void 0;
1731
1788
  }
1732
1789
  });
1733
1790
  }
1734
- function formatTextValue(value) {
1735
- if (value == null || value === false) {
1736
- return "";
1737
- }
1738
- return String(value);
1791
+ function useProp(getter) {
1792
+ return __fictProp(createMemo(getter));
1739
1793
  }
1740
- function createAttributeBinding(el, key, value, setter) {
1741
- if (isReactive(value)) {
1742
- createRenderEffect(() => {
1743
- setter(el, key, value());
1744
- });
1745
- } else {
1746
- setter(el, key, value);
1794
+
1795
+ // src/transition.ts
1796
+ function startTransition(fn) {
1797
+ const prev = setTransitionContext(true);
1798
+ try {
1799
+ fn();
1800
+ } finally {
1801
+ setTransitionContext(prev);
1802
+ scheduleFlush();
1747
1803
  }
1748
1804
  }
1749
- function bindAttribute(el, key, getValue) {
1750
- let prevValue = void 0;
1751
- return createRenderEffect(() => {
1752
- const value = getValue();
1753
- if (value === prevValue) return;
1754
- prevValue = value;
1755
- if (value === void 0 || value === null || value === false) {
1756
- el.removeAttribute(key);
1757
- } else if (value === true) {
1758
- el.setAttribute(key, "");
1759
- } else {
1760
- el.setAttribute(key, String(value));
1761
- }
1762
- });
1805
+ function useTransition() {
1806
+ const pending = signal(false);
1807
+ const start = (fn) => {
1808
+ pending(true);
1809
+ startTransition(() => {
1810
+ try {
1811
+ fn();
1812
+ } finally {
1813
+ pending(false);
1814
+ }
1815
+ });
1816
+ };
1817
+ return [() => pending(), start];
1763
1818
  }
1764
- function bindProperty(el, key, getValue) {
1765
- const PROPERTY_BINDING_KEYS = /* @__PURE__ */ new Set([
1766
- "value",
1767
- "checked",
1768
- "selected",
1769
- "disabled",
1770
- "readOnly",
1771
- "multiple",
1772
- "muted"
1773
- ]);
1774
- let prevValue = void 0;
1775
- return createRenderEffect(() => {
1776
- const next = getValue();
1777
- if (next === prevValue) return;
1778
- prevValue = next;
1779
- if (PROPERTY_BINDING_KEYS.has(key) && (next === void 0 || next === null)) {
1780
- const fallback = key === "checked" || key === "selected" ? false : "";
1781
- el[key] = fallback;
1782
- return;
1819
+ function useDeferredValue(getValue) {
1820
+ const deferredValue = signal(getValue());
1821
+ createEffect(() => {
1822
+ const newValue = getValue();
1823
+ const currentDeferred = untrack(() => deferredValue());
1824
+ if (currentDeferred !== newValue) {
1825
+ startTransition(() => {
1826
+ deferredValue(newValue);
1827
+ });
1783
1828
  }
1784
- ;
1785
- el[key] = next;
1786
1829
  });
1830
+ return () => deferredValue();
1787
1831
  }
1788
- function createStyleBinding(el, value) {
1789
- if (isReactive(value)) {
1790
- let prev;
1791
- createRenderEffect(() => {
1792
- const next = value();
1793
- applyStyle(el, next, prev);
1794
- prev = next;
1795
- });
1796
- } else {
1797
- applyStyle(el, value, void 0);
1798
- }
1832
+
1833
+ // src/scheduler.ts
1834
+ function batch2(fn) {
1835
+ return batch(fn);
1799
1836
  }
1800
- function bindStyle(el, getValue) {
1801
- let prev;
1802
- return createRenderEffect(() => {
1803
- const next = getValue();
1804
- applyStyle(el, next, prev);
1805
- prev = next;
1806
- });
1837
+ function untrack2(fn) {
1838
+ return untrack(fn);
1807
1839
  }
1808
- function applyStyle(el, value, prev) {
1809
- if (typeof value === "string") {
1810
- el.style.cssText = value;
1811
- } else if (value && typeof value === "object") {
1812
- const styles = value;
1813
- if (typeof prev === "string") {
1814
- el.style.cssText = "";
1815
- }
1816
- if (prev && typeof prev === "object") {
1817
- const prevStyles = prev;
1818
- for (const key of Object.keys(prevStyles)) {
1819
- if (!(key in styles)) {
1820
- const cssProperty = key.replace(/([A-Z])/g, "-$1").toLowerCase();
1821
- el.style.removeProperty(cssProperty);
1822
- }
1823
- }
1824
- }
1825
- for (const [prop, v] of Object.entries(styles)) {
1826
- if (v != null) {
1827
- const cssProperty = prop.replace(/([A-Z])/g, "-$1").toLowerCase();
1828
- const unitless = isUnitlessStyleProperty(prop) || isUnitlessStyleProperty(cssProperty);
1829
- const valueStr = typeof v === "number" && !unitless ? `${v}px` : String(v);
1830
- el.style.setProperty(cssProperty, valueStr);
1831
- } else {
1832
- const cssProperty = prop.replace(/([A-Z])/g, "-$1").toLowerCase();
1833
- el.style.removeProperty(cssProperty);
1834
- }
1835
- }
1836
- } else {
1837
- if (prev && typeof prev === "object") {
1838
- const prevStyles = prev;
1839
- for (const key of Object.keys(prevStyles)) {
1840
- const cssProperty = key.replace(/([A-Z])/g, "-$1").toLowerCase();
1841
- el.style.removeProperty(cssProperty);
1842
- }
1843
- } else if (typeof prev === "string") {
1844
- el.style.cssText = "";
1845
- }
1840
+
1841
+ // src/dom.ts
1842
+ var SVG_NS = "http://www.w3.org/2000/svg";
1843
+ var MATHML_NS = "http://www.w3.org/1998/Math/MathML";
1844
+ function render(view, container) {
1845
+ const root = createRootContext();
1846
+ const prev = pushRoot(root);
1847
+ let dom;
1848
+ try {
1849
+ const output = view();
1850
+ dom = createElement(output);
1851
+ } finally {
1852
+ popRoot(prev);
1846
1853
  }
1854
+ container.replaceChildren(dom);
1855
+ container.setAttribute("data-fict-fine-grained", "1");
1856
+ flushOnMount(root);
1857
+ const teardown = () => {
1858
+ destroyRoot(root);
1859
+ container.innerHTML = "";
1860
+ };
1861
+ return teardown;
1847
1862
  }
1848
- function isUnitlessStyleProperty(prop) {
1849
- return UnitlessStyles.has(prop);
1850
- }
1851
- function createClassBinding(el, value) {
1852
- if (isReactive(value)) {
1853
- let prev = {};
1854
- createRenderEffect(() => {
1855
- const next = value();
1856
- prev = applyClass(el, next, prev);
1857
- });
1858
- } else {
1859
- applyClass(el, value, {});
1860
- }
1863
+ function createElement(node) {
1864
+ return createElementWithContext(node, null);
1861
1865
  }
1862
- function bindClass(el, getValue) {
1863
- let prev = {};
1864
- return createRenderEffect(() => {
1865
- const next = getValue();
1866
- prev = applyClass(el, next, prev);
1867
- });
1866
+ function resolveNamespace(tagName, namespace) {
1867
+ if (tagName === "svg") return "svg";
1868
+ if (tagName === "math") return "mathml";
1869
+ if (namespace === "mathml") return "mathml";
1870
+ if (namespace === "svg") return "svg";
1871
+ if (SVGElements.has(tagName)) return "svg";
1872
+ return null;
1868
1873
  }
1869
- function toggleClassKey(node, key, value) {
1870
- const classNames = key.trim().split(/\s+/);
1871
- for (let i = 0, len = classNames.length; i < len; i++) {
1872
- node.classList.toggle(classNames[i], value);
1874
+ function createElementWithContext(node, namespace) {
1875
+ if (node instanceof Node) {
1876
+ return node;
1873
1877
  }
1874
- }
1875
- function applyClass(el, value, prev) {
1876
- const prevState = prev && typeof prev === "object" ? prev : {};
1877
- if (typeof value === "string") {
1878
- el.className = value;
1879
- return {};
1878
+ if (node === null || node === void 0 || node === false) {
1879
+ return document.createTextNode("");
1880
1880
  }
1881
- if (value && typeof value === "object") {
1882
- const classes = value;
1883
- const classKeys = Object.keys(classes);
1884
- const prevKeys = Object.keys(prevState);
1885
- for (let i = 0, len = prevKeys.length; i < len; i++) {
1886
- const key = prevKeys[i];
1887
- if (!key || key === "undefined" || classes[key]) continue;
1888
- toggleClassKey(el, key, false);
1889
- delete prevState[key];
1881
+ if (typeof node === "object" && node !== null && !(node instanceof Node)) {
1882
+ if ("marker" in node) {
1883
+ const handle = node;
1884
+ if (typeof handle.dispose === "function") {
1885
+ registerRootCleanup(handle.dispose);
1886
+ }
1887
+ return createElement(handle.marker);
1890
1888
  }
1891
- for (let i = 0, len = classKeys.length; i < len; i++) {
1892
- const key = classKeys[i];
1893
- const classValue = !!classes[key];
1894
- if (!key || key === "undefined" || prevState[key] === classValue || !classValue) continue;
1895
- toggleClassKey(el, key, true);
1896
- prevState[key] = classValue;
1889
+ const nodeRecord = node;
1890
+ if (nodeRecord[PRIMITIVE_PROXY]) {
1891
+ const primitiveGetter = nodeRecord[Symbol.toPrimitive];
1892
+ const value = typeof primitiveGetter === "function" ? primitiveGetter.call(node, "default") : node;
1893
+ return document.createTextNode(value == null || value === false ? "" : String(value));
1897
1894
  }
1898
- return prevState;
1899
1895
  }
1900
- if (!value) {
1901
- for (const key of Object.keys(prevState)) {
1902
- if (key && key !== "undefined") {
1903
- toggleClassKey(el, key, false);
1904
- }
1896
+ if (Array.isArray(node)) {
1897
+ const frag = document.createDocumentFragment();
1898
+ for (const child of node) {
1899
+ appendChildNode(frag, child, namespace);
1905
1900
  }
1906
- return {};
1901
+ return frag;
1907
1902
  }
1908
- return prevState;
1909
- }
1910
- function classList(node, value, prev = {}) {
1911
- return applyClass(node, value, prev);
1912
- }
1913
- function insert(parent, getValue, markerOrCreateElement, createElementFn) {
1914
- let marker;
1915
- let ownsMarker = false;
1916
- let createFn = createElementFn;
1917
- if (markerOrCreateElement instanceof Node) {
1918
- marker = markerOrCreateElement;
1919
- createFn = createElementFn;
1920
- } else {
1921
- marker = document.createComment("fict:insert");
1922
- parent.appendChild(marker);
1923
- createFn = markerOrCreateElement;
1924
- ownsMarker = true;
1903
+ if (typeof node === "string" || typeof node === "number") {
1904
+ return document.createTextNode(String(node));
1925
1905
  }
1926
- let currentNodes = [];
1927
- let currentText = null;
1928
- let currentRoot2 = null;
1929
- const clearCurrentNodes = () => {
1930
- if (currentNodes.length > 0) {
1931
- removeNodes(currentNodes);
1932
- currentNodes = [];
1933
- }
1934
- };
1935
- const setTextNode = (textValue, shouldInsert, parentNode) => {
1936
- if (!currentText) {
1937
- currentText = document.createTextNode(textValue);
1938
- } else if (currentText.data !== textValue) {
1939
- currentText.data = textValue;
1940
- }
1941
- if (!shouldInsert) {
1942
- clearCurrentNodes();
1943
- return;
1944
- }
1945
- if (currentNodes.length === 1 && currentNodes[0] === currentText) {
1946
- return;
1947
- }
1948
- clearCurrentNodes();
1949
- insertNodesBefore(parentNode, [currentText], marker);
1950
- currentNodes = [currentText];
1951
- };
1952
- const dispose = createRenderEffect(() => {
1953
- const value = getValue();
1954
- const parentNode = marker.parentNode;
1955
- const isPrimitive = value == null || value === false || typeof value === "string" || typeof value === "number" || typeof value === "boolean";
1956
- if (isPrimitive) {
1957
- if (currentRoot2) {
1958
- destroyRoot(currentRoot2);
1959
- currentRoot2 = null;
1960
- }
1961
- if (!parentNode) {
1962
- clearCurrentNodes();
1963
- return;
1964
- }
1965
- const textValue = value == null || value === false ? "" : String(value);
1966
- const shouldInsert = value != null && value !== false;
1967
- setTextNode(textValue, shouldInsert, parentNode);
1968
- return;
1969
- }
1970
- if (currentRoot2) {
1971
- destroyRoot(currentRoot2);
1972
- currentRoot2 = null;
1973
- }
1974
- clearCurrentNodes();
1975
- const root = createRootContext();
1976
- const prev = pushRoot(root);
1977
- let nodes = [];
1978
- try {
1979
- let newNode;
1980
- if (value instanceof Node) {
1981
- newNode = value;
1982
- } else if (Array.isArray(value)) {
1983
- if (value.every((v) => v instanceof Node)) {
1984
- newNode = value;
1985
- } else {
1986
- newNode = createFn ? createFn(value) : document.createTextNode(String(value));
1906
+ if (typeof node === "boolean") {
1907
+ return document.createTextNode("");
1908
+ }
1909
+ const vnode = node;
1910
+ if (typeof vnode.type === "function") {
1911
+ const rawProps = unwrapProps(vnode.props ?? {});
1912
+ const baseProps = vnode.key === void 0 ? rawProps : new Proxy(rawProps, {
1913
+ get(target, prop, receiver) {
1914
+ if (prop === "key") return vnode.key;
1915
+ return Reflect.get(target, prop, receiver);
1916
+ },
1917
+ has(target, prop) {
1918
+ if (prop === "key") return true;
1919
+ return prop in target;
1920
+ },
1921
+ ownKeys(target) {
1922
+ const keys = new Set(Reflect.ownKeys(target));
1923
+ keys.add("key");
1924
+ return Array.from(keys);
1925
+ },
1926
+ getOwnPropertyDescriptor(target, prop) {
1927
+ if (prop === "key") {
1928
+ return { enumerable: true, configurable: true, value: vnode.key };
1987
1929
  }
1988
- } else {
1989
- newNode = createFn ? createFn(value) : document.createTextNode(String(value));
1930
+ return Object.getOwnPropertyDescriptor(target, prop);
1990
1931
  }
1991
- nodes = toNodeArray(newNode);
1992
- if (parentNode) {
1993
- insertNodesBefore(parentNode, nodes, marker);
1932
+ });
1933
+ const props = createPropsProxy(baseProps);
1934
+ try {
1935
+ __fictPushContext();
1936
+ const rendered = vnode.type(props);
1937
+ __fictPopContext();
1938
+ return createElementWithContext(rendered, namespace);
1939
+ } catch (err) {
1940
+ __fictPopContext();
1941
+ if (handleSuspend(err)) {
1942
+ return document.createComment("fict:suspend");
1994
1943
  }
1995
- } finally {
1996
- popRoot(prev);
1997
- flushOnMount(root);
1944
+ handleError(err, { source: "render", componentName: vnode.type.name });
1945
+ throw err;
1998
1946
  }
1999
- currentRoot2 = root;
2000
- currentNodes = nodes;
2001
- });
2002
- return () => {
2003
- dispose();
2004
- if (currentRoot2) {
2005
- destroyRoot(currentRoot2);
2006
- currentRoot2 = null;
1947
+ }
1948
+ if (vnode.type === Fragment) {
1949
+ const frag = document.createDocumentFragment();
1950
+ const children = vnode.props?.children;
1951
+ appendChildren(frag, children, namespace);
1952
+ return frag;
1953
+ }
1954
+ const tagName = typeof vnode.type === "string" ? vnode.type : "div";
1955
+ const resolvedNamespace = resolveNamespace(tagName, namespace);
1956
+ const el = resolvedNamespace === "svg" ? document.createElementNS(SVG_NS, tagName) : resolvedNamespace === "mathml" ? document.createElementNS(MATHML_NS, tagName) : document.createElement(tagName);
1957
+ applyProps(el, vnode.props ?? {}, resolvedNamespace === "svg");
1958
+ appendChildren(
1959
+ el,
1960
+ vnode.props?.children,
1961
+ tagName === "foreignObject" ? null : resolvedNamespace
1962
+ );
1963
+ return el;
1964
+ }
1965
+ function template(html, isImportNode, isSVG, isMathML) {
1966
+ let node = null;
1967
+ const create = () => {
1968
+ const t = isMathML ? document.createElementNS(MATHML_NS, "template") : document.createElement("template");
1969
+ t.innerHTML = html;
1970
+ if (isSVG) {
1971
+ return t.content.firstChild.firstChild;
2007
1972
  }
2008
- clearCurrentNodes();
2009
- if (ownsMarker) {
2010
- marker.parentNode?.removeChild(marker);
1973
+ if (isMathML) {
1974
+ return t.firstChild;
2011
1975
  }
1976
+ return t.content.firstChild;
2012
1977
  };
1978
+ const fn = isImportNode ? () => untrack2(() => document.importNode(node || (node = create()), true)) : () => (node || (node = create())).cloneNode(true);
1979
+ fn.cloneNode = fn;
1980
+ return fn;
2013
1981
  }
2014
- function createChildBinding(parent, getValue, createElementFn) {
2015
- const marker = document.createComment("fict:child");
2016
- parent.appendChild(marker);
2017
- const dispose = createRenderEffect(() => {
2018
- const root = createRootContext();
2019
- const prev = pushRoot(root);
2020
- let nodes = [];
2021
- let handledError = false;
2022
- try {
2023
- const value = getValue();
2024
- if (value == null || value === false) {
2025
- return;
2026
- }
2027
- const output = createElementFn(value);
2028
- nodes = toNodeArray(output);
2029
- const parentNode = marker.parentNode;
2030
- if (parentNode) {
2031
- insertNodesBefore(parentNode, nodes, marker);
2032
- }
2033
- return () => {
2034
- destroyRoot(root);
2035
- removeNodes(nodes);
2036
- };
2037
- } catch (err) {
2038
- if (handleSuspend(err, root)) {
2039
- handledError = true;
2040
- destroyRoot(root);
2041
- return;
2042
- }
2043
- if (handleError(err, { source: "renderChild" }, root)) {
2044
- handledError = true;
2045
- destroyRoot(root);
2046
- return;
2047
- }
2048
- throw err;
2049
- } finally {
2050
- popRoot(prev);
2051
- if (!handledError) {
2052
- flushOnMount(root);
2053
- }
1982
+ function isBindingHandle(node) {
1983
+ return node !== null && typeof node === "object" && "marker" in node && "dispose" in node && typeof node.dispose === "function";
1984
+ }
1985
+ function appendChildNode(parent, child, namespace) {
1986
+ if (child === null || child === void 0 || child === false) {
1987
+ return;
1988
+ }
1989
+ if (isBindingHandle(child)) {
1990
+ appendChildNode(parent, child.marker, namespace);
1991
+ child.flush?.();
1992
+ return;
1993
+ }
1994
+ if (typeof child === "function" && child.length === 0) {
1995
+ const childGetter = child;
1996
+ createChildBinding(parent, childGetter, (node) => createElementWithContext(node, namespace));
1997
+ return;
1998
+ }
1999
+ if (Array.isArray(child)) {
2000
+ for (const item of child) {
2001
+ appendChildNode(parent, item, namespace);
2054
2002
  }
2055
- });
2056
- return {
2057
- marker,
2058
- dispose: () => {
2059
- dispose();
2060
- marker.parentNode?.removeChild(marker);
2003
+ return;
2004
+ }
2005
+ let domNode;
2006
+ if (typeof child !== "object" || child === null) {
2007
+ domNode = document.createTextNode(String(child ?? ""));
2008
+ } else {
2009
+ domNode = createElementWithContext(child, namespace);
2010
+ }
2011
+ if (domNode.nodeType === 11) {
2012
+ const children = Array.from(domNode.childNodes);
2013
+ for (const node of children) {
2014
+ appendChildNode(parent, node, namespace);
2061
2015
  }
2062
- };
2016
+ return;
2017
+ }
2018
+ if (domNode.ownerDocument !== parent.ownerDocument && parent.ownerDocument) {
2019
+ parent.ownerDocument.adoptNode(domNode);
2020
+ }
2021
+ try {
2022
+ parent.appendChild(domNode);
2023
+ } catch (e) {
2024
+ if (parent.ownerDocument) {
2025
+ const clone = parent.ownerDocument.importNode(domNode, true);
2026
+ parent.appendChild(clone);
2027
+ return;
2028
+ }
2029
+ throw e;
2030
+ }
2063
2031
  }
2064
- function delegateEvents(eventNames, doc = window.document) {
2065
- const e = doc[$$EVENTS] || (doc[$$EVENTS] = /* @__PURE__ */ new Set());
2066
- for (let i = 0, l = eventNames.length; i < l; i++) {
2067
- const name = eventNames[i];
2068
- if (!e.has(name)) {
2069
- e.add(name);
2070
- doc.addEventListener(name, globalEventHandler);
2032
+ function appendChildren(parent, children, namespace) {
2033
+ if (children === void 0) return;
2034
+ if (Array.isArray(children)) {
2035
+ for (const child of children) {
2036
+ appendChildren(parent, child, namespace);
2071
2037
  }
2038
+ return;
2072
2039
  }
2040
+ appendChildNode(parent, children, namespace);
2073
2041
  }
2074
- function clearDelegatedEvents(doc = window.document) {
2075
- const e = doc[$$EVENTS];
2076
- if (e) {
2077
- for (const name of e.keys()) {
2078
- doc.removeEventListener(name, globalEventHandler);
2042
+ function applyRef(el, value) {
2043
+ if (typeof value === "function") {
2044
+ const refFn = value;
2045
+ refFn(el);
2046
+ if (getCurrentRoot()) {
2047
+ registerRootCleanup(() => {
2048
+ refFn(null);
2049
+ });
2050
+ }
2051
+ } else if (value && typeof value === "object" && "current" in value) {
2052
+ const refObj = value;
2053
+ refObj.current = el;
2054
+ if (getCurrentRoot()) {
2055
+ registerRootCleanup(() => {
2056
+ refObj.current = null;
2057
+ });
2079
2058
  }
2080
- delete doc[$$EVENTS];
2081
2059
  }
2082
2060
  }
2083
- function globalEventHandler(e) {
2084
- let node = e.target;
2085
- const key = `$$${e.type}`;
2086
- const dataKey = `${key}Data`;
2087
- const oriTarget = e.target;
2088
- const oriCurrentTarget = e.currentTarget;
2089
- const retarget = (value) => Object.defineProperty(e, "target", {
2090
- configurable: true,
2091
- value
2092
- });
2093
- const handleNode = () => {
2094
- if (!node) return false;
2095
- const handler = node[key];
2096
- if (handler && !node.disabled) {
2097
- const resolveData = (value) => {
2098
- if (typeof value === "function") {
2099
- try {
2100
- const fn = value;
2101
- return fn.length > 0 ? fn(e) : fn();
2102
- } catch {
2103
- return value();
2104
- }
2061
+ function applyProps(el, props, isSVG = false) {
2062
+ props = unwrapProps(props);
2063
+ const tagName = el.tagName;
2064
+ const isCE = tagName.includes("-") || "is" in props;
2065
+ for (const [key, value] of Object.entries(props)) {
2066
+ if (key === "children") continue;
2067
+ if (key === "ref") {
2068
+ applyRef(el, value);
2069
+ continue;
2070
+ }
2071
+ if (isEventKey(key)) {
2072
+ bindEvent(
2073
+ el,
2074
+ eventNameFromProp(key),
2075
+ value
2076
+ );
2077
+ continue;
2078
+ }
2079
+ if (key.slice(0, 3) === "on:") {
2080
+ bindEvent(
2081
+ el,
2082
+ key.slice(3),
2083
+ value,
2084
+ false
2085
+ // Non-delegated
2086
+ );
2087
+ continue;
2088
+ }
2089
+ if (key.slice(0, 10) === "oncapture:") {
2090
+ bindEvent(
2091
+ el,
2092
+ key.slice(10),
2093
+ value,
2094
+ true
2095
+ // Capture
2096
+ );
2097
+ continue;
2098
+ }
2099
+ if (key === "class" || key === "className") {
2100
+ createClassBinding(el, value);
2101
+ continue;
2102
+ }
2103
+ if (key === "classList") {
2104
+ createClassBinding(el, value);
2105
+ continue;
2106
+ }
2107
+ if (key === "style") {
2108
+ createStyleBinding(
2109
+ el,
2110
+ value
2111
+ );
2112
+ continue;
2113
+ }
2114
+ if (key === "dangerouslySetInnerHTML" && value && typeof value === "object") {
2115
+ const htmlValue = value.__html;
2116
+ if (htmlValue !== void 0) {
2117
+ if (isReactive(htmlValue)) {
2118
+ createAttributeBinding(el, "innerHTML", htmlValue, setInnerHTML);
2119
+ } else {
2120
+ el.innerHTML = htmlValue;
2105
2121
  }
2106
- return value;
2107
- };
2108
- const rawData = node[dataKey];
2109
- const hasData = rawData !== void 0;
2110
- const resolvedNodeData = hasData ? resolveData(rawData) : void 0;
2111
- if (typeof handler === "function") {
2112
- callEventHandler(handler, e, node, hasData ? resolvedNodeData : void 0);
2113
- } else if (Array.isArray(handler)) {
2114
- const tupleData = resolveData(handler[1]);
2115
- callEventHandler(handler[0], e, node, tupleData);
2116
2122
  }
2117
- if (e.cancelBubble) return false;
2123
+ continue;
2118
2124
  }
2119
- const shadowHost = node.host;
2120
- if (shadowHost && typeof shadowHost !== "string" && !shadowHost._$host && node.contains(e.target)) {
2121
- retarget(shadowHost);
2125
+ if (ChildProperties.has(key)) {
2126
+ createAttributeBinding(el, key, value, setProperty);
2127
+ continue;
2122
2128
  }
2123
- return true;
2124
- };
2125
- const walkUpTree = () => {
2126
- while (handleNode() && node) {
2127
- node = node._$host || node.parentNode || node.host;
2129
+ if (key.slice(0, 5) === "attr:") {
2130
+ createAttributeBinding(el, key.slice(5), value, setAttribute);
2131
+ continue;
2128
2132
  }
2129
- };
2130
- Object.defineProperty(e, "currentTarget", {
2131
- configurable: true,
2132
- get() {
2133
- return node || document;
2133
+ if (key.slice(0, 5) === "bool:") {
2134
+ createAttributeBinding(el, key.slice(5), value, setBoolAttribute);
2135
+ continue;
2134
2136
  }
2135
- });
2136
- if (e.composedPath) {
2137
- const path = e.composedPath();
2138
- retarget(path[0]);
2139
- for (let i = 0; i < path.length - 2; i++) {
2140
- node = path[i];
2141
- if (!handleNode()) break;
2142
- if (node._$host) {
2143
- node = node._$host;
2144
- walkUpTree();
2145
- break;
2137
+ if (key.slice(0, 5) === "prop:") {
2138
+ createAttributeBinding(el, key.slice(5), value, setProperty);
2139
+ continue;
2140
+ }
2141
+ const propAlias = !isSVG ? getPropAlias(key, tagName) : void 0;
2142
+ if (propAlias || !isSVG && Properties.has(key) || isCE && !isSVG) {
2143
+ const propName = propAlias || key;
2144
+ if (isCE && !Properties.has(key)) {
2145
+ createAttributeBinding(
2146
+ el,
2147
+ toPropertyName(propName),
2148
+ value,
2149
+ setProperty
2150
+ );
2151
+ } else {
2152
+ createAttributeBinding(el, propName, value, setProperty);
2146
2153
  }
2147
- if (node.parentNode === oriCurrentTarget) {
2148
- break;
2154
+ continue;
2155
+ }
2156
+ if (isSVG && key.indexOf(":") > -1) {
2157
+ const [prefix, name] = key.split(":");
2158
+ const ns = SVGNamespace[prefix];
2159
+ if (ns) {
2160
+ createAttributeBinding(
2161
+ el,
2162
+ key,
2163
+ value,
2164
+ (el2, _key, val) => setAttributeNS(el2, ns, name, val)
2165
+ );
2166
+ continue;
2149
2167
  }
2150
2168
  }
2151
- } else {
2152
- walkUpTree();
2169
+ const attrName = Aliases[key] || key;
2170
+ createAttributeBinding(el, attrName, value, setAttribute);
2153
2171
  }
2154
- retarget(oriTarget);
2155
2172
  }
2156
- function addEventListener(node, name, handler, delegate) {
2157
- if (handler == null) return;
2158
- if (delegate) {
2159
- if (Array.isArray(handler)) {
2160
- node[`$$${name}`] = handler[0];
2161
- node[`$$${name}Data`] = handler[1];
2162
- } else {
2163
- node[`$$${name}`] = handler;
2164
- }
2165
- } else if (Array.isArray(handler)) {
2166
- const handlerFn = handler[0];
2167
- node.addEventListener(name, (e) => handlerFn.call(node, handler[1], e));
2168
- } else {
2169
- node.addEventListener(name, handler);
2170
- }
2173
+ function toPropertyName(name) {
2174
+ return name.toLowerCase().replace(/-([a-z])/g, (_, w) => w.toUpperCase());
2171
2175
  }
2172
- function bindEvent(el, eventName, handler, options2) {
2173
- if (handler == null) return () => {
2174
- };
2175
- const rootRef = getCurrentRoot();
2176
- if (DelegatedEvents.has(eventName) && !options2) {
2177
- const key = `$$${eventName}`;
2178
- delegateEvents([eventName]);
2179
- const resolveHandler = isReactive(handler) ? handler : () => handler;
2180
- el[key] = function(...args) {
2181
- try {
2182
- const fn = resolveHandler();
2183
- callEventHandler(fn, args[0], el);
2184
- } catch (err) {
2185
- handleError(err, { source: "event", eventName }, rootRef);
2186
- }
2187
- };
2188
- return () => {
2189
- el[key] = void 0;
2190
- };
2176
+ var setAttribute = (el, key, value) => {
2177
+ if (value === void 0 || value === null || value === false) {
2178
+ el.removeAttribute(key);
2179
+ return;
2191
2180
  }
2192
- const getHandler = isReactive(handler) ? handler : () => handler;
2193
- const wrapped = (event) => {
2194
- try {
2195
- const resolved = getHandler();
2196
- callEventHandler(resolved, event, el);
2197
- } catch (err) {
2198
- if (handleError(err, { source: "event", eventName }, rootRef)) {
2199
- return;
2181
+ if (value === true) {
2182
+ el.setAttribute(key, "");
2183
+ return;
2184
+ }
2185
+ const valueType = typeof value;
2186
+ if (valueType === "string" || valueType === "number") {
2187
+ el.setAttribute(key, String(value));
2188
+ return;
2189
+ }
2190
+ if (key in el) {
2191
+ el[key] = value;
2192
+ return;
2193
+ }
2194
+ el.setAttribute(key, String(value));
2195
+ };
2196
+ var setProperty = (el, key, value) => {
2197
+ if (value === void 0 || value === null) {
2198
+ const fallback = key === "checked" || key === "selected" ? false : "";
2199
+ el[key] = fallback;
2200
+ return;
2201
+ }
2202
+ if (key === "style" && typeof value === "object" && value !== null) {
2203
+ for (const k in value) {
2204
+ const v = value[k];
2205
+ if (v !== void 0) {
2206
+ el.style[k] = String(v);
2200
2207
  }
2201
- throw err;
2202
2208
  }
2203
- };
2204
- el.addEventListener(eventName, wrapped, options2);
2205
- const cleanup = () => el.removeEventListener(eventName, wrapped, options2);
2206
- registerRootCleanup(cleanup);
2207
- return cleanup;
2209
+ return;
2210
+ }
2211
+ el[key] = value;
2212
+ };
2213
+ var setInnerHTML = (el, _key, value) => {
2214
+ el.innerHTML = value == null ? "" : String(value);
2215
+ };
2216
+ var setBoolAttribute = (el, key, value) => {
2217
+ if (value) {
2218
+ el.setAttribute(key, "");
2219
+ } else {
2220
+ el.removeAttribute(key);
2221
+ }
2222
+ };
2223
+ function setAttributeNS(el, namespace, name, value) {
2224
+ if (value == null) {
2225
+ el.removeAttributeNS(namespace, name);
2226
+ } else {
2227
+ el.setAttributeNS(namespace, name, String(value));
2228
+ }
2208
2229
  }
2209
- function bindRef(el, ref) {
2210
- if (ref == null) return () => {
2211
- };
2212
- const getRef = isReactive(ref) ? ref : () => ref;
2213
- const applyRef2 = (refValue) => {
2214
- if (refValue == null) return;
2215
- if (typeof refValue === "function") {
2216
- refValue(el);
2217
- } else if (typeof refValue === "object" && "current" in refValue) {
2218
- refValue.current = el;
2230
+ function isEventKey(key) {
2231
+ return key.startsWith("on") && key.length > 2 && key[2].toUpperCase() === key[2];
2232
+ }
2233
+ function eventNameFromProp(key) {
2234
+ return key.slice(2).toLowerCase();
2235
+ }
2236
+
2237
+ // src/reconcile.ts
2238
+ function reconcileArrays(parentNode, a, b) {
2239
+ const bLength = b.length;
2240
+ let aEnd = a.length;
2241
+ let bEnd = bLength;
2242
+ let aStart = 0;
2243
+ let bStart = 0;
2244
+ const after = aEnd > 0 ? a[aEnd - 1].nextSibling : null;
2245
+ let map = null;
2246
+ while (aStart < aEnd || bStart < bEnd) {
2247
+ if (a[aStart] === b[bStart]) {
2248
+ aStart++;
2249
+ bStart++;
2250
+ continue;
2219
2251
  }
2220
- };
2221
- const initialRef = getRef();
2222
- applyRef2(initialRef);
2223
- if (isReactive(ref)) {
2224
- const cleanup2 = createRenderEffect(() => {
2225
- const currentRef = getRef();
2226
- applyRef2(currentRef);
2227
- });
2228
- registerRootCleanup(cleanup2);
2229
- const nullifyCleanup = () => {
2230
- const currentRef = getRef();
2231
- if (currentRef && typeof currentRef === "object" && "current" in currentRef) {
2232
- currentRef.current = null;
2252
+ while (a[aEnd - 1] === b[bEnd - 1]) {
2253
+ aEnd--;
2254
+ bEnd--;
2255
+ }
2256
+ if (aEnd === aStart) {
2257
+ const node = bEnd < bLength ? bStart ? b[bStart - 1].nextSibling : b[bEnd - bStart] ?? null : after;
2258
+ const count = bEnd - bStart;
2259
+ const doc = parentNode.ownerDocument;
2260
+ if (count > 1 && doc) {
2261
+ const frag = doc.createDocumentFragment();
2262
+ for (let i = bStart; i < bEnd; i++) {
2263
+ frag.appendChild(b[i]);
2264
+ }
2265
+ parentNode.insertBefore(frag, node);
2266
+ bStart = bEnd;
2267
+ } else {
2268
+ while (bStart < bEnd) {
2269
+ parentNode.insertBefore(b[bStart++], node);
2270
+ }
2271
+ }
2272
+ } else if (bEnd === bStart) {
2273
+ while (aStart < aEnd) {
2274
+ const nodeToRemove = a[aStart];
2275
+ if (!map || !map.has(nodeToRemove)) {
2276
+ nodeToRemove.parentNode?.removeChild(nodeToRemove);
2277
+ }
2278
+ aStart++;
2279
+ }
2280
+ } else if (a[aStart] === b[bEnd - 1] && b[bStart] === a[aEnd - 1]) {
2281
+ const node = a[--aEnd].nextSibling;
2282
+ parentNode.insertBefore(b[bStart++], a[aStart++].nextSibling);
2283
+ parentNode.insertBefore(b[--bEnd], node);
2284
+ a[aEnd] = b[bEnd];
2285
+ } else {
2286
+ if (!map) {
2287
+ map = /* @__PURE__ */ new Map();
2288
+ let i = bStart;
2289
+ while (i < bEnd) {
2290
+ map.set(b[i], i++);
2291
+ }
2292
+ }
2293
+ const index = map.get(a[aStart]);
2294
+ if (index != null) {
2295
+ if (bStart < index && index < bEnd) {
2296
+ let i = aStart;
2297
+ let sequence = 1;
2298
+ let t;
2299
+ while (++i < aEnd && i < bEnd) {
2300
+ t = map.get(a[i]);
2301
+ if (t == null || t !== index + sequence) break;
2302
+ sequence++;
2303
+ }
2304
+ if (sequence > index - bStart) {
2305
+ const node = a[aStart];
2306
+ while (bStart < index) {
2307
+ parentNode.insertBefore(b[bStart++], node);
2308
+ }
2309
+ } else {
2310
+ parentNode.replaceChild(b[bStart++], a[aStart++]);
2311
+ }
2312
+ } else {
2313
+ aStart++;
2314
+ }
2315
+ } else {
2316
+ const nodeToRemove = a[aStart++];
2317
+ nodeToRemove.parentNode?.removeChild(nodeToRemove);
2233
2318
  }
2234
- };
2235
- registerRootCleanup(nullifyCleanup);
2236
- return () => {
2237
- cleanup2();
2238
- nullifyCleanup();
2239
- };
2240
- }
2241
- const cleanup = () => {
2242
- const refValue = getRef();
2243
- if (refValue && typeof refValue === "object" && "current" in refValue) {
2244
- refValue.current = null;
2245
2319
  }
2246
- };
2247
- registerRootCleanup(cleanup);
2248
- return cleanup;
2249
- }
2250
- function spread(node, props = {}, isSVG = false, skipChildren = false) {
2251
- const prevProps = {};
2252
- if (!skipChildren && "children" in props) {
2253
- createRenderEffect(() => {
2254
- prevProps.children = props.children;
2255
- });
2256
2320
  }
2257
- createRenderEffect(() => {
2258
- if (typeof props.ref === "function") {
2259
- ;
2260
- props.ref(node);
2261
- }
2262
- });
2263
- createRenderEffect(() => {
2264
- assign(node, props, isSVG, true, prevProps, true);
2265
- });
2266
- return prevProps;
2267
2321
  }
2268
- function assign(node, props, isSVG = false, skipChildren = false, prevProps = {}, skipRef = false) {
2269
- props = props || {};
2270
- for (const prop in prevProps) {
2271
- if (!(prop in props)) {
2272
- if (prop === "children") continue;
2273
- prevProps[prop] = assignProp(node, prop, null, prevProps[prop], isSVG, skipRef, props);
2322
+
2323
+ // src/list-helpers.ts
2324
+ function moveNodesBefore(parent, nodes, anchor) {
2325
+ for (let i = nodes.length - 1; i >= 0; i--) {
2326
+ const node = nodes[i];
2327
+ if (!node || !(node instanceof Node)) {
2328
+ throw new Error("Invalid node in moveNodesBefore");
2274
2329
  }
2275
- }
2276
- for (const prop in props) {
2277
- if (prop === "children") {
2278
- if (!skipChildren) {
2279
- prevProps.children = props.children;
2330
+ if (node.nextSibling !== anchor) {
2331
+ if (node.ownerDocument !== parent.ownerDocument && parent.ownerDocument) {
2332
+ parent.ownerDocument.adoptNode(node);
2333
+ }
2334
+ try {
2335
+ parent.insertBefore(node, anchor);
2336
+ } catch (e) {
2337
+ if (parent.ownerDocument) {
2338
+ try {
2339
+ const clone = parent.ownerDocument.importNode(node, true);
2340
+ parent.insertBefore(clone, anchor);
2341
+ continue;
2342
+ } catch {
2343
+ }
2344
+ }
2345
+ throw e;
2280
2346
  }
2281
- continue;
2282
2347
  }
2283
- const value = props[prop];
2284
- prevProps[prop] = assignProp(node, prop, value, prevProps[prop], isSVG, skipRef, props);
2348
+ anchor = node;
2285
2349
  }
2286
2350
  }
2287
- function assignProp(node, prop, value, prev, isSVG, skipRef, props) {
2288
- if (prop === "style") {
2289
- applyStyle(node, value, prev);
2290
- return value;
2291
- }
2292
- if (prop === "classList") {
2293
- return applyClass(node, value, prev);
2351
+ function moveMarkerBlock(parent, block, anchor) {
2352
+ const nodes = collectBlockNodes(block);
2353
+ if (nodes.length === 0) return;
2354
+ moveNodesBefore(parent, nodes, anchor);
2355
+ }
2356
+ function destroyMarkerBlock(block) {
2357
+ if (block.root) {
2358
+ destroyRoot(block.root);
2294
2359
  }
2295
- if (value === prev) return prev;
2296
- if (prop === "ref") {
2297
- if (!skipRef && typeof value === "function") {
2298
- value(node);
2360
+ removeBlockRange(block);
2361
+ }
2362
+ function collectBlockNodes(block) {
2363
+ const nodes = [];
2364
+ let cursor = block.start;
2365
+ while (cursor) {
2366
+ nodes.push(cursor);
2367
+ if (cursor === block.end) {
2368
+ break;
2299
2369
  }
2300
- return value;
2301
- }
2302
- if (prop.slice(0, 3) === "on:") {
2303
- const eventName = prop.slice(3);
2304
- if (prev) node.removeEventListener(eventName, prev);
2305
- if (value) node.addEventListener(eventName, value);
2306
- return value;
2307
- }
2308
- if (prop.slice(0, 10) === "oncapture:") {
2309
- const eventName = prop.slice(10);
2310
- if (prev) node.removeEventListener(eventName, prev, true);
2311
- if (value) node.addEventListener(eventName, value, true);
2312
- return value;
2370
+ cursor = cursor.nextSibling;
2313
2371
  }
2314
- if (prop.slice(0, 2) === "on") {
2315
- const eventName = prop.slice(2).toLowerCase();
2316
- const shouldDelegate = DelegatedEvents.has(eventName);
2317
- if (!shouldDelegate && prev) {
2318
- const handler = Array.isArray(prev) ? prev[0] : prev;
2319
- node.removeEventListener(eventName, handler);
2320
- }
2321
- if (shouldDelegate || value) {
2322
- addEventListener(node, eventName, value, shouldDelegate);
2323
- if (shouldDelegate) delegateEvents([eventName]);
2372
+ return nodes;
2373
+ }
2374
+ function removeBlockRange(block) {
2375
+ let cursor = block.start;
2376
+ while (cursor) {
2377
+ const next = cursor.nextSibling;
2378
+ cursor.parentNode?.removeChild(cursor);
2379
+ if (cursor === block.end) {
2380
+ break;
2324
2381
  }
2325
- return value;
2382
+ cursor = next;
2326
2383
  }
2327
- if (prop.slice(0, 5) === "attr:") {
2328
- if (value == null) node.removeAttribute(prop.slice(5));
2329
- else node.setAttribute(prop.slice(5), String(value));
2330
- return value;
2384
+ }
2385
+ function createVersionedSignalAccessor(initialValue) {
2386
+ let current = initialValue;
2387
+ let version = 0;
2388
+ const track2 = signal(version);
2389
+ function accessor(value) {
2390
+ if (arguments.length === 0) {
2391
+ track2();
2392
+ return current;
2393
+ }
2394
+ current = value;
2395
+ version++;
2396
+ track2(version);
2331
2397
  }
2332
- if (prop.slice(0, 5) === "bool:") {
2333
- if (value) node.setAttribute(prop.slice(5), "");
2334
- else node.removeAttribute(prop.slice(5));
2335
- return value;
2336
- }
2337
- if (prop.slice(0, 5) === "prop:") {
2338
- node[prop.slice(5)] = value;
2339
- return value;
2340
- }
2341
- if (prop === "class" || prop === "className") {
2342
- if (value == null) node.removeAttribute("class");
2343
- else node.className = String(value);
2344
- return value;
2345
- }
2346
- const isCE = node.nodeName.includes("-") || "is" in props;
2347
- if (!isSVG) {
2348
- const propAlias = getPropAlias(prop, node.tagName);
2349
- const isProperty = Properties.has(prop);
2350
- const isChildProp = ChildProperties.has(prop);
2351
- if (propAlias || isProperty || isChildProp || isCE) {
2352
- const propName = propAlias || prop;
2353
- if (isCE && !isProperty && !isChildProp) {
2354
- node[toPropertyName(propName)] = value;
2355
- } else {
2356
- node[propName] = value;
2357
- }
2358
- return value;
2359
- }
2360
- }
2361
- if (isSVG && prop.indexOf(":") > -1) {
2362
- const [prefix, name] = prop.split(":");
2363
- const ns = SVGNamespace[prefix];
2364
- if (ns) {
2365
- if (value == null) node.removeAttributeNS(ns, name);
2366
- else node.setAttributeNS(ns, name, String(value));
2367
- return value;
2368
- }
2369
- }
2370
- const attrName = Aliases[prop] || prop;
2371
- if (value == null) node.removeAttribute(attrName);
2372
- else node.setAttribute(attrName, String(value));
2373
- return value;
2374
- }
2375
- function toPropertyName(name) {
2376
- return name.toLowerCase().replace(/-([a-z])/g, (_, w) => w.toUpperCase());
2398
+ return accessor;
2377
2399
  }
2378
- function createConditional(condition, renderTrue, createElementFn, renderFalse) {
2379
- const startMarker = document.createComment("fict:cond:start");
2380
- const endMarker = document.createComment("fict:cond:end");
2381
- const fragment = document.createDocumentFragment();
2382
- fragment.append(startMarker, endMarker);
2383
- let currentNodes = [];
2384
- let currentRoot2 = null;
2385
- let lastCondition = void 0;
2386
- let pendingRender = false;
2387
- const conditionMemo = computed(condition);
2388
- const runConditional = () => {
2389
- const cond = conditionMemo();
2390
- const parent = startMarker.parentNode;
2391
- if (!parent) {
2392
- pendingRender = true;
2393
- return;
2394
- }
2395
- pendingRender = false;
2396
- if (lastCondition === cond && currentNodes.length > 0) {
2397
- return;
2398
- }
2399
- if (lastCondition === cond && lastCondition === false && renderFalse === void 0) {
2400
- return;
2401
- }
2402
- lastCondition = cond;
2403
- if (currentRoot2) {
2404
- destroyRoot(currentRoot2);
2405
- currentRoot2 = null;
2400
+ function createKeyedListContainer() {
2401
+ const startMarker = document.createComment("fict:list:start");
2402
+ const endMarker = document.createComment("fict:list:end");
2403
+ const dispose = () => {
2404
+ for (const block of container.blocks.values()) {
2405
+ destroyRoot(block.root);
2406
2406
  }
2407
- removeNodes(currentNodes);
2408
- currentNodes = [];
2409
- const render2 = cond ? renderTrue : renderFalse;
2410
- if (!render2) {
2407
+ container.blocks.clear();
2408
+ container.nextBlocks.clear();
2409
+ if (!startMarker.parentNode || !endMarker.parentNode) {
2410
+ container.currentNodes = [];
2411
+ container.nextNodes = [];
2412
+ container.orderedBlocks.length = 0;
2413
+ container.nextOrderedBlocks.length = 0;
2414
+ container.orderedIndexByKey.clear();
2411
2415
  return;
2412
2416
  }
2413
- const root = createRootContext();
2414
- const prev = pushRoot(root);
2415
- let handledError = false;
2416
- try {
2417
- const output = untrack(render2);
2418
- if (output == null || output === false) {
2419
- return;
2420
- }
2421
- const el = createElementFn(output);
2422
- const nodes = toNodeArray(el);
2423
- insertNodesBefore(parent, nodes, endMarker);
2424
- currentNodes = nodes;
2425
- } catch (err) {
2426
- if (handleSuspend(err, root)) {
2427
- handledError = true;
2428
- destroyRoot(root);
2429
- return;
2430
- }
2431
- if (handleError(err, { source: "renderChild" }, root)) {
2432
- handledError = true;
2433
- destroyRoot(root);
2434
- return;
2435
- }
2436
- throw err;
2437
- } finally {
2438
- popRoot(prev);
2439
- if (!handledError) {
2440
- flushOnMount(root);
2441
- currentRoot2 = root;
2442
- } else {
2443
- currentRoot2 = null;
2444
- }
2445
- }
2417
+ const range = document.createRange();
2418
+ range.setStartBefore(startMarker);
2419
+ range.setEndAfter(endMarker);
2420
+ range.deleteContents();
2421
+ container.currentNodes = [];
2422
+ container.nextNodes = [];
2423
+ container.nextBlocks.clear();
2424
+ container.orderedBlocks.length = 0;
2425
+ container.nextOrderedBlocks.length = 0;
2426
+ container.orderedIndexByKey.clear();
2446
2427
  };
2447
- const dispose = createRenderEffect(runConditional);
2448
- return {
2449
- marker: fragment,
2450
- flush: () => {
2451
- if (pendingRender) {
2452
- runConditional();
2453
- }
2454
- },
2455
- dispose: () => {
2456
- dispose();
2457
- if (currentRoot2) {
2458
- destroyRoot(currentRoot2);
2459
- }
2460
- removeNodes(currentNodes);
2461
- currentNodes = [];
2462
- startMarker.parentNode?.removeChild(startMarker);
2463
- endMarker.parentNode?.removeChild(endMarker);
2428
+ const container = {
2429
+ startMarker,
2430
+ endMarker,
2431
+ blocks: /* @__PURE__ */ new Map(),
2432
+ nextBlocks: /* @__PURE__ */ new Map(),
2433
+ currentNodes: [startMarker, endMarker],
2434
+ nextNodes: [],
2435
+ orderedBlocks: [],
2436
+ nextOrderedBlocks: [],
2437
+ orderedIndexByKey: /* @__PURE__ */ new Map(),
2438
+ dispose
2439
+ };
2440
+ return container;
2441
+ }
2442
+ function createKeyedBlock(key, item, index, render2, needsIndex = true, hostRoot) {
2443
+ const itemSig = createVersionedSignalAccessor(item);
2444
+ const indexSig = needsIndex ? signal(index) : ((next) => {
2445
+ if (arguments.length === 0) return index;
2446
+ index = next;
2447
+ return index;
2448
+ });
2449
+ const root = createRootContext(hostRoot);
2450
+ const prevRoot = pushRoot(root);
2451
+ const prevSub = setActiveSub(void 0);
2452
+ let nodes = [];
2453
+ try {
2454
+ const rendered = render2(itemSig, indexSig, key);
2455
+ if (rendered instanceof Node || Array.isArray(rendered) && rendered.every((n) => n instanceof Node)) {
2456
+ nodes = toNodeArray(rendered);
2457
+ } else {
2458
+ const element = createElement(rendered);
2459
+ nodes = toNodeArray(element);
2464
2460
  }
2461
+ } finally {
2462
+ setActiveSub(prevSub);
2463
+ popRoot(prevRoot);
2464
+ flushOnMount(root);
2465
+ }
2466
+ return {
2467
+ key,
2468
+ nodes,
2469
+ root,
2470
+ item: itemSig,
2471
+ index: indexSig,
2472
+ rawItem: item,
2473
+ rawIndex: index
2465
2474
  };
2466
2475
  }
2467
- function createList(items, renderItem, createElementFn, getKey) {
2468
- const startMarker = document.createComment("fict:list:start");
2469
- const endMarker = document.createComment("fict:list:end");
2476
+ function getFirstNodeAfter(marker) {
2477
+ return marker.nextSibling;
2478
+ }
2479
+ function isNodeBetweenMarkers(node, startMarker, endMarker) {
2480
+ let current = startMarker.nextSibling;
2481
+ while (current && current !== endMarker) {
2482
+ if (current === node) return true;
2483
+ current = current.nextSibling;
2484
+ }
2485
+ return false;
2486
+ }
2487
+ function createKeyedList(getItems, keyFn, renderItem, needsIndex) {
2488
+ const resolvedNeedsIndex = arguments.length >= 4 ? !!needsIndex : renderItem.length > 1;
2489
+ return createFineGrainedKeyedList(getItems, keyFn, renderItem, resolvedNeedsIndex);
2490
+ }
2491
+ function createFineGrainedKeyedList(getItems, keyFn, renderItem, needsIndex) {
2492
+ const container = createKeyedListContainer();
2493
+ const hostRoot = getCurrentRoot();
2470
2494
  const fragment = document.createDocumentFragment();
2471
- fragment.append(startMarker, endMarker);
2472
- const nodeMap = /* @__PURE__ */ new Map();
2495
+ fragment.append(container.startMarker, container.endMarker);
2473
2496
  let pendingItems = null;
2474
- const runListUpdate = () => {
2475
- const arr = items();
2476
- const parent = startMarker.parentNode;
2477
- if (!parent) {
2478
- pendingItems = arr;
2479
- return;
2480
- }
2481
- pendingItems = null;
2482
- const newNodeMap = /* @__PURE__ */ new Map();
2483
- const blocks = [];
2484
- for (let i = 0; i < arr.length; i++) {
2485
- const item = arr[i];
2486
- const key = getKey ? getKey(item, i) : i;
2487
- const existing = nodeMap.get(key);
2488
- let block;
2489
- if (existing) {
2490
- const previousValue = existing.value();
2491
- if (!getKey && previousValue !== item) {
2492
- destroyRoot(existing.root);
2493
- removeBlockNodes(existing);
2494
- block = mountBlock(item, i, renderItem, parent, endMarker, createElementFn);
2495
- } else {
2496
- const previousIndex = existing.index();
2497
- existing.value(item);
2498
- existing.index(i);
2499
- if (previousValue === item) {
2500
- bumpBlockVersion(existing);
2501
- }
2502
- const needsRerender = getKey ? true : previousValue !== item || previousIndex !== i;
2503
- block = needsRerender ? rerenderBlock(existing, createElementFn) : existing;
2504
- }
2505
- } else {
2506
- block = mountBlock(item, i, renderItem, parent, endMarker, createElementFn);
2507
- }
2508
- newNodeMap.set(key, block);
2509
- blocks.push(block);
2510
- }
2511
- for (const [key, managed] of nodeMap) {
2512
- if (!newNodeMap.has(key)) {
2513
- destroyRoot(managed.root);
2514
- removeBlockNodes(managed);
2515
- }
2516
- }
2517
- let anchor = endMarker;
2518
- for (let i = blocks.length - 1; i >= 0; i--) {
2519
- const block = blocks[i];
2520
- insertNodesBefore(parent, block.nodes, anchor);
2521
- if (block.nodes.length > 0) {
2522
- anchor = block.nodes[0];
2523
- }
2524
- }
2525
- nodeMap.clear();
2526
- for (const [k, v] of newNodeMap) {
2527
- nodeMap.set(k, v);
2528
- }
2497
+ let disposed = false;
2498
+ const performDiff = () => {
2499
+ if (disposed) return;
2500
+ batch2(() => {
2501
+ const newItems = pendingItems || getItems();
2502
+ pendingItems = null;
2503
+ const oldBlocks = container.blocks;
2504
+ const newBlocks = container.nextBlocks;
2505
+ const prevOrderedBlocks = container.orderedBlocks;
2506
+ const nextOrderedBlocks = container.nextOrderedBlocks;
2507
+ const orderedIndexByKey = container.orderedIndexByKey;
2508
+ newBlocks.clear();
2509
+ nextOrderedBlocks.length = 0;
2510
+ orderedIndexByKey.clear();
2511
+ const endParent = container.endMarker.parentNode;
2512
+ const startParent = container.startMarker.parentNode;
2513
+ const parent = endParent && startParent && endParent === startParent && endParent.isConnected ? endParent : null;
2514
+ if (!parent) {
2515
+ pendingItems = newItems;
2516
+ queueMicrotask(performDiff);
2517
+ return;
2518
+ }
2519
+ if (newItems.length === 0) {
2520
+ if (oldBlocks.size > 0) {
2521
+ for (const block of oldBlocks.values()) {
2522
+ destroyRoot(block.root);
2523
+ removeNodes(block.nodes);
2524
+ }
2525
+ }
2526
+ oldBlocks.clear();
2527
+ newBlocks.clear();
2528
+ prevOrderedBlocks.length = 0;
2529
+ nextOrderedBlocks.length = 0;
2530
+ orderedIndexByKey.clear();
2531
+ container.currentNodes.length = 0;
2532
+ container.currentNodes.push(container.startMarker, container.endMarker);
2533
+ container.nextNodes.length = 0;
2534
+ return;
2535
+ }
2536
+ const prevCount = prevOrderedBlocks.length;
2537
+ let appendCandidate = prevCount > 0 && newItems.length >= prevCount;
2538
+ const appendedBlocks = [];
2539
+ newItems.forEach((item, index) => {
2540
+ const key = keyFn(item, index);
2541
+ const existed = oldBlocks.has(key);
2542
+ let block = oldBlocks.get(key);
2543
+ if (block) {
2544
+ if (block.rawItem !== item) {
2545
+ block.rawItem = item;
2546
+ block.item(item);
2547
+ }
2548
+ if (needsIndex && block.rawIndex !== index) {
2549
+ block.rawIndex = index;
2550
+ block.index(index);
2551
+ }
2552
+ }
2553
+ const existingBlock = newBlocks.get(key);
2554
+ if (existingBlock && existingBlock !== block) {
2555
+ destroyRoot(existingBlock.root);
2556
+ removeNodes(existingBlock.nodes);
2557
+ }
2558
+ if (block) {
2559
+ newBlocks.set(key, block);
2560
+ oldBlocks.delete(key);
2561
+ } else {
2562
+ const existingBlock2 = newBlocks.get(key);
2563
+ if (existingBlock2) {
2564
+ destroyRoot(existingBlock2.root);
2565
+ removeNodes(existingBlock2.nodes);
2566
+ }
2567
+ block = createKeyedBlock(key, item, index, renderItem, needsIndex, hostRoot);
2568
+ }
2569
+ const resolvedBlock = block;
2570
+ newBlocks.set(key, resolvedBlock);
2571
+ const position = orderedIndexByKey.get(key);
2572
+ if (position !== void 0) {
2573
+ appendCandidate = false;
2574
+ }
2575
+ if (appendCandidate) {
2576
+ if (index < prevCount) {
2577
+ if (!prevOrderedBlocks[index] || prevOrderedBlocks[index].key !== key) {
2578
+ appendCandidate = false;
2579
+ }
2580
+ } else if (existed) {
2581
+ appendCandidate = false;
2582
+ }
2583
+ }
2584
+ if (position !== void 0) {
2585
+ const prior = nextOrderedBlocks[position];
2586
+ if (prior && prior !== resolvedBlock) {
2587
+ destroyRoot(prior.root);
2588
+ removeNodes(prior.nodes);
2589
+ }
2590
+ nextOrderedBlocks[position] = resolvedBlock;
2591
+ } else {
2592
+ orderedIndexByKey.set(key, nextOrderedBlocks.length);
2593
+ nextOrderedBlocks.push(resolvedBlock);
2594
+ }
2595
+ if (appendCandidate && index >= prevCount) {
2596
+ appendedBlocks.push(resolvedBlock);
2597
+ }
2598
+ });
2599
+ const canAppend = appendCandidate && prevCount > 0 && newItems.length > prevCount && oldBlocks.size === 0 && appendedBlocks.length > 0;
2600
+ if (canAppend) {
2601
+ const appendedNodes = [];
2602
+ for (const block of appendedBlocks) {
2603
+ for (let i = 0; i < block.nodes.length; i++) {
2604
+ appendedNodes.push(block.nodes[i]);
2605
+ }
2606
+ }
2607
+ if (appendedNodes.length > 0) {
2608
+ insertNodesBefore(parent, appendedNodes, container.endMarker);
2609
+ const currentNodes = container.currentNodes;
2610
+ currentNodes.pop();
2611
+ for (let i = 0; i < appendedNodes.length; i++) {
2612
+ currentNodes.push(appendedNodes[i]);
2613
+ }
2614
+ currentNodes.push(container.endMarker);
2615
+ }
2616
+ container.blocks = newBlocks;
2617
+ container.nextBlocks = oldBlocks;
2618
+ container.orderedBlocks = nextOrderedBlocks;
2619
+ container.nextOrderedBlocks = prevOrderedBlocks;
2620
+ return;
2621
+ }
2622
+ if (oldBlocks.size > 0) {
2623
+ for (const block of oldBlocks.values()) {
2624
+ destroyRoot(block.root);
2625
+ removeNodes(block.nodes);
2626
+ }
2627
+ oldBlocks.clear();
2628
+ }
2629
+ if (newBlocks.size > 0 || container.currentNodes.length > 0) {
2630
+ const prevNodes = container.currentNodes;
2631
+ const nextNodes = container.nextNodes;
2632
+ nextNodes.length = 0;
2633
+ nextNodes.push(container.startMarker);
2634
+ for (let i = 0; i < nextOrderedBlocks.length; i++) {
2635
+ const nodes = nextOrderedBlocks[i].nodes;
2636
+ for (let j = 0; j < nodes.length; j++) {
2637
+ nextNodes.push(nodes[j]);
2638
+ }
2639
+ }
2640
+ nextNodes.push(container.endMarker);
2641
+ reconcileArrays(parent, prevNodes, nextNodes);
2642
+ container.currentNodes = nextNodes;
2643
+ container.nextNodes = prevNodes;
2644
+ }
2645
+ container.blocks = newBlocks;
2646
+ container.nextBlocks = oldBlocks;
2647
+ container.orderedBlocks = nextOrderedBlocks;
2648
+ container.nextOrderedBlocks = prevOrderedBlocks;
2649
+ });
2529
2650
  };
2530
- const dispose = createRenderEffect(runListUpdate);
2651
+ const effectDispose = createRenderEffect(performDiff);
2531
2652
  return {
2532
2653
  marker: fragment,
2654
+ startMarker: container.startMarker,
2655
+ endMarker: container.endMarker,
2656
+ // Flush pending items - call after markers are inserted into DOM
2533
2657
  flush: () => {
2534
2658
  if (pendingItems !== null) {
2535
- runListUpdate();
2659
+ performDiff();
2536
2660
  }
2537
2661
  },
2538
2662
  dispose: () => {
2539
- dispose();
2540
- for (const [, managed] of nodeMap) {
2541
- destroyRoot(managed.root);
2542
- removeBlockNodes(managed);
2543
- }
2544
- nodeMap.clear();
2545
- startMarker.parentNode?.removeChild(startMarker);
2546
- endMarker.parentNode?.removeChild(endMarker);
2663
+ disposed = true;
2664
+ effectDispose?.();
2665
+ container.dispose();
2547
2666
  }
2548
2667
  };
2549
2668
  }
2550
- function createShow(el, condition, displayValue) {
2551
- const originalDisplay = displayValue ?? el.style.display;
2552
- createRenderEffect(() => {
2553
- el.style.display = condition() ? originalDisplay : "none";
2554
- });
2669
+
2670
+ // src/binding.ts
2671
+ function isReactive(value) {
2672
+ return typeof value === "function" && value.length === 0;
2555
2673
  }
2556
- function createPortal(container, render2, createElementFn) {
2557
- const parentRoot = getCurrentRoot();
2558
- const marker = document.createComment("fict:portal");
2559
- container.appendChild(marker);
2560
- let currentNodes = [];
2561
- let currentRoot2 = null;
2562
- const dispose = createRenderEffect(() => {
2563
- if (currentRoot2) {
2564
- destroyRoot(currentRoot2);
2565
- currentRoot2 = null;
2566
- }
2567
- if (currentNodes.length > 0) {
2568
- removeNodes(currentNodes);
2569
- currentNodes = [];
2570
- }
2571
- const root = createRootContext();
2572
- const prev = pushRoot(root);
2573
- let handledError = false;
2574
- try {
2575
- const output = render2();
2576
- if (output != null && output !== false) {
2577
- const el = createElementFn(output);
2578
- const nodes = toNodeArray(el);
2579
- if (marker.parentNode) {
2580
- insertNodesBefore(marker.parentNode, nodes, marker);
2674
+ function unwrap2(value) {
2675
+ return isReactive(value) ? value() : value;
2676
+ }
2677
+ function callEventHandler(handler, event, node, data) {
2678
+ if (!handler) return;
2679
+ const context = node ?? event.currentTarget ?? void 0;
2680
+ const invoke = (fn) => {
2681
+ if (typeof fn === "function") {
2682
+ const result = data === void 0 ? fn.call(context, event) : fn.call(context, data, event);
2683
+ if (typeof result === "function" && result !== fn) {
2684
+ if (data === void 0) {
2685
+ result.call(context, event);
2686
+ } else {
2687
+ result.call(context, data, event);
2581
2688
  }
2582
- currentNodes = nodes;
2583
- }
2584
- } catch (err) {
2585
- if (handleSuspend(err, root)) {
2586
- handledError = true;
2587
- destroyRoot(root);
2588
- currentNodes = [];
2589
- return;
2590
- }
2591
- if (handleError(err, { source: "renderChild" }, root)) {
2592
- handledError = true;
2593
- destroyRoot(root);
2594
- currentNodes = [];
2595
- return;
2596
- }
2597
- throw err;
2598
- } finally {
2599
- popRoot(prev);
2600
- if (!handledError) {
2601
- flushOnMount(root);
2602
- currentRoot2 = root;
2603
- } else {
2604
- currentRoot2 = null;
2689
+ } else if (result && typeof result.handleEvent === "function") {
2690
+ result.handleEvent.call(result, event);
2605
2691
  }
2692
+ } else if (fn && typeof fn.handleEvent === "function") {
2693
+ fn.handleEvent.call(fn, event);
2606
2694
  }
2607
- });
2608
- const portalDispose = () => {
2609
- dispose();
2610
- if (currentRoot2) {
2611
- destroyRoot(currentRoot2);
2612
- }
2613
- if (currentNodes.length > 0) {
2614
- removeNodes(currentNodes);
2615
- }
2616
- marker.parentNode?.removeChild(marker);
2617
2695
  };
2618
- if (parentRoot) {
2619
- parentRoot.destroyCallbacks.push(portalDispose);
2696
+ invoke(handler);
2697
+ }
2698
+ var PRIMITIVE_PROXY = Symbol("fict:primitive-proxy");
2699
+ var PRIMITIVE_PROXY_RAW_VALUE = Symbol("fict:primitive-proxy:raw-value");
2700
+ function unwrapPrimitive(value) {
2701
+ if (value && typeof value === "object" && PRIMITIVE_PROXY in value) {
2702
+ const getRawValue = value[PRIMITIVE_PROXY_RAW_VALUE];
2703
+ if (typeof getRawValue === "function") {
2704
+ return getRawValue();
2705
+ }
2620
2706
  }
2621
- return {
2622
- marker,
2623
- dispose: portalDispose
2624
- };
2707
+ return value;
2625
2708
  }
2626
- function mountBlock(initialValue, initialIndex, renderItem, parent, anchor, createElementFn) {
2627
- const start = document.createComment("fict:block:start");
2628
- const end = document.createComment("fict:block:end");
2629
- const valueSig = signal(initialValue);
2630
- const indexSig = signal(initialIndex);
2631
- const versionSig = signal(0);
2632
- const valueProxy = createValueProxy(() => {
2633
- versionSig();
2634
- return valueSig();
2635
- });
2636
- const renderCurrent = () => renderItem(valueProxy, indexSig());
2637
- const root = createRootContext();
2638
- const prev = pushRoot(root);
2639
- const nodes = [start];
2640
- let handledError = false;
2641
- try {
2642
- const output = renderCurrent();
2643
- if (output != null && output !== false) {
2644
- const el = createElementFn(output);
2645
- const rendered = toNodeArray(el);
2646
- nodes.push(...rendered);
2709
+ function createTextBinding(value) {
2710
+ const text = document.createTextNode("");
2711
+ if (isReactive(value)) {
2712
+ createRenderEffect(() => {
2713
+ const v = value();
2714
+ const fmt = formatTextValue(v);
2715
+ if (text.data !== fmt) {
2716
+ text.data = fmt;
2717
+ }
2718
+ });
2719
+ } else {
2720
+ text.data = formatTextValue(value);
2721
+ }
2722
+ return text;
2723
+ }
2724
+ function bindText(textNode, getValue) {
2725
+ return createRenderEffect(() => {
2726
+ const value = formatTextValue(getValue());
2727
+ if (textNode.data !== value) {
2728
+ textNode.data = value;
2647
2729
  }
2648
- nodes.push(end);
2649
- insertNodesBefore(parent, nodes, anchor);
2650
- } catch (err) {
2651
- if (handleSuspend(err, root)) {
2652
- handledError = true;
2653
- nodes.push(end);
2654
- insertNodesBefore(parent, nodes, anchor);
2655
- } else if (handleError(err, { source: "renderChild" }, root)) {
2656
- handledError = true;
2657
- nodes.push(end);
2658
- insertNodesBefore(parent, nodes, anchor);
2730
+ });
2731
+ }
2732
+ function formatTextValue(value) {
2733
+ if (value == null || value === false) {
2734
+ return "";
2735
+ }
2736
+ return String(value);
2737
+ }
2738
+ function createAttributeBinding(el, key, value, setter) {
2739
+ if (isReactive(value)) {
2740
+ createRenderEffect(() => {
2741
+ setter(el, key, value());
2742
+ });
2743
+ } else {
2744
+ setter(el, key, value);
2745
+ }
2746
+ }
2747
+ function bindAttribute(el, key, getValue) {
2748
+ let prevValue = void 0;
2749
+ return createRenderEffect(() => {
2750
+ const value = getValue();
2751
+ if (value === prevValue) return;
2752
+ prevValue = value;
2753
+ if (value === void 0 || value === null || value === false) {
2754
+ el.removeAttribute(key);
2755
+ } else if (value === true) {
2756
+ el.setAttribute(key, "");
2659
2757
  } else {
2660
- throw err;
2758
+ el.setAttribute(key, String(value));
2661
2759
  }
2662
- } finally {
2663
- popRoot(prev);
2664
- if (!handledError) {
2665
- flushOnMount(root);
2666
- } else {
2667
- destroyRoot(root);
2760
+ });
2761
+ }
2762
+ function bindProperty(el, key, getValue) {
2763
+ const PROPERTY_BINDING_KEYS = /* @__PURE__ */ new Set([
2764
+ "value",
2765
+ "checked",
2766
+ "selected",
2767
+ "disabled",
2768
+ "readOnly",
2769
+ "multiple",
2770
+ "muted"
2771
+ ]);
2772
+ let prevValue = void 0;
2773
+ return createRenderEffect(() => {
2774
+ const next = getValue();
2775
+ if (next === prevValue) return;
2776
+ prevValue = next;
2777
+ if (PROPERTY_BINDING_KEYS.has(key) && (next === void 0 || next === null)) {
2778
+ const fallback = key === "checked" || key === "selected" ? false : "";
2779
+ el[key] = fallback;
2780
+ return;
2668
2781
  }
2782
+ ;
2783
+ el[key] = next;
2784
+ });
2785
+ }
2786
+ function createStyleBinding(el, value) {
2787
+ const target = el;
2788
+ if (isReactive(value)) {
2789
+ let prev;
2790
+ createRenderEffect(() => {
2791
+ const next = value();
2792
+ applyStyle(target, next, prev);
2793
+ prev = next;
2794
+ });
2795
+ } else {
2796
+ applyStyle(target, value, void 0);
2669
2797
  }
2670
- return {
2671
- nodes,
2672
- root,
2673
- value: valueSig,
2674
- index: indexSig,
2675
- version: versionSig,
2676
- start,
2677
- end,
2678
- valueProxy,
2679
- renderCurrent
2680
- };
2681
2798
  }
2682
- function rerenderBlock(block, createElementFn) {
2683
- const currentContent = block.nodes.slice(1, Math.max(1, block.nodes.length - 1));
2684
- const currentNode = currentContent.length === 1 ? currentContent[0] : null;
2685
- clearRoot(block.root);
2686
- const prev = pushRoot(block.root);
2687
- let nextOutput;
2688
- let handledError = false;
2689
- try {
2690
- nextOutput = block.renderCurrent();
2691
- } catch (err) {
2692
- if (handleSuspend(err, block.root)) {
2693
- handledError = true;
2694
- popRoot(prev);
2695
- destroyRoot(block.root);
2696
- block.nodes = [block.start, block.end];
2697
- return block;
2799
+ function bindStyle(el, getValue) {
2800
+ const target = el;
2801
+ let prev;
2802
+ return createRenderEffect(() => {
2803
+ const next = getValue();
2804
+ applyStyle(target, next, prev);
2805
+ prev = next;
2806
+ });
2807
+ }
2808
+ function applyStyle(el, value, prev) {
2809
+ if (typeof value === "string") {
2810
+ el.style.cssText = value;
2811
+ } else if (value && typeof value === "object") {
2812
+ const styles = value;
2813
+ if (typeof prev === "string") {
2814
+ el.style.cssText = "";
2698
2815
  }
2699
- if (handleError(err, { source: "renderChild" }, block.root)) {
2700
- handledError = true;
2701
- popRoot(prev);
2702
- destroyRoot(block.root);
2703
- block.nodes = [block.start, block.end];
2704
- return block;
2816
+ if (prev && typeof prev === "object") {
2817
+ const prevStyles = prev;
2818
+ for (const key of Object.keys(prevStyles)) {
2819
+ if (!(key in styles)) {
2820
+ const cssProperty = key.replace(/([A-Z])/g, "-$1").toLowerCase();
2821
+ el.style.removeProperty(cssProperty);
2822
+ }
2823
+ }
2705
2824
  }
2706
- throw err;
2707
- } finally {
2708
- if (!handledError) {
2709
- popRoot(prev);
2825
+ for (const [prop, v] of Object.entries(styles)) {
2826
+ if (v != null) {
2827
+ const cssProperty = prop.replace(/([A-Z])/g, "-$1").toLowerCase();
2828
+ const unitless = isUnitlessStyleProperty(prop) || isUnitlessStyleProperty(cssProperty);
2829
+ const valueStr = typeof v === "number" && !unitless ? `${v}px` : String(v);
2830
+ el.style.setProperty(cssProperty, valueStr);
2831
+ } else {
2832
+ const cssProperty = prop.replace(/([A-Z])/g, "-$1").toLowerCase();
2833
+ el.style.removeProperty(cssProperty);
2834
+ }
2710
2835
  }
2711
- }
2712
- if (isFragmentVNode(nextOutput) && currentContent.length > 0) {
2713
- const patched = patchFragmentChildren(currentContent, nextOutput.props?.children);
2714
- if (patched) {
2715
- block.nodes = [block.start, ...currentContent, block.end];
2716
- return block;
2836
+ } else {
2837
+ if (prev && typeof prev === "object") {
2838
+ const prevStyles = prev;
2839
+ for (const key of Object.keys(prevStyles)) {
2840
+ const cssProperty = key.replace(/([A-Z])/g, "-$1").toLowerCase();
2841
+ el.style.removeProperty(cssProperty);
2842
+ }
2843
+ } else if (typeof prev === "string") {
2844
+ el.style.cssText = "";
2717
2845
  }
2718
2846
  }
2719
- if (currentNode && patchNode(currentNode, nextOutput)) {
2720
- block.nodes = [block.start, currentNode, block.end];
2721
- return block;
2722
- }
2723
- clearContent(block);
2724
- if (nextOutput != null && nextOutput !== false) {
2725
- const newNodes = toNodeArray(
2726
- nextOutput instanceof Node ? nextOutput : createElementFn(nextOutput)
2727
- );
2728
- insertNodesBefore(block.start.parentNode, newNodes, block.end);
2729
- block.nodes = [block.start, ...newNodes, block.end];
2847
+ }
2848
+ function isUnitlessStyleProperty(prop) {
2849
+ return UnitlessStyles.has(prop);
2850
+ }
2851
+ function createClassBinding(el, value) {
2852
+ if (isReactive(value)) {
2853
+ let prev = {};
2854
+ createRenderEffect(() => {
2855
+ const next = value();
2856
+ prev = applyClass(el, next, prev);
2857
+ });
2730
2858
  } else {
2731
- block.nodes = [block.start, block.end];
2859
+ applyClass(el, value, {});
2732
2860
  }
2733
- return block;
2734
2861
  }
2735
- function patchElement(el, output) {
2736
- if (output === null || output === void 0 || output === false || typeof output === "string" || typeof output === "number") {
2737
- el.textContent = output === null || output === void 0 || output === false ? "" : String(output);
2738
- return true;
2862
+ function bindClass(el, getValue) {
2863
+ let prev = {};
2864
+ return createRenderEffect(() => {
2865
+ const next = getValue();
2866
+ prev = applyClass(el, next, prev);
2867
+ });
2868
+ }
2869
+ function toggleClassKey(node, key, value) {
2870
+ const classNames = key.trim().split(/\s+/);
2871
+ for (let i = 0, len = classNames.length; i < len; i++) {
2872
+ node.classList.toggle(classNames[i], value);
2739
2873
  }
2740
- if (output instanceof Text) {
2741
- el.textContent = output.data;
2742
- return true;
2874
+ }
2875
+ function applyClass(el, value, prev) {
2876
+ const prevState = prev && typeof prev === "object" ? prev : {};
2877
+ if (typeof value === "string") {
2878
+ el.className = value;
2879
+ return {};
2743
2880
  }
2744
- if (output && typeof output === "object" && !(output instanceof Node)) {
2745
- const vnode = output;
2746
- if (typeof vnode.type === "string" && vnode.type.toLowerCase() === el.tagName.toLowerCase()) {
2747
- const children = vnode.props?.children;
2748
- const props = vnode.props ?? {};
2749
- for (const [key, value] of Object.entries(props)) {
2750
- if (key === "children" || key === "key") continue;
2751
- if (typeof value === "string" || typeof value === "number" || typeof value === "boolean" || value === null || value === void 0) {
2752
- if (key === "class" || key === "className") {
2753
- el.setAttribute("class", value === false || value === null ? "" : String(value));
2754
- } else if (key === "style" && typeof value === "string") {
2755
- el.style.cssText = value;
2756
- } else if (value === false || value === null || value === void 0) {
2757
- el.removeAttribute(key);
2758
- } else if (value === true) {
2759
- el.setAttribute(key, "");
2760
- } else {
2761
- el.setAttribute(key, String(value));
2762
- }
2763
- }
2764
- }
2765
- if (typeof children === "string" || typeof children === "number" || children === null || children === void 0 || children === false) {
2766
- el.textContent = children === null || children === void 0 || children === false ? "" : String(children);
2767
- return true;
2768
- }
2769
- if (children && typeof children === "object" && !Array.isArray(children) && !(children instanceof Node)) {
2770
- const childVNode = children;
2771
- if (typeof childVNode.type === "string") {
2772
- const childEl = el.querySelector(childVNode.type);
2773
- if (childEl && patchElement(childEl, children)) {
2774
- return true;
2775
- }
2776
- }
2777
- }
2778
- return false;
2881
+ if (value && typeof value === "object") {
2882
+ const classes = value;
2883
+ const classKeys = Object.keys(classes);
2884
+ const prevKeys = Object.keys(prevState);
2885
+ for (let i = 0, len = prevKeys.length; i < len; i++) {
2886
+ const key = prevKeys[i];
2887
+ if (!key || key === "undefined" || classes[key]) continue;
2888
+ toggleClassKey(el, key, false);
2889
+ delete prevState[key];
2890
+ }
2891
+ for (let i = 0, len = classKeys.length; i < len; i++) {
2892
+ const key = classKeys[i];
2893
+ const classValue = !!classes[key];
2894
+ if (!key || key === "undefined" || prevState[key] === classValue || !classValue) continue;
2895
+ toggleClassKey(el, key, true);
2896
+ prevState[key] = classValue;
2779
2897
  }
2898
+ return prevState;
2780
2899
  }
2781
- if (output instanceof Node) {
2782
- if (output.nodeType === Node.ELEMENT_NODE) {
2783
- const nextEl = output;
2784
- if (nextEl.tagName.toLowerCase() === el.tagName.toLowerCase()) {
2785
- el.textContent = nextEl.textContent;
2786
- return true;
2900
+ if (!value) {
2901
+ for (const key of Object.keys(prevState)) {
2902
+ if (key && key !== "undefined") {
2903
+ toggleClassKey(el, key, false);
2787
2904
  }
2788
- } else if (output.nodeType === Node.TEXT_NODE) {
2789
- el.textContent = output.data;
2790
- return true;
2791
2905
  }
2906
+ return {};
2792
2907
  }
2793
- return false;
2794
- }
2795
- function patchNode(currentNode, nextOutput) {
2796
- if (!currentNode) return false;
2797
- if (currentNode instanceof Text && (nextOutput === null || nextOutput === void 0 || nextOutput === false || typeof nextOutput === "string" || typeof nextOutput === "number" || nextOutput instanceof Text)) {
2798
- const nextText = nextOutput instanceof Text ? nextOutput.data : nextOutput === null || nextOutput === void 0 || nextOutput === false ? "" : String(nextOutput);
2799
- currentNode.data = nextText;
2800
- return true;
2801
- }
2802
- if (currentNode instanceof Element && patchElement(currentNode, nextOutput)) {
2803
- return true;
2804
- }
2805
- if (nextOutput instanceof Node && currentNode === nextOutput) {
2806
- return true;
2807
- }
2808
- return false;
2908
+ return prevState;
2809
2909
  }
2810
- function isFragmentVNode(value) {
2811
- return value != null && typeof value === "object" && !(value instanceof Node) && value.type === Fragment;
2910
+ function classList(node, value, prev = {}) {
2911
+ return applyClass(node, value, prev);
2812
2912
  }
2813
- function normalizeChildren(children, result = []) {
2814
- if (children === void 0) {
2815
- return result;
2913
+ function insert(parent, getValue, markerOrCreateElement, createElementFn) {
2914
+ const hostRoot = getCurrentRoot();
2915
+ let marker;
2916
+ let ownsMarker = false;
2917
+ let createFn = createElementFn;
2918
+ if (markerOrCreateElement instanceof Node) {
2919
+ marker = markerOrCreateElement;
2920
+ createFn = createElementFn;
2921
+ } else {
2922
+ marker = document.createComment("fict:insert");
2923
+ parent.appendChild(marker);
2924
+ createFn = markerOrCreateElement;
2925
+ ownsMarker = true;
2816
2926
  }
2817
- if (Array.isArray(children)) {
2818
- for (const child of children) {
2819
- normalizeChildren(child, result);
2927
+ let currentNodes = [];
2928
+ let currentText = null;
2929
+ let currentRoot2 = null;
2930
+ const clearCurrentNodes = () => {
2931
+ if (currentNodes.length > 0) {
2932
+ removeNodes(currentNodes);
2933
+ currentNodes = [];
2820
2934
  }
2821
- return result;
2822
- }
2823
- if (children === null || children === false) {
2824
- return result;
2825
- }
2826
- result.push(children);
2827
- return result;
2828
- }
2829
- function patchFragmentChildren(nodes, children) {
2830
- const normalized = normalizeChildren(children);
2831
- if (normalized.length !== nodes.length) {
2832
- return false;
2833
- }
2834
- for (let i = 0; i < normalized.length; i++) {
2835
- if (!patchNode(nodes[i], normalized[i])) {
2836
- return false;
2935
+ };
2936
+ const setTextNode = (textValue, shouldInsert, parentNode) => {
2937
+ if (!currentText) {
2938
+ currentText = document.createTextNode(textValue);
2939
+ } else if (currentText.data !== textValue) {
2940
+ currentText.data = textValue;
2837
2941
  }
2838
- }
2839
- return true;
2840
- }
2841
- function clearContent(block) {
2842
- const nodes = block.nodes.slice(1, Math.max(1, block.nodes.length - 1));
2843
- removeNodes(nodes);
2844
- }
2845
- function removeBlockNodes(block) {
2846
- let cursor = block.start;
2847
- const end = block.end;
2848
- while (cursor) {
2849
- const next = cursor.nextSibling;
2850
- cursor.parentNode?.removeChild(cursor);
2851
- if (cursor === end) break;
2852
- cursor = next;
2853
- }
2854
- }
2855
- function bumpBlockVersion(block) {
2856
- block.version(block.version() + 1);
2857
- }
2858
-
2859
- // src/scope.ts
2860
- function createScope() {
2861
- let dispose = null;
2862
- const stop = () => {
2863
- if (dispose) {
2864
- dispose();
2865
- dispose = null;
2942
+ if (!shouldInsert) {
2943
+ clearCurrentNodes();
2944
+ return;
2866
2945
  }
2946
+ if (currentNodes.length === 1 && currentNodes[0] === currentText) {
2947
+ return;
2948
+ }
2949
+ clearCurrentNodes();
2950
+ insertNodesBefore(parentNode, [currentText], marker);
2951
+ currentNodes = [currentText];
2867
2952
  };
2868
- const run = (fn) => {
2869
- stop();
2870
- const { dispose: rootDispose, value } = createRoot(fn);
2871
- dispose = rootDispose;
2872
- return value;
2873
- };
2874
- registerRootCleanup(stop);
2875
- return { run, stop };
2876
- }
2877
- function runInScope(flag, fn) {
2878
- const scope = createScope();
2879
- const evaluate = () => isReactive(flag) ? flag() : !!flag;
2880
- createEffect(() => {
2881
- const enabled = evaluate();
2882
- if (enabled) {
2883
- scope.run(fn);
2884
- } else {
2885
- scope.stop();
2953
+ const dispose = createRenderEffect(() => {
2954
+ const value = getValue();
2955
+ const parentNode = marker.parentNode;
2956
+ const isPrimitive = value == null || value === false || typeof value === "string" || typeof value === "number" || typeof value === "boolean";
2957
+ if (isPrimitive) {
2958
+ if (currentRoot2) {
2959
+ destroyRoot(currentRoot2);
2960
+ currentRoot2 = null;
2961
+ }
2962
+ if (!parentNode) {
2963
+ clearCurrentNodes();
2964
+ return;
2965
+ }
2966
+ const textValue = value == null || value === false ? "" : String(value);
2967
+ const shouldInsert = value != null && value !== false;
2968
+ setTextNode(textValue, shouldInsert, parentNode);
2969
+ return;
2970
+ }
2971
+ if (currentRoot2) {
2972
+ destroyRoot(currentRoot2);
2973
+ currentRoot2 = null;
2974
+ }
2975
+ clearCurrentNodes();
2976
+ const root = createRootContext(hostRoot);
2977
+ const prev = pushRoot(root);
2978
+ let nodes = [];
2979
+ try {
2980
+ let newNode;
2981
+ if (value instanceof Node) {
2982
+ newNode = value;
2983
+ } else if (Array.isArray(value)) {
2984
+ if (value.every((v) => v instanceof Node)) {
2985
+ newNode = value;
2986
+ } else {
2987
+ if (createFn) {
2988
+ const mapped = [];
2989
+ for (const item of value) {
2990
+ mapped.push(...toNodeArray(createFn(item)));
2991
+ }
2992
+ newNode = mapped;
2993
+ } else {
2994
+ newNode = document.createTextNode(String(value));
2995
+ }
2996
+ }
2997
+ } else {
2998
+ newNode = createFn ? createFn(value) : document.createTextNode(String(value));
2999
+ }
3000
+ nodes = toNodeArray(newNode);
3001
+ if (parentNode) {
3002
+ insertNodesBefore(parentNode, nodes, marker);
3003
+ }
3004
+ } finally {
3005
+ popRoot(prev);
3006
+ flushOnMount(root);
2886
3007
  }
3008
+ currentRoot2 = root;
3009
+ currentNodes = nodes;
2887
3010
  });
2888
- onCleanup(scope.stop);
2889
- }
2890
-
2891
- // src/hooks.ts
2892
- var ctxStack = [];
2893
- function __fictUseContext() {
2894
- if (ctxStack.length === 0) {
2895
- const ctx2 = { slots: [], cursor: 0 };
2896
- ctxStack.push(ctx2);
2897
- return ctx2;
2898
- }
2899
- const ctx = ctxStack[ctxStack.length - 1];
2900
- ctx.cursor = 0;
2901
- return ctx;
2902
- }
2903
- function __fictPushContext() {
2904
- const ctx = { slots: [], cursor: 0 };
2905
- ctxStack.push(ctx);
2906
- return ctx;
2907
- }
2908
- function __fictPopContext() {
2909
- ctxStack.pop();
3011
+ return () => {
3012
+ dispose();
3013
+ if (currentRoot2) {
3014
+ destroyRoot(currentRoot2);
3015
+ currentRoot2 = null;
3016
+ }
3017
+ clearCurrentNodes();
3018
+ if (ownsMarker) {
3019
+ marker.parentNode?.removeChild(marker);
3020
+ }
3021
+ };
2910
3022
  }
2911
- function __fictResetContext() {
2912
- ctxStack.length = 0;
3023
+ function createChildBinding(parent, getValue, createElementFn) {
3024
+ const marker = document.createComment("fict:child");
3025
+ parent.appendChild(marker);
3026
+ const hostRoot = getCurrentRoot();
3027
+ const dispose = createRenderEffect(() => {
3028
+ const root = createRootContext(hostRoot);
3029
+ const prev = pushRoot(root);
3030
+ let nodes = [];
3031
+ let handledError = false;
3032
+ try {
3033
+ const value = getValue();
3034
+ if (value == null || value === false) {
3035
+ return;
3036
+ }
3037
+ const output = createElementFn(value);
3038
+ nodes = toNodeArray(output);
3039
+ const parentNode = marker.parentNode;
3040
+ if (parentNode) {
3041
+ insertNodesBefore(parentNode, nodes, marker);
3042
+ }
3043
+ return () => {
3044
+ destroyRoot(root);
3045
+ removeNodes(nodes);
3046
+ };
3047
+ } catch (err) {
3048
+ if (handleSuspend(err, root)) {
3049
+ handledError = true;
3050
+ destroyRoot(root);
3051
+ return;
3052
+ }
3053
+ if (handleError(err, { source: "renderChild" }, root)) {
3054
+ handledError = true;
3055
+ destroyRoot(root);
3056
+ return;
3057
+ }
3058
+ throw err;
3059
+ } finally {
3060
+ popRoot(prev);
3061
+ if (!handledError) {
3062
+ flushOnMount(root);
3063
+ }
3064
+ }
3065
+ });
3066
+ return {
3067
+ marker,
3068
+ dispose: () => {
3069
+ dispose();
3070
+ marker.parentNode?.removeChild(marker);
3071
+ }
3072
+ };
2913
3073
  }
2914
- function __fictUseSignal(ctx, initial, slot) {
2915
- const index = slot ?? ctx.cursor++;
2916
- if (!ctx.slots[index]) {
2917
- ctx.slots[index] = signal(initial);
3074
+ function delegateEvents(eventNames, doc = window.document) {
3075
+ const e = doc[$$EVENTS] || (doc[$$EVENTS] = /* @__PURE__ */ new Set());
3076
+ for (let i = 0, l = eventNames.length; i < l; i++) {
3077
+ const name = eventNames[i];
3078
+ if (!e.has(name)) {
3079
+ e.add(name);
3080
+ doc.addEventListener(name, globalEventHandler);
3081
+ }
2918
3082
  }
2919
- return ctx.slots[index];
2920
3083
  }
2921
- function __fictUseMemo(ctx, fn, slot) {
2922
- const index = slot ?? ctx.cursor++;
2923
- if (!ctx.slots[index]) {
2924
- ctx.slots[index] = createMemo(fn);
3084
+ function clearDelegatedEvents(doc = window.document) {
3085
+ const e = doc[$$EVENTS];
3086
+ if (e) {
3087
+ for (const name of e.keys()) {
3088
+ doc.removeEventListener(name, globalEventHandler);
3089
+ }
3090
+ delete doc[$$EVENTS];
2925
3091
  }
2926
- return ctx.slots[index];
2927
3092
  }
2928
- function __fictUseEffect(ctx, fn, slot) {
2929
- const index = slot ?? ctx.cursor++;
2930
- if (!ctx.slots[index]) {
2931
- ctx.slots[index] = createEffect(fn);
3093
+ function globalEventHandler(e) {
3094
+ let node = e.target;
3095
+ const key = `$$${e.type}`;
3096
+ const dataKey = `${key}Data`;
3097
+ const oriTarget = e.target;
3098
+ const oriCurrentTarget = e.currentTarget;
3099
+ const retarget = (value) => Object.defineProperty(e, "target", {
3100
+ configurable: true,
3101
+ value
3102
+ });
3103
+ const handleNode = () => {
3104
+ if (!node) return false;
3105
+ const handler = node[key];
3106
+ if (handler && !node.disabled) {
3107
+ const resolveData = (value) => {
3108
+ if (typeof value === "function") {
3109
+ try {
3110
+ const fn = value;
3111
+ return fn.length > 0 ? fn(e) : fn();
3112
+ } catch {
3113
+ return value();
3114
+ }
3115
+ }
3116
+ return value;
3117
+ };
3118
+ const rawData = node[dataKey];
3119
+ const hasData = rawData !== void 0;
3120
+ const resolvedNodeData = hasData ? resolveData(rawData) : void 0;
3121
+ if (typeof handler === "function") {
3122
+ callEventHandler(handler, e, node, hasData ? resolvedNodeData : void 0);
3123
+ } else if (Array.isArray(handler)) {
3124
+ const tupleData = resolveData(handler[1]);
3125
+ callEventHandler(handler[0], e, node, tupleData);
3126
+ }
3127
+ if (e.cancelBubble) return false;
3128
+ }
3129
+ const shadowHost = node.host;
3130
+ if (shadowHost && typeof shadowHost !== "string" && !shadowHost._$host && node.contains(e.target)) {
3131
+ retarget(shadowHost);
3132
+ }
3133
+ return true;
3134
+ };
3135
+ const walkUpTree = () => {
3136
+ while (handleNode() && node) {
3137
+ node = node._$host || node.parentNode || node.host;
3138
+ }
3139
+ };
3140
+ Object.defineProperty(e, "currentTarget", {
3141
+ configurable: true,
3142
+ get() {
3143
+ return node || document;
3144
+ }
3145
+ });
3146
+ if (e.composedPath) {
3147
+ const path = e.composedPath();
3148
+ retarget(path[0]);
3149
+ for (let i = 0; i < path.length - 2; i++) {
3150
+ node = path[i];
3151
+ if (!handleNode()) break;
3152
+ if (node._$host) {
3153
+ node = node._$host;
3154
+ walkUpTree();
3155
+ break;
3156
+ }
3157
+ if (node.parentNode === oriCurrentTarget) {
3158
+ break;
3159
+ }
3160
+ }
3161
+ } else {
3162
+ walkUpTree();
2932
3163
  }
3164
+ retarget(oriTarget);
2933
3165
  }
2934
- function __fictRender(ctx, fn) {
2935
- ctxStack.push(ctx);
2936
- ctx.cursor = 0;
2937
- try {
2938
- return fn();
2939
- } finally {
2940
- ctxStack.pop();
3166
+ function addEventListener(node, name, handler, delegate) {
3167
+ if (handler == null) return;
3168
+ if (delegate) {
3169
+ if (Array.isArray(handler)) {
3170
+ node[`$$${name}`] = handler[0];
3171
+ node[`$$${name}Data`] = handler[1];
3172
+ } else {
3173
+ node[`$$${name}`] = handler;
3174
+ }
3175
+ } else if (Array.isArray(handler)) {
3176
+ const handlerFn = handler[0];
3177
+ node.addEventListener(name, (e) => handlerFn.call(node, handler[1], e));
3178
+ } else {
3179
+ node.addEventListener(name, handler);
2941
3180
  }
2942
3181
  }
2943
-
2944
- // src/versioned-signal.ts
2945
- function createVersionedSignal(initialValue, options2) {
2946
- const equals = options2?.equals ?? Object.is;
2947
- const value = signal(initialValue);
2948
- const version = signal(0);
2949
- const bumpVersion = () => {
2950
- const next = version() + 1;
2951
- version(next);
3182
+ function bindEvent(el, eventName, handler, options2) {
3183
+ if (handler == null) return () => {
2952
3184
  };
2953
- return {
2954
- read: () => {
2955
- version();
2956
- return value();
2957
- },
2958
- write: (next) => {
2959
- const prev = value();
2960
- if (!equals(prev, next)) {
2961
- value(next);
3185
+ const rootRef = getCurrentRoot();
3186
+ if (DelegatedEvents.has(eventName) && !options2) {
3187
+ const key = `$$${eventName}`;
3188
+ delegateEvents([eventName]);
3189
+ const resolveHandler = isReactive(handler) ? handler : () => handler;
3190
+ el[key] = function(...args) {
3191
+ try {
3192
+ const fn = resolveHandler();
3193
+ callEventHandler(fn, args[0], el);
3194
+ } catch (err) {
3195
+ handleError(err, { source: "event", eventName }, rootRef);
3196
+ }
3197
+ };
3198
+ return () => {
3199
+ el[key] = void 0;
3200
+ };
3201
+ }
3202
+ const getHandler = isReactive(handler) ? handler : () => handler;
3203
+ const wrapped = (event) => {
3204
+ try {
3205
+ const resolved = getHandler();
3206
+ callEventHandler(resolved, event, el);
3207
+ } catch (err) {
3208
+ if (handleError(err, { source: "event", eventName }, rootRef)) {
2962
3209
  return;
2963
3210
  }
2964
- bumpVersion();
2965
- },
2966
- force: () => {
2967
- bumpVersion();
2968
- },
2969
- peekVersion: () => version(),
2970
- peekValue: () => value()
3211
+ throw err;
3212
+ }
2971
3213
  };
3214
+ el.addEventListener(eventName, wrapped, options2);
3215
+ const cleanup = () => el.removeEventListener(eventName, wrapped, options2);
3216
+ registerRootCleanup(cleanup);
3217
+ return cleanup;
2972
3218
  }
2973
-
2974
- // src/props.ts
2975
- var propGetters = /* @__PURE__ */ new WeakSet();
2976
- var rawToProxy = /* @__PURE__ */ new WeakMap();
2977
- var proxyToRaw = /* @__PURE__ */ new WeakMap();
2978
- function __fictProp(getter) {
2979
- if (typeof getter === "function" && getter.length === 0) {
2980
- propGetters.add(getter);
3219
+ function bindRef(el, ref) {
3220
+ if (ref == null) return () => {
3221
+ };
3222
+ const getRef = isReactive(ref) ? ref : () => ref;
3223
+ const applyRef2 = (refValue) => {
3224
+ if (refValue == null) return;
3225
+ if (typeof refValue === "function") {
3226
+ refValue(el);
3227
+ } else if (typeof refValue === "object" && "current" in refValue) {
3228
+ refValue.current = el;
3229
+ }
3230
+ };
3231
+ const initialRef = getRef();
3232
+ applyRef2(initialRef);
3233
+ if (isReactive(ref)) {
3234
+ const cleanup2 = createRenderEffect(() => {
3235
+ const currentRef = getRef();
3236
+ applyRef2(currentRef);
3237
+ });
3238
+ registerRootCleanup(cleanup2);
3239
+ const nullifyCleanup = () => {
3240
+ const currentRef = getRef();
3241
+ if (currentRef && typeof currentRef === "object" && "current" in currentRef) {
3242
+ currentRef.current = null;
3243
+ }
3244
+ };
3245
+ registerRootCleanup(nullifyCleanup);
3246
+ return () => {
3247
+ cleanup2();
3248
+ nullifyCleanup();
3249
+ };
2981
3250
  }
2982
- return getter;
2983
- }
2984
- function isPropGetter(value) {
2985
- return typeof value === "function" && propGetters.has(value);
3251
+ const cleanup = () => {
3252
+ const refValue = getRef();
3253
+ if (refValue && typeof refValue === "object" && "current" in refValue) {
3254
+ refValue.current = null;
3255
+ }
3256
+ };
3257
+ registerRootCleanup(cleanup);
3258
+ return cleanup;
2986
3259
  }
2987
- function createPropsProxy(props) {
2988
- if (!props || typeof props !== "object") {
2989
- return props;
2990
- }
2991
- if (proxyToRaw.has(props)) {
2992
- return props;
2993
- }
2994
- const cached = rawToProxy.get(props);
2995
- if (cached) {
2996
- return cached;
3260
+ function spread(node, props = {}, isSVG = false, skipChildren = false) {
3261
+ const prevProps = {};
3262
+ if (!skipChildren && "children" in props) {
3263
+ createRenderEffect(() => {
3264
+ prevProps.children = props.children;
3265
+ });
2997
3266
  }
2998
- const proxy = new Proxy(props, {
2999
- get(target, prop, receiver) {
3000
- const value = Reflect.get(target, prop, receiver);
3001
- if (isPropGetter(value)) {
3002
- return value();
3003
- }
3004
- return value;
3005
- },
3006
- set(target, prop, value, receiver) {
3007
- return Reflect.set(target, prop, value, receiver);
3008
- },
3009
- has(target, prop) {
3010
- return prop in target;
3011
- },
3012
- ownKeys(target) {
3013
- return Reflect.ownKeys(target);
3014
- },
3015
- getOwnPropertyDescriptor(target, prop) {
3016
- return Object.getOwnPropertyDescriptor(target, prop);
3267
+ createRenderEffect(() => {
3268
+ if (typeof props.ref === "function") {
3269
+ ;
3270
+ props.ref(node);
3017
3271
  }
3018
3272
  });
3019
- rawToProxy.set(props, proxy);
3020
- proxyToRaw.set(proxy, props);
3021
- return proxy;
3273
+ createRenderEffect(() => {
3274
+ assign(node, props, isSVG, true, prevProps, true);
3275
+ });
3276
+ return prevProps;
3022
3277
  }
3023
- function unwrapProps(props) {
3024
- if (!props || typeof props !== "object") {
3025
- return props;
3278
+ function assign(node, props, isSVG = false, skipChildren = false, prevProps = {}, skipRef = false) {
3279
+ props = props || {};
3280
+ for (const prop in prevProps) {
3281
+ if (!(prop in props)) {
3282
+ if (prop === "children") continue;
3283
+ prevProps[prop] = assignProp(node, prop, null, prevProps[prop], isSVG, skipRef, props);
3284
+ }
3026
3285
  }
3027
- return proxyToRaw.get(props) ?? props;
3028
- }
3029
- function __fictPropsRest(props, exclude) {
3030
- const raw = unwrapProps(props);
3031
- const out = {};
3032
- const excludeSet = new Set(exclude);
3033
- for (const key of Reflect.ownKeys(raw)) {
3034
- if (excludeSet.has(key)) continue;
3035
- out[key] = raw[key];
3286
+ for (const prop in props) {
3287
+ if (prop === "children") {
3288
+ if (!skipChildren) {
3289
+ prevProps.children = props.children;
3290
+ }
3291
+ continue;
3292
+ }
3293
+ const value = props[prop];
3294
+ prevProps[prop] = assignProp(node, prop, value, prevProps[prop], isSVG, skipRef, props);
3036
3295
  }
3037
- return createPropsProxy(out);
3038
3296
  }
3039
- function mergeProps(...sources) {
3040
- const validSources = sources.filter(
3041
- (s) => s != null && (typeof s === "object" || typeof s === "function")
3042
- );
3043
- if (validSources.length === 0) {
3044
- return {};
3297
+ function assignProp(node, prop, value, prev, isSVG, skipRef, props) {
3298
+ if (prop === "style") {
3299
+ applyStyle(node, value, prev);
3300
+ return value;
3045
3301
  }
3046
- if (validSources.length === 1 && typeof validSources[0] === "object") {
3047
- return validSources[0];
3302
+ if (prop === "classList") {
3303
+ return applyClass(node, value, prev);
3048
3304
  }
3049
- const resolveSource = (src) => {
3050
- const value = typeof src === "function" ? src() : src;
3051
- if (!value || typeof value !== "object") return void 0;
3052
- return unwrapProps(value);
3053
- };
3054
- return new Proxy({}, {
3055
- get(_, prop) {
3056
- if (typeof prop === "symbol") {
3057
- return void 0;
3058
- }
3059
- for (let i = validSources.length - 1; i >= 0; i--) {
3060
- const src = validSources[i];
3061
- const raw = resolveSource(src);
3062
- if (!raw || !(prop in raw)) continue;
3063
- const value = raw[prop];
3064
- if (typeof src === "function" && !isPropGetter(value)) {
3065
- return __fictProp(() => {
3066
- const latest = resolveSource(src);
3067
- if (!latest || !(prop in latest)) return void 0;
3068
- return latest[prop];
3069
- });
3070
- }
3071
- return value;
3072
- }
3073
- return void 0;
3074
- },
3075
- has(_, prop) {
3076
- for (const src of validSources) {
3077
- const raw = resolveSource(src);
3078
- if (raw && prop in raw) {
3079
- return true;
3080
- }
3081
- }
3082
- return false;
3083
- },
3084
- ownKeys() {
3085
- const keys = /* @__PURE__ */ new Set();
3086
- for (const src of validSources) {
3087
- const raw = resolveSource(src);
3088
- if (raw) {
3089
- for (const key of Reflect.ownKeys(raw)) {
3090
- keys.add(key);
3091
- }
3092
- }
3093
- }
3094
- return Array.from(keys);
3095
- },
3096
- getOwnPropertyDescriptor(_, prop) {
3097
- for (let i = validSources.length - 1; i >= 0; i--) {
3098
- const raw = resolveSource(validSources[i]);
3099
- if (raw && prop in raw) {
3100
- return {
3101
- enumerable: true,
3102
- configurable: true,
3103
- get: () => {
3104
- const value = raw[prop];
3105
- return value;
3106
- }
3107
- };
3108
- }
3109
- }
3110
- return void 0;
3305
+ if (value === prev) return prev;
3306
+ if (prop === "ref") {
3307
+ if (!skipRef && typeof value === "function") {
3308
+ value(node);
3111
3309
  }
3112
- });
3113
- }
3114
- function useProp(getter) {
3115
- return __fictProp(createMemo(getter));
3116
- }
3117
-
3118
- // src/ref.ts
3119
- function createRef() {
3120
- return { current: null };
3121
- }
3122
-
3123
- // src/transition.ts
3124
- function startTransition(fn) {
3125
- const prev = setTransitionContext(true);
3126
- try {
3127
- fn();
3128
- } finally {
3129
- setTransitionContext(prev);
3130
- scheduleFlush();
3310
+ return value;
3131
3311
  }
3132
- }
3133
- function useTransition() {
3134
- const pending = signal(false);
3135
- const start = (fn) => {
3136
- pending(true);
3137
- startTransition(() => {
3138
- try {
3139
- fn();
3140
- } finally {
3141
- pending(false);
3142
- }
3143
- });
3144
- };
3145
- return [() => pending(), start];
3146
- }
3147
- function useDeferredValue(getValue) {
3148
- const deferredValue = signal(getValue());
3149
- createEffect(() => {
3150
- const newValue = getValue();
3151
- const currentDeferred = untrack(() => deferredValue());
3152
- if (currentDeferred !== newValue) {
3153
- startTransition(() => {
3154
- deferredValue(newValue);
3155
- });
3156
- }
3157
- });
3158
- return () => deferredValue();
3159
- }
3160
-
3161
- // src/scheduler.ts
3162
- function batch2(fn) {
3163
- return batch(fn);
3164
- }
3165
- function untrack2(fn) {
3166
- return untrack(fn);
3167
- }
3168
-
3169
- // src/dom.ts
3170
- function render(view, container) {
3171
- const root = createRootContext();
3172
- const prev = pushRoot(root);
3173
- let dom;
3174
- try {
3175
- const output = view();
3176
- dom = createElement(output);
3177
- } finally {
3178
- popRoot(prev);
3179
- }
3180
- container.replaceChildren(dom);
3181
- container.setAttribute("data-fict-fine-grained", "1");
3182
- flushOnMount(root);
3183
- const teardown = () => {
3184
- destroyRoot(root);
3185
- container.innerHTML = "";
3186
- };
3187
- return teardown;
3188
- }
3189
- function createElement(node) {
3190
- if (node instanceof Node) {
3191
- return node;
3312
+ if (prop.slice(0, 3) === "on:") {
3313
+ const eventName = prop.slice(3);
3314
+ if (prev) node.removeEventListener(eventName, prev);
3315
+ if (value) node.addEventListener(eventName, value);
3316
+ return value;
3192
3317
  }
3193
- if (node === null || node === void 0 || node === false) {
3194
- return document.createTextNode("");
3318
+ if (prop.slice(0, 10) === "oncapture:") {
3319
+ const eventName = prop.slice(10);
3320
+ if (prev) node.removeEventListener(eventName, prev, true);
3321
+ if (value) node.addEventListener(eventName, value, true);
3322
+ return value;
3195
3323
  }
3196
- if (typeof node === "object" && node !== null && !(node instanceof Node)) {
3197
- if ("marker" in node) {
3198
- return createElement(node.marker);
3324
+ if (prop.slice(0, 2) === "on") {
3325
+ const eventName = prop.slice(2).toLowerCase();
3326
+ const shouldDelegate = DelegatedEvents.has(eventName);
3327
+ if (!shouldDelegate && prev) {
3328
+ const handler = Array.isArray(prev) ? prev[0] : prev;
3329
+ node.removeEventListener(eventName, handler);
3199
3330
  }
3200
- const nodeRecord = node;
3201
- if (nodeRecord[PRIMITIVE_PROXY]) {
3202
- const primitiveGetter = nodeRecord[Symbol.toPrimitive];
3203
- const value = typeof primitiveGetter === "function" ? primitiveGetter.call(node, "default") : node;
3204
- return document.createTextNode(value == null || value === false ? "" : String(value));
3331
+ if (shouldDelegate || value) {
3332
+ addEventListener(node, eventName, value, shouldDelegate);
3333
+ if (shouldDelegate) delegateEvents([eventName]);
3205
3334
  }
3335
+ return value;
3206
3336
  }
3207
- if (Array.isArray(node)) {
3208
- const frag = document.createDocumentFragment();
3209
- for (const child of node) {
3210
- appendChildNode(frag, child);
3211
- }
3212
- return frag;
3337
+ if (prop.slice(0, 5) === "attr:") {
3338
+ if (value == null) node.removeAttribute(prop.slice(5));
3339
+ else node.setAttribute(prop.slice(5), String(value));
3340
+ return value;
3213
3341
  }
3214
- if (typeof node === "string" || typeof node === "number") {
3215
- return document.createTextNode(String(node));
3342
+ if (prop.slice(0, 5) === "bool:") {
3343
+ if (value) node.setAttribute(prop.slice(5), "");
3344
+ else node.removeAttribute(prop.slice(5));
3345
+ return value;
3216
3346
  }
3217
- if (typeof node === "boolean") {
3218
- return document.createTextNode("");
3347
+ if (prop.slice(0, 5) === "prop:") {
3348
+ node[prop.slice(5)] = value;
3349
+ return value;
3219
3350
  }
3220
- const vnode = node;
3221
- if (typeof vnode.type === "function") {
3222
- const rawProps = unwrapProps(vnode.props ?? {});
3223
- const baseProps = vnode.key === void 0 ? rawProps : new Proxy(rawProps, {
3224
- get(target, prop, receiver) {
3225
- if (prop === "key") return vnode.key;
3226
- return Reflect.get(target, prop, receiver);
3227
- },
3228
- has(target, prop) {
3229
- if (prop === "key") return true;
3230
- return prop in target;
3231
- },
3232
- ownKeys(target) {
3233
- const keys = new Set(Reflect.ownKeys(target));
3234
- keys.add("key");
3235
- return Array.from(keys);
3236
- },
3237
- getOwnPropertyDescriptor(target, prop) {
3238
- if (prop === "key") {
3239
- return { enumerable: true, configurable: true, value: vnode.key };
3240
- }
3241
- return Object.getOwnPropertyDescriptor(target, prop);
3242
- }
3243
- });
3244
- const props = createPropsProxy(baseProps);
3245
- try {
3246
- __fictPushContext();
3247
- const rendered = vnode.type(props);
3248
- __fictPopContext();
3249
- return createElement(rendered);
3250
- } catch (err) {
3251
- __fictPopContext();
3252
- if (handleSuspend(err)) {
3253
- return document.createComment("fict:suspend");
3351
+ if (prop === "class" || prop === "className") {
3352
+ if (value == null) node.removeAttribute("class");
3353
+ else node.className = String(value);
3354
+ return value;
3355
+ }
3356
+ const isCE = node.nodeName.includes("-") || "is" in props;
3357
+ if (!isSVG) {
3358
+ const propAlias = getPropAlias(prop, node.tagName);
3359
+ const isProperty = Properties.has(prop);
3360
+ const isChildProp = ChildProperties.has(prop);
3361
+ if (propAlias || isProperty || isChildProp || isCE) {
3362
+ const propName = propAlias || prop;
3363
+ if (isCE && !isProperty && !isChildProp) {
3364
+ node[toPropertyName2(propName)] = value;
3365
+ } else {
3366
+ node[propName] = value;
3254
3367
  }
3255
- handleError(err, { source: "render", componentName: vnode.type.name });
3256
- throw err;
3368
+ return value;
3257
3369
  }
3258
3370
  }
3259
- if (vnode.type === Fragment) {
3260
- const frag = document.createDocumentFragment();
3261
- const children = vnode.props?.children;
3262
- appendChildren(frag, children);
3263
- return frag;
3264
- }
3265
- const tagName = typeof vnode.type === "string" ? vnode.type : "div";
3266
- const el = document.createElement(tagName);
3267
- applyProps(el, vnode.props ?? {});
3268
- return el;
3269
- }
3270
- function template(html, isImportNode, isSVG, isMathML) {
3271
- let node = null;
3272
- const create = () => {
3273
- const t = isMathML ? document.createElementNS("http://www.w3.org/1998/Math/MathML", "template") : document.createElement("template");
3274
- t.innerHTML = html;
3275
- if (isSVG) {
3276
- return t.content.firstChild.firstChild;
3277
- }
3278
- if (isMathML) {
3279
- return t.firstChild;
3371
+ if (isSVG && prop.indexOf(":") > -1) {
3372
+ const [prefix, name] = prop.split(":");
3373
+ const ns = SVGNamespace[prefix];
3374
+ if (ns) {
3375
+ if (value == null) node.removeAttributeNS(ns, name);
3376
+ else node.setAttributeNS(ns, name, String(value));
3377
+ return value;
3280
3378
  }
3281
- return t.content.firstChild;
3282
- };
3283
- const fn = isImportNode ? () => untrack2(() => document.importNode(node || (node = create()), true)) : () => (node || (node = create())).cloneNode(true);
3284
- fn.cloneNode = fn;
3285
- return fn;
3379
+ }
3380
+ const attrName = Aliases[prop] || prop;
3381
+ if (value == null) node.removeAttribute(attrName);
3382
+ else node.setAttribute(attrName, String(value));
3383
+ return value;
3286
3384
  }
3287
- function isBindingHandle(node) {
3288
- return node !== null && typeof node === "object" && "marker" in node && "dispose" in node && typeof node.dispose === "function";
3385
+ function toPropertyName2(name) {
3386
+ return name.toLowerCase().replace(/-([a-z])/g, (_, w) => w.toUpperCase());
3289
3387
  }
3290
- function appendChildNode(parent, child) {
3291
- if (child === null || child === void 0 || child === false) {
3292
- return;
3293
- }
3294
- if (isBindingHandle(child)) {
3295
- appendChildNode(parent, child.marker);
3296
- child.flush?.();
3297
- return;
3298
- }
3299
- if (typeof child === "function" && child.length === 0) {
3300
- const childGetter = child;
3301
- createChildBinding(parent, childGetter, createElement);
3302
- return;
3303
- }
3304
- if (Array.isArray(child)) {
3305
- for (const item of child) {
3306
- appendChildNode(parent, item);
3307
- }
3308
- return;
3309
- }
3310
- let domNode;
3311
- if (typeof child !== "object" || child === null) {
3312
- domNode = document.createTextNode(String(child ?? ""));
3313
- } else {
3314
- domNode = createElement(child);
3315
- }
3316
- if (domNode.nodeType === 11) {
3317
- const children = Array.from(domNode.childNodes);
3318
- for (const node of children) {
3319
- appendChildNode(parent, node);
3388
+ function createConditional(condition, renderTrue, createElementFn, renderFalse) {
3389
+ const startMarker = document.createComment("fict:cond:start");
3390
+ const endMarker = document.createComment("fict:cond:end");
3391
+ const fragment = document.createDocumentFragment();
3392
+ fragment.append(startMarker, endMarker);
3393
+ const hostRoot = getCurrentRoot();
3394
+ let currentNodes = [];
3395
+ let currentRoot2 = null;
3396
+ let lastCondition = void 0;
3397
+ let pendingRender = false;
3398
+ const conditionMemo = computed(condition);
3399
+ const runConditional = () => {
3400
+ const cond = conditionMemo();
3401
+ const parent = startMarker.parentNode;
3402
+ if (!parent) {
3403
+ pendingRender = true;
3404
+ return;
3320
3405
  }
3321
- return;
3322
- }
3323
- if (domNode.ownerDocument !== parent.ownerDocument && parent.ownerDocument) {
3324
- parent.ownerDocument.adoptNode(domNode);
3325
- }
3326
- try {
3327
- parent.appendChild(domNode);
3328
- } catch (e) {
3329
- if (parent.ownerDocument) {
3330
- const clone = parent.ownerDocument.importNode(domNode, true);
3331
- parent.appendChild(clone);
3406
+ pendingRender = false;
3407
+ if (lastCondition === cond && currentNodes.length > 0) {
3332
3408
  return;
3333
3409
  }
3334
- throw e;
3335
- }
3336
- }
3337
- function appendChildren(parent, children) {
3338
- if (children === void 0) return;
3339
- if (Array.isArray(children)) {
3340
- for (const child of children) {
3341
- appendChildren(parent, child);
3410
+ if (lastCondition === cond && lastCondition === false && renderFalse === void 0) {
3411
+ return;
3342
3412
  }
3343
- return;
3344
- }
3345
- appendChildNode(parent, children);
3346
- }
3347
- function applyRef(el, value) {
3348
- if (typeof value === "function") {
3349
- const refFn = value;
3350
- refFn(el);
3351
- if (getCurrentRoot()) {
3352
- registerRootCleanup(() => {
3353
- refFn(null);
3354
- });
3413
+ lastCondition = cond;
3414
+ if (currentRoot2) {
3415
+ destroyRoot(currentRoot2);
3416
+ currentRoot2 = null;
3355
3417
  }
3356
- } else if (value && typeof value === "object" && "current" in value) {
3357
- const refObj = value;
3358
- refObj.current = el;
3359
- if (getCurrentRoot()) {
3360
- registerRootCleanup(() => {
3361
- refObj.current = null;
3362
- });
3418
+ removeNodes(currentNodes);
3419
+ currentNodes = [];
3420
+ const render2 = cond ? renderTrue : renderFalse;
3421
+ if (!render2) {
3422
+ return;
3363
3423
  }
3364
- }
3365
- }
3366
- function applyProps(el, props, isSVG = false) {
3367
- props = unwrapProps(props);
3368
- const tagName = el.tagName;
3369
- const isCE = tagName.includes("-") || "is" in props;
3370
- for (const [key, value] of Object.entries(props)) {
3371
- if (key === "children") continue;
3372
- if (key === "ref") {
3373
- applyRef(el, value);
3374
- continue;
3375
- }
3376
- if (isEventKey(key)) {
3377
- bindEvent(
3378
- el,
3379
- eventNameFromProp(key),
3380
- value
3381
- );
3382
- continue;
3383
- }
3384
- if (key.slice(0, 3) === "on:") {
3385
- bindEvent(
3386
- el,
3387
- key.slice(3),
3388
- value,
3389
- false
3390
- // Non-delegated
3391
- );
3392
- continue;
3393
- }
3394
- if (key.slice(0, 10) === "oncapture:") {
3395
- bindEvent(
3396
- el,
3397
- key.slice(10),
3398
- value,
3399
- true
3400
- // Capture
3401
- );
3402
- continue;
3403
- }
3404
- if (key === "class" || key === "className") {
3405
- createClassBinding(el, value);
3406
- continue;
3424
+ const root = createRootContext(hostRoot);
3425
+ const prev = pushRoot(root);
3426
+ let handledError = false;
3427
+ try {
3428
+ const output = untrack(render2);
3429
+ if (output == null || output === false) {
3430
+ return;
3431
+ }
3432
+ const el = createElementFn(output);
3433
+ const nodes = toNodeArray(el);
3434
+ insertNodesBefore(parent, nodes, endMarker);
3435
+ currentNodes = nodes;
3436
+ } catch (err) {
3437
+ if (handleSuspend(err, root)) {
3438
+ handledError = true;
3439
+ destroyRoot(root);
3440
+ return;
3441
+ }
3442
+ if (handleError(err, { source: "renderChild" }, root)) {
3443
+ handledError = true;
3444
+ destroyRoot(root);
3445
+ return;
3446
+ }
3447
+ throw err;
3448
+ } finally {
3449
+ popRoot(prev);
3450
+ if (!handledError) {
3451
+ flushOnMount(root);
3452
+ currentRoot2 = root;
3453
+ } else {
3454
+ currentRoot2 = null;
3455
+ }
3407
3456
  }
3408
- if (key === "classList") {
3409
- createClassBinding(el, value);
3410
- continue;
3457
+ };
3458
+ const dispose = createRenderEffect(runConditional);
3459
+ return {
3460
+ marker: fragment,
3461
+ flush: () => {
3462
+ if (pendingRender) {
3463
+ runConditional();
3464
+ }
3465
+ },
3466
+ dispose: () => {
3467
+ dispose();
3468
+ if (currentRoot2) {
3469
+ destroyRoot(currentRoot2);
3470
+ }
3471
+ removeNodes(currentNodes);
3472
+ currentNodes = [];
3473
+ startMarker.parentNode?.removeChild(startMarker);
3474
+ endMarker.parentNode?.removeChild(endMarker);
3411
3475
  }
3412
- if (key === "style") {
3413
- createStyleBinding(
3414
- el,
3415
- value
3416
- );
3417
- continue;
3476
+ };
3477
+ }
3478
+ function createList(items, renderItem, createElementFn, getKey) {
3479
+ const startMarker = document.createComment("fict:list:start");
3480
+ const endMarker = document.createComment("fict:list:end");
3481
+ const fragment = document.createDocumentFragment();
3482
+ fragment.append(startMarker, endMarker);
3483
+ const hostRoot = getCurrentRoot();
3484
+ const nodeMap = /* @__PURE__ */ new Map();
3485
+ let pendingItems = null;
3486
+ const runListUpdate = () => {
3487
+ const arr = items();
3488
+ const parent = startMarker.parentNode;
3489
+ if (!parent) {
3490
+ pendingItems = arr;
3491
+ return;
3418
3492
  }
3419
- if (key === "dangerouslySetInnerHTML" && value && typeof value === "object") {
3420
- const htmlValue = value.__html;
3421
- if (htmlValue !== void 0) {
3422
- if (isReactive(htmlValue)) {
3423
- createAttributeBinding(el, "innerHTML", htmlValue, setInnerHTML);
3493
+ pendingItems = null;
3494
+ const newNodeMap = /* @__PURE__ */ new Map();
3495
+ const blocks = [];
3496
+ for (let i = 0; i < arr.length; i++) {
3497
+ const item = arr[i];
3498
+ const key = getKey ? getKey(item, i) : i;
3499
+ const existing = nodeMap.get(key);
3500
+ let block;
3501
+ if (existing) {
3502
+ const previousValue = existing.value();
3503
+ if (!getKey && previousValue !== item) {
3504
+ destroyRoot(existing.root);
3505
+ removeBlockNodes(existing);
3506
+ block = mountBlock(item, i, renderItem, parent, endMarker, createElementFn, hostRoot);
3424
3507
  } else {
3425
- el.innerHTML = htmlValue;
3508
+ const previousIndex = existing.index();
3509
+ existing.value(item);
3510
+ existing.index(i);
3511
+ const needsRerender = getKey ? true : previousValue !== item || previousIndex !== i;
3512
+ block = needsRerender ? rerenderBlock(existing, createElementFn) : existing;
3426
3513
  }
3514
+ } else {
3515
+ block = mountBlock(item, i, renderItem, parent, endMarker, createElementFn, hostRoot);
3427
3516
  }
3428
- continue;
3429
- }
3430
- if (ChildProperties.has(key)) {
3431
- createAttributeBinding(el, key, value, setProperty);
3432
- continue;
3517
+ newNodeMap.set(key, block);
3518
+ blocks.push(block);
3433
3519
  }
3434
- if (key.slice(0, 5) === "attr:") {
3435
- createAttributeBinding(el, key.slice(5), value, setAttribute);
3436
- continue;
3520
+ for (const [key, managed] of nodeMap) {
3521
+ if (!newNodeMap.has(key)) {
3522
+ destroyRoot(managed.root);
3523
+ removeBlockNodes(managed);
3524
+ }
3437
3525
  }
3438
- if (key.slice(0, 5) === "bool:") {
3439
- createAttributeBinding(el, key.slice(5), value, setBoolAttribute);
3440
- continue;
3526
+ let anchor = endMarker;
3527
+ for (let i = blocks.length - 1; i >= 0; i--) {
3528
+ const block = blocks[i];
3529
+ insertNodesBefore(parent, block.nodes, anchor);
3530
+ if (block.nodes.length > 0) {
3531
+ anchor = block.nodes[0];
3532
+ }
3441
3533
  }
3442
- if (key.slice(0, 5) === "prop:") {
3443
- createAttributeBinding(el, key.slice(5), value, setProperty);
3444
- continue;
3534
+ nodeMap.clear();
3535
+ for (const [k, v] of newNodeMap) {
3536
+ nodeMap.set(k, v);
3445
3537
  }
3446
- const propAlias = !isSVG ? getPropAlias(key, tagName) : void 0;
3447
- if (propAlias || !isSVG && Properties.has(key) || isCE && !isSVG) {
3448
- const propName = propAlias || key;
3449
- if (isCE && !Properties.has(key)) {
3450
- createAttributeBinding(
3451
- el,
3452
- toPropertyName2(propName),
3453
- value,
3454
- setProperty
3455
- );
3456
- } else {
3457
- createAttributeBinding(el, propName, value, setProperty);
3538
+ };
3539
+ const dispose = createRenderEffect(runListUpdate);
3540
+ return {
3541
+ marker: fragment,
3542
+ flush: () => {
3543
+ if (pendingItems !== null) {
3544
+ runListUpdate();
3458
3545
  }
3459
- continue;
3460
- }
3461
- if (isSVG && key.indexOf(":") > -1) {
3462
- const [prefix, name] = key.split(":");
3463
- const ns = SVGNamespace[prefix];
3464
- if (ns) {
3465
- createAttributeBinding(
3466
- el,
3467
- key,
3468
- value,
3469
- (el2, _key, val) => setAttributeNS(el2, ns, name, val)
3470
- );
3471
- continue;
3546
+ },
3547
+ dispose: () => {
3548
+ dispose();
3549
+ for (const [, managed] of nodeMap) {
3550
+ destroyRoot(managed.root);
3551
+ removeBlockNodes(managed);
3472
3552
  }
3553
+ nodeMap.clear();
3554
+ startMarker.parentNode?.removeChild(startMarker);
3555
+ endMarker.parentNode?.removeChild(endMarker);
3473
3556
  }
3474
- const attrName = Aliases[key] || key;
3475
- createAttributeBinding(el, attrName, value, setAttribute);
3476
- }
3477
- const children = props.children;
3478
- appendChildren(el, children);
3557
+ };
3479
3558
  }
3480
- function toPropertyName2(name) {
3481
- return name.toLowerCase().replace(/-([a-z])/g, (_, w) => w.toUpperCase());
3559
+ function createShow(el, condition, displayValue) {
3560
+ const originalDisplay = displayValue ?? el.style.display;
3561
+ createRenderEffect(() => {
3562
+ el.style.display = condition() ? originalDisplay : "none";
3563
+ });
3482
3564
  }
3483
- var setAttribute = (el, key, value) => {
3484
- if (value === void 0 || value === null || value === false) {
3485
- el.removeAttribute(key);
3486
- return;
3487
- }
3488
- if (value === true) {
3489
- el.setAttribute(key, "");
3490
- return;
3491
- }
3492
- const valueType = typeof value;
3493
- if (valueType === "string" || valueType === "number") {
3494
- el.setAttribute(key, String(value));
3495
- return;
3496
- }
3497
- if (key in el) {
3498
- el[key] = value;
3499
- return;
3500
- }
3501
- el.setAttribute(key, String(value));
3502
- };
3503
- var setProperty = (el, key, value) => {
3504
- if (value === void 0 || value === null) {
3505
- const fallback = key === "checked" || key === "selected" ? false : "";
3506
- el[key] = fallback;
3507
- return;
3508
- }
3509
- if (key === "style" && typeof value === "object" && value !== null) {
3510
- for (const k in value) {
3511
- const v = value[k];
3512
- if (v !== void 0) {
3513
- el.style[k] = String(v);
3514
- }
3515
- }
3516
- return;
3517
- }
3518
- el[key] = value;
3519
- };
3520
- var setInnerHTML = (el, _key, value) => {
3521
- el.innerHTML = value == null ? "" : String(value);
3522
- };
3523
- var setBoolAttribute = (el, key, value) => {
3524
- if (value) {
3525
- el.setAttribute(key, "");
3526
- } else {
3527
- el.removeAttribute(key);
3528
- }
3529
- };
3530
- function setAttributeNS(el, namespace, name, value) {
3531
- if (value == null) {
3532
- el.removeAttributeNS(namespace, name);
3533
- } else {
3534
- el.setAttributeNS(namespace, name, String(value));
3535
- }
3536
- }
3537
- function isEventKey(key) {
3538
- return key.startsWith("on") && key.length > 2 && key[2].toUpperCase() === key[2];
3539
- }
3540
- function eventNameFromProp(key) {
3541
- return key.slice(2).toLowerCase();
3542
- }
3543
-
3544
- // src/error-boundary.ts
3545
- function ErrorBoundary(props) {
3546
- const fragment = document.createDocumentFragment();
3547
- const marker = document.createComment("fict:error-boundary");
3548
- fragment.appendChild(marker);
3549
- const currentView = signal(props.children ?? null);
3550
- let cleanup;
3551
- let activeNodes = [];
3552
- let renderingFallback = false;
3553
- const toView = (err) => {
3554
- if (err != null) {
3555
- return typeof props.fallback === "function" ? props.fallback(err) : props.fallback;
3556
- }
3557
- return props.children ?? null;
3558
- };
3559
- const renderValue = (value) => {
3560
- if (cleanup) {
3561
- cleanup();
3562
- cleanup = void 0;
3563
- }
3564
- if (activeNodes.length) {
3565
- removeNodes(activeNodes);
3566
- activeNodes = [];
3565
+ function createPortal(container, render2, createElementFn) {
3566
+ const parentRoot = getCurrentRoot();
3567
+ const marker = document.createComment("fict:portal");
3568
+ container.appendChild(marker);
3569
+ let currentNodes = [];
3570
+ let currentRoot2 = null;
3571
+ const dispose = createRenderEffect(() => {
3572
+ if (currentRoot2) {
3573
+ destroyRoot(currentRoot2);
3574
+ currentRoot2 = null;
3567
3575
  }
3568
- if (value == null || value === false) {
3569
- return;
3576
+ if (currentNodes.length > 0) {
3577
+ removeNodes(currentNodes);
3578
+ currentNodes = [];
3570
3579
  }
3571
- const root = createRootContext();
3580
+ const root = createRootContext(parentRoot);
3572
3581
  const prev = pushRoot(root);
3573
- let nodes = [];
3582
+ let handledError = false;
3574
3583
  try {
3575
- const output = createElement(value);
3576
- nodes = toNodeArray(output);
3577
- const parentNode = marker.parentNode;
3578
- if (parentNode) {
3579
- insertNodesBefore(parentNode, nodes, marker);
3584
+ const output = render2();
3585
+ if (output != null && output !== false) {
3586
+ const el = createElementFn(output);
3587
+ const nodes = toNodeArray(el);
3588
+ if (marker.parentNode) {
3589
+ insertNodesBefore(marker.parentNode, nodes, marker);
3590
+ }
3591
+ currentNodes = nodes;
3580
3592
  }
3581
3593
  } catch (err) {
3582
- popRoot(prev);
3583
- flushOnMount(root);
3584
- destroyRoot(root);
3585
- if (renderingFallback) {
3586
- throw err;
3594
+ if (handleSuspend(err, root)) {
3595
+ handledError = true;
3596
+ destroyRoot(root);
3597
+ currentNodes = [];
3598
+ return;
3587
3599
  }
3588
- renderingFallback = true;
3589
- try {
3590
- renderValue(toView(err));
3591
- } finally {
3592
- renderingFallback = false;
3600
+ if (handleError(err, { source: "renderChild" }, root)) {
3601
+ handledError = true;
3602
+ destroyRoot(root);
3603
+ currentNodes = [];
3604
+ return;
3605
+ }
3606
+ throw err;
3607
+ } finally {
3608
+ popRoot(prev);
3609
+ if (!handledError) {
3610
+ flushOnMount(root);
3611
+ currentRoot2 = root;
3612
+ } else {
3613
+ currentRoot2 = null;
3593
3614
  }
3594
- props.onError?.(err);
3595
- return;
3596
3615
  }
3597
- popRoot(prev);
3598
- flushOnMount(root);
3599
- cleanup = () => {
3600
- destroyRoot(root);
3601
- removeNodes(nodes);
3602
- };
3603
- activeNodes = nodes;
3604
- };
3605
- createEffect(() => {
3606
- const value = currentView();
3607
- renderValue(value);
3608
- });
3609
- registerErrorHandler((err) => {
3610
- renderValue(toView(err));
3611
- props.onError?.(err);
3612
- return true;
3613
3616
  });
3614
- if (props.resetKeys !== void 0) {
3615
- const isGetter = typeof props.resetKeys === "function" && props.resetKeys.length === 0;
3616
- const getter = isGetter ? props.resetKeys : void 0;
3617
- let prev = isGetter ? getter() : props.resetKeys;
3618
- createEffect(() => {
3619
- const next = getter ? getter() : props.resetKeys;
3620
- if (prev !== next) {
3621
- prev = next;
3622
- renderValue(toView(null));
3623
- }
3624
- });
3617
+ const portalDispose = () => {
3618
+ dispose();
3619
+ if (currentRoot2) {
3620
+ destroyRoot(currentRoot2);
3621
+ }
3622
+ if (currentNodes.length > 0) {
3623
+ removeNodes(currentNodes);
3624
+ }
3625
+ marker.parentNode?.removeChild(marker);
3626
+ };
3627
+ if (parentRoot) {
3628
+ parentRoot.destroyCallbacks.push(portalDispose);
3625
3629
  }
3626
- return fragment;
3627
- }
3628
-
3629
- // src/suspense.ts
3630
- function createSuspenseToken() {
3631
- let resolve;
3632
- let reject;
3633
- const promise = new Promise((res, rej) => {
3634
- resolve = res;
3635
- reject = rej;
3636
- });
3637
3630
  return {
3638
- token: {
3639
- then: promise.then.bind(promise)
3640
- },
3641
- resolve,
3642
- reject
3631
+ marker,
3632
+ dispose: portalDispose
3643
3633
  };
3644
3634
  }
3645
- var isThenable = (value) => typeof value === "object" && value !== null && typeof value.then === "function";
3646
- function Suspense(props) {
3647
- const currentView = signal(props.children ?? null);
3648
- const pending = signal(0);
3649
- let resolvedOnce = false;
3650
- let epoch = 0;
3651
- const hostRoot = getCurrentRoot();
3652
- const toFallback = (err) => typeof props.fallback === "function" ? props.fallback(err) : props.fallback;
3653
- const switchView = (view) => {
3654
- currentView(view);
3655
- renderView(view);
3656
- };
3657
- const renderView = (view) => {
3658
- if (cleanup) {
3659
- cleanup();
3660
- cleanup = void 0;
3661
- }
3662
- if (activeNodes.length) {
3663
- removeNodes(activeNodes);
3664
- activeNodes = [];
3635
+ function mountBlock(initialValue, initialIndex, renderItem, parent, anchor, createElementFn, hostRoot) {
3636
+ const start = document.createComment("fict:block:start");
3637
+ const end = document.createComment("fict:block:end");
3638
+ const valueSig = createVersionedSignalAccessor(initialValue);
3639
+ const indexSig = signal(initialIndex);
3640
+ const renderCurrent = () => renderItem(valueSig, indexSig);
3641
+ const root = createRootContext(hostRoot);
3642
+ const prev = pushRoot(root);
3643
+ const nodes = [start];
3644
+ let handledError = false;
3645
+ try {
3646
+ const output = renderCurrent();
3647
+ if (output != null && output !== false) {
3648
+ const el = createElementFn(output);
3649
+ const rendered = toNodeArray(el);
3650
+ nodes.push(...rendered);
3665
3651
  }
3666
- if (view == null || view === false) {
3667
- return;
3652
+ nodes.push(end);
3653
+ insertNodesBefore(parent, nodes, anchor);
3654
+ } catch (err) {
3655
+ if (handleSuspend(err, root)) {
3656
+ handledError = true;
3657
+ nodes.push(end);
3658
+ insertNodesBefore(parent, nodes, anchor);
3659
+ } else if (handleError(err, { source: "renderChild" }, root)) {
3660
+ handledError = true;
3661
+ nodes.push(end);
3662
+ insertNodesBefore(parent, nodes, anchor);
3663
+ } else {
3664
+ throw err;
3668
3665
  }
3669
- const root = createRootContext(hostRoot);
3670
- const prev = pushRoot(root);
3671
- let nodes = [];
3672
- try {
3673
- const output = createElement(view);
3674
- nodes = toNodeArray(output);
3675
- const suspendedAttempt = nodes.length > 0 && nodes.every((node) => node instanceof Comment && node.data === "fict:suspend");
3676
- if (suspendedAttempt) {
3677
- popRoot(prev);
3678
- destroyRoot(root);
3679
- return;
3680
- }
3681
- const parentNode = marker.parentNode;
3682
- if (parentNode) {
3683
- insertNodesBefore(parentNode, nodes, marker);
3684
- }
3685
- } catch (err) {
3686
- popRoot(prev);
3666
+ } finally {
3667
+ popRoot(prev);
3668
+ if (!handledError) {
3687
3669
  flushOnMount(root);
3670
+ } else {
3688
3671
  destroyRoot(root);
3689
- handleError(err, { source: "render" });
3690
- return;
3691
3672
  }
3692
- popRoot(prev);
3693
- flushOnMount(root);
3694
- cleanup = () => {
3695
- destroyRoot(root);
3696
- removeNodes(nodes);
3697
- };
3698
- activeNodes = nodes;
3673
+ }
3674
+ return {
3675
+ nodes,
3676
+ root,
3677
+ value: valueSig,
3678
+ index: indexSig,
3679
+ start,
3680
+ end,
3681
+ renderCurrent
3699
3682
  };
3700
- const fragment = document.createDocumentFragment();
3701
- const marker = document.createComment("fict:suspense");
3702
- fragment.appendChild(marker);
3703
- let cleanup;
3704
- let activeNodes = [];
3705
- const onResolveMaybe = () => {
3706
- if (!resolvedOnce) {
3707
- resolvedOnce = true;
3708
- props.onResolve?.();
3683
+ }
3684
+ function rerenderBlock(block, createElementFn) {
3685
+ const currentContent = block.nodes.slice(1, Math.max(1, block.nodes.length - 1));
3686
+ const currentNode = currentContent.length === 1 ? currentContent[0] : null;
3687
+ clearRoot(block.root);
3688
+ const prev = pushRoot(block.root);
3689
+ let nextOutput;
3690
+ let handledError = false;
3691
+ try {
3692
+ nextOutput = block.renderCurrent();
3693
+ } catch (err) {
3694
+ if (handleSuspend(err, block.root)) {
3695
+ handledError = true;
3696
+ popRoot(prev);
3697
+ destroyRoot(block.root);
3698
+ block.nodes = [block.start, block.end];
3699
+ return block;
3709
3700
  }
3710
- };
3711
- registerSuspenseHandler((token) => {
3712
- const tokenEpoch = epoch;
3713
- pending(pending() + 1);
3714
- switchView(toFallback());
3715
- const thenable = token.then ? token : isThenable(token) ? token : null;
3716
- if (thenable) {
3717
- thenable.then(
3718
- () => {
3719
- if (epoch !== tokenEpoch) return;
3720
- pending(Math.max(0, pending() - 1));
3721
- if (pending() === 0) {
3722
- switchView(props.children ?? null);
3723
- onResolveMaybe();
3724
- }
3725
- },
3726
- (err) => {
3727
- if (epoch !== tokenEpoch) return;
3728
- pending(Math.max(0, pending() - 1));
3729
- props.onReject?.(err);
3730
- handleError(err, { source: "render" }, hostRoot);
3731
- }
3732
- );
3733
- return true;
3701
+ if (handleError(err, { source: "renderChild" }, block.root)) {
3702
+ handledError = true;
3703
+ popRoot(prev);
3704
+ destroyRoot(block.root);
3705
+ block.nodes = [block.start, block.end];
3706
+ return block;
3734
3707
  }
3735
- return false;
3736
- });
3737
- createEffect(() => {
3738
- renderView(currentView());
3739
- });
3740
- if (props.resetKeys !== void 0) {
3741
- const isGetter = typeof props.resetKeys === "function" && props.resetKeys.length === 0;
3742
- const getter = isGetter ? props.resetKeys : void 0;
3743
- let prev = isGetter ? getter() : props.resetKeys;
3744
- createEffect(() => {
3745
- const next = getter ? getter() : props.resetKeys;
3746
- if (prev !== next) {
3747
- prev = next;
3748
- epoch++;
3749
- pending(0);
3750
- switchView(props.children ?? null);
3751
- }
3752
- });
3753
- }
3754
- return fragment;
3755
- }
3756
-
3757
- // src/reconcile.ts
3758
- function reconcileArrays(parentNode, a, b) {
3759
- const bLength = b.length;
3760
- let aEnd = a.length;
3761
- let bEnd = bLength;
3762
- let aStart = 0;
3763
- let bStart = 0;
3764
- const after = aEnd > 0 ? a[aEnd - 1].nextSibling : null;
3765
- let map = null;
3766
- while (aStart < aEnd || bStart < bEnd) {
3767
- if (a[aStart] === b[bStart]) {
3768
- aStart++;
3769
- bStart++;
3770
- continue;
3708
+ throw err;
3709
+ } finally {
3710
+ if (!handledError) {
3711
+ popRoot(prev);
3771
3712
  }
3772
- while (a[aEnd - 1] === b[bEnd - 1]) {
3773
- aEnd--;
3774
- bEnd--;
3713
+ }
3714
+ if (isFragmentVNode(nextOutput) && currentContent.length > 0) {
3715
+ const patched = patchFragmentChildren(currentContent, nextOutput.props?.children);
3716
+ if (patched) {
3717
+ block.nodes = [block.start, ...currentContent, block.end];
3718
+ return block;
3775
3719
  }
3776
- if (aEnd === aStart) {
3777
- const node = bEnd < bLength ? bStart ? b[bStart - 1].nextSibling : b[bEnd - bStart] ?? null : after;
3778
- const count = bEnd - bStart;
3779
- const doc = parentNode.ownerDocument;
3780
- if (count > 1 && doc) {
3781
- const frag = doc.createDocumentFragment();
3782
- for (let i = bStart; i < bEnd; i++) {
3783
- frag.appendChild(b[i]);
3784
- }
3785
- parentNode.insertBefore(frag, node);
3786
- bStart = bEnd;
3787
- } else {
3788
- while (bStart < bEnd) {
3789
- parentNode.insertBefore(b[bStart++], node);
3790
- }
3791
- }
3792
- } else if (bEnd === bStart) {
3793
- while (aStart < aEnd) {
3794
- const nodeToRemove = a[aStart];
3795
- if (!map || !map.has(nodeToRemove)) {
3796
- nodeToRemove.parentNode?.removeChild(nodeToRemove);
3720
+ }
3721
+ if (currentNode && patchNode(currentNode, nextOutput)) {
3722
+ block.nodes = [block.start, currentNode, block.end];
3723
+ return block;
3724
+ }
3725
+ clearContent(block);
3726
+ if (nextOutput != null && nextOutput !== false) {
3727
+ const newNodes = toNodeArray(
3728
+ nextOutput instanceof Node ? nextOutput : createElementFn(nextOutput)
3729
+ );
3730
+ insertNodesBefore(block.start.parentNode, newNodes, block.end);
3731
+ block.nodes = [block.start, ...newNodes, block.end];
3732
+ } else {
3733
+ block.nodes = [block.start, block.end];
3734
+ }
3735
+ return block;
3736
+ }
3737
+ function patchElement(el, output) {
3738
+ if (output === null || output === void 0 || output === false || typeof output === "string" || typeof output === "number") {
3739
+ el.textContent = output === null || output === void 0 || output === false ? "" : String(output);
3740
+ return true;
3741
+ }
3742
+ if (output instanceof Text) {
3743
+ el.textContent = output.data;
3744
+ return true;
3745
+ }
3746
+ if (output && typeof output === "object" && !(output instanceof Node)) {
3747
+ const vnode = output;
3748
+ if (typeof vnode.type === "string" && vnode.type.toLowerCase() === el.tagName.toLowerCase()) {
3749
+ const children = vnode.props?.children;
3750
+ const props = vnode.props ?? {};
3751
+ for (const [key, value] of Object.entries(props)) {
3752
+ if (key === "children" || key === "key") continue;
3753
+ if (typeof value === "string" || typeof value === "number" || typeof value === "boolean" || value === null || value === void 0) {
3754
+ if (key === "class" || key === "className") {
3755
+ el.setAttribute("class", value === false || value === null ? "" : String(value));
3756
+ } else if (key === "style" && typeof value === "string") {
3757
+ el.style.cssText = value;
3758
+ } else if (value === false || value === null || value === void 0) {
3759
+ el.removeAttribute(key);
3760
+ } else if (value === true) {
3761
+ el.setAttribute(key, "");
3762
+ } else {
3763
+ el.setAttribute(key, String(value));
3764
+ }
3797
3765
  }
3798
- aStart++;
3799
3766
  }
3800
- } else if (a[aStart] === b[bEnd - 1] && b[bStart] === a[aEnd - 1]) {
3801
- const node = a[--aEnd].nextSibling;
3802
- parentNode.insertBefore(b[bStart++], a[aStart++].nextSibling);
3803
- parentNode.insertBefore(b[--bEnd], node);
3804
- a[aEnd] = b[bEnd];
3805
- } else {
3806
- if (!map) {
3807
- map = /* @__PURE__ */ new Map();
3808
- let i = bStart;
3809
- while (i < bEnd) {
3810
- map.set(b[i], i++);
3811
- }
3767
+ if (typeof children === "string" || typeof children === "number" || children === null || children === void 0 || children === false) {
3768
+ el.textContent = children === null || children === void 0 || children === false ? "" : String(children);
3769
+ return true;
3812
3770
  }
3813
- const index = map.get(a[aStart]);
3814
- if (index != null) {
3815
- if (bStart < index && index < bEnd) {
3816
- let i = aStart;
3817
- let sequence = 1;
3818
- let t;
3819
- while (++i < aEnd && i < bEnd) {
3820
- t = map.get(a[i]);
3821
- if (t == null || t !== index + sequence) break;
3822
- sequence++;
3823
- }
3824
- if (sequence > index - bStart) {
3825
- const node = a[aStart];
3826
- while (bStart < index) {
3827
- parentNode.insertBefore(b[bStart++], node);
3828
- }
3829
- } else {
3830
- parentNode.replaceChild(b[bStart++], a[aStart++]);
3771
+ if (children && typeof children === "object" && !Array.isArray(children) && !(children instanceof Node)) {
3772
+ const childVNode = children;
3773
+ if (typeof childVNode.type === "string") {
3774
+ const childEl = el.querySelector(childVNode.type);
3775
+ if (childEl && patchElement(childEl, children)) {
3776
+ return true;
3831
3777
  }
3832
- } else {
3833
- aStart++;
3834
3778
  }
3835
- } else {
3836
- const nodeToRemove = a[aStart++];
3837
- nodeToRemove.parentNode?.removeChild(nodeToRemove);
3838
3779
  }
3780
+ return false;
3839
3781
  }
3840
3782
  }
3841
- }
3842
-
3843
- // src/list-helpers.ts
3844
- function moveNodesBefore(parent, nodes, anchor) {
3845
- for (let i = nodes.length - 1; i >= 0; i--) {
3846
- const node = nodes[i];
3847
- if (!node || !(node instanceof Node)) {
3848
- throw new Error("Invalid node in moveNodesBefore");
3849
- }
3850
- if (node.nextSibling !== anchor) {
3851
- if (node.ownerDocument !== parent.ownerDocument && parent.ownerDocument) {
3852
- parent.ownerDocument.adoptNode(node);
3853
- }
3854
- try {
3855
- parent.insertBefore(node, anchor);
3856
- } catch (e) {
3857
- if (parent.ownerDocument) {
3858
- try {
3859
- const clone = parent.ownerDocument.importNode(node, true);
3860
- parent.insertBefore(clone, anchor);
3861
- continue;
3862
- } catch {
3863
- }
3864
- }
3865
- throw e;
3783
+ if (output instanceof Node) {
3784
+ if (output.nodeType === Node.ELEMENT_NODE) {
3785
+ const nextEl = output;
3786
+ if (nextEl.tagName.toLowerCase() === el.tagName.toLowerCase()) {
3787
+ el.textContent = nextEl.textContent;
3788
+ return true;
3866
3789
  }
3790
+ } else if (output.nodeType === Node.TEXT_NODE) {
3791
+ el.textContent = output.data;
3792
+ return true;
3867
3793
  }
3868
- anchor = node;
3869
3794
  }
3795
+ return false;
3870
3796
  }
3871
- function moveMarkerBlock(parent, block, anchor) {
3872
- const nodes = collectBlockNodes(block);
3873
- if (nodes.length === 0) return;
3874
- moveNodesBefore(parent, nodes, anchor);
3797
+ function patchNode(currentNode, nextOutput) {
3798
+ if (!currentNode) return false;
3799
+ if (currentNode instanceof Text && (nextOutput === null || nextOutput === void 0 || nextOutput === false || typeof nextOutput === "string" || typeof nextOutput === "number" || nextOutput instanceof Text)) {
3800
+ const nextText = nextOutput instanceof Text ? nextOutput.data : nextOutput === null || nextOutput === void 0 || nextOutput === false ? "" : String(nextOutput);
3801
+ currentNode.data = nextText;
3802
+ return true;
3803
+ }
3804
+ if (currentNode instanceof Element && patchElement(currentNode, nextOutput)) {
3805
+ return true;
3806
+ }
3807
+ if (nextOutput instanceof Node && currentNode === nextOutput) {
3808
+ return true;
3809
+ }
3810
+ return false;
3875
3811
  }
3876
- function destroyMarkerBlock(block) {
3877
- if (block.root) {
3878
- destroyRoot(block.root);
3812
+ function isFragmentVNode(value) {
3813
+ return value != null && typeof value === "object" && !(value instanceof Node) && value.type === Fragment;
3814
+ }
3815
+ function normalizeChildren(children, result = []) {
3816
+ if (children === void 0) {
3817
+ return result;
3879
3818
  }
3880
- removeBlockRange(block);
3819
+ if (Array.isArray(children)) {
3820
+ for (const child of children) {
3821
+ normalizeChildren(child, result);
3822
+ }
3823
+ return result;
3824
+ }
3825
+ if (children === null || children === false) {
3826
+ return result;
3827
+ }
3828
+ result.push(children);
3829
+ return result;
3881
3830
  }
3882
- function collectBlockNodes(block) {
3883
- const nodes = [];
3884
- let cursor = block.start;
3885
- while (cursor) {
3886
- nodes.push(cursor);
3887
- if (cursor === block.end) {
3888
- break;
3831
+ function patchFragmentChildren(nodes, children) {
3832
+ const normalized = normalizeChildren(children);
3833
+ if (normalized.length !== nodes.length) {
3834
+ return false;
3835
+ }
3836
+ for (let i = 0; i < normalized.length; i++) {
3837
+ if (!patchNode(nodes[i], normalized[i])) {
3838
+ return false;
3889
3839
  }
3890
- cursor = cursor.nextSibling;
3891
3840
  }
3892
- return nodes;
3841
+ return true;
3893
3842
  }
3894
- function removeBlockRange(block) {
3843
+ function clearContent(block) {
3844
+ const nodes = block.nodes.slice(1, Math.max(1, block.nodes.length - 1));
3845
+ removeNodes(nodes);
3846
+ }
3847
+ function removeBlockNodes(block) {
3895
3848
  let cursor = block.start;
3849
+ const end = block.end;
3896
3850
  while (cursor) {
3897
3851
  const next = cursor.nextSibling;
3898
3852
  cursor.parentNode?.removeChild(cursor);
3899
- if (cursor === block.end) {
3900
- break;
3901
- }
3853
+ if (cursor === end) break;
3902
3854
  cursor = next;
3903
3855
  }
3904
3856
  }
3905
- function createVersionedSignalAccessor(initialValue) {
3906
- let current = initialValue;
3907
- let version = 0;
3908
- const track2 = signal(version);
3909
- function accessor(value) {
3910
- if (arguments.length === 0) {
3911
- track2();
3912
- return current;
3913
- }
3914
- current = value;
3915
- version++;
3916
- track2(version);
3917
- }
3918
- return accessor;
3919
- }
3920
- function createKeyedListContainer() {
3921
- const startMarker = document.createComment("fict:list:start");
3922
- const endMarker = document.createComment("fict:list:end");
3923
- const dispose = () => {
3924
- for (const block of container.blocks.values()) {
3925
- destroyRoot(block.root);
3857
+
3858
+ // src/scope.ts
3859
+ function createScope() {
3860
+ let dispose = null;
3861
+ const stop = () => {
3862
+ if (dispose) {
3863
+ dispose();
3864
+ dispose = null;
3926
3865
  }
3927
- container.blocks.clear();
3928
- container.nextBlocks.clear();
3929
- const range = document.createRange();
3930
- range.setStartBefore(startMarker);
3931
- range.setEndAfter(endMarker);
3932
- range.deleteContents();
3933
- container.currentNodes = [];
3934
- container.nextNodes = [];
3935
- container.nextBlocks.clear();
3936
- container.orderedBlocks.length = 0;
3937
- container.nextOrderedBlocks.length = 0;
3938
- container.orderedIndexByKey.clear();
3939
3866
  };
3940
- const container = {
3941
- startMarker,
3942
- endMarker,
3943
- blocks: /* @__PURE__ */ new Map(),
3944
- nextBlocks: /* @__PURE__ */ new Map(),
3945
- currentNodes: [startMarker, endMarker],
3946
- nextNodes: [],
3947
- orderedBlocks: [],
3948
- nextOrderedBlocks: [],
3949
- orderedIndexByKey: /* @__PURE__ */ new Map(),
3950
- dispose
3867
+ const run = (fn) => {
3868
+ stop();
3869
+ const { dispose: rootDispose, value } = createRoot(fn);
3870
+ dispose = rootDispose;
3871
+ return value;
3951
3872
  };
3952
- return container;
3873
+ registerRootCleanup(stop);
3874
+ return { run, stop };
3953
3875
  }
3954
- function createKeyedBlock(key, item, index, render2, needsIndex = true) {
3955
- const itemSig = createVersionedSignalAccessor(item);
3956
- const indexSig = needsIndex ? signal(index) : ((next) => {
3957
- if (arguments.length === 0) return index;
3958
- index = next;
3959
- return index;
3960
- });
3961
- const root = createRootContext();
3962
- const prevRoot = pushRoot(root);
3963
- const prevSub = setActiveSub(void 0);
3964
- let nodes = [];
3965
- try {
3966
- const rendered = render2(itemSig, indexSig, key);
3967
- if (rendered instanceof Node || Array.isArray(rendered) && rendered.every((n) => n instanceof Node)) {
3968
- nodes = toNodeArray(rendered);
3876
+ function runInScope(flag, fn) {
3877
+ const scope = createScope();
3878
+ const evaluate = () => isReactive(flag) ? flag() : !!flag;
3879
+ createEffect(() => {
3880
+ const enabled = evaluate();
3881
+ if (enabled) {
3882
+ scope.run(fn);
3969
3883
  } else {
3970
- const element = createElement(rendered);
3971
- nodes = toNodeArray(element);
3884
+ scope.stop();
3972
3885
  }
3973
- } finally {
3974
- setActiveSub(prevSub);
3975
- popRoot(prevRoot);
3976
- flushOnMount(root);
3977
- }
3886
+ });
3887
+ onCleanup(scope.stop);
3888
+ }
3889
+
3890
+ // src/versioned-signal.ts
3891
+ function createVersionedSignal(initialValue, options2) {
3892
+ const equals = options2?.equals ?? Object.is;
3893
+ const value = signal(initialValue);
3894
+ const version = signal(0);
3895
+ const bumpVersion = () => {
3896
+ const next = version() + 1;
3897
+ version(next);
3898
+ };
3978
3899
  return {
3979
- key,
3980
- nodes,
3981
- root,
3982
- item: itemSig,
3983
- index: indexSig,
3984
- rawItem: item,
3985
- rawIndex: index
3900
+ read: () => {
3901
+ version();
3902
+ return value();
3903
+ },
3904
+ write: (next) => {
3905
+ const prev = value();
3906
+ if (!equals(prev, next)) {
3907
+ value(next);
3908
+ return;
3909
+ }
3910
+ bumpVersion();
3911
+ },
3912
+ force: () => {
3913
+ bumpVersion();
3914
+ },
3915
+ peekVersion: () => version(),
3916
+ peekValue: () => value()
3986
3917
  };
3987
3918
  }
3988
- function getFirstNodeAfter(marker) {
3989
- return marker.nextSibling;
3990
- }
3991
- function isNodeBetweenMarkers(node, startMarker, endMarker) {
3992
- let current = startMarker.nextSibling;
3993
- while (current && current !== endMarker) {
3994
- if (current === node) return true;
3995
- current = current.nextSibling;
3996
- }
3997
- return false;
3998
- }
3999
- function createKeyedList(getItems, keyFn, renderItem, needsIndex) {
4000
- const resolvedNeedsIndex = arguments.length >= 4 ? !!needsIndex : renderItem.length > 1;
4001
- return createFineGrainedKeyedList(getItems, keyFn, renderItem, resolvedNeedsIndex);
3919
+
3920
+ // src/ref.ts
3921
+ function createRef() {
3922
+ return { current: null };
4002
3923
  }
4003
- function createFineGrainedKeyedList(getItems, keyFn, renderItem, needsIndex) {
4004
- const container = createKeyedListContainer();
3924
+
3925
+ // src/error-boundary.ts
3926
+ function ErrorBoundary(props) {
4005
3927
  const fragment = document.createDocumentFragment();
4006
- fragment.append(container.startMarker, container.endMarker);
4007
- let pendingItems = null;
4008
- let disposed = false;
4009
- const performDiff = () => {
4010
- if (disposed) return;
4011
- batch2(() => {
4012
- const newItems = pendingItems || getItems();
4013
- pendingItems = null;
4014
- const oldBlocks = container.blocks;
4015
- const newBlocks = container.nextBlocks;
4016
- const prevOrderedBlocks = container.orderedBlocks;
4017
- const nextOrderedBlocks = container.nextOrderedBlocks;
4018
- const orderedIndexByKey = container.orderedIndexByKey;
4019
- newBlocks.clear();
4020
- nextOrderedBlocks.length = 0;
4021
- orderedIndexByKey.clear();
4022
- const endParent = container.endMarker.parentNode;
4023
- const startParent = container.startMarker.parentNode;
4024
- const parent = endParent && startParent && endParent === startParent && endParent.isConnected ? endParent : null;
4025
- if (!parent) {
4026
- pendingItems = newItems;
4027
- queueMicrotask(performDiff);
4028
- return;
4029
- }
4030
- if (newItems.length === 0) {
4031
- if (oldBlocks.size > 0) {
4032
- for (const block of oldBlocks.values()) {
4033
- destroyRoot(block.root);
4034
- removeNodes(block.nodes);
4035
- }
4036
- }
4037
- oldBlocks.clear();
4038
- newBlocks.clear();
4039
- prevOrderedBlocks.length = 0;
4040
- nextOrderedBlocks.length = 0;
4041
- orderedIndexByKey.clear();
4042
- container.currentNodes.length = 0;
4043
- container.currentNodes.push(container.startMarker, container.endMarker);
4044
- container.nextNodes.length = 0;
4045
- return;
4046
- }
4047
- const prevCount = prevOrderedBlocks.length;
4048
- let appendCandidate = prevCount > 0 && newItems.length >= prevCount;
4049
- const appendedBlocks = [];
4050
- newItems.forEach((item, index) => {
4051
- const key = keyFn(item, index);
4052
- const existed = oldBlocks.has(key);
4053
- let block = oldBlocks.get(key);
4054
- if (block) {
4055
- if (block.rawItem !== item) {
4056
- block.rawItem = item;
4057
- block.item(item);
4058
- }
4059
- if (needsIndex && block.rawIndex !== index) {
4060
- block.rawIndex = index;
4061
- block.index(index);
4062
- }
4063
- }
4064
- const existingBlock = newBlocks.get(key);
4065
- if (existingBlock && existingBlock !== block) {
4066
- destroyRoot(existingBlock.root);
4067
- removeNodes(existingBlock.nodes);
4068
- }
4069
- if (block) {
4070
- newBlocks.set(key, block);
4071
- oldBlocks.delete(key);
4072
- } else {
4073
- const existingBlock2 = newBlocks.get(key);
4074
- if (existingBlock2) {
4075
- destroyRoot(existingBlock2.root);
4076
- removeNodes(existingBlock2.nodes);
4077
- }
4078
- block = createKeyedBlock(key, item, index, renderItem, needsIndex);
4079
- }
4080
- const resolvedBlock = block;
4081
- newBlocks.set(key, resolvedBlock);
4082
- const position = orderedIndexByKey.get(key);
4083
- if (position !== void 0) {
4084
- appendCandidate = false;
4085
- }
4086
- if (appendCandidate) {
4087
- if (index < prevCount) {
4088
- if (!prevOrderedBlocks[index] || prevOrderedBlocks[index].key !== key) {
4089
- appendCandidate = false;
4090
- }
4091
- } else if (existed) {
4092
- appendCandidate = false;
4093
- }
4094
- }
4095
- if (position !== void 0) {
4096
- const prior = nextOrderedBlocks[position];
4097
- if (prior && prior !== resolvedBlock) {
4098
- destroyRoot(prior.root);
4099
- removeNodes(prior.nodes);
4100
- }
4101
- nextOrderedBlocks[position] = resolvedBlock;
4102
- } else {
4103
- orderedIndexByKey.set(key, nextOrderedBlocks.length);
4104
- nextOrderedBlocks.push(resolvedBlock);
4105
- }
4106
- if (appendCandidate && index >= prevCount) {
4107
- appendedBlocks.push(resolvedBlock);
4108
- }
4109
- });
4110
- const canAppend = appendCandidate && prevCount > 0 && newItems.length > prevCount && oldBlocks.size === 0 && appendedBlocks.length > 0;
4111
- if (canAppend) {
4112
- const appendedNodes = [];
4113
- for (const block of appendedBlocks) {
4114
- for (let i = 0; i < block.nodes.length; i++) {
4115
- appendedNodes.push(block.nodes[i]);
4116
- }
4117
- }
4118
- if (appendedNodes.length > 0) {
4119
- insertNodesBefore(parent, appendedNodes, container.endMarker);
4120
- const currentNodes = container.currentNodes;
4121
- currentNodes.pop();
4122
- for (let i = 0; i < appendedNodes.length; i++) {
4123
- currentNodes.push(appendedNodes[i]);
4124
- }
4125
- currentNodes.push(container.endMarker);
4126
- }
4127
- container.blocks = newBlocks;
4128
- container.nextBlocks = oldBlocks;
4129
- container.orderedBlocks = nextOrderedBlocks;
4130
- container.nextOrderedBlocks = prevOrderedBlocks;
4131
- return;
3928
+ const marker = document.createComment("fict:error-boundary");
3929
+ fragment.appendChild(marker);
3930
+ const currentView = signal(props.children ?? null);
3931
+ const hostRoot = getCurrentRoot();
3932
+ let cleanup;
3933
+ let activeNodes = [];
3934
+ let renderingFallback = false;
3935
+ const toView = (err) => {
3936
+ if (err != null) {
3937
+ return typeof props.fallback === "function" ? props.fallback(err) : props.fallback;
3938
+ }
3939
+ return props.children ?? null;
3940
+ };
3941
+ const renderValue = (value) => {
3942
+ if (cleanup) {
3943
+ cleanup();
3944
+ cleanup = void 0;
3945
+ }
3946
+ if (activeNodes.length) {
3947
+ removeNodes(activeNodes);
3948
+ activeNodes = [];
3949
+ }
3950
+ if (value == null || value === false) {
3951
+ return;
3952
+ }
3953
+ const root = createRootContext(hostRoot);
3954
+ const prev = pushRoot(root);
3955
+ let nodes = [];
3956
+ try {
3957
+ const output = createElement(value);
3958
+ nodes = toNodeArray(output);
3959
+ const parentNode = marker.parentNode;
3960
+ if (parentNode) {
3961
+ insertNodesBefore(parentNode, nodes, marker);
4132
3962
  }
4133
- if (oldBlocks.size > 0) {
4134
- for (const block of oldBlocks.values()) {
4135
- destroyRoot(block.root);
4136
- removeNodes(block.nodes);
4137
- }
4138
- oldBlocks.clear();
3963
+ } catch (err) {
3964
+ popRoot(prev);
3965
+ flushOnMount(root);
3966
+ destroyRoot(root);
3967
+ if (renderingFallback) {
3968
+ throw err;
4139
3969
  }
4140
- if (newBlocks.size > 0 || container.currentNodes.length > 0) {
4141
- const prevNodes = container.currentNodes;
4142
- const nextNodes = container.nextNodes;
4143
- nextNodes.length = 0;
4144
- nextNodes.push(container.startMarker);
4145
- for (let i = 0; i < nextOrderedBlocks.length; i++) {
4146
- const nodes = nextOrderedBlocks[i].nodes;
4147
- for (let j = 0; j < nodes.length; j++) {
4148
- nextNodes.push(nodes[j]);
4149
- }
4150
- }
4151
- nextNodes.push(container.endMarker);
4152
- reconcileArrays(parent, prevNodes, nextNodes);
4153
- container.currentNodes = nextNodes;
4154
- container.nextNodes = prevNodes;
3970
+ renderingFallback = true;
3971
+ try {
3972
+ renderValue(toView(err));
3973
+ } finally {
3974
+ renderingFallback = false;
4155
3975
  }
4156
- container.blocks = newBlocks;
4157
- container.nextBlocks = oldBlocks;
4158
- container.orderedBlocks = nextOrderedBlocks;
4159
- container.nextOrderedBlocks = prevOrderedBlocks;
4160
- });
3976
+ props.onError?.(err);
3977
+ return;
3978
+ }
3979
+ popRoot(prev);
3980
+ flushOnMount(root);
3981
+ cleanup = () => {
3982
+ destroyRoot(root);
3983
+ removeNodes(nodes);
3984
+ };
3985
+ activeNodes = nodes;
4161
3986
  };
4162
- const effectDispose = createRenderEffect(performDiff);
4163
- return {
4164
- marker: fragment,
4165
- startMarker: container.startMarker,
4166
- endMarker: container.endMarker,
4167
- // Flush pending items - call after markers are inserted into DOM
4168
- flush: () => {
4169
- if (pendingItems !== null) {
4170
- performDiff();
3987
+ createEffect(() => {
3988
+ const value = currentView();
3989
+ renderValue(value);
3990
+ });
3991
+ registerErrorHandler((err) => {
3992
+ renderValue(toView(err));
3993
+ props.onError?.(err);
3994
+ return true;
3995
+ });
3996
+ if (props.resetKeys !== void 0) {
3997
+ const isGetter = typeof props.resetKeys === "function" && props.resetKeys.length === 0;
3998
+ const getter = isGetter ? props.resetKeys : void 0;
3999
+ let prev = isGetter ? getter() : props.resetKeys;
4000
+ createEffect(() => {
4001
+ const next = getter ? getter() : props.resetKeys;
4002
+ if (prev !== next) {
4003
+ prev = next;
4004
+ renderValue(toView(null));
4171
4005
  }
4006
+ });
4007
+ }
4008
+ return fragment;
4009
+ }
4010
+
4011
+ // src/suspense.ts
4012
+ function createSuspenseToken() {
4013
+ let resolve;
4014
+ let reject;
4015
+ const promise = new Promise((res, rej) => {
4016
+ resolve = res;
4017
+ reject = rej;
4018
+ });
4019
+ return {
4020
+ token: {
4021
+ then: promise.then.bind(promise)
4172
4022
  },
4173
- dispose: () => {
4174
- disposed = true;
4175
- effectDispose?.();
4176
- container.dispose();
4023
+ resolve,
4024
+ reject
4025
+ };
4026
+ }
4027
+ var isThenable = (value) => typeof value === "object" && value !== null && typeof value.then === "function";
4028
+ function Suspense(props) {
4029
+ const currentView = signal(props.children ?? null);
4030
+ const pending = signal(0);
4031
+ let resolvedOnce = false;
4032
+ let epoch = 0;
4033
+ const hostRoot = getCurrentRoot();
4034
+ const toFallback = (err) => typeof props.fallback === "function" ? props.fallback(err) : props.fallback;
4035
+ const switchView = (view) => {
4036
+ currentView(view);
4037
+ renderView(view);
4038
+ };
4039
+ const renderView = (view) => {
4040
+ if (cleanup) {
4041
+ cleanup();
4042
+ cleanup = void 0;
4043
+ }
4044
+ if (activeNodes.length) {
4045
+ removeNodes(activeNodes);
4046
+ activeNodes = [];
4047
+ }
4048
+ if (view == null || view === false) {
4049
+ return;
4050
+ }
4051
+ const root = createRootContext(hostRoot);
4052
+ const prev = pushRoot(root);
4053
+ let nodes = [];
4054
+ try {
4055
+ const output = createElement(view);
4056
+ nodes = toNodeArray(output);
4057
+ const suspendedAttempt = nodes.length > 0 && nodes.every((node) => node instanceof Comment && node.data === "fict:suspend");
4058
+ if (suspendedAttempt) {
4059
+ popRoot(prev);
4060
+ destroyRoot(root);
4061
+ return;
4062
+ }
4063
+ const parentNode = marker.parentNode;
4064
+ if (parentNode) {
4065
+ insertNodesBefore(parentNode, nodes, marker);
4066
+ }
4067
+ } catch (err) {
4068
+ popRoot(prev);
4069
+ flushOnMount(root);
4070
+ destroyRoot(root);
4071
+ handleError(err, { source: "render" });
4072
+ return;
4073
+ }
4074
+ popRoot(prev);
4075
+ flushOnMount(root);
4076
+ cleanup = () => {
4077
+ destroyRoot(root);
4078
+ removeNodes(nodes);
4079
+ };
4080
+ activeNodes = nodes;
4081
+ };
4082
+ const fragment = document.createDocumentFragment();
4083
+ const marker = document.createComment("fict:suspense");
4084
+ fragment.appendChild(marker);
4085
+ let cleanup;
4086
+ let activeNodes = [];
4087
+ const onResolveMaybe = () => {
4088
+ if (!resolvedOnce) {
4089
+ resolvedOnce = true;
4090
+ props.onResolve?.();
4177
4091
  }
4178
4092
  };
4093
+ registerSuspenseHandler((token) => {
4094
+ const tokenEpoch = epoch;
4095
+ pending(pending() + 1);
4096
+ switchView(toFallback());
4097
+ const thenable = token.then ? token : isThenable(token) ? token : null;
4098
+ if (thenable) {
4099
+ thenable.then(
4100
+ () => {
4101
+ if (epoch !== tokenEpoch) return;
4102
+ pending(Math.max(0, pending() - 1));
4103
+ if (pending() === 0) {
4104
+ switchView(props.children ?? null);
4105
+ onResolveMaybe();
4106
+ }
4107
+ },
4108
+ (err) => {
4109
+ if (epoch !== tokenEpoch) return;
4110
+ pending(Math.max(0, pending() - 1));
4111
+ props.onReject?.(err);
4112
+ handleError(err, { source: "render" }, hostRoot);
4113
+ }
4114
+ );
4115
+ return true;
4116
+ }
4117
+ return false;
4118
+ });
4119
+ createEffect(() => {
4120
+ renderView(currentView());
4121
+ });
4122
+ if (props.resetKeys !== void 0) {
4123
+ const isGetter = typeof props.resetKeys === "function" && props.resetKeys.length === 0;
4124
+ const getter = isGetter ? props.resetKeys : void 0;
4125
+ let prev = isGetter ? getter() : props.resetKeys;
4126
+ createEffect(() => {
4127
+ const next = getter ? getter() : props.resetKeys;
4128
+ if (prev !== next) {
4129
+ prev = next;
4130
+ epoch++;
4131
+ pending(0);
4132
+ switchView(props.children ?? null);
4133
+ }
4134
+ });
4135
+ }
4136
+ return fragment;
4179
4137
  }
4180
4138
 
4181
4139
  export { $effect, $memo, $state, Aliases, BooleanAttributes, ChildProperties, DelegatedEvents, ErrorBoundary, Fragment, Properties, SVGElements, SVGNamespace, Suspense, UnitlessStyles, __fictPopContext, __fictProp, __fictPropsRest, __fictPushContext, __fictRender, __fictResetContext, __fictUseContext, __fictUseEffect, __fictUseMemo, __fictUseSignal, addEventListener, assign, batch2 as batch, bindAttribute, bindClass, bindEvent, bindProperty, bindRef, bindStyle, bindText, callEventHandler, classList, clearDelegatedEvents, createAttributeBinding, createChildBinding, createClassBinding, createConditional, createEffect, createElement, createKeyedBlock, createKeyedList, createKeyedListContainer, createList, createMemo, createPortal, createPropsProxy, createRef, createRenderEffect, createRoot, createScope, createSelector, createShow, signal as createSignal, createStore, createStyleBinding, createSuspenseToken, createTextBinding, createVersionedSignal, delegateEvents, destroyMarkerBlock, effectScope, getDevtoolsHook, getFirstNodeAfter, getPropAlias, insert, insertNodesBefore, isNodeBetweenMarkers, isReactive, mergeProps, moveMarkerBlock, moveNodesBefore, onCleanup, onDestroy, onMount, __fictProp as prop, reconcileArrays, removeNodes, render, runInScope, setCycleProtectionOptions, spread, startTransition, template, toNodeArray, untrack2 as untrack, unwrap2 as unwrap, unwrapPrimitive, useDeferredValue, useProp, useTransition };