cogsbox-state 0.5.377 → 0.5.379

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.377",
3
+ "version": "0.5.379",
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
@@ -1933,11 +1933,13 @@ function createProxyHandler<T>(
1933
1933
  console.log(
1934
1934
  "ACTION (GETTING_HEIGHTS): Setting range to end and starting loop."
1935
1935
  );
1936
+
1936
1937
  setRange({
1937
1938
  startIndex: Math.max(0, totalCount - 10 - overscan),
1938
1939
  endIndex: totalCount,
1939
1940
  });
1940
1941
 
1942
+ let intervalId: NodeJS.Timeout;
1941
1943
  intervalId = setInterval(() => {
1942
1944
  const lastItemIndex = totalCount - 1;
1943
1945
  const shadowArray =
@@ -1949,15 +1951,43 @@ function createProxyHandler<T>(
1949
1951
 
1950
1952
  if (lastItemHeight > 0) {
1951
1953
  clearInterval(intervalId);
1954
+
1952
1955
  if (!shouldNotScroll.current) {
1953
- console.log(
1954
- "ACTION (GETTING_HEIGHTS): Measurement success -> SCROLLING_TO_BOTTOM"
1955
- );
1956
+ const prevCount = prevTotalCountRef.current;
1957
+ const addedItems = totalCount - prevCount;
1958
+ const smallAddition = addedItems > 0 && addedItems <= 3;
1959
+
1960
+ if (smallAddition) {
1961
+ // Let DOM render before measuring + scrolling
1962
+ requestAnimationFrame(() => {
1963
+ const prevBottom =
1964
+ positions[prevCount] ?? container.scrollHeight;
1965
+ const newBottom = container.scrollHeight;
1966
+ const delta = newBottom - prevBottom;
1967
+
1968
+ if (delta > 0) {
1969
+ container.scrollBy({
1970
+ top: delta,
1971
+ behavior: "smooth",
1972
+ });
1973
+ }
1956
1974
 
1957
- setStatus("SCROLLING_TO_BOTTOM");
1975
+ console.log(
1976
+ "ACTION (GETTING_HEIGHTS): Small addition -> LOCKED_AT_BOTTOM"
1977
+ );
1978
+ setStatus("LOCKED_AT_BOTTOM");
1979
+ });
1980
+ } else {
1981
+ console.log(
1982
+ "ACTION (GETTING_HEIGHTS): Large change -> SCROLLING_TO_BOTTOM"
1983
+ );
1984
+ setStatus("SCROLLING_TO_BOTTOM");
1985
+ }
1958
1986
  }
1959
1987
  }
1960
- }, 100);
1988
+ }, 50);
1989
+
1990
+ return () => clearInterval(intervalId);
1961
1991
  } else if (status === "SCROLLING_TO_BOTTOM") {
1962
1992
  console.log(
1963
1993
  "ACTION (SCROLLING_TO_BOTTOM): Executing scroll."
@@ -1987,33 +2017,23 @@ function createProxyHandler<T>(
1987
2017
  return () => clearTimeout(timeoutId);
1988
2018
  }
1989
2019
 
1990
- // If status is IDLE_NOT_AT_BOTTOM or LOCKED_AT_BOTTOM, we do nothing here.
1991
- // The scroll has either finished or been disabled by the user.
1992
-
1993
2020
  return () => {
1994
2021
  if (intervalId) clearInterval(intervalId);
1995
2022
  };
1996
2023
  }, [status, totalCount, positions]);
1997
2024
 
1998
- // --- 3. USER INTERACTION & RANGE UPDATER ---
1999
2025
  useEffect(() => {
2000
2026
  const container = containerRef.current;
2001
2027
  if (!container) return;
2002
2028
 
2003
- // The scroll distance threshold. One item's height is a great default.
2004
2029
  const scrollThreshold = itemHeight;
2005
2030
 
2006
2031
  const handleUserScroll = () => {
2007
- // Essential guard for our own programmatic scrolls.
2008
2032
  if (isProgrammaticScroll.current) {
2009
2033
  return;
2010
2034
  }
2011
2035
 
2012
2036
  const scrollTop = container.scrollTop;
2013
-
2014
- // --- THE CORE LOGIC YOU REQUESTED ---
2015
- // Is the user just wiggling the scrollbar? If so, exit.
2016
- // This is a very cheap check that runs on every scroll event.
2017
2037
  if (
2018
2038
  Math.abs(scrollTop - lastUpdateAtScrollTop.current) <
2019
2039
  scrollThreshold
@@ -2021,8 +2041,6 @@ function createProxyHandler<T>(
2021
2041
  return;
2022
2042
  }
2023
2043
 
2024
- // --- IF WE ARE HERE, WE HAVE SCROLLED A "DECENT AMOUNT" ---
2025
-
2026
2044
  console.log(
2027
2045
  `Threshold passed at ${scrollTop}px. Recalculating range...`
2028
2046
  );