From 4f436ff6f4e8e0c49f440d0ef5a5a6d65a0cb711 Mon Sep 17 00:00:00 2001 From: inokawa <48897392+inokawa@users.noreply.github.com> Date: Sun, 23 Jul 2023 14:00:09 +0900 Subject: [PATCH] Remove onScrollOffsetChange argument from store --- src/core/store.ts | 18 ++++++++---------- src/react/VList.tsx | 10 +++++----- 2 files changed, 13 insertions(+), 15 deletions(-) diff --git a/src/core/store.ts b/src/core/store.ts index e02ec5713..5c8172cb9 100644 --- a/src/core/store.ts +++ b/src/core/store.ts @@ -56,9 +56,10 @@ type Actions = type Subscriber = (sync?: boolean) => void; -export const UPDATE_SCROLL = 0b001; -export const UPDATE_SIZE = 0b010; -export const UPDATE_JUMP = 0b100; +export const UPDATE_SCROLL = 0b0001; +export const UPDATE_SIZE = 0b0010; +export const UPDATE_JUMP = 0b0100; +export const UPDATE_SCROLL_WITH_EVENT = 0b1000; export type VirtualStore = { _getCache(): CacheSnapshot; @@ -88,8 +89,7 @@ export const createVirtualStore = ( initialItemCount: number = 0, isReverse: boolean, onScrollStateChange: (scrolling: boolean) => void, - cacheSnapshot?: CacheSnapshot, - onScrollOffsetChange?: (offset: number) => void + cacheSnapshot?: CacheSnapshot ): VirtualStore => { const shouldAutoEstimateItemSize = !itemSize; const initialItemSize = itemSize || 40; @@ -301,11 +301,13 @@ export const createVirtualStore = ( // // Update synchronously if scrolled a lot shouldSync = abs(scrollOffset - payload) > viewportSize; + + mutated += UPDATE_SCROLL_WITH_EVENT; } // Scroll offset may exceed min or max especially in Safari's elastic scrolling. scrollOffset = max(0, min(getScrollOffsetMax(), payload)); - mutated = UPDATE_SCROLL; + mutated += UPDATE_SCROLL; break; } case ACTION_SCROLL_END: { @@ -327,10 +329,6 @@ export const createVirtualStore = ( } cb(shouldSync); }); - - if (type === ACTION_SCROLL) { - onScrollOffsetChange && onScrollOffsetChange(scrollOffset); - } } if (exists(updatedScrollState)) { onScrollStateChange(updatedScrollState); diff --git a/src/react/VList.tsx b/src/react/VList.tsx index c8fc48a43..40b5cd4b4 100644 --- a/src/react/VList.tsx +++ b/src/react/VList.tsx @@ -8,7 +8,7 @@ import { useEffect, useState, } from "react"; -import { createVirtualStore } from "../core/store"; +import { UPDATE_SCROLL_WITH_EVENT, createVirtualStore } from "../core/store"; import { useIsomorphicLayoutEffect } from "./useIsomorphicLayoutEffect"; import { useSelector } from "./useSelector"; import { exists, max, min, values } from "../core/utils"; @@ -190,11 +190,11 @@ export const VList = forwardRef( onScrollStop[refKey] && onScrollStop[refKey](); } }, - cache, - (offset) => { - onScroll[refKey] && onScroll[refKey](offset); - } + cache ); + _store._subscribe(UPDATE_SCROLL_WITH_EVENT, () => { + onScroll[refKey] && onScroll[refKey](store._getScrollOffset()); + }); return [ _store,