mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-20 18:22:25 +00:00
perf(cctv): fetch the active camera's pictures only while they can be seen
This commit is contained in:
1 parent
6e0baacd57
commit
3bda877a8a
5 files changed
+282
-4
No files matched your search
@@ -388,6 +388,8 @@ test('the frame preview renders each step of a camera frame request', async (t)
|
||||
globalThis.Image = prior;
|
||||
});
|
||||
const { controls, byId } = panel(t);
|
||||
// The preview loads while the panel is open; the panel starts collapsed.
|
||||
byId('cctv-panel').classList.remove('collapsed');
|
||||
const frame = byId('cctv-frame');
|
||||
const wrap = byId('cctv-frame-wrap');
|
||||
const badge = byId('cctv-source-badge');
|
||||
@@ -606,3 +608,58 @@ test('the meta line of a disabled layer does not describe its last camera', () =
|
||||
);
|
||||
assert.match(presentCctvPanel(activeState, true).meta, /^Austin · HDG /);
|
||||
});
|
||||
|
||||
test('the still preview is fetched only while the tab and the panel are shown', async (t) => {
|
||||
const prior = globalThis.Image;
|
||||
const requests = [];
|
||||
globalThis.Image = class {
|
||||
constructor() {
|
||||
requests.push(this);
|
||||
}
|
||||
};
|
||||
const hidden = Object.getOwnPropertyDescriptor(document, 'hidden');
|
||||
let tabHidden = false;
|
||||
Object.defineProperty(document, 'hidden', {
|
||||
configurable: true,
|
||||
get: () => tabHidden,
|
||||
});
|
||||
t.after(() => {
|
||||
globalThis.Image = prior;
|
||||
if (hidden) Object.defineProperty(document, 'hidden', hidden);
|
||||
else delete document.hidden;
|
||||
});
|
||||
const { controls, byId } = panel(t);
|
||||
const panelElement = byId('cctv-panel');
|
||||
const state = (frameUrl) => ({
|
||||
enabled: true,
|
||||
count: 1,
|
||||
cameraIndex: [camera],
|
||||
activeCameraId: 'a',
|
||||
activeCamera: { ...camera, frameUrl, sourceKind: 'snapshot' },
|
||||
});
|
||||
|
||||
// Collapsed panel: nothing is requested, however often the state arrives.
|
||||
assert.equal(panelElement.classList.contains('collapsed'), true);
|
||||
controls._renderCctvState(state('a1.jpg'));
|
||||
controls._renderCctvState(state('a2.jpg'));
|
||||
assert.equal(requests.length, 0, 'collapsed panel');
|
||||
|
||||
// Opening it renders again (the real panel does so from a class observer)
|
||||
// and asks for the current picture once.
|
||||
panelElement.classList.remove('collapsed');
|
||||
controls._renderCctvState(state('a2.jpg'));
|
||||
assert.equal(requests.length, 1, 'expanded panel');
|
||||
assert.equal(requests[0].src, 'a2.jpg');
|
||||
requests[0].onload();
|
||||
|
||||
// A background tab: refreshed URLs are not requested.
|
||||
tabHidden = true;
|
||||
controls._renderCctvState(state('a3.jpg'));
|
||||
assert.equal(requests.length, 1, 'hidden tab');
|
||||
|
||||
// Back in the tab: the next render fetches the current URL.
|
||||
tabHidden = false;
|
||||
controls._renderCctvState(state('a3.jpg'));
|
||||
assert.equal(requests.length, 2, 'visible tab');
|
||||
assert.equal(requests[1].src, 'a3.jpg');
|
||||
});
|
||||
@@ -274,11 +274,14 @@ export function _renderCctvState(state) {
|
||||
const liveIntent =
|
||||
enabled && !!activeCamera?.isVideo && !activeCamera?.browserMediaUrl;
|
||||
this.store.setLive(liveIntent);
|
||||
// A background tab or a collapsed panel shows nothing, so neither plays the
|
||||
// video nor fetches the still; the observer and the visibilitychange listener
|
||||
// render again when the panel is shown.
|
||||
const panelShown =
|
||||
!(typeof document !== 'undefined' && document.hidden) &&
|
||||
!this._cctvPanel?.classList?.contains('collapsed');
|
||||
if (this._cctvVideo) {
|
||||
const visible =
|
||||
liveIntent &&
|
||||
!document.hidden &&
|
||||
!this._cctvPanel?.classList.contains('collapsed');
|
||||
const visible = liveIntent && panelShown;
|
||||
if (!visible || this._cctvVideoCameraId !== activeId) {
|
||||
this._cctvVideoSurface?.stop();
|
||||
this._cctvVideoSurface = null;
|
||||
@@ -303,6 +306,7 @@ export function _renderCctvState(state) {
|
||||
// immediate so navigation never waits on the prior camera's request.
|
||||
if (
|
||||
nextSrc &&
|
||||
panelShown &&
|
||||
(cameraChanged || (!frameLoading && this._frame.currentSrc !== nextSrc))
|
||||
) {
|
||||
this._queueCctvFrame(nextSrc, nextCameraId, cameraChanged);
|
||||
|
||||
Reference in new issue
Block a user