Street Level photo viewer: keyless ranking, closed waits, a moved canvas

Review follow-ups to the previous commit:

- photoViewers.js ranks a presentation without a mountKey as mounted when
  it began presenting (after the latest key seen, before any later mount),
  so a photo viewer mounting while a keyless one shows is drawn and the
  keyless one superseded; supersede is queued whenever more than one
  presents after any change, not only when the pick changes.
- A panorama's open still waiting on a draw when the last view leaves
  fails as closed and leaves no photo shown, instead of uploading to a
  canvas never drawn. A mount waiting on its first draw then also fails as
  closed and is unmounted, so a view subscribing later sees nothing.
- A view handing back another canvas element than the renderer's drops
  the renderer and draws the panorama again on the new one, as after a
  lost context.
- The guard test allows a .src write in loadImage only on the one `image`
  binding, created by new Image() and never rebound or shadowed, and
  refuses `(expr).src =` and bracket property writes anywhere.
- The test fakes' shared view is reset before each test
  (resetPhotoView), so a viewer left by an earlier test is neither drawn
  with nor superseded by a later test's.

Nothing changes in the running app.

Co-authored-by: Manjunath Paparaju <[email protected]>
This commit is contained in:
Sameh KhamisandManjunath Paparaju committed 2026-11-19 16:04:21 -08:00
1 parent 33ac0545ef
commit e6f21777ba
11 files changed
+327 -50

No files matched your search

+28 -17
View File
@@ -9,31 +9,40 @@
* Two fields of a presentation are read here (see ViewerAdapter.subscribeView
* in registry.js):
* - `mountKey`, a number larger for each later mount, orders the mounts. A
* presentation without one counts as the latest mount; among several
* without, the one that began presenting last is.
* presentation without one counts as mounted when it began presenting:
* after every mount presenting before it, before any later mount, keyed
* or not.
* - `actions.supersede()`, where present, is called on each presentation
* not drawn once the pick changes. It runs on a microtask, after the
* presentation that won has reached every subscriber: a subscriber never
* sees the superseded one withdraw before it sees the one replacing it.
* not drawn, whenever more than one presents. It runs on a microtask,
* after the presentation that won has reached every subscriber: a
* subscriber never sees the superseded one withdraw before it sees the
* one replacing it.
* @param {Iterable<{subscribeView?: Function}>} viewers
* @param {(view: ?object) => void} listener
* @returns {() => void} Unsubscribe.
*/
export function subscribePhotoViewers(viewers, listener) {
// Each viewer's current presentation, and when it began presenting.
// Each viewer's current presentation and its rank, {key, arrival}.
const shown = new Map();
let arrivals = 0;
/** The largest mount key seen: a keyless mount ranks just after it. */
let latestKey = -Infinity;
let ready = false;
let closed = false;
let superseding = false;
let last;
/** Whether `a` was mounted after `b`. */
const later = (a, b) => {
const key = ({ view }) =>
Number.isFinite(view.mountKey) ? view.mountKey : Infinity;
const ka = key(a);
const kb = key(b);
return ka !== kb ? ka > kb : a.arrival > b.arrival;
const later = (a, b) =>
a.key !== b.key ? a.key > b.key : a.arrival > b.arrival;
/** A new mount's rank: its key, or, without one, the latest key seen. */
const rank = (view) => {
const keyed = Number.isFinite(view.mountKey);
if (keyed) latestKey = Math.max(latestKey, view.mountKey);
return {
key: keyed ? view.mountKey : latestKey,
// A keyed mount comes before a keyless one ranked on its key.
arrival: keyed ? 0 : ++arrivals,
};
};
// The others are not drawn: a mount still waiting on the view never
// would be.
@@ -48,10 +57,12 @@ export function subscribePhotoViewers(viewers, listener) {
for (const entry of shown.values())
if (!next || later(entry, next)) next = entry;
next = next?.view ?? null;
if (!ready || next === last) return;
last = next;
listener(next);
if (next && shown.size > 1 && !superseding) {
if (!ready) return;
if (next !== last) {
last = next;
listener(next);
}
if (last && shown.size > 1 && !superseding) {
superseding = true;
queueMicrotask(supersede);
}
@@ -64,7 +75,7 @@ export function subscribePhotoViewers(viewers, listener) {
if (!view) shown.delete(viewer);
else if (entry && entry.view.mountKey === view.mountKey)
entry.view = view;
else shown.set(viewer, { view, arrival: ++arrivals });
else shown.set(viewer, { view, ...rank(view) });
publish();
});
if (typeof unsubscribe === 'function') unsubscribes.push(unsubscribe);