mirror of
https://github.com/shuaiplus/nodewarden.git
synced 2026-08-05 06:50:10 +00:00
perf: throttle jsQR camera fallback to a few decodes per second
This commit is contained in:
@@ -229,6 +229,7 @@ export default function VaultEditor(props: VaultEditorProps) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
let stopped = false;
|
let stopped = false;
|
||||||
|
let lastCanvasScan = 0;
|
||||||
const detector = createTotpQrDetector();
|
const detector = createTotpQrDetector();
|
||||||
if (!navigator.mediaDevices?.getUserMedia) {
|
if (!navigator.mediaDevices?.getUserMedia) {
|
||||||
setTotpQrStatus(t('txt_totp_qr_camera_unavailable'));
|
setTotpQrStatus(t('txt_totp_qr_camera_unavailable'));
|
||||||
@@ -255,7 +256,16 @@ export default function VaultEditor(props: VaultEditorProps) {
|
|||||||
// Fall back to jsQR when the native detector is present but not usable.
|
// Fall back to jsQR when the native detector is present but not usable.
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!value) value = decodeTotpQrCanvas(video);
|
// The jsQR fallback runs a synchronous full-frame decode, so throttle
|
||||||
|
// it to a few times per second instead of every animation frame to
|
||||||
|
// avoid pegging the CPU while a code is being aligned.
|
||||||
|
if (!value) {
|
||||||
|
const now = performance.now();
|
||||||
|
if (now - lastCanvasScan >= 250) {
|
||||||
|
lastCanvasScan = now;
|
||||||
|
value = decodeTotpQrCanvas(video);
|
||||||
|
}
|
||||||
|
}
|
||||||
if (value && applyTotpQrValue(value)) return;
|
if (value && applyTotpQrValue(value)) return;
|
||||||
} catch {
|
} catch {
|
||||||
// Keep the camera active; transient frame decode failures are common.
|
// Keep the camera active; transient frame decode failures are common.
|
||||||
|
|||||||
Reference in New Issue
Block a user