Skip to content

Commit c30fbfc

Browse files
authored
perf(gatsby-cli): dont retain logs in memory in non-ink loggers (#34045)
1 parent 3f65d17 commit c30fbfc

File tree

6 files changed

+34
-24
lines changed

6 files changed

+34
-24
lines changed

packages/gatsby-cli/src/reporter/loggers/ink/cli.tsx

+4-2
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,15 @@ import { Message, IMessageProps } from "./components/messages"
88
import { Error as ErrorComponent } from "./components/error"
99
import Develop from "./components/develop"
1010
import PageTree from "./components/pageTree"
11-
import { IGatsbyCLIState, IActivity } from "../../redux/types"
11+
import { IGatsbyCLIState, IActivity, ILog } from "../../redux/types"
1212
import { ActivityLogLevels } from "../../constants"
1313
import { IStructuredError } from "../../../structured-errors/types"
1414

1515
const showProgress = isTTY()
1616

1717
interface ICLIProps {
1818
logs: IGatsbyCLIState
19+
messages: Array<ILog>
1920
showStatusBar: boolean
2021
showPageTree: boolean
2122
}
@@ -48,7 +49,8 @@ class CLI extends React.Component<ICLIProps, ICLIState> {
4849

4950
render(): React.ReactElement {
5051
const {
51-
logs: { messages, activities },
52+
logs: { activities },
53+
messages,
5254
showStatusBar,
5355
showPageTree,
5456
} = this.props

packages/gatsby-cli/src/reporter/loggers/ink/context.tsx

+27-6
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,43 @@
11
import React, { useState, useLayoutEffect, createContext } from "react"
22
import { getStore, onLogAction } from "../../redux"
3-
import { IGatsbyCLIState } from "../../redux/types"
3+
import { IGatsbyCLIState, ActionsUnion, ILog } from "../../redux/types"
44
import { IRenderPageArgs } from "../../types"
5+
import { Actions } from "../../constants"
56

6-
const StoreStateContext = createContext<{
7+
interface IStoreStateContext {
78
logs: IGatsbyCLIState
9+
messages: Array<ILog>
810
pageTree: IRenderPageArgs | null
9-
}>(getStore().getState())
11+
}
12+
13+
const StoreStateContext = createContext<IStoreStateContext>({
14+
...getStore().getState(),
15+
messages: [],
16+
})
1017

1118
export const StoreStateProvider: React.FC = ({
1219
children,
1320
}): React.ReactElement => {
14-
const [state, setState] = useState(getStore().getState())
21+
const [state, setState] = useState<IStoreStateContext>({
22+
...getStore().getState(),
23+
messages: [],
24+
})
1525

1626
useLayoutEffect(
1727
() =>
18-
onLogAction(() => {
19-
setState(getStore().getState())
28+
onLogAction((action: ActionsUnion) => {
29+
if (action.type === Actions.Log) {
30+
setState(state => {
31+
return {
32+
...state,
33+
messages: [...state.messages, action.payload],
34+
}
35+
})
36+
} else {
37+
setState(state => {
38+
return { ...getStore().getState(), messages: state.messages }
39+
})
40+
}
2041
}),
2142
[]
2243
)

packages/gatsby-cli/src/reporter/loggers/ink/index.tsx

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const ConnectedCLI: React.FC = (): React.ReactElement => {
1717
showStatusBar={Boolean(showStatusBar)}
1818
showPageTree={Boolean(showPageTree)}
1919
logs={state.logs}
20+
messages={state.messages}
2021
/>
2122
)
2223
}

packages/gatsby-cli/src/reporter/redux/internal-actions.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ export const createLog = ({
128128
type: Actions.Log,
129129
payload: {
130130
level,
131-
text,
131+
text: !text ? `\u2800` : text,
132132
statusText,
133133
duration,
134134
group,

packages/gatsby-cli/src/reporter/redux/reducers/logs.ts

-13
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { Actions } from "../../constants"
33

44
export const reducer = (
55
state: IGatsbyCLIState = {
6-
messages: [],
76
activities: {},
87
status: ``,
98
},
@@ -17,18 +16,6 @@ export const reducer = (
1716
}
1817
}
1918

20-
case Actions.Log: {
21-
if (!action.payload.text) {
22-
// set empty character to fix ink
23-
action.payload.text = `\u2800`
24-
}
25-
26-
return {
27-
...state,
28-
messages: [...state.messages, action.payload],
29-
}
30-
}
31-
3219
case Actions.StartActivity: {
3320
const { id } = action.payload
3421
return {

packages/gatsby-cli/src/reporter/redux/types.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import { ErrorCategory } from "../../structured-errors/error-map"
44
import { IRenderPageArgs } from "../types"
55

66
export interface IGatsbyCLIState {
7-
messages: Array<ILog>
87
activities: {
98
[id: string]: IActivity
109
}
@@ -37,7 +36,7 @@ export interface IActivity {
3736
errored?: boolean
3837
}
3938

40-
interface ILog {
39+
export interface ILog {
4140
level: string
4241
text: string | undefined
4342
statusText: string | undefined

0 commit comments

Comments
 (0)