Skip to content

Commit

Permalink
Separate the concern of numEvents & numFilteredEvents to correct page…
Browse files Browse the repository at this point in the history
… switching in the renderer.
  • Loading branch information
junhaoliao committed Sep 2, 2024
1 parent 61578a6 commit 1abdeb8
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 24 deletions.
16 changes: 9 additions & 7 deletions new-log-viewer/src/components/Layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ const Layout = () => {
const {
fileName,
numEvents,
numFilteredEvents,
pageNum,

changeLogLevelFilter,
Expand All @@ -191,7 +192,7 @@ const Layout = () => {
const [selectedLogLevels, setSelectedLogLevels] =
useState<number[]>(LOG_LEVEL_NAMES_LIST as number[]);
const logEventNumRef = useRef<Nullable<number>>(logEventNum);
const numEventsRef = useRef<Nullable<number>>(numEvents);
const numFilteredEventsRef = useRef<Nullable<number>>(numFilteredEvents);

const handleCopyLinkButtonClick = () => {
copyPermalinkToClipboard({}, {logEventNum: numEvents});
Expand Down Expand Up @@ -243,7 +244,7 @@ const Layout = () => {
}
break;
case ACTION_NAME.LAST_PAGE:
updateWindowUrlHashParams({logEventNum: numEventsRef.current});
updateWindowUrlHashParams({logEventNum: numFilteredEventsRef.current});
break;
case ACTION_NAME.PAGE_TOP:
goToPositionAndCenter(editor, {lineNumber: 1, column: 1});
Expand All @@ -265,10 +266,10 @@ const Layout = () => {
logEventNumRef.current = logEventNum;
}, [logEventNum]);

// Synchronize `numEventsRef` with `numEvents`.
// Synchronize `numFilteredEventsRef` with `numFilteredEvents`.
useEffect(() => {
numEventsRef.current = numEvents;
}, [numEvents]);
numFilteredEventsRef.current = numFilteredEvents;
}, [numFilteredEvents]);

return (
<>
Expand All @@ -282,8 +283,9 @@ const Layout = () => {
1 :
logEventNum}
onChange={handleLogEventNumInputChange}/>
{" "}
|
{" / "}
{numFilteredEvents}
{" | "}
PageNum -
{" "}
{pageNum}
Expand Down
27 changes: 17 additions & 10 deletions new-log-viewer/src/contexts/StateContextProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ interface StateContextType {
fileName: string,
logData: string,
logLevelFilter: LogLevelFilter,
numFilteredEvents: number,
numEvents: number,
numPages: number,
pageNum: Nullable<number>,
Expand All @@ -57,6 +58,7 @@ const STATE_DEFAULT: Readonly<StateContextType> = Object.freeze({
fileName: "",
logData: "Loading...",
logLevelFilter: null,
numFilteredEvents: 0,
numEvents: 0,

Check warning on line 62 in new-log-viewer/src/contexts/StateContextProvider.tsx

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

new-log-viewer/src/contexts/StateContextProvider.tsx#L62

[sort-keys] Expected object keys to be in natural ascending order. 'numEvents' should be before 'numFilteredEvents'.
numPages: 0,
pageNum: 0,
Expand Down Expand Up @@ -137,6 +139,7 @@ const StateContextProvider = ({children}: StateContextProviderProps) => {
const [fileName, setFileName] = useState<string>(STATE_DEFAULT.fileName);
const [logData, setLogData] = useState<string>(STATE_DEFAULT.logData);
const [numEvents, setNumEvents] = useState<number>(STATE_DEFAULT.numEvents);
const [numFilteredEvents, setNumFilteredEvents] = useState<number>(STATE_DEFAULT.numFilteredEvents);

Check warning on line 142 in new-log-viewer/src/contexts/StateContextProvider.tsx

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

new-log-viewer/src/contexts/StateContextProvider.tsx#L142

[@stylistic/js/max-len] This line has a length of 104. Maximum allowed is 100.
const beginLineNumToLogEventNumRef =
useRef<BeginLineNumToLogEventNumMap>(STATE_DEFAULT.beginLineNumToLogEventNum);
const logEventNumRef = useRef(logEventNum);
Expand All @@ -154,18 +157,21 @@ const StateContextProvider = ({children}: StateContextProviderProps) => {
setFileName(args.fileName);
setNumEvents(args.numEvents);
break;
case WORKER_RESP_CODE.NOTIFICATION:
// eslint-disable-next-line no-warning-comments
// TODO: notifications should be shown in the UI when the NotificationProvider
// is added
console.error(args.logLevel, args.message);
break;
case WORKER_RESP_CODE.PAGE_DATA: {
setLogData(args.logs);
beginLineNumToLogEventNumRef.current = args.beginLineNumToLogEventNum;
const lastLogEventNum = getLastLogEventNum(args.beginLineNumToLogEventNum);
updateLogEventNumInUrl(lastLogEventNum, logEventNumRef.current);
break;
}
case WORKER_RESP_CODE.NOTIFICATION:
// eslint-disable-next-line no-warning-comments
// TODO: notifications should be shown in the UI when the NotificationProvider
// is added
console.error(args.logLevel, args.message);
case WORKER_RESP_CODE.VIEW_INFO:
setNumFilteredEvents(args.numFilteredEvents);
break;
default:
console.error(`Unexpected ev.data: ${JSON.stringify(ev.data)}`);
Expand Down Expand Up @@ -215,12 +221,12 @@ const StateContextProvider = ({children}: StateContextProviderProps) => {

// On `numEvents` update, recalculate `numPagesRef`.
useEffect(() => {
if (STATE_DEFAULT.numEvents === numEvents) {
if (STATE_DEFAULT.numFilteredEvents === numFilteredEvents) {
return;
}

numPagesRef.current = getChunkNum(numEvents, getConfig(CONFIG_KEY.PAGE_SIZE));
}, [numEvents]);
numPagesRef.current = getChunkNum(numFilteredEvents, getConfig(CONFIG_KEY.PAGE_SIZE));
}, [numFilteredEvents]);

// On `logEventNum` update, clamp it then switch page if necessary or simply update the URL.
useEffect(() => {
Expand All @@ -238,7 +244,7 @@ const StateContextProvider = ({children}: StateContextProviderProps) => {
if (STATE_DEFAULT.pageNum !== pageNumRef.current) {
if (newPageNum === pageNumRef.current) {
// Don't need to switch pages so just update `logEventNum` in the URL.
updateLogEventNumInUrl(numEvents, logEventNumRef.current);
updateLogEventNumInUrl(numFilteredEvents, logEventNumRef.current);
} else {
// NOTE: We don't need to call `updateLogEventNumInUrl()` since it's called when
// handling the `WORKER_RESP_CODE.PAGE_DATA` response (the response to
Expand All @@ -255,7 +261,7 @@ const StateContextProvider = ({children}: StateContextProviderProps) => {

pageNumRef.current = newPageNum;
}, [
numEvents,
numFilteredEvents,
logEventNum,
]);

Expand Down Expand Up @@ -290,6 +296,7 @@ const StateContextProvider = ({children}: StateContextProviderProps) => {
fileName: fileName,
logData: logData,
logLevelFilter: logLevelFilterRef.current,
numFilteredEvents: numFilteredEvents,
numEvents: numEvents,

Check warning on line 300 in new-log-viewer/src/contexts/StateContextProvider.tsx

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

new-log-viewer/src/contexts/StateContextProvider.tsx#L300

[sort-keys] Expected object keys to be in natural ascending order. 'numEvents' should be before 'numFilteredEvents'.
numPages: numPagesRef.current,
pageNum: pageNumRef.current,
Expand Down
4 changes: 4 additions & 0 deletions new-log-viewer/src/services/LogFileManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ class LogFileManager {
return this.#numEvents;
}

get numFilteredEvents () {
return this.#numFilteredEvents;
}

/**
* Creates a new LogFileManager.
*
Expand Down
6 changes: 6 additions & 0 deletions new-log-viewer/src/services/MainWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ onmessage = async (ev: MessageEvent<MainWorkerReqMessage>) => {
fileName: LOG_FILE_MANAGER.fileName,
numEvents: LOG_FILE_MANAGER.numEvents,
});
postResp(WORKER_RESP_CODE.VIEW_INFO, {
numFilteredEvents: LOG_FILE_MANAGER.numFilteredEvents,
});
postResp(
WORKER_RESP_CODE.PAGE_DATA,
LOG_FILE_MANAGER.loadPage(args.cursor)
Expand All @@ -64,6 +67,9 @@ onmessage = async (ev: MessageEvent<MainWorkerReqMessage>) => {
}
if ("undefined" !== typeof args.decoderOptions) {
LOG_FILE_MANAGER.setDecoderOptions(args.decoderOptions);
postResp(WORKER_RESP_CODE.VIEW_INFO, {
numFilteredEvents: LOG_FILE_MANAGER.numFilteredEvents,
});
}
postResp(
WORKER_RESP_CODE.PAGE_DATA,
Expand Down
18 changes: 11 additions & 7 deletions new-log-viewer/src/typings/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,10 @@ enum WORKER_REQ_CODE {
}

enum WORKER_RESP_CODE {
LOG_FILE_INFO = "fileInfo",
PAGE_DATA = "pageData",
LOG_FILE_INFO = "logFileInfo",
NOTIFICATION = "notification",
PAGE_DATA = "pageData",
VIEW_INFO = "viewInfo",
}

type WorkerReqMap = {
Expand All @@ -66,15 +67,18 @@ type WorkerRespMap = {
fileName: string,
numEvents: number,
},
[WORKER_RESP_CODE.NOTIFICATION]: {
logLevel: LOG_LEVEL,
message: string,
},
[WORKER_RESP_CODE.PAGE_DATA]: {
logs: string,
beginLineNumToLogEventNum: BeginLineNumToLogEventNumMap,
cursorLineNum: number
},
[WORKER_RESP_CODE.NOTIFICATION]: {
logLevel: LOG_LEVEL,
message: string
cursorLineNum: number,
},
[WORKER_RESP_CODE.VIEW_INFO]: {
numFilteredEvents: number,
}
};

type WorkerReq<T extends WORKER_REQ_CODE> = T extends keyof WorkerReqMap ?
Expand Down

0 comments on commit 1abdeb8

Please sign in to comment.