Skip to content

new-log-viewer: Avoid redundant loadPage after loadFile when loading a URL with a logEventNum specified.#55

Merged
junhaoliao merged 4 commits intoy-scope:mainfrom
junhaoliao:redundant-loadPage
Aug 19, 2024
Merged

new-log-viewer: Avoid redundant loadPage after loadFile when loading a URL with a logEventNum specified.#55
junhaoliao merged 4 commits intoy-scope:mainfrom
junhaoliao:redundant-loadPage

Conversation

@junhaoliao
Copy link
Copy Markdown
Member

@junhaoliao junhaoliao commented Aug 19, 2024

References

new-log-viewer series: #45 #46 #48 #51 #52 #53

#48 new-log-viewer: Add UrlContextProvider to provide URL parameters and use them in the StateContextProvider.

It was found that when logEventNum is specified in the web application URL, a redundant loadPage request is sent to the main service worker after an initial loadFile request.

image
Debug prints which show a redundant loadPage request is sent after loadFile

Description

  1. new-log-viewer: Avoid redundant loadPage after loadFile when logEventNum is specified in the URL.

Validation performed

  1. Referred to the validation steps in Add support for loading files, decoding them as JSONL, and formatting them using a Logback-like format string. #46 to start the debug server.
  2. Loaded http://localhost:3010/?filePath=http://localhost:3010/test/example.jsonl#logEventNum=10 and observed no loadPage request is sent after loadFile.
    image
    Screenshot showing no loadPage request is sent after loadFile

@junhaoliao junhaoliao marked this pull request as ready for review August 19, 2024 01:43
@junhaoliao
Copy link
Copy Markdown
Member Author

@Henry8192 kindly help review this

@junhaoliao junhaoliao changed the title new-log-viewer: avoid redundant loadPage after loadFile when logEventNum is specified in the URL. new-log-viewer: Avoid redundant loadPage after loadFile when logEventNum is specified in the URL. Aug 19, 2024
Comment on lines +196 to +200
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);
} else {
Copy link
Copy Markdown
Contributor

@Henry8192 Henry8192 Aug 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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);
} else {
if (STATE_DEFAULT.pageNum === pageNumRef.current) {
return;
} else if (newPageNum === pageNumRef.current) {
// Don't need to switch pages so just update `logEventNum` in the URL.
updateLogEventNumInUrl(numEvents, logEventNumRef.current);
} else {

How about making this an early return? We can avoid if inside ifs

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm... How about

Suggested change
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);
} else {
if (STATE_DEFAULT.pageNum !== pageNumRef.current) {
pageNumRef.current = newPageNum;
return;
} else if (newPageNum === pageNumRef.current) {
// Don't need to switch pages so just update `logEventNum` in the URL.
updateLogEventNumInUrl(numEvents, logEventNumRef.current);
} else {

? i.e. update pageNumRef.current before returning.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking about avoiding duplicated code when I attempted to add the early return. If we agree this style is better, I'm fine duplicating the line to update pageNumRef.current.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed the !== to ===, will that save pageNumRef.current = newPageNum;?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense. There's no update pageNumRef.current = newPageNum if they're the same.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for misreading the code. I think we still need the pageNumRef.current = newPageNum update before returning, right? Say what if newPageNum !== STATE_DEFAULT.pageNum?

@junhaoliao junhaoliao requested a review from Henry8192 August 19, 2024 16:36
@kirkrodrigues
Copy link
Copy Markdown
Member

For the PR title, how about:

new-log-viewer: Avoid redundant loadPage after loadFile when loading a URL with a logEventNum specified.

@junhaoliao junhaoliao changed the title new-log-viewer: Avoid redundant loadPage after loadFile when logEventNum is specified in the URL. new-log-viewer: Avoid redundant loadPage after loadFile when loading a URL with a logEventNum specified. Aug 19, 2024
@junhaoliao junhaoliao merged commit 5c0960c into y-scope:main Aug 19, 2024
@junhaoliao junhaoliao deleted the redundant-loadPage branch August 19, 2024 20:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants