Files
gods-eye-view/src/layers/streetLevel/photoViewers.js
T
Sameh KhamisandManjunath Paparaju 22485dcb01 Street Level photo viewer: a failed mount spares the next, stable keyless ranks
Second review follow-ups:

- A mount woken without being drawn unmounts only its own presentation:
  an earlier mount overtaken by a second mount of the same viewer fails
  as closed and leaves the second one mounted and presented.
- A mount whose last view left fails only if no view has subscribed
  since; one subscribed in the same tick draws it.
- Mount keys come from photoViewers.js's nextViewKey(), and a keyless
  presentation is stamped from the same counter when any picker first
  sees it, kept until it presents null: its rank no longer depends on the
  picker's lifetime or the order it subscribes in.
- A canvas handed back while a panorama's open waits on its draw takes
  the panorama: the open builds its renderer there instead of falling
  back to the front view. A canvas the panorama moved off is not moved
  back to, so two views drawing their own cannot take it in turns.
- The guard test also refuses compound writes (+=, ||=, ??=, &&=…),
  destructuring writes to properties, Object.assign (other than onto a
  new Error), Object.defineProperty and Reflect.set, and a catch binding
  that shadows loadImage's image.

Nothing changes in the running app.

Co-authored-by: Manjunath Paparaju <[email protected]>
2026-11-19 16:23:42 -08:00

103 lines
3.6 KiB
JavaScript

/** The last key handed out by `nextViewKey()`. */
let viewKeys = 0;
/**
* A key no viewer or picker was ever given: photo viewers key their mounts
* and canvases with it, and a presentation without a mount key is stamped
* with one when first seen, so both rank on one scale.
*/
export function nextViewKey() {
return ++viewKeys;
}
/**
* Each keyless viewer's current mount, `{actions, key}`: stamped once, by
* whichever picker sees it first, and kept until it presents null, so a
* picker made later ranks it the same.
*/
const keylessMounts = new WeakMap();
/**
* Receive the photo viewer to draw, out of several photo viewers mounting
* in one host (see photoViewer.js): the one mounted last, 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).
*
* Two fields of a presentation are read here (see ViewerAdapter.subscribeView
* in registry.js):
* - `mountKey`, a number larger for each later mount (from `nextViewKey()`),
* orders the mounts. A presentation without one counts as mounted when
* any picker first saw it present: it is stamped with a `nextViewKey()`
* then, the same for every picker, however many are made later.
* - `actions.supersede()`, where present, is called on each presentation
* 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 its rank, {view, key}.
const shown = new Map();
let ready = false;
let closed = false;
let superseding = false;
let last;
/** A mount's rank: its key, or the stamp of its first presentation. */
const rank = (viewer, view) => {
if (Number.isFinite(view.mountKey)) return view.mountKey;
let mount = keylessMounts.get(viewer);
if (!mount || mount.actions !== view.actions) {
mount = { actions: view.actions, key: nextViewKey() };
keylessMounts.set(viewer, mount);
}
return mount.key;
};
// The others are not drawn: a mount still waiting on the view never
// would be.
const supersede = () => {
superseding = false;
if (closed || !last) return;
for (const { view } of [...shown.values()])
if (view !== last) view.actions?.supersede?.();
};
const publish = () => {
let next = null;
for (const entry of shown.values())
if (!next || entry.key > next.key) next = entry;
next = next?.view ?? null;
if (!ready) return;
if (next !== last) {
last = next;
listener(next);
}
if (last && shown.size > 1 && !superseding) {
superseding = true;
queueMicrotask(supersede);
}
};
const unsubscribes = [];
for (const viewer of viewers) {
const unsubscribe = viewer?.subscribeView?.((view) => {
if (closed) return;
if (!view) {
shown.delete(viewer);
keylessMounts.delete(viewer);
} else shown.set(viewer, { view, key: rank(viewer, view) });
publish();
});
if (typeof unsubscribe === 'function') unsubscribes.push(unsubscribe);
}
ready = true;
publish();
if (last === undefined) listener((last = null));
return () => {
closed = true;
for (const unsubscribe of unsubscribes.splice(0)) unsubscribe();
};
}