2021-01-20 18:01:00 +01:00
|
|
|
import { h } from 'preact';
|
2021-01-25 18:49:00 +01:00
|
|
|
import CameraImage from './CameraImage';
|
2021-01-27 01:18:45 +01:00
|
|
|
import { useCallback, useState } from 'preact/hooks';
|
2021-01-20 18:01:00 +01:00
|
|
|
|
2021-01-27 01:18:45 +01:00
|
|
|
const MIN_LOAD_TIMEOUT_MS = 200;
|
2021-01-20 18:01:00 +01:00
|
|
|
|
2022-02-27 15:04:12 +01:00
|
|
|
export default function AutoUpdatingCameraImage({ camera, searchParams = '', showFps = true, className }) {
|
2021-01-20 18:01:00 +01:00
|
|
|
const [key, setKey] = useState(Date.now());
|
2021-01-27 01:18:45 +01:00
|
|
|
const [fps, setFps] = useState(0);
|
2021-01-20 18:01:00 +01:00
|
|
|
|
2021-01-27 01:18:45 +01:00
|
|
|
const handleLoad = useCallback(() => {
|
|
|
|
const loadTime = Date.now() - key;
|
|
|
|
setFps((1000 / Math.max(loadTime, MIN_LOAD_TIMEOUT_MS)).toFixed(1));
|
|
|
|
setTimeout(
|
|
|
|
() => {
|
|
|
|
setKey(Date.now());
|
|
|
|
},
|
|
|
|
loadTime > MIN_LOAD_TIMEOUT_MS ? 1 : MIN_LOAD_TIMEOUT_MS
|
|
|
|
);
|
2021-02-09 20:35:33 +01:00
|
|
|
}, [key, setFps]);
|
2021-01-27 01:18:45 +01:00
|
|
|
|
|
|
|
return (
|
2022-02-27 15:04:12 +01:00
|
|
|
<div className={className}>
|
2021-01-27 01:18:45 +01:00
|
|
|
<CameraImage camera={camera} onload={handleLoad} searchParams={`cache=${key}&${searchParams}`} />
|
|
|
|
{showFps ? <span className="text-xs">Displaying at {fps}fps</span> : null}
|
|
|
|
</div>
|
|
|
|
);
|
2021-01-20 18:01:00 +01:00
|
|
|
}
|