mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-20 10:12:18 +00:00
Street Level photo viewer: settle opens, a gapless picker, a stated contract
Follow-ups to the photo viewer's move to a Vue view (#1868): - A panorama's open waits for a draw of the canvas counted from before it is shown, so a view that draws at once inside its listener no longer leaves the open pending; and when the last view unsubscribes, an open waiting on a draw goes on instead of hanging. - photoViewers.js supersedes the mounts not drawn on a microtask, after the winning presentation has reached every subscriber: a second subscriber sees the earlier mount, then the later one, never null between. - A presentation without a mountKey counts as the latest mount (among several without, the one that began presenting last), and one without actions or supersede is passed over without throwing. Both fields are documented on ViewerAdapter.subscribeView in registry.js and in the store's comment. - The guard test also refuses .width, .height, .ariaLabel, .id and .value writes in photoViewer.js, and .src writes outside loadImage, where only the off-page Image it creates may take one. - The test fakes draw every viewer through one shared view behind photoViewers.js's picker, as the application does, so provider and layer tests see supersede. Nothing changes in the running app. Co-authored-by: Manjunath Paparaju <[email protected]>
This commit is contained in:
1 parent
12dc0de20d
commit
33ac0545ef
9 files changed
+337
-49
No files matched your search
@@ -1,37 +1,70 @@
|
||||
/**
|
||||
* 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).
|
||||
* 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, orders the mounts. A
|
||||
* presentation without one counts as the latest mount; among several
|
||||
* without, the one that began presenting last is.
|
||||
* - `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.
|
||||
* @param {Iterable<{subscribeView?: Function}>} viewers
|
||||
* @param {(view: ?object) => void} listener
|
||||
* @returns {() => void} Unsubscribe.
|
||||
*/
|
||||
export function subscribePhotoViewers(viewers, listener) {
|
||||
// Each viewer's current presentation.
|
||||
// Each viewer's current presentation, and when it began presenting.
|
||||
const shown = new Map();
|
||||
let arrivals = 0;
|
||||
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;
|
||||
};
|
||||
// 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 view of shown.values())
|
||||
if (!next || view.mountKey > next.mountKey) next = view;
|
||||
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);
|
||||
// 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();
|
||||
if (next && shown.size > 1 && !superseding) {
|
||||
superseding = true;
|
||||
queueMicrotask(supersede);
|
||||
}
|
||||
};
|
||||
const unsubscribes = [];
|
||||
for (const viewer of viewers) {
|
||||
const unsubscribe = viewer?.subscribeView?.((view) => {
|
||||
if (view) shown.set(viewer, view);
|
||||
else shown.delete(viewer);
|
||||
if (closed) return;
|
||||
const entry = shown.get(viewer);
|
||||
if (!view) shown.delete(viewer);
|
||||
else if (entry && entry.view.mountKey === view.mountKey)
|
||||
entry.view = view;
|
||||
else shown.set(viewer, { view, arrival: ++arrivals });
|
||||
publish();
|
||||
});
|
||||
if (typeof unsubscribe === 'function') unsubscribes.push(unsubscribe);
|
||||
@@ -40,6 +73,7 @@ export function subscribePhotoViewers(viewers, listener) {
|
||||
publish();
|
||||
if (last === undefined) listener((last = null));
|
||||
return () => {
|
||||
closed = true;
|
||||
for (const unsubscribe of unsubscribes.splice(0)) unsubscribe();
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user