Skip to content

Commit

Permalink
Kicking a problem a little further down the line (#610)
Browse files Browse the repository at this point in the history
* fix(editor) Extract built-in modules to a separate file for better resolving

* fix(editor) Tie built in versions to those in our package.json

* fix(editor) Moved error and console log tracking into OpenFileEditor

* chore(tests) Isolating the failing to test

* chore(tests) Now run both performance tests

* wtf

* fix(Editor) Remove setTimeout around console logging

* chore(Editor) Pulled out 2 hooks from OpenFileEditor
  • Loading branch information
Rheeseyb authored Oct 13, 2020
1 parent 3ea8d5a commit 87a16a4
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 76 deletions.
154 changes: 80 additions & 74 deletions editor/src/components/editor/editor-component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,6 @@ const EmptyArray: Array<RuntimeErrorInfo> = []

const ConsoleLogSizeLimit = 100
const EmptyConsoleLogs: Array<ConsoleLog> = []
let consoleLogTimeoutID: number | null = null
let canvasConsoleLogsVariable: Array<ConsoleLog> = EmptyConsoleLogs

export const EditorComponentInner = betterReactMemo(
'EditorComponentInner',
Expand Down Expand Up @@ -194,63 +192,6 @@ export const EditorComponentInner = betterReactMemo(
[updateDeltaWidth],
)

const [runtimeErrors, setRuntimeErrors] = React.useState<Array<RuntimeErrorInfo>>(EmptyArray)

const onRuntimeError = React.useCallback(
(editedFile: string, error: FancyError, errorInfo?: React.ErrorInfo) => {
setRuntimeErrors([
{
editedFile: editedFile,
error: error,
errorInfo: Utils.defaultIfNull(null, errorInfo),
},
])
},
[],
)

const clearRuntimeErrors = React.useCallback(() => {
setRuntimeErrors(EmptyArray)
}, [])

const [canvasConsoleLogsState, setCanvasConsoleLogsState] = React.useState<Array<ConsoleLog>>(
[],
)

const modifyLogs = React.useCallback(
(updateLogs: (logs: Array<ConsoleLog>) => Array<ConsoleLog>) => {
const updatedLogs = updateLogs(canvasConsoleLogsVariable)
if (updatedLogs !== canvasConsoleLogsVariable) {
canvasConsoleLogsVariable = updateLogs(canvasConsoleLogsVariable)
if (consoleLogTimeoutID != null) {
clearTimeout(consoleLogTimeoutID)
}
consoleLogTimeoutID = setTimeout(() => {
setCanvasConsoleLogsState(canvasConsoleLogsVariable)
consoleLogTimeoutID = null
})
}
},
[setCanvasConsoleLogsState],
)

const clearConsoleLogs = React.useCallback(() => {
modifyLogs((_) => EmptyConsoleLogs)
}, [modifyLogs])

const addToConsoleLogs = React.useCallback(
(log: ConsoleLog) => {
modifyLogs((logs) => {
let result = [...logs, log]
while (result.length > ConsoleLogSizeLimit) {
result.shift()
}
return result
})
},
[modifyLogs],
)

const canvasIsLive = useEditorState((store) => isLiveMode(store.editor.mode))

const toggleLiveCanvas = React.useCallback(
Expand Down Expand Up @@ -392,14 +333,7 @@ export const EditorComponentInner = betterReactMemo(
overflowX: 'hidden',
}}
>
<OpenFileEditor
runtimeErrors={runtimeErrors}
onRuntimeError={onRuntimeError}
clearRuntimeErrors={clearRuntimeErrors}
canvasConsoleLogs={canvasConsoleLogsState}
clearConsoleLogs={clearConsoleLogs}
addToConsoleLogs={addToConsoleLogs}
/>
<OpenFileEditor />
</SimpleFlexRow>
</SimpleFlexColumn>
{/* insert more columns here */}
Expand Down Expand Up @@ -491,16 +425,76 @@ const HelpTriangle = () => (
</div>
)

interface OpenFileEditorProps {
function useRuntimeErrors(): {
runtimeErrors: Array<RuntimeErrorInfo>
onRuntimeError: (editedFile: string, error: FancyError, errorInfo?: React.ErrorInfo) => void
clearRuntimeErrors: () => void
canvasConsoleLogs: Array<ConsoleLog>
clearConsoleLogs: () => void
} {
const [runtimeErrors, setRuntimeErrors] = React.useState<Array<RuntimeErrorInfo>>(EmptyArray)

const onRuntimeError = React.useCallback(
(editedFile: string, error: FancyError, errorInfo?: React.ErrorInfo) => {
setRuntimeErrors([
{
editedFile: editedFile,
error: error,
errorInfo: Utils.defaultIfNull(null, errorInfo),
},
])
},
[],
)

const clearRuntimeErrors = React.useCallback(() => {
setRuntimeErrors(EmptyArray)
}, [])

return {
runtimeErrors: runtimeErrors,
onRuntimeError: onRuntimeError,
clearRuntimeErrors: clearRuntimeErrors,
}
}

function useConsoleLogs(): {
consoleLogs: Array<ConsoleLog>
addToConsoleLogs: (log: ConsoleLog) => void
clearConsoleLogs: () => void
} {
const [consoleLogs, setConsoleLogs] = React.useState<Array<ConsoleLog>>(EmptyConsoleLogs)

const modifyLogs = React.useCallback(
(updateLogs: (logs: Array<ConsoleLog>) => Array<ConsoleLog>) => {
setConsoleLogs(updateLogs)
},
[setConsoleLogs],
)

const clearConsoleLogs = React.useCallback(() => {
modifyLogs((_) => EmptyConsoleLogs)
}, [modifyLogs])

const addToConsoleLogs = React.useCallback(
(log: ConsoleLog) => {
modifyLogs((logs) => {
let result = [...logs, log]
while (result.length > ConsoleLogSizeLimit) {
result.shift()
}
return result
})
},
[modifyLogs],
)

return {
consoleLogs: consoleLogs,
addToConsoleLogs: addToConsoleLogs,
clearConsoleLogs: clearConsoleLogs,
}
}

const OpenFileEditor = betterReactMemo('OpenFileEditor', (props: OpenFileEditorProps) => {
const OpenFileEditor = betterReactMemo('OpenFileEditor', () => {
const {
noFileOpen,
isUiJsFileOpen,
Expand All @@ -517,20 +511,32 @@ const OpenFileEditor = betterReactMemo('OpenFileEditor', (props: OpenFileEditorP
}
})

const { runtimeErrors, onRuntimeError, clearRuntimeErrors } = useRuntimeErrors()
const { consoleLogs, addToConsoleLogs, clearConsoleLogs } = useConsoleLogs()

if (noFileOpen) {
return <Subdued>No file open</Subdued>
} else if (areReleaseNotesOpen) {
return <ReleaseNotesContent />
} else if (isUiJsFileOpen) {
return <SplitViewCanvasRoot {...props} />
return (
<SplitViewCanvasRoot
runtimeErrors={runtimeErrors}
onRuntimeError={onRuntimeError}
clearRuntimeErrors={clearRuntimeErrors}
canvasConsoleLogs={consoleLogs}
clearConsoleLogs={clearConsoleLogs}
addToConsoleLogs={addToConsoleLogs}
/>
)
} else if (isUserConfigurationOpen) {
return <UserConfiguration />
} else {
return (
<ScriptEditor
relevantPanel={'misccodeeditor'}
runtimeErrors={props.runtimeErrors}
canvasConsoleLogs={props.canvasConsoleLogs}
runtimeErrors={runtimeErrors}
canvasConsoleLogs={consoleLogs}
/>
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ describe('React Render Count Tests - ', () => {
)

const renderCountAfter = renderResult.getNumberOfRenders()
expect(renderCountAfter - renderCountBefore).toBeGreaterThan(750) // if this breaks, GREAT NEWS but update the test please :)
expect(renderCountAfter - renderCountBefore).toBeLessThan(760)
expect(renderCountAfter - renderCountBefore).toBeGreaterThan(695) // if this breaks, GREAT NEWS but update the test please :)
expect(renderCountAfter - renderCountBefore).toBeLessThan(705)
})

it('Changing the selected view', async () => {
Expand Down

0 comments on commit 87a16a4

Please sign in to comment.