bobe 0.0.52 → 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.
@@ -1683,6 +1683,44 @@ class Interpreter {
1683
1683
  node.realAfter = this.insertAfterAnchor('context-after');
1684
1684
  return node;
1685
1685
  }
1686
+ formatForCollection(collection) {
1687
+ const res = {
1688
+ arr: [],
1689
+ keys: null
1690
+ };
1691
+ if (Array.isArray(collection)) {
1692
+ res.arr = collection;
1693
+ return res;
1694
+ }
1695
+ if (collection instanceof Map) {
1696
+ res.keys = [];
1697
+ collection.forEach((v, k) => {
1698
+ res.arr.push(v);
1699
+ res.keys.push(k);
1700
+ });
1701
+ return res;
1702
+ }
1703
+ if (typeof collection === 'object' && collection !== null) {
1704
+ if (collection[Symbol.iterator]) {
1705
+ res.arr = Array.from(collection);
1706
+ return res;
1707
+ }
1708
+ res.arr = Object.values(collection);
1709
+ res.keys = Object.keys(collection);
1710
+ return res;
1711
+ }
1712
+ if (typeof collection === 'number') {
1713
+ res.arr = Array.from({
1714
+ length: collection
1715
+ });
1716
+ return res;
1717
+ }
1718
+ if (typeof collection === 'string') {
1719
+ res.arr = collection.split('');
1720
+ return res;
1721
+ }
1722
+ return res;
1723
+ }
1686
1724
  forDeclaration() {
1687
1725
  const arrExp = this.tokenizer.jsExp().value;
1688
1726
  this.tokenizer.nextToken();
@@ -1719,6 +1757,8 @@ class Interpreter {
1719
1757
  realBefore: prevSibling?.realAfter || prevSibling,
1720
1758
  realAfter: null,
1721
1759
  arr: null,
1760
+ reactiveArr: null,
1761
+ keys: null,
1722
1762
  arrSignal: null,
1723
1763
  itemExp,
1724
1764
  indexName,
@@ -1745,11 +1785,17 @@ class Interpreter {
1745
1785
  const snapshotForUpdate = _objectWithoutProperties(_forNode$snapshot, _excluded);
1746
1786
  let isFirstRender = true;
1747
1787
  forNode.effect = new this.Effect(() => {
1748
- let arr = arrSignal.get();
1788
+ const collection = arrSignal.get();
1789
+ const _this$formatForCollec = this.formatForCollection(collection),
1790
+ formattedArr = _this$formatForCollec.arr,
1791
+ keys = _this$formatForCollec.keys;
1792
+ let arr = formattedArr;
1749
1793
  arr[aoye.Keys.Iterator];
1750
1794
  const prevCtx = aoye.getPulling();
1751
1795
  aoye.setPulling(null);
1752
- forNode.arr = arr = aoye.toRaw(arr);
1796
+ forNode.reactiveArr = formattedArr;
1797
+ forNode.arr = Array.isArray(collection) ? aoye.toRaw(arr) : arr;
1798
+ forNode.keys = keys;
1753
1799
  const children = forNode.children;
1754
1800
  if (isFirstRender) {
1755
1801
  const len = arr.length;
@@ -1787,7 +1833,7 @@ class Interpreter {
1787
1833
  for (let i = minLen; i--;) {
1788
1834
  const child = children[i];
1789
1835
  newChildren[i] = child;
1790
- this.reuseForItem(child, arr[i], itemExp, i, indexName);
1836
+ this.reuseForItem(child, arr[i], itemExp, i, indexName, keys?.[i]);
1791
1837
  }
1792
1838
  } else {
1793
1839
  let s = 0,
@@ -1800,7 +1846,7 @@ class Interpreter {
1800
1846
  const key = forNode.getKey(itemData);
1801
1847
  if (old === key) {
1802
1848
  newChildren[s] = child;
1803
- this.reuseForItem(child, arr[s], itemExp, s, indexName);
1849
+ this.reuseForItem(child, arr[s], itemExp, s, indexName, keys?.[s]);
1804
1850
  s++;
1805
1851
  } else {
1806
1852
  break;
@@ -1813,7 +1859,7 @@ class Interpreter {
1813
1859
  const key = forNode.getKey(itemData);
1814
1860
  if (old === key) {
1815
1861
  newChildren[e2] = child;
1816
- this.reuseForItem(child, arr[e2], itemExp, e2, indexName);
1862
+ this.reuseForItem(child, arr[e2], itemExp, e2, indexName, keys?.[e2]);
1817
1863
  e1--;
1818
1864
  e2--;
1819
1865
  } else {
@@ -1855,7 +1901,7 @@ class Interpreter {
1855
1901
  }
1856
1902
  const child = children[i];
1857
1903
  newChildren[newI] = child;
1858
- this.reuseForItem(child, arr[newI], itemExp, newI, indexName);
1904
+ this.reuseForItem(child, arr[newI], itemExp, newI, indexName, keys?.[newI]);
1859
1905
  new2oldI[newI - s2] = i;
1860
1906
  key2new.delete(key);
1861
1907
  if (newI < maxIncNewI) {
@@ -1941,15 +1987,15 @@ class Interpreter {
1941
1987
  this.remove(child.realAfter);
1942
1988
  child.effect.dispose();
1943
1989
  }
1944
- reuseForItem(child, data, itemExp, i, indexName) {
1990
+ reuseForItem(child, data, itemExp, i, indexName, indexValue) {
1945
1991
  if (typeof itemExp === 'string') {
1946
1992
  child.data[itemExp] = data;
1947
1993
  if (indexName) {
1948
- child.data[indexName] = i;
1994
+ child.data[indexName] = indexValue ?? i;
1949
1995
  }
1950
1996
  } else {
1951
1997
  indexName = indexName || KEY_INDEX;
1952
- child.data[indexName] = i;
1998
+ child.data[indexName] = indexValue ?? i;
1953
1999
  }
1954
2000
  }
1955
2001
  forItemId = 0;
@@ -1983,23 +2029,24 @@ class Interpreter {
1983
2029
  itemExp = forNode.itemExp,
1984
2030
  vars = forNode.vars,
1985
2031
  arrSignal = forNode.arrSignal,
1986
- getKey = forNode.getKey;
2032
+ getKey = forNode.getKey,
2033
+ keys = forNode.keys;
1987
2034
  let indexName = forNode.indexName;
1988
2035
  let data;
1989
2036
  if (typeof itemExp === 'string') {
1990
2037
  data = aoye.deepSignal(indexName ? {
1991
2038
  [itemExp]: arr[i],
1992
- [indexName]: i
2039
+ [indexName]: keys?.[i] ?? i
1993
2040
  } : {
1994
2041
  [itemExp]: arr[i]
1995
2042
  }, aoye.getPulling());
1996
2043
  } else {
1997
2044
  indexName = indexName ?? KEY_INDEX;
1998
2045
  const rawData = {
1999
- [indexName]: i
2046
+ [indexName]: keys?.[i] ?? i
2000
2047
  };
2001
2048
  data = aoye.deepSignal(rawData, aoye.getPulling());
2002
- const computedData = new aoye.Computed(() => itemExp(arrSignal.get()[getKey ? data[indexName] : i]));
2049
+ const computedData = new aoye.Computed(() => itemExp((arrSignal.get(), forNode.reactiveArr)[getKey ? data[indexName] : keys ? (data[indexName], i) : i]));
2003
2050
  const cells = data[aoye.Keys.Meta].cells;
2004
2051
  for (let i = 0; i < vars.length; i++) {
2005
2052
  const name = vars[i];