bobe 0.0.53 → 0.0.54

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/bobe.cjs.js CHANGED
@@ -1656,6 +1656,44 @@ class Interpreter {
1656
1656
  node.realAfter = this.insertAfterAnchor('context-after');
1657
1657
  return node;
1658
1658
  }
1659
+ formatForCollection(collection) {
1660
+ const res = {
1661
+ arr: [],
1662
+ keys: null
1663
+ };
1664
+ if (Array.isArray(collection)) {
1665
+ res.arr = collection;
1666
+ return res;
1667
+ }
1668
+ if (collection instanceof Map) {
1669
+ res.keys = [];
1670
+ collection.forEach((v, k) => {
1671
+ res.arr.push(v);
1672
+ res.keys.push(k);
1673
+ });
1674
+ return res;
1675
+ }
1676
+ if (typeof collection === 'object' && collection !== null) {
1677
+ if (collection[Symbol.iterator]) {
1678
+ res.arr = Array.from(collection);
1679
+ return res;
1680
+ }
1681
+ res.arr = Object.values(collection);
1682
+ res.keys = Object.keys(collection);
1683
+ return res;
1684
+ }
1685
+ if (typeof collection === 'number') {
1686
+ res.arr = Array.from({
1687
+ length: collection
1688
+ });
1689
+ return res;
1690
+ }
1691
+ if (typeof collection === 'string') {
1692
+ res.arr = collection.split('');
1693
+ return res;
1694
+ }
1695
+ return res;
1696
+ }
1659
1697
  forDeclaration() {
1660
1698
  const arrExp = this.tokenizer.jsExp().value;
1661
1699
  this.tokenizer.nextToken();
@@ -1692,6 +1730,8 @@ class Interpreter {
1692
1730
  realBefore: prevSibling?.realAfter || prevSibling,
1693
1731
  realAfter: null,
1694
1732
  arr: null,
1733
+ reactiveArr: null,
1734
+ keys: null,
1695
1735
  arrSignal: null,
1696
1736
  itemExp,
1697
1737
  indexName,
@@ -1718,11 +1758,17 @@ class Interpreter {
1718
1758
  const snapshotForUpdate = _objectWithoutProperties(_forNode$snapshot, _excluded);
1719
1759
  let isFirstRender = true;
1720
1760
  forNode.effect = new this.Effect(() => {
1721
- let arr = arrSignal.get();
1761
+ const collection = arrSignal.get();
1762
+ const _this$formatForCollec = this.formatForCollection(collection),
1763
+ formattedArr = _this$formatForCollec.arr,
1764
+ keys = _this$formatForCollec.keys;
1765
+ let arr = formattedArr;
1722
1766
  arr[aoye.Keys.Iterator];
1723
1767
  const prevCtx = aoye.getPulling();
1724
1768
  aoye.setPulling(null);
1725
- forNode.arr = arr = aoye.toRaw(arr);
1769
+ forNode.reactiveArr = formattedArr;
1770
+ forNode.arr = Array.isArray(collection) ? aoye.toRaw(arr) : arr;
1771
+ forNode.keys = keys;
1726
1772
  const children = forNode.children;
1727
1773
  if (isFirstRender) {
1728
1774
  const len = arr.length;
@@ -1760,7 +1806,7 @@ class Interpreter {
1760
1806
  for (let i = minLen; i--;) {
1761
1807
  const child = children[i];
1762
1808
  newChildren[i] = child;
1763
- this.reuseForItem(child, arr[i], itemExp, i, indexName);
1809
+ this.reuseForItem(child, arr[i], itemExp, i, indexName, keys?.[i]);
1764
1810
  }
1765
1811
  } else {
1766
1812
  let s = 0,
@@ -1773,7 +1819,7 @@ class Interpreter {
1773
1819
  const key = forNode.getKey(itemData);
1774
1820
  if (old === key) {
1775
1821
  newChildren[s] = child;
1776
- this.reuseForItem(child, arr[s], itemExp, s, indexName);
1822
+ this.reuseForItem(child, arr[s], itemExp, s, indexName, keys?.[s]);
1777
1823
  s++;
1778
1824
  } else {
1779
1825
  break;
@@ -1786,7 +1832,7 @@ class Interpreter {
1786
1832
  const key = forNode.getKey(itemData);
1787
1833
  if (old === key) {
1788
1834
  newChildren[e2] = child;
1789
- this.reuseForItem(child, arr[e2], itemExp, e2, indexName);
1835
+ this.reuseForItem(child, arr[e2], itemExp, e2, indexName, keys?.[e2]);
1790
1836
  e1--;
1791
1837
  e2--;
1792
1838
  } else {
@@ -1828,7 +1874,7 @@ class Interpreter {
1828
1874
  }
1829
1875
  const child = children[i];
1830
1876
  newChildren[newI] = child;
1831
- this.reuseForItem(child, arr[newI], itemExp, newI, indexName);
1877
+ this.reuseForItem(child, arr[newI], itemExp, newI, indexName, keys?.[newI]);
1832
1878
  new2oldI[newI - s2] = i;
1833
1879
  key2new.delete(key);
1834
1880
  if (newI < maxIncNewI) {
@@ -1914,15 +1960,15 @@ class Interpreter {
1914
1960
  this.remove(child.realAfter);
1915
1961
  child.effect.dispose();
1916
1962
  }
1917
- reuseForItem(child, data, itemExp, i, indexName) {
1963
+ reuseForItem(child, data, itemExp, i, indexName, indexValue) {
1918
1964
  if (typeof itemExp === 'string') {
1919
1965
  child.data[itemExp] = data;
1920
1966
  if (indexName) {
1921
- child.data[indexName] = i;
1967
+ child.data[indexName] = indexValue ?? i;
1922
1968
  }
1923
1969
  } else {
1924
1970
  indexName = indexName || KEY_INDEX;
1925
- child.data[indexName] = i;
1971
+ child.data[indexName] = indexValue ?? i;
1926
1972
  }
1927
1973
  }
1928
1974
  forItemId = 0;
@@ -1956,23 +2002,24 @@ class Interpreter {
1956
2002
  itemExp = forNode.itemExp,
1957
2003
  vars = forNode.vars,
1958
2004
  arrSignal = forNode.arrSignal,
1959
- getKey = forNode.getKey;
2005
+ getKey = forNode.getKey,
2006
+ keys = forNode.keys;
1960
2007
  let indexName = forNode.indexName;
1961
2008
  let data;
1962
2009
  if (typeof itemExp === 'string') {
1963
2010
  data = aoye.deepSignal(indexName ? {
1964
2011
  [itemExp]: arr[i],
1965
- [indexName]: i
2012
+ [indexName]: keys?.[i] ?? i
1966
2013
  } : {
1967
2014
  [itemExp]: arr[i]
1968
2015
  }, aoye.getPulling());
1969
2016
  } else {
1970
2017
  indexName = indexName ?? KEY_INDEX;
1971
2018
  const rawData = {
1972
- [indexName]: i
2019
+ [indexName]: keys?.[i] ?? i
1973
2020
  };
1974
2021
  data = aoye.deepSignal(rawData, aoye.getPulling());
1975
- const computedData = new aoye.Computed(() => itemExp(arrSignal.get()[getKey ? data[indexName] : i]));
2022
+ const computedData = new aoye.Computed(() => itemExp((arrSignal.get(), forNode.reactiveArr)[getKey ? data[indexName] : keys ? (data[indexName], i) : i]));
1976
2023
  const cells = data[aoye.Keys.Meta].cells;
1977
2024
  for (let i = 0; i < vars.length; i++) {
1978
2025
  const name = vars[i];