-
Notifications
You must be signed in to change notification settings - Fork 16
refactor: Reorganize Zustand state retrievals to prevent potential circular dependencies (resolves #264); Add error logging before postPopUp()
.
#265
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
ddb5889
53149f7
2ba737b
0cef615
b419218
56f48a6
22e3732
6d2d5d9
3468220
10697e9
b07cf05
43ebb53
6695ad0
d4732b4
84643e4
b29cd52
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -109,43 +109,48 @@ const getPageNumCursor = ( | |||||||
const useViewStore = create<ViewState>((set, get) => ({ | ||||||||
...VIEW_STORE_DEFAULT, | ||||||||
filterLogs: (filter: LogLevelFilter) => { | ||||||||
const {updatePageData} = get(); | ||||||||
const {logEventNum, postPopUp} = useContextStore.getState(); | ||||||||
const {logFileManagerProxy} = useLogFileManagerStore.getState(); | ||||||||
const {setUiState} = useUiStore.getState(); | ||||||||
setUiState(UI_STATE.FAST_LOADING); | ||||||||
|
||||||||
const {clearQuery} = useQueryStore.getState(); | ||||||||
clearQuery(); | ||||||||
Comment on lines
+115
to
+116
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. see the last comment |
||||||||
|
||||||||
(async () => { | ||||||||
const {logFileManagerProxy} = useLogFileManagerStore.getState(); | ||||||||
const {logEventNum} = useContextStore.getState(); | ||||||||
const {isPrettified} = get(); | ||||||||
const pageData = await logFileManagerProxy.setFilter( | ||||||||
{ | ||||||||
code: CURSOR_CODE.EVENT_NUM, | ||||||||
args: { | ||||||||
eventNum: logEventNum, | ||||||||
}, | ||||||||
}, | ||||||||
get().isPrettified, | ||||||||
isPrettified, | ||||||||
filter | ||||||||
); | ||||||||
|
||||||||
const {updatePageData} = get(); | ||||||||
updatePageData(pageData); | ||||||||
|
||||||||
const {startQuery} = useQueryStore.getState(); | ||||||||
startQuery(); | ||||||||
})().catch((e: unknown) => { | ||||||||
console.error(e); | ||||||||
|
||||||||
const {postPopUp} = useContextStore.getState(); | ||||||||
postPopUp({ | ||||||||
Comment on lines
+141
to
142
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See the last comment |
||||||||
level: LOG_LEVEL.ERROR, | ||||||||
message: String(e), | ||||||||
timeoutMillis: DO_NOT_TIMEOUT_VALUE, | ||||||||
title: "Action failed", | ||||||||
}); | ||||||||
}); | ||||||||
const {startQuery} = useQueryStore.getState(); | ||||||||
startQuery(); | ||||||||
}, | ||||||||
loadPageByAction: (navAction: NavigationAction) => { | ||||||||
const {isPrettified, numPages, pageNum, updatePageData} = get(); | ||||||||
const {logEventNum, postPopUp} = useContextStore.getState(); | ||||||||
const {logFileManagerProxy} = useLogFileManagerStore.getState(); | ||||||||
const {fileSrc, loadFile} = useLogFileStore.getState(); | ||||||||
const {uiState, setUiState} = useUiStore.getState(); | ||||||||
if (navAction.code === ACTION_NAME.RELOAD) { | ||||||||
const {fileSrc, loadFile} = useLogFileStore.getState(); | ||||||||
const {logEventNum} = useContextStore.getState(); | ||||||||
if (null === fileSrc || CONTEXT_STORE_DEFAULT.logEventNum === logEventNum) { | ||||||||
throw new Error( | ||||||||
`Unexpected fileSrc=${JSON.stringify( | ||||||||
|
@@ -161,25 +166,33 @@ const useViewStore = create<ViewState>((set, get) => ({ | |||||||
return; | ||||||||
} | ||||||||
|
||||||||
const {uiState, setUiState} = useUiStore.getState(); | ||||||||
if (UI_STATE.READY !== uiState) { | ||||||||
console.warn("Skipping navigation: page load in progress."); | ||||||||
|
||||||||
return; | ||||||||
} | ||||||||
setUiState(UI_STATE.FAST_LOADING); | ||||||||
|
||||||||
const {numPages, pageNum} = get(); | ||||||||
const cursor = getPageNumCursor(navAction, pageNum, numPages); | ||||||||
if (null === cursor) { | ||||||||
console.error(`Error with nav action ${navAction.code}.`); | ||||||||
|
||||||||
return; | ||||||||
} | ||||||||
|
||||||||
setUiState(UI_STATE.FAST_LOADING); | ||||||||
|
||||||||
(async () => { | ||||||||
const {logFileManagerProxy} = useLogFileManagerStore.getState(); | ||||||||
const {isPrettified} = get(); | ||||||||
const pageData = await logFileManagerProxy.loadPage(cursor, isPrettified); | ||||||||
|
||||||||
const {updatePageData} = get(); | ||||||||
updatePageData(pageData); | ||||||||
})().catch((e: unknown) => { | ||||||||
console.error(e); | ||||||||
|
||||||||
const {postPopUp} = useContextStore.getState(); | ||||||||
postPopUp({ | ||||||||
level: LOG_LEVEL.ERROR, | ||||||||
message: String(e), | ||||||||
|
@@ -201,16 +214,17 @@ const useViewStore = create<ViewState>((set, get) => ({ | |||||||
set({pageNum: newPageNum}); | ||||||||
}, | ||||||||
updateIsPrettified: (newIsPrettified: boolean) => { | ||||||||
const {updatePageData} = get(); | ||||||||
const {logEventNum, postPopUp} = useContextStore.getState(); | ||||||||
const {logFileManagerProxy} = useLogFileManagerStore.getState(); | ||||||||
const {setUiState} = useUiStore.getState(); | ||||||||
if (newIsPrettified === get().isPrettified) { | ||||||||
const {isPrettified} = get(); | ||||||||
if (newIsPrettified === isPrettified) { | ||||||||
return; | ||||||||
} | ||||||||
set({isPrettified: newIsPrettified}); | ||||||||
|
||||||||
const {setUiState} = useUiStore.getState(); | ||||||||
setUiState(UI_STATE.FAST_LOADING); | ||||||||
Comment on lines
+222
to
223
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See the last comment |
||||||||
|
||||||||
set({isPrettified: newIsPrettified}); | ||||||||
|
||||||||
const {logEventNum} = useContextStore.getState(); | ||||||||
let cursor: CursorType = {code: CURSOR_CODE.LAST_EVENT, args: null}; | ||||||||
if (CONTEXT_STORE_DEFAULT.logEventNum !== logEventNum) { | ||||||||
cursor = { | ||||||||
|
@@ -220,9 +234,15 @@ const useViewStore = create<ViewState>((set, get) => ({ | |||||||
} | ||||||||
|
||||||||
(async () => { | ||||||||
const {logFileManagerProxy} = useLogFileManagerStore.getState(); | ||||||||
const pageData = await logFileManagerProxy.loadPage(cursor, newIsPrettified); | ||||||||
|
||||||||
const {updatePageData} = get(); | ||||||||
updatePageData(pageData); | ||||||||
})().catch((e: unknown) => { | ||||||||
console.error(e); | ||||||||
|
||||||||
const {postPopUp} = useContextStore.getState(); | ||||||||
postPopUp({ | ||||||||
level: LOG_LEVEL.ERROR, | ||||||||
message: String(e), | ||||||||
|
@@ -232,7 +252,6 @@ const useViewStore = create<ViewState>((set, get) => ({ | |||||||
}); | ||||||||
}, | ||||||||
updatePageData: (pageData: PageData) => { | ||||||||
const {setUiState} = useUiStore.getState(); | ||||||||
set({ | ||||||||
logData: pageData.logs, | ||||||||
numPages: pageData.numPages, | ||||||||
|
@@ -242,6 +261,7 @@ const useViewStore = create<ViewState>((set, get) => ({ | |||||||
updateWindowUrlHashParams({ | ||||||||
logEventNum: pageData.logEventNum, | ||||||||
}); | ||||||||
const {setUiState} = useUiStore.getState(); | ||||||||
setUiState(UI_STATE.READY); | ||||||||
Comment on lines
+264
to
265
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
I personally prefer calling state functions this way, but feel free to use whatever style looks best for you. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. good point raised. we previously discussed both approaches and we ended up preferring the destructuring syntax, to avoid chaining the calls |
||||||||
}, | ||||||||
})); | ||||||||
|
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧹 Nitpick (assertive)
Consider moving postPopUp inside the async function.
For consistency with the pattern of other state retrievals in this PR, consider moving this line inside the async function, closer to where it's used in the catch block at line 153.
- const {postPopUp} = useContextStore.getState(); (async () => { const {logFileManagerProxy} = useLogFileManagerProxyStore.getState(); const decoderOptions = getConfig(CONFIG_KEY.DECODER_OPTIONS);
🤖 Prompt for AI Agents
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if i move it into the async function, don't i need to retrieve
postPopUp
again in the catch handler?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@junhaoliao You're absolutely right! If you move
postPopUp
into the main async function body, you would need to retrieve it again in the catch handler since the scope would be different.Looking at other changes in this PR, I see a pattern where state retrievals are moved closer to their actual usage points. For
postPopUp
specifically, a consistent approach would be to move it directly inside the catch handler where it's needed:This approach keeps the state retrieval closest to where it's used, which seems to be the pattern in this refactoring.
✏️ Learnings added