From 2c4d735b41e7025a5d3691f1afc9c98372a847d7 Mon Sep 17 00:00:00 2001 From: Dave Marco Date: Sat, 7 Sep 2024 00:11:42 +0000 Subject: [PATCH] second fix for bouncing --- .../src/contexts/StateContextProvider.tsx | 38 +++++++++++-------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/new-log-viewer/src/contexts/StateContextProvider.tsx b/new-log-viewer/src/contexts/StateContextProvider.tsx index ca09d86f..2a98e8eb 100644 --- a/new-log-viewer/src/contexts/StateContextProvider.tsx +++ b/new-log-viewer/src/contexts/StateContextProvider.tsx @@ -144,14 +144,10 @@ const StateContextProvider = ({children}: StateContextProviderProps) => { const {filePath, logEventNum} = useContext(UrlContext); const [fileName, setFileName] = useState(STATE_DEFAULT.fileName); - const [firstLogEventNumPerPage, setFirstLogEventNumPerPage] = - useState(STATE_DEFAULT.firstLogEventNumPerPage); - const [lastLogEventNumPerPage, setLastLogEventNumPerPage] = - useState(STATE_DEFAULT.lastLogEventNumPerPage); + const [logData, setLogData] = useState(STATE_DEFAULT.logData); const [numEvents, setNumEvents] = useState(STATE_DEFAULT.numEvents); - const [numFilteredEvents, setNumFilteredEvents] = - useState(STATE_DEFAULT.numFilteredEvents); + const beginLineNumToLogEventNumRef = useRef(STATE_DEFAULT.beginLineNumToLogEventNum); const logEventNumRef = useRef(logEventNum); @@ -159,6 +155,15 @@ const StateContextProvider = ({children}: StateContextProviderProps) => { const numPagesRef = useRef(STATE_DEFAULT.numPages); const pageNumRef = useRef>(STATE_DEFAULT.pageNum); + const firstLogEventNumPerPage = + useRef(STATE_DEFAULT.firstLogEventNumPerPage); + const lastLogEventNumPerPage = + useRef(STATE_DEFAULT.lastLogEventNumPerPage); + const numFilteredEvents = + useRef(STATE_DEFAULT.numFilteredEvents); + + + const mainWorkerRef = useRef(null); const handleMainWorkerResp = useCallback((ev: MessageEvent) => { @@ -179,6 +184,7 @@ const StateContextProvider = ({children}: StateContextProviderProps) => { setLogData(args.logs); beginLineNumToLogEventNumRef.current = args.beginLineNumToLogEventNum; const lastLogEventNum = getLastLogEventNum(args.beginLineNumToLogEventNum); + updateLogEventNumInUrl( lastLogEventNum, Array.from(args.beginLineNumToLogEventNum.values()) @@ -189,9 +195,9 @@ const StateContextProvider = ({children}: StateContextProviderProps) => { break; } case WORKER_RESP_CODE.VIEW_INFO: - setNumFilteredEvents(args.numFilteredEvents); - setFirstLogEventNumPerPage(args.firstLogEventNumPerPage); - setLastLogEventNumPerPage(args.lastLogEventNumPerPage); + numFilteredEvents.current = args.numFilteredEvents + firstLogEventNumPerPage.current = args.firstLogEventNumPerPage + lastLogEventNumPerPage.current = args.lastLogEventNumPerPage break; default: console.error(`Unexpected ev.data: ${JSON.stringify(ev.data)}`); @@ -238,22 +244,22 @@ const StateContextProvider = ({children}: StateContextProviderProps) => { // On `numEvents` update, recalculate `numPagesRef`. useEffect(() => { - if (STATE_DEFAULT.numFilteredEvents === numFilteredEvents) { + if (STATE_DEFAULT.numFilteredEvents === numFilteredEvents.current) { return; } - numPagesRef.current = getChunkNum(numFilteredEvents, getConfig(CONFIG_KEY.PAGE_SIZE)); + numPagesRef.current = getChunkNum(numFilteredEvents.current, getConfig(CONFIG_KEY.PAGE_SIZE)); }, [numFilteredEvents]); // On `logEventNum` update, clamp it then switch page if necessary or simply update the URL. useEffect(() => { if (null === mainWorkerRef.current || URL_HASH_PARAMS_DEFAULT.logEventNum === logEventNum || - 0 === firstLogEventNumPerPage.length) { + 0 === firstLogEventNumPerPage.current.length) { return; } const newPageNum = 1 + - firstLogEventNumPerPage.findLastIndex((value: number) => value <= logEventNum); + firstLogEventNumPerPage.current.findLastIndex((value: number) => value <= logEventNum); if (0 === newPageNum) { return; @@ -312,12 +318,12 @@ const StateContextProvider = ({children}: StateContextProviderProps) => { value={{ beginLineNumToLogEventNum: beginLineNumToLogEventNumRef.current, fileName: fileName, - firstLogEventNumPerPage: firstLogEventNumPerPage, - lastLogEventNumPerPage: lastLogEventNumPerPage, + firstLogEventNumPerPage: firstLogEventNumPerPage.current, + lastLogEventNumPerPage: lastLogEventNumPerPage.current, logData: logData, logLevelFilter: logLevelFilterRef.current, numEvents: numEvents, - numFilteredEvents: numFilteredEvents, + numFilteredEvents: numFilteredEvents.current, numPages: numPagesRef.current, pageNum: pageNumRef.current,