* Reapply "Add ping pong health, remove a timeout interval, fix up netwo… (#1771)
This reverts commit 1913519f68
.
* 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>
135 lines
3.8 KiB
TypeScript
135 lines
3.8 KiB
TypeScript
import { useLayoutEffect, useEffect, useRef } from 'react'
|
|
import { useStore } from '../useStore'
|
|
import { engineCommandManager, kclManager } from 'lib/singletons'
|
|
import { deferExecution } from 'lib/utils'
|
|
import { Themes } from 'lib/theme'
|
|
import { makeDefaultPlanes } from 'lang/wasm'
|
|
|
|
export function useSetupEngineManager(
|
|
streamRef: React.RefObject<HTMLDivElement>,
|
|
token?: string,
|
|
settings = {
|
|
pool: null,
|
|
theme: Themes.System,
|
|
highlightEdges: true,
|
|
enableSSAO: true,
|
|
} as {
|
|
pool: string | null
|
|
theme: Themes
|
|
highlightEdges: boolean
|
|
enableSSAO: boolean
|
|
}
|
|
) {
|
|
const {
|
|
setMediaStream,
|
|
setIsStreamReady,
|
|
setStreamDimensions,
|
|
streamDimensions,
|
|
} = useStore((s) => ({
|
|
setMediaStream: s.setMediaStream,
|
|
setIsStreamReady: s.setIsStreamReady,
|
|
setStreamDimensions: s.setStreamDimensions,
|
|
streamDimensions: s.streamDimensions,
|
|
}))
|
|
|
|
const streamWidth = streamRef?.current?.offsetWidth
|
|
const streamHeight = streamRef?.current?.offsetHeight
|
|
|
|
const hasSetNonZeroDimensions = useRef<boolean>(false)
|
|
|
|
if (settings.pool) {
|
|
// override the pool param (?pool=) to request a specific engine instance
|
|
// from a particular pool.
|
|
engineCommandManager.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(
|
|
streamWidth,
|
|
streamHeight
|
|
)
|
|
if (!hasSetNonZeroDimensions.current && quadHeight && quadWidth) {
|
|
engineCommandManager.start({
|
|
setMediaStream,
|
|
setIsStreamReady,
|
|
width: quadWidth,
|
|
height: quadHeight,
|
|
executeCode: () => {
|
|
// We only want to execute the code here that we already have set.
|
|
// Nothing else.
|
|
return kclManager.executeCode(true, true)
|
|
},
|
|
token,
|
|
settings,
|
|
makeDefaultPlanes: () => {
|
|
return makeDefaultPlanes(kclManager.engineCommandManager)
|
|
},
|
|
})
|
|
setStreamDimensions({
|
|
streamWidth: quadWidth,
|
|
streamHeight: quadHeight,
|
|
})
|
|
hasSetNonZeroDimensions.current = true
|
|
}
|
|
}
|
|
|
|
useLayoutEffect(startEngineInstance, [
|
|
streamRef?.current?.offsetWidth,
|
|
streamRef?.current?.offsetHeight,
|
|
])
|
|
|
|
useEffect(() => {
|
|
const handleResize = deferExecution(() => {
|
|
const { width, height } = getDimensions(
|
|
streamRef?.current?.offsetWidth,
|
|
streamRef?.current?.offsetHeight
|
|
)
|
|
if (
|
|
streamDimensions.streamWidth !== width ||
|
|
streamDimensions.streamHeight !== height
|
|
) {
|
|
engineCommandManager.handleResize({
|
|
streamWidth: width,
|
|
streamHeight: height,
|
|
})
|
|
setStreamDimensions({
|
|
streamWidth: width,
|
|
streamHeight: height,
|
|
})
|
|
}
|
|
}, 500)
|
|
|
|
const onOnline = () => {
|
|
startEngineInstance()
|
|
}
|
|
|
|
const onOffline = () => {
|
|
engineCommandManager.tearDown()
|
|
}
|
|
|
|
window.addEventListener('online', onOnline)
|
|
window.addEventListener('offline', onOffline)
|
|
window.addEventListener('resize', handleResize)
|
|
return () => {
|
|
window.removeEventListener('online', onOnline)
|
|
window.removeEventListener('offline', onOffline)
|
|
window.removeEventListener('resize', handleResize)
|
|
}
|
|
}, [])
|
|
}
|
|
|
|
function getDimensions(streamWidth?: number, streamHeight?: number) {
|
|
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
|
|
)
|
|
const quadWidth = Math.round((width * ratio) / 4) * 4
|
|
const quadHeight = Math.round((height * ratio) / 4) * 4
|
|
return { width: quadWidth, height: quadHeight }
|
|
}
|