cogsbox-state 0.5.406 → 0.5.407

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cogsbox-state",
3
- "version": "0.5.406",
3
+ "version": "0.5.407",
4
4
  "description": "React state management library with form controls and server sync",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
package/src/CogsState.tsx CHANGED
@@ -1806,6 +1806,8 @@ function createProxyHandler<T>(
1806
1806
  // Simplified useVirtualView approach
1807
1807
  // Optimal approach - replace the useVirtualView implementation
1808
1808
  // Complete useVirtualView implementation with comprehensive logging
1809
+ // Complete fixed useVirtualView implementation
1810
+ // Complete fixed useVirtualView implementation
1809
1811
  if (prop === "useVirtualView") {
1810
1812
  return (
1811
1813
  options: VirtualViewOptions
@@ -1817,19 +1819,13 @@ function createProxyHandler<T>(
1817
1819
  dependencies = [],
1818
1820
  } = options;
1819
1821
 
1820
- console.log("useVirtualView initialized with:", {
1821
- itemHeight,
1822
- overscan,
1823
- stickToBottom,
1824
- });
1825
-
1826
1822
  const containerRef = useRef<HTMLDivElement | null>(null);
1827
1823
  const [range, setRange] = useState({
1828
1824
  startIndex: 0,
1829
1825
  endIndex: 10,
1830
1826
  });
1831
1827
  const [shadowUpdateTrigger, setShadowUpdateTrigger] = useState(0);
1832
- const isUserScrollingRef = useRef(false);
1828
+ const isProgrammaticScrollRef = useRef(false); // Track if we're scrolling programmatically
1833
1829
  const shouldStickToBottomRef = useRef(true);
1834
1830
  const scrollToBottomIntervalRef = useRef<NodeJS.Timeout | null>(
1835
1831
  null
@@ -1837,11 +1833,9 @@ function createProxyHandler<T>(
1837
1833
 
1838
1834
  // Subscribe to shadow state updates
1839
1835
  useEffect(() => {
1840
- console.log("Setting up shadow state subscription");
1841
1836
  const unsubscribe = getGlobalStore
1842
1837
  .getState()
1843
1838
  .subscribeToShadowState(stateKey, () => {
1844
- console.log("Shadow state updated");
1845
1839
  setShadowUpdateTrigger((prev) => prev + 1);
1846
1840
  });
1847
1841
  return unsubscribe;
@@ -1852,11 +1846,9 @@ function createProxyHandler<T>(
1852
1846
  path
1853
1847
  ) as any[];
1854
1848
  const totalCount = sourceArray.length;
1855
- console.log("Source array length:", totalCount);
1856
1849
 
1857
1850
  // Calculate heights and positions
1858
1851
  const { totalHeight, positions } = useMemo(() => {
1859
- console.log("Recalculating heights and positions");
1860
1852
  const shadowArray =
1861
1853
  getGlobalStore.getState().getShadowMetadata(stateKey, path) ||
1862
1854
  [];
@@ -1868,7 +1860,6 @@ function createProxyHandler<T>(
1868
1860
  shadowArray[i]?.virtualizer?.itemHeight;
1869
1861
  height += measuredHeight || itemHeight;
1870
1862
  }
1871
- console.log("Total height calculated:", height);
1872
1863
  return { totalHeight: height, positions: pos };
1873
1864
  }, [
1874
1865
  totalCount,
@@ -1882,10 +1873,6 @@ function createProxyHandler<T>(
1882
1873
  const virtualState = useMemo(() => {
1883
1874
  const start = Math.max(0, range.startIndex);
1884
1875
  const end = Math.min(totalCount, range.endIndex);
1885
- console.log("Creating virtual state for range:", {
1886
- start,
1887
- end,
1888
- });
1889
1876
  const validIndices = Array.from(
1890
1877
  { length: end - start },
1891
1878
  (_, i) => start + i
@@ -1899,41 +1886,12 @@ function createProxyHandler<T>(
1899
1886
 
1900
1887
  // Handle auto-scroll to bottom
1901
1888
  useEffect(() => {
1902
- console.log("Auto-scroll effect triggered:", {
1903
- stickToBottom,
1904
- hasContainer: !!containerRef.current,
1905
- totalCount,
1906
- shouldStickToBottom: shouldStickToBottomRef.current,
1907
- range,
1908
- });
1909
-
1910
- if (!stickToBottom) {
1911
- console.log("Stick to bottom is false, skipping");
1912
- return;
1913
- }
1914
-
1915
- if (!containerRef.current) {
1916
- console.log("No container ref, skipping");
1917
- return;
1918
- }
1919
-
1920
- if (totalCount === 0) {
1921
- console.log("No items, skipping");
1922
- return;
1923
- }
1924
-
1925
- if (!shouldStickToBottomRef.current) {
1926
- console.log(
1927
- "Should not stick to bottom (user scrolled), skipping"
1928
- );
1889
+ if (!stickToBottom || !containerRef.current || totalCount === 0)
1929
1890
  return;
1930
- }
1931
-
1932
- console.log("Proceeding with auto-scroll logic");
1891
+ if (!shouldStickToBottomRef.current) return;
1933
1892
 
1934
1893
  // Clear any existing interval
1935
1894
  if (scrollToBottomIntervalRef.current) {
1936
- console.log("Clearing existing scroll interval");
1937
1895
  clearInterval(scrollToBottomIntervalRef.current);
1938
1896
  }
1939
1897
 
@@ -1942,34 +1900,29 @@ function createProxyHandler<T>(
1942
1900
  const isInitialLoad = range.endIndex < jumpThreshold;
1943
1901
  const isBigJump = totalCount > range.endIndex + jumpThreshold;
1944
1902
 
1945
- console.log("Jump check:", {
1946
- isInitialLoad,
1947
- isBigJump,
1948
- totalCount,
1949
- currentEndIndex: range.endIndex,
1950
- });
1951
-
1952
1903
  if (isInitialLoad || isBigJump) {
1904
+ // Set programmatic scroll flag BEFORE changing range
1905
+ isProgrammaticScrollRef.current = true;
1906
+
1953
1907
  const newRange = {
1954
1908
  startIndex: Math.max(0, totalCount - 20),
1955
1909
  endIndex: totalCount,
1956
1910
  };
1957
- console.log("Jumping to end range:", newRange);
1958
1911
  setRange(newRange);
1912
+
1913
+ // Reset flag after a delay to ensure scroll events are ignored
1914
+ setTimeout(() => {
1915
+ isProgrammaticScrollRef.current = false;
1916
+ }, 100);
1959
1917
  }
1960
1918
 
1961
1919
  // Keep scrolling to bottom until we're actually there
1962
1920
  let attempts = 0;
1963
1921
  const maxAttempts = 50; // 5 seconds max
1964
1922
 
1965
- console.log("Starting scroll-to-bottom interval");
1966
-
1967
1923
  scrollToBottomIntervalRef.current = setInterval(() => {
1968
1924
  const container = containerRef.current;
1969
- if (!container) {
1970
- console.log("Container lost during interval");
1971
- return;
1972
- }
1925
+ if (!container) return;
1973
1926
 
1974
1927
  attempts++;
1975
1928
 
@@ -1978,32 +1931,23 @@ function createProxyHandler<T>(
1978
1931
  const actualBottom = scrollHeight;
1979
1932
  const isAtBottom = actualBottom - currentBottom < 5;
1980
1933
 
1981
- console.log(`Scroll attempt ${attempts}:`, {
1982
- currentBottom,
1983
- actualBottom,
1984
- isAtBottom,
1985
- scrollTop,
1986
- scrollHeight,
1987
- clientHeight,
1988
- });
1989
-
1990
1934
  if (isAtBottom || attempts >= maxAttempts) {
1991
- console.log(
1992
- isAtBottom
1993
- ? "Successfully reached bottom!"
1994
- : "Timeout - giving up"
1995
- );
1996
1935
  clearInterval(scrollToBottomIntervalRef.current!);
1997
1936
  scrollToBottomIntervalRef.current = null;
1998
1937
  } else {
1999
- console.log("Scrolling to", container.scrollHeight);
1938
+ // Set flag before scrolling
1939
+ isProgrammaticScrollRef.current = true;
2000
1940
  container.scrollTop = container.scrollHeight;
1941
+
1942
+ // Reset flag after a short delay
1943
+ setTimeout(() => {
1944
+ isProgrammaticScrollRef.current = false;
1945
+ }, 50);
2001
1946
  }
2002
1947
  }, 100);
2003
1948
 
2004
1949
  // Cleanup
2005
1950
  return () => {
2006
- console.log("Cleaning up scroll interval");
2007
1951
  if (scrollToBottomIntervalRef.current) {
2008
1952
  clearInterval(scrollToBottomIntervalRef.current);
2009
1953
  scrollToBottomIntervalRef.current = null;
@@ -2014,18 +1958,16 @@ function createProxyHandler<T>(
2014
1958
  // Handle user scroll
2015
1959
  useEffect(() => {
2016
1960
  const container = containerRef.current;
2017
- if (!container) {
2018
- console.log("No container for scroll handler");
2019
- return;
2020
- }
2021
-
2022
- console.log("Setting up scroll handler");
2023
-
2024
- let scrollTimeout: NodeJS.Timeout;
1961
+ if (!container) return;
2025
1962
 
2026
1963
  const handleScroll = () => {
1964
+ // Ignore programmatic scrolls
1965
+ if (isProgrammaticScrollRef.current) {
1966
+ return;
1967
+ }
1968
+
1969
+ // This is a real user scroll
2027
1970
  if (scrollToBottomIntervalRef.current) {
2028
- console.log("User scrolled - stopping auto-scroll");
2029
1971
  clearInterval(scrollToBottomIntervalRef.current);
2030
1972
  scrollToBottomIntervalRef.current = null;
2031
1973
  }
@@ -2034,17 +1976,8 @@ function createProxyHandler<T>(
2034
1976
  const isAtBottom =
2035
1977
  scrollHeight - scrollTop - clientHeight < 10;
2036
1978
 
2037
- // Update whether we should stick to bottom
1979
+ // Only update this for real user scrolls
2038
1980
  shouldStickToBottomRef.current = isAtBottom;
2039
- console.log("User scroll - at bottom:", isAtBottom);
2040
-
2041
- // Mark as user scrolling
2042
- clearTimeout(scrollTimeout);
2043
- isUserScrollingRef.current = true;
2044
- scrollTimeout = setTimeout(() => {
2045
- isUserScrollingRef.current = false;
2046
- console.log("User stopped scrolling");
2047
- }, 150);
2048
1981
 
2049
1982
  // Update visible range
2050
1983
  let startIndex = 0;
@@ -2064,49 +1997,49 @@ function createProxyHandler<T>(
2064
1997
  endIndex = i;
2065
1998
  }
2066
1999
 
2067
- const newRange = {
2000
+ setRange({
2068
2001
  startIndex: Math.max(0, startIndex),
2069
2002
  endIndex: Math.min(totalCount, endIndex + 1 + overscan),
2070
- };
2071
-
2072
- console.log("Updating visible range:", newRange);
2073
- setRange(newRange);
2003
+ });
2074
2004
  };
2075
2005
 
2076
2006
  container.addEventListener("scroll", handleScroll, {
2077
2007
  passive: true,
2078
2008
  });
2079
- console.log("Initial scroll calculation");
2080
2009
  handleScroll(); // Initial calculation
2081
2010
 
2082
2011
  return () => {
2083
- console.log("Removing scroll handler");
2084
2012
  container.removeEventListener("scroll", handleScroll);
2085
- clearTimeout(scrollTimeout);
2086
2013
  };
2087
2014
  }, [positions, totalCount, itemHeight, overscan]);
2088
2015
 
2089
2016
  const scrollToBottom = useCallback(
2090
2017
  (behavior: ScrollBehavior = "auto") => {
2091
- console.log("Manual scrollToBottom called");
2092
2018
  shouldStickToBottomRef.current = true;
2019
+ isProgrammaticScrollRef.current = true;
2093
2020
  if (containerRef.current) {
2094
2021
  containerRef.current.scrollTop =
2095
2022
  containerRef.current.scrollHeight;
2096
2023
  }
2024
+ setTimeout(() => {
2025
+ isProgrammaticScrollRef.current = false;
2026
+ }, 100);
2097
2027
  },
2098
2028
  []
2099
2029
  );
2100
2030
 
2101
2031
  const scrollToIndex = useCallback(
2102
2032
  (index: number, behavior: ScrollBehavior = "smooth") => {
2103
- console.log("scrollToIndex called:", index);
2033
+ isProgrammaticScrollRef.current = true;
2104
2034
  if (containerRef.current && positions[index] !== undefined) {
2105
2035
  containerRef.current.scrollTo({
2106
2036
  top: positions[index],
2107
2037
  behavior,
2108
2038
  });
2109
2039
  }
2040
+ setTimeout(() => {
2041
+ isProgrammaticScrollRef.current = false;
2042
+ }, 100);
2110
2043
  },
2111
2044
  [positions]
2112
2045
  );
@@ -2129,12 +2062,6 @@ function createProxyHandler<T>(
2129
2062
  },
2130
2063
  };
2131
2064
 
2132
- console.log("Returning virtualizer with props:", {
2133
- range,
2134
- totalHeight,
2135
- startPosition: positions[range.startIndex] || 0,
2136
- });
2137
-
2138
2065
  return {
2139
2066
  virtualState,
2140
2067
  virtualizerProps,