Skip to content

Commit b424a86

Browse files
authored
refactor: Extract isPrettified from loadPage's parameters into a variable in LogFileManager (fixes #354). (#362)
1 parent 621638d commit b424a86

File tree

6 files changed

+27
-20
lines changed

6 files changed

+27
-20
lines changed

src/services/LogFileManager/index.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ class LogFileManager {
6262

6363
#queryCount: number = 0;
6464

65+
#isPrettified: boolean = false;
66+
6567
/**
6668
* Private constructor for LogFileManager. This is not intended to be invoked publicly.
6769
* Instead, use LogFileManager.create() to create a new instance of the class.
@@ -194,6 +196,10 @@ class LogFileManager {
194196
}
195197
}
196198

199+
setIsPrettified (isPrettified: boolean) {
200+
this.#isPrettified = isPrettified;
201+
}
202+
197203
/**
198204
* Exports a chunk of log events, sends the results to the renderer, and schedules the next
199205
* chunk if more log events remain.
@@ -229,12 +235,11 @@ class LogFileManager {
229235
* Loads a page of log events based on the provided cursor.
230236
*
231237
* @param cursor The cursor indicating the page to load. See {@link CursorType}.
232-
* @param isPrettified Are the log messages pretty printed.
233238
* @return An object containing the logs as a string, a map of line numbers to log event
234239
* numbers, and the line number of the first line in the cursor identified event.
235240
* @throws {Error} if any error occurs during decode.
236241
*/
237-
loadPage (cursor: CursorType, isPrettified: boolean): PageData {
242+
loadPage (cursor: CursorType): PageData {
238243
console.debug(`loadPage: cursor=${JSON.stringify(cursor)}`);
239244
const filteredLogEventMap = this.#decoder.getFilteredLogEventMap();
240245
const numActiveEvents: number = filteredLogEventMap ?
@@ -264,7 +269,7 @@ class LogFileManager {
264269
const beginLineNumToLogEventNum: BeginLineNumToLogEventNumMap = new Map();
265270
let currentLine = 1;
266271
results.forEach(({message, logEventNum}) => {
267-
const printedMsg = (isPrettified) ?
272+
const printedMsg = (this.#isPrettified) ?
268273
`${jsBeautify(message)}\n` :
269274
message;
270275

src/services/LogFileManagerProxy.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,20 +42,24 @@ class LogFileManagerProxy {
4242
};
4343
}
4444

45-
loadPage (cursor: CursorType, isPrettified: boolean): PageData {
45+
loadPage (cursor: CursorType): PageData {
4646
const logFileManager = this.#getLogFileManager();
47-
return logFileManager.loadPage(cursor, isPrettified);
47+
return logFileManager.loadPage(cursor);
4848
}
4949

5050
setFilter (
5151
cursor: CursorType,
52-
isPrettified: boolean,
5352
logLevelFilter: LogLevelFilter
5453
): PageData {
5554
const logFileManager = this.#getLogFileManager();
5655
logFileManager.setLogLevelFilter(logLevelFilter);
5756

58-
return this.loadPage(cursor, isPrettified);
57+
return this.loadPage(cursor);
58+
}
59+
60+
setIsPrettified (isPrettified: boolean): void {
61+
const logFileManager = this.#getLogFileManager();
62+
logFileManager.setIsPrettified(isPrettified);
5963
}
6064

6165
exportLogs (): void {

src/stores/logFileStore.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,7 @@ const useLogFileStore = create<LogFileState>((set) => ({
145145

146146
set(fileInfo);
147147

148-
const {isPrettified} = useViewStore.getState();
149-
const pageData = await logFileManagerProxy.loadPage(cursor, isPrettified);
148+
const pageData = await logFileManagerProxy.loadPage(cursor);
150149
updatePageData(pageData);
151150
setUiState(UI_STATE.READY);
152151

src/stores/viewStore/createViewFilterSlice.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,14 @@ const createViewFilterSlice: StateCreator<
2828
setUiState(UI_STATE.FAST_LOADING);
2929
(async () => {
3030
const {logFileManagerProxy} = useLogFileManagerStore.getState();
31-
const {isPrettified, logEventNum} = get();
31+
const {logEventNum} = get();
3232
const pageData = await logFileManagerProxy.setFilter(
3333
{
3434
code: CURSOR_CODE.EVENT_NUM,
3535
args: {
3636
eventNum: logEventNum,
3737
},
3838
},
39-
isPrettified,
4039
filter
4140
);
4241

src/stores/viewStore/createViewPageSlice.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,7 @@ const createViewPageSlice: StateCreator<
139139

140140
(async () => {
141141
const {logFileManagerProxy} = useLogFileManagerStore.getState();
142-
const {isPrettified} = get();
143-
const pageData = await logFileManagerProxy.loadPage(cursor, isPrettified);
142+
const pageData = await logFileManagerProxy.loadPage(cursor);
144143
const {updatePageData} = get();
145144
updatePageData(pageData);
146145
setUiState(UI_STATE.READY);

src/utils/url/urlHash.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,10 @@ const getCursorFromHashParams = ({isPrettified, logEventNum, timestamp}: {
3838
return null;
3939
}
4040

41-
const {
42-
isPrettified: prevIsPrettified, setIsPrettified, setLogEventNum,
43-
} = useViewStore.getState();
41+
const {isPrettified: prevIsPrettified, setLogEventNum} = useViewStore.getState();
4442
const clampedLogEventNum = clamp(logEventNum, 1, numEvents);
4543

4644
if (isPrettified !== prevIsPrettified) {
47-
setIsPrettified(isPrettified);
48-
4945
return {
5046
code: CURSOR_CODE.EVENT_NUM,
5147
args: {eventNum: clampedLogEventNum},
@@ -84,7 +80,7 @@ const getCursorFromHashParams = ({isPrettified, logEventNum, timestamp}: {
8480
const updateViewHashParams = () => {
8581
const {isPrettified, logEventNum, timestamp} = getWindowUrlHashParams();
8682
updateWindowUrlHashParams({
87-
isPrettified: URL_HASH_PARAMS_DEFAULT.isPrettified,
83+
isPrettified: isPrettified,
8884
timestamp: URL_HASH_PARAMS_DEFAULT.timestamp,
8985
});
9086

@@ -97,7 +93,12 @@ const updateViewHashParams = () => {
9793
(async () => {
9894
const {logFileManagerProxy} = useLogFileManagerProxyStore.getState();
9995
const {updatePageData} = useViewStore.getState();
100-
const pageData = await logFileManagerProxy.loadPage(cursor, isPrettified);
96+
const {isPrettified: prevIsPrettified, setIsPrettified} = useViewStore.getState();
97+
if (isPrettified !== prevIsPrettified) {
98+
setIsPrettified(isPrettified);
99+
await logFileManagerProxy.setIsPrettified(isPrettified);
100+
}
101+
const pageData = await logFileManagerProxy.loadPage(cursor);
101102
updatePageData(pageData);
102103
const {setUiState} = useUiStore.getState();
103104
setUiState(UI_STATE.READY);

0 commit comments

Comments
 (0)