mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-20 10:12:18 +00:00
Street Level photo viewer: fresh elements per mount, and one viewer drawn
Fixes from review of the move to StreetLevelPhotoViewer: - Each mount and each panorama canvas now takes a key no viewer reuses, and the view keys its root by the mount. Before, switching KartaView and Panoramax in one tick let Vue patch the old viewer's canvas, whose destroyed renderer had lost its context, into the new one, so 360° photos fell back to their front view. - Of the viewers mounting in the host, the view draws the one mounted last, by mount key (photoViewers.js, which subscribePhotoViewer() now uses), whatever order they present in. A viewer still mounting when a later one is drawn is superseded: it unmounts and its mount fails as closed, rather than staying pending and being drawn once the later one goes. - A panorama's first image is uploaded once the view has drawn its canvas shown (the view hands the canvas back on every draw), so it is never sized into a hidden canvas. - The tests' fake view now draws as Vue does: once per tick with the final value, keeping elements while their keys are unchanged and handing every element back on each draw. StreetLevelPhotoViewer.test.mjs covers the switch in one tick and the overtaken mount with the real view. - The guard test's check that the photo viewer writes no DOM covers inserting, moving, removing and replacing elements, attributes, classes, styles and the hidden, disabled and text properties. Co-authored-by: Manjunath Paparaju <[email protected]>
This commit is contained in:
1 parent
766364f691
commit
2a0e21774b
17 files changed
+340
-107
No files matched your search
@@ -5,6 +5,7 @@ import { createCredits } from './credits.js';
|
||||
import { createPickRouter } from './pickRouter.js';
|
||||
import { createSelection } from './selection.js';
|
||||
import { createViewerHost } from './viewerHost.js';
|
||||
import { subscribePhotoViewers } from './photoViewers.js';
|
||||
import { filtersOf, requiresKeyIdFor, validateProviders } from './registry.js';
|
||||
import { normalizeFilter, resolveFilter, sameFilter } from './filter.js';
|
||||
import { worldTimeFor } from '../../time/worldClock.js';
|
||||
@@ -706,38 +707,18 @@ export function createStreetLevelLayer({
|
||||
/**
|
||||
* Receive every presentation of the photo viewer the providers whose
|
||||
* photos are plain files (KartaView, Panoramax) mount in the viewer
|
||||
* host, starting with the current one; null while none is mounted. A
|
||||
* view draws it (see photoViewer.js); with nobody subscribed, those
|
||||
* providers' photos cannot open.
|
||||
* host, the one mounted last (see photoViewers.js), starting with the
|
||||
* current one; null while none is mounted. A view draws it (see
|
||||
* photoViewer.js); with nobody subscribed, those providers' photos
|
||||
* cannot open.
|
||||
* @param {(view: ?object) => void} listener
|
||||
* @returns {() => void} Unsubscribe.
|
||||
*/
|
||||
subscribePhotoViewer(listener) {
|
||||
// Each mounted viewer's presentation, the latest mount last.
|
||||
const shown = new Map();
|
||||
let ready = false;
|
||||
let last;
|
||||
const publish = () => {
|
||||
const next = [...shown.values()].at(-1) ?? null;
|
||||
if (!ready || next === last) return;
|
||||
last = next;
|
||||
listener(next);
|
||||
};
|
||||
const unsubscribes = [];
|
||||
for (const [id, entry] of state.providers) {
|
||||
const unsubscribe = entry.instance.viewer?.subscribeView?.((view) => {
|
||||
shown.delete(id);
|
||||
if (view) shown.set(id, view);
|
||||
publish();
|
||||
});
|
||||
if (typeof unsubscribe === 'function') unsubscribes.push(unsubscribe);
|
||||
}
|
||||
ready = true;
|
||||
publish();
|
||||
if (last === undefined) listener((last = null));
|
||||
return () => {
|
||||
for (const unsubscribe of unsubscribes.splice(0)) unsubscribe();
|
||||
};
|
||||
return subscribePhotoViewers(
|
||||
[...state.providers.values()].map((entry) => entry.instance.viewer),
|
||||
listener,
|
||||
);
|
||||
},
|
||||
setProviderEnabled,
|
||||
openImage,
|
||||
|
||||
@@ -2177,7 +2177,7 @@ test('proceed() is asked after the lookup: false loads nothing and clears the sp
|
||||
);
|
||||
});
|
||||
|
||||
test('subscribePhotoViewer hands on the photo viewer a provider presents, the latest mount’s while two overlap', async (t) => {
|
||||
test('subscribePhotoViewer hands on the photo viewer a provider presents, the later mount’s while two overlap', async (t) => {
|
||||
const presenting = (id, pickPrefix) => {
|
||||
const provider = fakeProvider({ id, pickPrefix });
|
||||
const create = provider.create;
|
||||
@@ -2202,8 +2202,9 @@ test('subscribePhotoViewer hands on the photo viewer a provider presents, the la
|
||||
const shown = [];
|
||||
const unsubscribe = layer.subscribePhotoViewer((view) => shown.push(view));
|
||||
assert.deepEqual(shown, [null], 'the current one first');
|
||||
const viewA = { host: 'a' };
|
||||
const viewB = { host: 'b' };
|
||||
const supersede = () => {};
|
||||
const viewA = { host: 'a', mountKey: 1, actions: { supersede } };
|
||||
const viewB = { host: 'b', mountKey: 2, actions: { supersede } };
|
||||
a.present(viewA);
|
||||
b.present(viewB);
|
||||
a.present(null);
|
||||
|
||||
@@ -36,6 +36,14 @@ function tooManyPixels(width, height) {
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Keys for each mount's and each canvas's elements, never reused by any
|
||||
* viewer: a view that sees one viewer unmount and another mount in the same
|
||||
* tick still draws them fresh elements (a destroyed renderer leaves its
|
||||
* canvas's context lost).
|
||||
*/
|
||||
let viewKeys = 0;
|
||||
|
||||
/** Panorama texture widths: enough for a docked viewer, and for an expanded one. */
|
||||
const PANO_MID_SIZE = Object.freeze([2048, 1024]);
|
||||
export const PANO_FULL_SIZE = Object.freeze([4096, 2048]);
|
||||
@@ -215,14 +223,18 @@ export function panoSizeFor(canvasPixels, fovDeg) {
|
||||
*
|
||||
* The adapter draws nothing itself. It presents the viewer through
|
||||
* `subscribeView()` (the street-level layer's `subscribePhotoViewer()`): a
|
||||
* fresh `{host, providerId, label, alt, kind, src, fit, canvasKey, dragging,
|
||||
* prevLabel, prevDisabled, nextLabel, nextDisabled, actions}` on each change,
|
||||
* fresh `{host, mountKey, providerId, label, alt, kind, src, fit, canvasKey,
|
||||
* dragging, prevLabel, prevDisabled, nextLabel, nextDisabled, actions}` on
|
||||
* each change,
|
||||
* or null while unmounted. A view draws it into `host` (the application's is
|
||||
* `StreetLevelPhotoViewer`), forwards its events to `actions` and hands each
|
||||
* element back through `actions.attach(name, element)`: the panorama canvas
|
||||
* to draw on, the frame and picture to measure, the root and buttons to
|
||||
* keep focus. A new `canvasKey` asks for a fresh canvas (a lost WebGL
|
||||
* context). Mounting with no view subscribed fails.
|
||||
* keep focus. Each mount has its own `mountKey`, and a new `canvasKey` asks
|
||||
* for a fresh canvas (a lost WebGL context): neither is ever reused.
|
||||
* `actions.supersede()` tells a viewer another one is drawn in its place:
|
||||
* it unmounts, and a mount still waiting to be drawn fails as closed.
|
||||
* Mounting with no view subscribed fails.
|
||||
*
|
||||
* Photos are normalized by the provider: `{id, sequenceId, lon, lat,
|
||||
* heading, isPano, capturedAt}` plus whatever its `sources` read.
|
||||
@@ -269,7 +281,7 @@ export function createPhotoViewer({
|
||||
/** What the view draws; null while unmounted. */
|
||||
let presented = null;
|
||||
const viewListeners = new Set();
|
||||
/** Waiting for the view's root (a mount) or a fresh canvas (an open). */
|
||||
/** Waiting for the view's root (a mount), or its canvas: a fresh one, or the next draw. */
|
||||
const rootWaiters = new Set();
|
||||
const canvasWaiters = new Set();
|
||||
let renderer = null;
|
||||
@@ -393,6 +405,15 @@ export function createPhotoViewer({
|
||||
});
|
||||
}
|
||||
|
||||
/** The view's next draw (it hands the canvas back each time), or none coming. */
|
||||
function nextDraw(signal) {
|
||||
if (!presented || signal?.aborted) return Promise.resolve();
|
||||
return new Promise((resolve) => {
|
||||
canvasWaiters.add(resolve);
|
||||
signal?.addEventListener?.('abort', resolve, { once: true });
|
||||
});
|
||||
}
|
||||
|
||||
function syncButtons() {
|
||||
const enabled = Boolean(photo?.sequenceId) && showing !== null;
|
||||
present({ prevDisabled: !enabled, nextDisabled: !enabled });
|
||||
@@ -461,7 +482,7 @@ export function createPhotoViewer({
|
||||
// A lost canvas hands back its lost context: the view draws a new
|
||||
// element in its place and hands it back.
|
||||
canvas = null;
|
||||
present({ canvasKey: presented.canvasKey + 1 });
|
||||
present({ canvasKey: ++viewKeys });
|
||||
// Show the photo again through the core, unless a newer one is opening.
|
||||
if (showing === 'pano' && photo && openingId === null) {
|
||||
const again = photo.id;
|
||||
@@ -654,6 +675,7 @@ export function createPhotoViewer({
|
||||
pointerup: current(onPointerUp),
|
||||
wheel: current(onWheel),
|
||||
contextLost: current(onContextLost),
|
||||
supersede: current(() => adapter.unmount()),
|
||||
step: current((delta, event) => {
|
||||
event?.stopPropagation?.();
|
||||
stepBy(delta);
|
||||
@@ -661,6 +683,7 @@ export function createPhotoViewer({
|
||||
};
|
||||
presented = {
|
||||
host,
|
||||
mountKey: ++viewKeys,
|
||||
providerId,
|
||||
label: t(
|
||||
'{name} photo. ← and → step along the sequence. On a 360° photo, drag or use Shift+← →, ↑ ↓ to look around, and + − to zoom',
|
||||
@@ -670,7 +693,7 @@ export function createPhotoViewer({
|
||||
kind: undefined,
|
||||
src: null,
|
||||
fit: renderMode === 'fill' ? 'cover' : 'contain',
|
||||
canvasKey: 0,
|
||||
canvasKey: ++viewKeys,
|
||||
dragging: false,
|
||||
prevLabel: t('Previous photo in this sequence (←)'),
|
||||
prevDisabled: true,
|
||||
@@ -841,8 +864,15 @@ export function createPhotoViewer({
|
||||
else if (!keepView || showing !== 'pano')
|
||||
renderer.setView({ yaw: 0, pitch: 0, fov: PANO_FOV_DEFAULT_DEG });
|
||||
photo = target;
|
||||
// Shown first: a hidden canvas measures 0 and would be sized 1×1.
|
||||
// Shown first: a hidden canvas measures 0 and would be sized 1×1. The
|
||||
// view draws it shown on a microtask, and hands the canvas back then.
|
||||
show('pano');
|
||||
await nextDraw(signal);
|
||||
if (seq !== openSeq || !renderer) {
|
||||
image.close?.();
|
||||
if (seq !== openSeq) return false;
|
||||
throw new Error(LOAD_FAILED);
|
||||
}
|
||||
renderer.setImage(image);
|
||||
panoWidth = first.width || 0;
|
||||
// A sharper version of the photo before was dropped by `open`; one of
|
||||
@@ -911,6 +941,8 @@ export function createPhotoViewer({
|
||||
const drawn = new Promise((resolve) => rootWaiters.add(resolve));
|
||||
build(host);
|
||||
if (!root) await drawn;
|
||||
// Unmounted or superseded before the view drew it.
|
||||
if (!root) throw new Error('Street-level viewer was closed');
|
||||
},
|
||||
|
||||
/** Resolves once the photo is on screen and its pose was emitted. */
|
||||
|
||||
@@ -595,21 +595,30 @@ test('a panorama reopened after a lost context waits for the view’s fresh canv
|
||||
};
|
||||
},
|
||||
});
|
||||
// A view that draws a replacement canvas a while after it is asked.
|
||||
// A view that draws on a microtask, as Vue does, handing its canvas back
|
||||
// each time, but draws a replacement canvas only a while after it is
|
||||
// asked.
|
||||
let shown = null;
|
||||
let canvas = { name: 'canvas', clientWidth: 400 };
|
||||
let late = null;
|
||||
viewer.subscribeView((view) => {
|
||||
if (view && !shown) {
|
||||
for (const name of ['root', 'picture', 'prev', 'next'])
|
||||
view.actions.attach(name, { name, clientWidth: 400 });
|
||||
view.actions.attach('canvas', { name: 'canvas 0', clientWidth: 400 });
|
||||
} else if (view && view.canvasKey !== shown.canvasKey)
|
||||
late = () =>
|
||||
view.actions.attach('canvas', {
|
||||
name: `canvas ${view.canvasKey}`,
|
||||
clientWidth: 400,
|
||||
});
|
||||
const first = view && !shown;
|
||||
const replaced = view && shown && view.canvasKey !== shown.canvasKey;
|
||||
shown = view;
|
||||
if (!view) return;
|
||||
queueMicrotask(() => {
|
||||
if (first)
|
||||
for (const name of ['root', 'picture', 'prev', 'next'])
|
||||
view.actions.attach(name, { name, clientWidth: 400 });
|
||||
if (replaced) {
|
||||
canvas = null;
|
||||
late = () => {
|
||||
canvas = { name: 'canvas again', clientWidth: 400 };
|
||||
shown.actions.attach('canvas', canvas);
|
||||
};
|
||||
}
|
||||
if (canvas) view.actions.attach('canvas', canvas);
|
||||
});
|
||||
});
|
||||
await viewer.mount({});
|
||||
await viewer.open('a');
|
||||
@@ -623,7 +632,7 @@ test('a panorama reopened after a lost context waits for the view’s fresh canv
|
||||
await opening;
|
||||
assert.deepEqual(
|
||||
canvases.map((canvas) => canvas.name),
|
||||
['canvas 0', 'canvas 1'],
|
||||
['canvas', 'canvas again'],
|
||||
);
|
||||
assert.equal(shown.kind, 'pano', 'not the front view');
|
||||
});
|
||||
@@ -0,0 +1,45 @@
|
||||
/**
|
||||
* Receive the photo viewer to draw, out of several photo viewers mounting
|
||||
* in one host (see photoViewer.js): the one mounted last, by its mount key,
|
||||
* whatever order they present in. One view draws one viewer, so a viewer
|
||||
* still mounting when a later one presents is superseded: it settles its
|
||||
* mount as closed and stops presenting. Starts with the current one (null
|
||||
* while none is mounted).
|
||||
* @param {Iterable<{subscribeView?: Function}>} viewers
|
||||
* @param {(view: ?object) => void} listener
|
||||
* @returns {() => void} Unsubscribe.
|
||||
*/
|
||||
export function subscribePhotoViewers(viewers, listener) {
|
||||
// Each viewer's current presentation.
|
||||
const shown = new Map();
|
||||
let ready = false;
|
||||
let last;
|
||||
const publish = () => {
|
||||
let next = null;
|
||||
for (const view of shown.values())
|
||||
if (!next || view.mountKey > next.mountKey) next = view;
|
||||
if (!ready || next === last) return;
|
||||
last = next;
|
||||
listener(next);
|
||||
// The others are not drawn: a mount still waiting on the view never
|
||||
// would be.
|
||||
if (next)
|
||||
for (const view of [...shown.values()])
|
||||
if (view.mountKey < next.mountKey) view.actions.supersede();
|
||||
};
|
||||
const unsubscribes = [];
|
||||
for (const viewer of viewers) {
|
||||
const unsubscribe = viewer?.subscribeView?.((view) => {
|
||||
if (view) shown.set(viewer, view);
|
||||
else shown.delete(viewer);
|
||||
publish();
|
||||
});
|
||||
if (typeof unsubscribe === 'function') unsubscribes.push(unsubscribe);
|
||||
}
|
||||
ready = true;
|
||||
publish();
|
||||
if (last === undefined) listener((last = null));
|
||||
return () => {
|
||||
for (const unsubscribe of unsubscribes.splice(0)) unsubscribe();
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
import assert from 'node:assert/strict';
|
||||
import test from 'node:test';
|
||||
import { subscribePhotoViewers } from './photoViewers.js';
|
||||
|
||||
/**
|
||||
* A photo viewer whose presentations the test sends; `superseded` counts.
|
||||
* Unlike photoViewer.js it keeps presenting once superseded, so the order
|
||||
* is seen to come from the mount keys alone.
|
||||
*/
|
||||
function fakeViewer() {
|
||||
const listeners = new Set();
|
||||
const viewer = {
|
||||
superseded: 0,
|
||||
view(mountKey) {
|
||||
return {
|
||||
mountKey,
|
||||
actions: { supersede: () => viewer.superseded++ },
|
||||
};
|
||||
},
|
||||
present(view) {
|
||||
for (const listener of [...listeners]) listener(view);
|
||||
},
|
||||
subscribeView(listener) {
|
||||
listeners.add(listener);
|
||||
listener(null);
|
||||
return () => listeners.delete(listener);
|
||||
},
|
||||
};
|
||||
return viewer;
|
||||
}
|
||||
|
||||
test('the later mount is drawn, whatever order the viewers present in', () => {
|
||||
const a = fakeViewer();
|
||||
const b = fakeViewer();
|
||||
const shown = [];
|
||||
const unsubscribe = subscribePhotoViewers([a, b], (view) => shown.push(view));
|
||||
assert.deepEqual(shown, [null], 'the current one first');
|
||||
const first = a.view(1);
|
||||
a.present(first);
|
||||
const later = b.view(2);
|
||||
b.present(later);
|
||||
assert.equal(shown.at(-1), later);
|
||||
assert.equal(a.superseded, 1, 'the earlier mount is told it is not drawn');
|
||||
// The earlier viewer presents again: it does not take the view back.
|
||||
a.present({ ...first, kind: 'flat' });
|
||||
assert.equal(shown.at(-1), later);
|
||||
assert.equal(shown.length, 3);
|
||||
b.present(null);
|
||||
assert.equal(shown.at(-1).mountKey, 1, 'the only one presenting');
|
||||
a.present(null);
|
||||
assert.equal(shown.at(-1), null);
|
||||
unsubscribe();
|
||||
a.present(first);
|
||||
assert.equal(shown.length, 5, 'unsubscribed');
|
||||
});
|
||||
@@ -13,7 +13,11 @@ import {
|
||||
TOO_HIGH_HINT,
|
||||
} from './policy.js';
|
||||
import { validateProviders } from '../../registry.js';
|
||||
import { fakeHost, until } from '../../../../testSupport/kartaViewFakes.mjs';
|
||||
import {
|
||||
drawn,
|
||||
fakeHost,
|
||||
until,
|
||||
} from '../../../../testSupport/kartaViewFakes.mjs';
|
||||
import {
|
||||
bindStreetViewer,
|
||||
coverageViewer,
|
||||
@@ -978,6 +982,7 @@ test('after a GPU reset the photo is drawn again by the viewer, not through the
|
||||
const canvas = () => host.children[0].children[1];
|
||||
const kind = () => host.children[0].dataset.kind;
|
||||
canvas().fire('webglcontextlost');
|
||||
await drawn();
|
||||
assert.equal(kind(), '', 'cleared while it is drawn again');
|
||||
assert.ok(await until(() => kind() === 'pano'), 'drawn again');
|
||||
assert.ok(poses.length >= 2);
|
||||
|
||||
@@ -1,7 +1,11 @@
|
||||
import assert from 'node:assert/strict';
|
||||
import test from 'node:test';
|
||||
import { createKartaViewViewer, loadImage, panoSizeFor } from './viewer.js';
|
||||
import { fakeHost, until } from '../../../../testSupport/kartaViewFakes.mjs';
|
||||
import {
|
||||
drawn,
|
||||
fakeHost,
|
||||
until,
|
||||
} from '../../../../testSupport/kartaViewFakes.mjs';
|
||||
|
||||
const PHOTOS = {
|
||||
flat1: {
|
||||
@@ -223,6 +227,7 @@ test('a flat photo shows its 1280 px version, then the sharp one, with a pose fa
|
||||
assert.equal(picture.src, loads[1].url, 'upgraded to the sharp version');
|
||||
assert.equal(viewer.supportsRenderModes(), true);
|
||||
viewer.setRenderMode('fill');
|
||||
await drawn();
|
||||
assert.equal(picture.style.objectFit, 'cover');
|
||||
});
|
||||
|
||||
@@ -357,12 +362,14 @@ test('a newer open wins; close hides the photo; unmount tears down the panorama'
|
||||
assert.equal(renderers.length, 0, 'nor built a WebGL context');
|
||||
await viewer.open('pano1');
|
||||
viewer.close();
|
||||
await drawn();
|
||||
const { picture, canvas, root } = parts(host);
|
||||
assert.equal(canvas.hidden, true);
|
||||
assert.equal(picture.hidden, true);
|
||||
assert.equal(root.dataset.kind, '');
|
||||
await assert.rejects(viewer.open('nope'), /no such photo/);
|
||||
viewer.unmount();
|
||||
await drawn();
|
||||
assert.equal(host.children.length, 0);
|
||||
assert.equal(renderers[0].destroyed, true);
|
||||
await assert.rejects(viewer.open('flat1'), /not mounted/);
|
||||
@@ -463,12 +470,14 @@ test('a lost WebGL context drops the panorama and shows the photo again on a new
|
||||
const lost = parts(host).canvas;
|
||||
lost.fire('webglcontextlost');
|
||||
assert.equal(renderers[0].destroyed, true);
|
||||
await drawn();
|
||||
// A lost canvas only ever gives back its lost context: a new element.
|
||||
assert.notEqual(parts(host).canvas, lost);
|
||||
assert.deepEqual(stepped, ['pano1'], 'reopened through the core');
|
||||
assert.ok(await until(() => renderers.length === 2), 'a new renderer');
|
||||
assert.equal(renderers[1].canvas, parts(host).canvas);
|
||||
assert.equal(poses.at(-1).imageId, 'pano1');
|
||||
await drawn();
|
||||
assert.equal(parts(host).canvas.hidden, false);
|
||||
});
|
||||
|
||||
@@ -931,7 +940,7 @@ test('a docked viewer keeps the 1,280 px photo; expanding it brings the 2,560 px
|
||||
viewer.resize();
|
||||
assert.ok(await until(() => loads.length === 2));
|
||||
assert.match(loads[1].url, /rs:fit:2560:2560/);
|
||||
assert.equal(parts(host).picture.src, loads[1].url);
|
||||
assert.ok(await until(() => parts(host).picture.src === loads[1].url));
|
||||
viewer.resize();
|
||||
for (let i = 0; i < 4; i++) await settle();
|
||||
assert.equal(loads.length, 2, 'once only');
|
||||
|
||||
@@ -14,7 +14,11 @@ import {
|
||||
} from './policy.js';
|
||||
import { validateProviders } from '../../registry.js';
|
||||
import { tileBounds } from '../../tileMath.js';
|
||||
import { fakeHost, until } from '../../../../testSupport/kartaViewFakes.mjs';
|
||||
import {
|
||||
drawn,
|
||||
fakeHost,
|
||||
until,
|
||||
} from '../../../../testSupport/kartaViewFakes.mjs';
|
||||
import {
|
||||
bindStreetViewer,
|
||||
coverageViewer,
|
||||
@@ -994,6 +998,8 @@ test('after a GPU reset the viewer draws the same picture itself, leaving the co
|
||||
const kind = () => host.children[0].dataset.kind;
|
||||
assert.equal(kind(), 'pano');
|
||||
canvas().fire('webglcontextlost');
|
||||
await drawn();
|
||||
assert.equal(kind(), '', 'cleared while it is drawn again');
|
||||
assert.ok(await until(() => kind() === 'pano'), 'drawn again');
|
||||
assert.deepEqual(poses, ['p-0', 'p-0']);
|
||||
assert.deepEqual(actions.opened, [], 'not through the core');
|
||||
|
||||
Reference in new issue
Block a user