import {PlainScroll} from "../../../Scroll"; import VirtualTable from "../../../VirtualTable"; import {reactive, watch} from "@peryl/react-compose"; export function useTableScroll( { refs, }: { refs: { virtual: undefined | null | typeof VirtualTable.use.class } } ) { const state = reactive({ getScroll: () => null as null | undefined | PlainScroll }); const freezeState = { handlers: [] as ((scroll: PlainScroll | null | undefined) => void)[], }; const onTableScrollRefer = (handler: (scroll: PlainScroll | null | undefined) => void) => { freezeState.handlers.push(handler); const scroll = state.getScroll(); if (!!scroll) { handler(scroll); } return () => { const index = freezeState.handlers.indexOf(handler); if (index > -1) { freezeState.handlers.splice(index, 1); } }; }; const getScroll = () => state.getScroll()!; watch(() => refs.virtual, virtual => { const scroll = virtual?.refs.scroll; freezeState.handlers.forEach(i => i(scroll)); state.getScroll = () => scroll; }); return { onTableScrollRefer, getScroll }; } export type iTableScrollRefer = ReturnType