Files
modeling-app/src/hooks/useSetupEngineManager.ts

219 lines
7.0 KiB
TypeScript
Raw Normal View History

import { useLayoutEffect, useEffect, useRef } from 'react'
import { engineCommandManager, kclManager } from 'lib/singletons'
import { deferExecution } from 'lib/utils'
import { Themes } from 'lib/theme'
import { makeDefaultPlanes, modifyGrid } from 'lang/wasm'
import { useModelingContext } from './useModelingContext'
import { useNetworkContext } from 'hooks/useNetworkContext'
import { useAppState, useAppStream } from 'AppState'
import { SettingsViaQueryString } from 'lib/settings/settingsTypes'
import {
EngineConnectionStateType,
DisconnectingType,
} from 'lang/std/engineConnection'
export function useSetupEngineManager(
streamRef: React.RefObject<HTMLDivElement>,
modelingSend: ReturnType<typeof useModelingContext>['send'],
modelingContext: ReturnType<typeof useModelingContext>['context'],
settings = {
pool: null,
theme: Themes.System,
highlightEdges: true,
enableSSAO: true,
showScaleGrid: false,
} as SettingsViaQueryString,
token?: string
) {
const networkContext = useNetworkContext()
const { pingPongHealth, immediateState } = networkContext
const { setAppState } = useAppState()
const { setMediaStream } = useAppStream()
const hasSetNonZeroDimensions = useRef<boolean>(false)
if (settings.pool) {
// override the pool param (?pool=) to request a specific engine instance
// from a particular pool.
engineCommandManager.settings.pool = settings.pool
}
const startEngineInstance = () => {
// Load the engine command manager once with the initial width and height,
// then we do not want to reload it.
const { width: quadWidth, height: quadHeight } = getDimensions(
streamRef?.current?.offsetWidth ?? 0,
streamRef?.current?.offsetHeight ?? 0
)
engineCommandManager.start({
setMediaStream: (mediaStream) => setMediaStream(mediaStream),
setIsStreamReady: (isStreamReady) => setAppState({ isStreamReady }),
width: quadWidth,
height: quadHeight,
token,
settings,
makeDefaultPlanes: () => {
return makeDefaultPlanes(kclManager.engineCommandManager)
},
modifyGrid: (hidden: boolean) => {
return modifyGrid(kclManager.engineCommandManager, hidden)
},
})
modelingSend({
type: 'Set context',
data: {
streamDimensions: {
streamWidth: quadWidth,
streamHeight: quadHeight,
},
},
})
hasSetNonZeroDimensions.current = true
Show when user can't connect because of a bad token (#2105) * Reapply "Add ping pong health, remove a timeout interval, fix up netwo… (#1771) This reverts commit 1913519f686d778413d4fead2e7d194d29c83fe9. * Fix build errors * Add new error states to network status notification * Remove unused variable * Refactor to use Context API for network status * Don't do any stream events if network is not ok * Catch LSP errors on bad auth * Show when authentication is bad (cookie header only) * Fix formatting * Fix up types * Revert awaiting on lsp failure * Fix tsc * wip * wip * fmt * Fix typing * Incorporate ping health; yarn make:dev; faster video stream loss notice * A snapshot a day keeps the bugs away! 📷🐛 (OS: ubuntu) * run ci pls * run ci pls * A snapshot a day keeps the bugs away! 📷🐛 (OS: ubuntu) * run ci pls again * Remove unused variables * Add new instructions on running Playwright anywhere * Please the Playwright. Praise the Playwright. * Correct a vitest * A snapshot a day keeps the bugs away! 📷🐛 (OS: ubuntu) * ci again * Fix tests unrelated to this PR * Fix flakiness in for segments tests * Bump to 2 workers * fmt * A snapshot a day keeps the bugs away! 📷🐛 (OS: ubuntu) * fmt * fmt * Fixups * A snapshot a day keeps the bugs away! 📷🐛 (OS: ubuntu) * ci * Set workers to 1 * Wait for network status listeners before connecting * Fix initial connection requirements and trying 2 workers again --------- Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
2024-06-04 08:32:24 -04:00
}
useLayoutEffect(() => {
const { width: quadWidth, height: quadHeight } = getDimensions(
streamRef?.current?.offsetWidth ?? 0,
streamRef?.current?.offsetHeight ?? 0
)
if (!hasSetNonZeroDimensions.current && quadHeight && quadWidth) {
startEngineInstance()
}
}, [
Show when user can't connect because of a bad token (#2105) * Reapply "Add ping pong health, remove a timeout interval, fix up netwo… (#1771) This reverts commit 1913519f686d778413d4fead2e7d194d29c83fe9. * Fix build errors * Add new error states to network status notification * Remove unused variable * Refactor to use Context API for network status * Don't do any stream events if network is not ok * Catch LSP errors on bad auth * Show when authentication is bad (cookie header only) * Fix formatting * Fix up types * Revert awaiting on lsp failure * Fix tsc * wip * wip * fmt * Fix typing * Incorporate ping health; yarn make:dev; faster video stream loss notice * A snapshot a day keeps the bugs away! 📷🐛 (OS: ubuntu) * run ci pls * run ci pls * A snapshot a day keeps the bugs away! 📷🐛 (OS: ubuntu) * run ci pls again * Remove unused variables * Add new instructions on running Playwright anywhere * Please the Playwright. Praise the Playwright. * Correct a vitest * A snapshot a day keeps the bugs away! 📷🐛 (OS: ubuntu) * ci again * Fix tests unrelated to this PR * Fix flakiness in for segments tests * Bump to 2 workers * fmt * A snapshot a day keeps the bugs away! 📷🐛 (OS: ubuntu) * fmt * fmt * Fixups * A snapshot a day keeps the bugs away! 📷🐛 (OS: ubuntu) * ci * Set workers to 1 * Wait for network status listeners before connecting * Fix initial connection requirements and trying 2 workers again --------- Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
2024-06-04 08:32:24 -04:00
streamRef?.current?.offsetWidth,
streamRef?.current?.offsetHeight,
modelingSend,
Show when user can't connect because of a bad token (#2105) * Reapply "Add ping pong health, remove a timeout interval, fix up netwo… (#1771) This reverts commit 1913519f686d778413d4fead2e7d194d29c83fe9. * Fix build errors * Add new error states to network status notification * Remove unused variable * Refactor to use Context API for network status * Don't do any stream events if network is not ok * Catch LSP errors on bad auth * Show when authentication is bad (cookie header only) * Fix formatting * Fix up types * Revert awaiting on lsp failure * Fix tsc * wip * wip * fmt * Fix typing * Incorporate ping health; yarn make:dev; faster video stream loss notice * A snapshot a day keeps the bugs away! 📷🐛 (OS: ubuntu) * run ci pls * run ci pls * A snapshot a day keeps the bugs away! 📷🐛 (OS: ubuntu) * run ci pls again * Remove unused variables * Add new instructions on running Playwright anywhere * Please the Playwright. Praise the Playwright. * Correct a vitest * A snapshot a day keeps the bugs away! 📷🐛 (OS: ubuntu) * ci again * Fix tests unrelated to this PR * Fix flakiness in for segments tests * Bump to 2 workers * fmt * A snapshot a day keeps the bugs away! 📷🐛 (OS: ubuntu) * fmt * fmt * Fixups * A snapshot a day keeps the bugs away! 📷🐛 (OS: ubuntu) * ci * Set workers to 1 * Wait for network status listeners before connecting * Fix initial connection requirements and trying 2 workers again --------- Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
2024-06-04 08:32:24 -04:00
])
useEffect(() => {
if (pingPongHealth === 'TIMEOUT') {
engineCommandManager.tearDown()
}
}, [pingPongHealth])
useEffect(() => {
const intervalId = setInterval(() => {
if (immediateState.type === EngineConnectionStateType.Disconnected) {
engineCommandManager.engineConnection = undefined
startEngineInstance()
}
}, 3000)
return () => {
clearInterval(intervalId)
}
}, [immediateState])
useEffect(() => {
engineCommandManager.settings.theme = settings.theme
const handleResize = deferExecution(() => {
const { width, height } = getDimensions(
streamRef?.current?.offsetWidth ?? 0,
streamRef?.current?.offsetHeight ?? 0
)
if (
modelingContext.store.streamDimensions.streamWidth !== width ||
modelingContext.store.streamDimensions.streamHeight !== height
) {
engineCommandManager.handleResize({
streamWidth: width,
streamHeight: height,
})
modelingSend({
type: 'Set context',
data: {
streamDimensions: {
streamWidth: width,
streamHeight: height,
},
},
})
}
}, 500)
Show when user can't connect because of a bad token (#2105) * Reapply "Add ping pong health, remove a timeout interval, fix up netwo… (#1771) This reverts commit 1913519f686d778413d4fead2e7d194d29c83fe9. * Fix build errors * Add new error states to network status notification * Remove unused variable * Refactor to use Context API for network status * Don't do any stream events if network is not ok * Catch LSP errors on bad auth * Show when authentication is bad (cookie header only) * Fix formatting * Fix up types * Revert awaiting on lsp failure * Fix tsc * wip * wip * fmt * Fix typing * Incorporate ping health; yarn make:dev; faster video stream loss notice * A snapshot a day keeps the bugs away! 📷🐛 (OS: ubuntu) * run ci pls * run ci pls * A snapshot a day keeps the bugs away! 📷🐛 (OS: ubuntu) * run ci pls again * Remove unused variables * Add new instructions on running Playwright anywhere * Please the Playwright. Praise the Playwright. * Correct a vitest * A snapshot a day keeps the bugs away! 📷🐛 (OS: ubuntu) * ci again * Fix tests unrelated to this PR * Fix flakiness in for segments tests * Bump to 2 workers * fmt * A snapshot a day keeps the bugs away! 📷🐛 (OS: ubuntu) * fmt * fmt * Fixups * A snapshot a day keeps the bugs away! 📷🐛 (OS: ubuntu) * ci * Set workers to 1 * Wait for network status listeners before connecting * Fix initial connection requirements and trying 2 workers again --------- Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
2024-06-04 08:32:24 -04:00
const onOnline = () => {
startEngineInstance()
}
const onVisibilityChange = () => {
if (window.document.visibilityState === 'visible') {
if (
!engineCommandManager.engineConnection?.isReady() &&
!engineCommandManager.engineConnection?.isConnecting()
) {
startEngineInstance()
}
}
}
window.document.addEventListener('visibilitychange', onVisibilityChange)
const onAnyInput = () => {
const isEngineNotReadyOrConnecting =
!engineCommandManager.engineConnection?.isReady() &&
!engineCommandManager.engineConnection?.isConnecting()
const conn = engineCommandManager.engineConnection
const isStreamPaused =
conn?.state.type === EngineConnectionStateType.Disconnecting &&
conn?.state.value.type === DisconnectingType.Pause
if (isEngineNotReadyOrConnecting || isStreamPaused) {
engineCommandManager.engineConnection = undefined
startEngineInstance()
}
Show when user can't connect because of a bad token (#2105) * Reapply "Add ping pong health, remove a timeout interval, fix up netwo… (#1771) This reverts commit 1913519f686d778413d4fead2e7d194d29c83fe9. * Fix build errors * Add new error states to network status notification * Remove unused variable * Refactor to use Context API for network status * Don't do any stream events if network is not ok * Catch LSP errors on bad auth * Show when authentication is bad (cookie header only) * Fix formatting * Fix up types * Revert awaiting on lsp failure * Fix tsc * wip * wip * fmt * Fix typing * Incorporate ping health; yarn make:dev; faster video stream loss notice * A snapshot a day keeps the bugs away! 📷🐛 (OS: ubuntu) * run ci pls * run ci pls * A snapshot a day keeps the bugs away! 📷🐛 (OS: ubuntu) * run ci pls again * Remove unused variables * Add new instructions on running Playwright anywhere * Please the Playwright. Praise the Playwright. * Correct a vitest * A snapshot a day keeps the bugs away! 📷🐛 (OS: ubuntu) * ci again * Fix tests unrelated to this PR * Fix flakiness in for segments tests * Bump to 2 workers * fmt * A snapshot a day keeps the bugs away! 📷🐛 (OS: ubuntu) * fmt * fmt * Fixups * A snapshot a day keeps the bugs away! 📷🐛 (OS: ubuntu) * ci * Set workers to 1 * Wait for network status listeners before connecting * Fix initial connection requirements and trying 2 workers again --------- Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
2024-06-04 08:32:24 -04:00
}
window.document.addEventListener('keydown', onAnyInput)
window.document.addEventListener('mousemove', onAnyInput)
window.document.addEventListener('mousedown', onAnyInput)
window.document.addEventListener('scroll', onAnyInput)
window.document.addEventListener('touchstart', onAnyInput)
Show when user can't connect because of a bad token (#2105) * Reapply "Add ping pong health, remove a timeout interval, fix up netwo… (#1771) This reverts commit 1913519f686d778413d4fead2e7d194d29c83fe9. * Fix build errors * Add new error states to network status notification * Remove unused variable * Refactor to use Context API for network status * Don't do any stream events if network is not ok * Catch LSP errors on bad auth * Show when authentication is bad (cookie header only) * Fix formatting * Fix up types * Revert awaiting on lsp failure * Fix tsc * wip * wip * fmt * Fix typing * Incorporate ping health; yarn make:dev; faster video stream loss notice * A snapshot a day keeps the bugs away! 📷🐛 (OS: ubuntu) * run ci pls * run ci pls * A snapshot a day keeps the bugs away! 📷🐛 (OS: ubuntu) * run ci pls again * Remove unused variables * Add new instructions on running Playwright anywhere * Please the Playwright. Praise the Playwright. * Correct a vitest * A snapshot a day keeps the bugs away! 📷🐛 (OS: ubuntu) * ci again * Fix tests unrelated to this PR * Fix flakiness in for segments tests * Bump to 2 workers * fmt * A snapshot a day keeps the bugs away! 📷🐛 (OS: ubuntu) * fmt * fmt * Fixups * A snapshot a day keeps the bugs away! 📷🐛 (OS: ubuntu) * ci * Set workers to 1 * Wait for network status listeners before connecting * Fix initial connection requirements and trying 2 workers again --------- Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
2024-06-04 08:32:24 -04:00
const onOffline = () => {
engineCommandManager.tearDown()
}
window.addEventListener('online', onOnline)
window.addEventListener('offline', onOffline)
window.addEventListener('resize', handleResize)
return () => {
window.document.removeEventListener(
'visibilitychange',
onVisibilityChange
)
window.document.removeEventListener('keydown', onAnyInput)
window.document.removeEventListener('mousemove', onAnyInput)
window.document.removeEventListener('mousedown', onAnyInput)
window.document.removeEventListener('scroll', onAnyInput)
window.document.removeEventListener('touchstart', onAnyInput)
Show when user can't connect because of a bad token (#2105) * Reapply "Add ping pong health, remove a timeout interval, fix up netwo… (#1771) This reverts commit 1913519f686d778413d4fead2e7d194d29c83fe9. * Fix build errors * Add new error states to network status notification * Remove unused variable * Refactor to use Context API for network status * Don't do any stream events if network is not ok * Catch LSP errors on bad auth * Show when authentication is bad (cookie header only) * Fix formatting * Fix up types * Revert awaiting on lsp failure * Fix tsc * wip * wip * fmt * Fix typing * Incorporate ping health; yarn make:dev; faster video stream loss notice * A snapshot a day keeps the bugs away! 📷🐛 (OS: ubuntu) * run ci pls * run ci pls * A snapshot a day keeps the bugs away! 📷🐛 (OS: ubuntu) * run ci pls again * Remove unused variables * Add new instructions on running Playwright anywhere * Please the Playwright. Praise the Playwright. * Correct a vitest * A snapshot a day keeps the bugs away! 📷🐛 (OS: ubuntu) * ci again * Fix tests unrelated to this PR * Fix flakiness in for segments tests * Bump to 2 workers * fmt * A snapshot a day keeps the bugs away! 📷🐛 (OS: ubuntu) * fmt * fmt * Fixups * A snapshot a day keeps the bugs away! 📷🐛 (OS: ubuntu) * ci * Set workers to 1 * Wait for network status listeners before connecting * Fix initial connection requirements and trying 2 workers again --------- Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
2024-06-04 08:32:24 -04:00
window.removeEventListener('online', onOnline)
window.removeEventListener('offline', onOffline)
window.removeEventListener('resize', handleResize)
}
// Engine relies on many settings so we should rebind events when it changes
// We have to list out the ones we care about because the settings object holds
// non-settings too...
}, [
settings.enableSSAO,
settings.highlightEdges,
settings.showScaleGrid,
settings.theme,
settings.pool,
])
}
function getDimensions(streamWidth?: number, streamHeight?: number) {
2024-08-01 22:30:08 +10:00
const factorOf = 4
const maxResolution = 2000
const width = streamWidth ? streamWidth : 0
const height = streamHeight ? streamHeight : 0
const ratio = Math.min(
Math.min(maxResolution / width, maxResolution / height),
1.0
)
2024-08-01 22:30:08 +10:00
const quadWidth = Math.round((width * ratio) / factorOf) * factorOf
const quadHeight = Math.round((height * ratio) / factorOf) * factorOf
return { width: quadWidth, height: quadHeight }
}