mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-20 18:22:25 +00:00
Merge branch 'main' into samehkhamis/find-it
This commit is contained in:
38 files changed
+1822
-147
No files matched your search
@@ -27,7 +27,9 @@ let instances = 0;
|
||||
* records fully cover, reused for later views inside it) and `zoom` (their
|
||||
* tile precision; `source.detailZoom(box)` names the zoom a view needs). Records use stable string ids and latitude /
|
||||
* longitude in degrees; optional directionDeg is a compass bearing [0, 360).
|
||||
* Manufacturer, operator and cameraType are optional display fields.
|
||||
* Manufacturer, operator, cameraType and model are optional display fields;
|
||||
* model only selects a vendored datasheet line (modelSpecs.js), never shown
|
||||
* as text.
|
||||
* Source label and attribution { name, description, text, href } identify the
|
||||
* actual provider; href must be HTTPS. Provider payload parsing stays in source.
|
||||
* Context services remove obsolete records with
|
||||
|
||||
@@ -0,0 +1,250 @@
|
||||
/**
|
||||
* @module alprModelSpecs
|
||||
*
|
||||
* Datasheet specs for the ALPR hardware that OpenStreetMap `model` tags name,
|
||||
* vendored from the CCTV Camera Database (CC0-1.0) and matched with the CCTV
|
||||
* layer's rules; DATA_SOURCES.md has the provenance. Flock Safety publishes no
|
||||
* datasheet resolution, so Flock models stay unresolved; an unknown model
|
||||
* never implies anything.
|
||||
*
|
||||
* Scope guardrail: records describe camera PRODUCTS (optics, resolution,
|
||||
* night vision) — never endpoints, operators or what a camera records.
|
||||
*/
|
||||
|
||||
import { createModelSpecLookup, lookupModelSpec } from '../cctv/modelSpecs.js';
|
||||
|
||||
/**
|
||||
* Records from the dataset's model-enrichment extract (v2.29.0), with the
|
||||
* bare product codes mappers tag ("L6Q") added as aliases where the title
|
||||
* names the product line ("Vigilant L6Q"). A tag without a generation or
|
||||
* variant could name any of them, so the SharpV G3 and SharpX XGA titles
|
||||
* carry theirs. Fields the card does not read (type, url, fov_deg, …) stay as
|
||||
* in the dataset, like the CCTV records.
|
||||
*/
|
||||
const RECORDS = [
|
||||
{
|
||||
id: 'motorola-solutions-l5f',
|
||||
brand: 'Motorola Solutions',
|
||||
model: 'Vigilant L5F',
|
||||
type: 'box',
|
||||
url: 'https://www.cctv-database.com/camera/motorola-solutions-l5f/',
|
||||
aliases: ['L5F', 'L5F Fixed LPR Camera System'],
|
||||
resolution: { mp: 1.6, w: 1440, h: 1080 },
|
||||
lens_count: 2,
|
||||
night_vision: { type: 'ir' },
|
||||
environment: ['outdoor'],
|
||||
},
|
||||
{
|
||||
id: 'motorola-solutions-l5m',
|
||||
brand: 'Motorola Solutions',
|
||||
model: 'Vigilant L5M',
|
||||
type: 'box',
|
||||
url: 'https://www.cctv-database.com/camera/motorola-solutions-l5m/',
|
||||
aliases: [
|
||||
'L5M',
|
||||
'L5M Mobile LPR Camera System',
|
||||
'L5M Mobile ANPR Camera System',
|
||||
],
|
||||
resolution: { mp: 1.6, w: 1440, h: 1080 },
|
||||
lens_count: 2,
|
||||
night_vision: { type: 'ir' },
|
||||
environment: ['outdoor'],
|
||||
},
|
||||
{
|
||||
id: 'motorola-solutions-l5q',
|
||||
brand: 'Motorola Solutions',
|
||||
model: 'Vigilant L5Q',
|
||||
type: 'box',
|
||||
url: 'https://www.cctv-database.com/camera/motorola-solutions-l5q/',
|
||||
aliases: ['L5Q', 'L5Q Quick Deploy LPR Camera System'],
|
||||
resolution: { mp: 3 },
|
||||
environment: ['outdoor'],
|
||||
},
|
||||
{
|
||||
id: 'motorola-solutions-l6d',
|
||||
brand: 'Motorola Solutions',
|
||||
model: 'Vigilant L6D',
|
||||
type: 'bullet',
|
||||
url: 'https://www.cctv-database.com/camera/motorola-solutions-l6d/',
|
||||
aliases: [
|
||||
'L6D',
|
||||
'L6D Dual-Purpose LPR Camera',
|
||||
'VSM-L6D-8IR',
|
||||
'VSM-L6D-7IR',
|
||||
],
|
||||
fov_deg: '52-3 horizontal',
|
||||
resolution: { mp: 1.6, w: 1440, h: 1080 },
|
||||
lens_count: 2,
|
||||
varifocal: true,
|
||||
night_vision: { type: 'ir' },
|
||||
environment: ['outdoor'],
|
||||
},
|
||||
{
|
||||
id: 'motorola-solutions-l6q',
|
||||
brand: 'Motorola Solutions',
|
||||
model: 'Vigilant L6Q',
|
||||
type: 'box',
|
||||
url: 'https://www.cctv-database.com/camera/motorola-solutions-l6q/',
|
||||
aliases: ['L6Q', 'L6Q Quick-Deploy LPR Camera System'],
|
||||
resolution: { mp: 2.1, w: 1920, h: 1080, label: '1080p' },
|
||||
night_vision: { type: 'ir' },
|
||||
environment: ['outdoor'],
|
||||
},
|
||||
{
|
||||
id: 'genetec-autovu-sharpv',
|
||||
brand: 'Genetec',
|
||||
model: 'AutoVu SharpV G3',
|
||||
type: 'box',
|
||||
url: 'https://www.cctv-database.com/camera/genetec-autovu-sharpv/',
|
||||
aliases: [
|
||||
'AU-V-WS850-LC-G3-000',
|
||||
'AU-V-BS850-LC-G3-0M0',
|
||||
'AU-V-BS850-LC-G3-0M1',
|
||||
],
|
||||
resolution: { mp: 2.3, w: 1920, h: 1200, label: '1920x1200' },
|
||||
lens_count: 2,
|
||||
night_vision: { type: 'ir' },
|
||||
environment: ['outdoor'],
|
||||
},
|
||||
{
|
||||
id: 'genetec-autovu-sharpv-g2',
|
||||
brand: 'Genetec',
|
||||
model: 'AutoVu SharpV G2',
|
||||
type: 'box',
|
||||
url: 'https://www.cctv-database.com/camera/genetec-autovu-sharpv-g2/',
|
||||
aliases: ['AutoVu SharpV G1', 'AU-V-WS850-LC-G2', 'AU-V-WS850-LC'],
|
||||
resolution: { mp: 1.2, w: 1280, h: 960, label: '1280x960' },
|
||||
lens_count: 2,
|
||||
night_vision: { type: 'ir' },
|
||||
environment: ['outdoor'],
|
||||
},
|
||||
{
|
||||
id: 'genetec-autovu-sharpx',
|
||||
brand: 'Genetec',
|
||||
model: 'AutoVu SharpX XGA',
|
||||
type: 'box',
|
||||
url: 'https://www.cctv-database.com/camera/genetec-autovu-sharpx/',
|
||||
// i18n-ignore: datasheet label
|
||||
resolution: { mp: 1, w: 1024, h: 946, label: 'XGA (1024x946)' },
|
||||
lens_count: 2,
|
||||
night_vision: { type: 'ir' },
|
||||
environment: ['outdoor'],
|
||||
},
|
||||
{
|
||||
id: 'genetec-autovu-sharpz3',
|
||||
brand: 'Genetec',
|
||||
model: 'AutoVu SharpZ3',
|
||||
type: 'box',
|
||||
url: 'https://www.cctv-database.com/camera/genetec-autovu-sharpz3/',
|
||||
resolution: { mp: 1.6, w: 1456, h: 1088, label: '1456x1088' },
|
||||
lens_count: 3,
|
||||
night_vision: { type: 'ir' },
|
||||
environment: ['outdoor'],
|
||||
},
|
||||
{
|
||||
id: 'axis-q1700-le',
|
||||
brand: 'Axis',
|
||||
model: 'Q1700-LE',
|
||||
type: 'box',
|
||||
url: 'https://www.cctv-database.com/camera/axis-q1700-le/',
|
||||
fov_deg: '16-2.3 horizontal / 9.6-1.3 vertical',
|
||||
resolution: { mp: 2, w: 1920, h: 1080, label: '1080p' },
|
||||
varifocal: true,
|
||||
night_vision: { type: 'ir', range_m: 50 },
|
||||
environment: ['outdoor'],
|
||||
ndaa_compliant: true,
|
||||
},
|
||||
{
|
||||
id: 'axis-q1800-le',
|
||||
brand: 'Axis',
|
||||
model: 'Q1800-LE',
|
||||
type: 'bullet',
|
||||
url: 'https://www.cctv-database.com/camera/axis-q1800-le/',
|
||||
aliases: ['03107-001', 'Q1800-LE-3 (License Plate Verifier kit)'],
|
||||
fov_deg: '38-2.3 horizontal',
|
||||
resolution: { mp: 2, w: 1920, h: 1080, label: '1080p' },
|
||||
varifocal: true,
|
||||
night_vision: { type: 'ir', range_m: 40 },
|
||||
environment: ['indoor', 'outdoor'],
|
||||
ndaa_compliant: true,
|
||||
},
|
||||
{
|
||||
id: 'verkada-cb62-te-f',
|
||||
brand: 'Verkada',
|
||||
model: 'CB62-TE-F',
|
||||
type: 'bullet',
|
||||
url: 'https://www.cctv-database.com/camera/verkada-cb62-te-f/',
|
||||
fov_deg: '42 to 17 H',
|
||||
resolution: { mp: 8.3, w: 3840, h: 2160, label: '4K' },
|
||||
varifocal: true,
|
||||
night_vision: { type: 'ir', range_m: 50 },
|
||||
environment: ['outdoor'],
|
||||
ndaa_compliant: true,
|
||||
},
|
||||
{
|
||||
id: 'verkada-ch52-e-f',
|
||||
brand: 'Verkada',
|
||||
model: 'CH52-E-F',
|
||||
type: 'panoramic',
|
||||
url: 'https://www.cctv-database.com/camera/verkada-ch52-e-f/',
|
||||
fov_deg: '89 to 37 H (per sensor); up to 328 combined',
|
||||
// i18n-ignore: datasheet label
|
||||
resolution: { mp: 5, w: 2688, h: 1944, label: '4 x 5MP (20MP)' },
|
||||
lens_count: 4,
|
||||
varifocal: true,
|
||||
night_vision: { type: 'ir', range_m: 30 },
|
||||
environment: ['outdoor'],
|
||||
ndaa_compliant: true,
|
||||
},
|
||||
{
|
||||
id: 'verkada-cp63-e',
|
||||
brand: 'Verkada',
|
||||
model: 'CP63-E',
|
||||
type: 'ptz',
|
||||
url: 'https://www.cctv-database.com/camera/verkada-cp63-e/',
|
||||
fov_deg: '64 to 2 H',
|
||||
resolution: { mp: 8.3, w: 3840, h: 2160, label: '4K' },
|
||||
ptz: true,
|
||||
varifocal: true,
|
||||
night_vision: { type: 'ir', range_m: 100 },
|
||||
environment: ['outdoor'],
|
||||
ndaa_compliant: true,
|
||||
},
|
||||
{
|
||||
id: 'verkada-cr63-e',
|
||||
brand: 'Verkada',
|
||||
model: 'CR63-E',
|
||||
type: 'bullet',
|
||||
url: 'https://www.cctv-database.com/camera/verkada-cr63-e/',
|
||||
fov_deg: '109 to 49 H',
|
||||
resolution: { mp: 8.3, w: 3840, h: 2160, label: '4K' },
|
||||
varifocal: true,
|
||||
night_vision: { type: 'ir', range_m: 20 },
|
||||
environment: ['indoor', 'outdoor'],
|
||||
},
|
||||
// The CCTV layer's record: one colour-neutral title for the B/G/W variants.
|
||||
lookupModelSpec('MIC-7522-Z30B', 'Bosch'),
|
||||
];
|
||||
|
||||
const lookupDatasheetSpec = createModelSpecLookup(RECORDS);
|
||||
|
||||
/**
|
||||
* Datasheet record for a mapped ALPR camera's `model` tag. A `manufacturer`
|
||||
* naming another maker rejects the match, so a "Flock Safety" camera tagged
|
||||
* "L6Q" stays unresolved rather than borrowing Motorola's specs.
|
||||
* @param {string|null} model - The OSM `model` tag.
|
||||
* @param {string|null} [manufacturer] - The OSM `manufacturer` tag.
|
||||
* @returns {Object|null} The spec record, or null when unknown.
|
||||
*/
|
||||
export function lookupAlprModelSpec(model, manufacturer) {
|
||||
// Adapter records arrive unnormalized and the matcher's word trim is
|
||||
// quadratic on long punctuation runs: match only tag-sized strings (the
|
||||
// built-in sources keep 180 characters).
|
||||
if (typeof model !== 'string' || model.length > 180) return null;
|
||||
if (
|
||||
manufacturer != null &&
|
||||
(typeof manufacturer !== 'string' || manufacturer.length > 180)
|
||||
)
|
||||
return null;
|
||||
return lookupDatasheetSpec(model, manufacturer);
|
||||
}
|
||||
@@ -0,0 +1,157 @@
|
||||
// src/layers/alpr/modelSpecs.test.mjs
|
||||
// Mapped ALPR model tags resolve to vendored datasheets, and never guess.
|
||||
import { test } from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { PbfWriter } from 'pbf';
|
||||
|
||||
import { lookupAlprModelSpec } from './modelSpecs.js';
|
||||
import { decodeAlprTile } from './tileRecords.js';
|
||||
import { alprLabelDetails } from './visuals.js';
|
||||
|
||||
const OSM = { attribution: { name: 'OpenStreetMap' } };
|
||||
|
||||
test('the model tags mappers write resolve to their datasheet record', () => {
|
||||
// [model tag, manufacturer tag, record id] as they appear on OSM nodes.
|
||||
for (const [model, manufacturer, id] of [
|
||||
['L6Q', 'Motorola Solutions', 'motorola-solutions-l6q'],
|
||||
['L5F', 'Motorola Solutions', 'motorola-solutions-l5f'],
|
||||
['L6D', 'Motorola Solutions', 'motorola-solutions-l6d'],
|
||||
['L6Q', null, 'motorola-solutions-l6q'],
|
||||
['AutoVu SharpV G3', 'Genetec', 'genetec-autovu-sharpv'],
|
||||
['AU-V-WS850-LC-G3-000', 'Genetec', 'genetec-autovu-sharpv'],
|
||||
['AutoVu SharpV G2', 'Genetec', 'genetec-autovu-sharpv-g2'],
|
||||
['AutoVu SharpV G1', 'Genetec', 'genetec-autovu-sharpv-g2'],
|
||||
['AutoVu SharpX XGA', 'Genetec', 'genetec-autovu-sharpx'],
|
||||
['Q1700-LE', 'Axis Communications', 'axis-q1700-le'],
|
||||
['Q1800-LE', 'Axis Communications', 'axis-q1800-le'],
|
||||
['CP63-E', 'Verkada Inc.', 'verkada-cp63-e'],
|
||||
['CB62-TE-F', 'Verkada Inc.', 'verkada-cb62-te-f'],
|
||||
['CH52-E-F', 'Verkada Inc.', 'verkada-ch52-e-f'],
|
||||
['CR63-E', 'Verkada', 'verkada-cr63-e'],
|
||||
['MIC-7522-Z30B', 'The Bosch Group', 'bosch-mic-7522-z30b'],
|
||||
])
|
||||
assert.equal(
|
||||
lookupAlprModelSpec(model, manufacturer)?.id,
|
||||
id,
|
||||
`${manufacturer} ${model}`,
|
||||
);
|
||||
});
|
||||
|
||||
test('unknown, mis-attributed or partial models stay unresolved', () => {
|
||||
for (const [model, manufacturer] of [
|
||||
['Falcon', 'Flock Safety'],
|
||||
// A Motorola code under another maker is a mapping error, not a match.
|
||||
['L6Q', 'Flock Safety'],
|
||||
// "AutoVu Sharp" is an older line than SharpV; "Q1800" names no variant.
|
||||
['AutoVu Sharp', 'Genetec'],
|
||||
['Q1800', 'Axis Communications'],
|
||||
// Without a maker, a code-less product name is never trusted.
|
||||
['AutoVu SharpV', null],
|
||||
// A SharpV without its generation could be G1, G2 or G3, and a SharpX
|
||||
// without XGA could be the VGA model.
|
||||
['AutoVu SharpV', 'Genetec'],
|
||||
['AutoVu SharpV HD', 'Genetec'],
|
||||
['Genetec AutoVu SharpV', 'Genetec'],
|
||||
['AutoVu SharpX', 'Genetec'],
|
||||
['UnicamVELOCITY3', null],
|
||||
[null, 'Motorola Solutions'],
|
||||
// Adapter text is matched only as a string of tag size.
|
||||
[['L6Q'], 'Motorola Solutions'],
|
||||
['L6Q' + ' IP'.repeat(60), 'Motorola Solutions'],
|
||||
])
|
||||
assert.equal(lookupAlprModelSpec(model, manufacturer), null);
|
||||
});
|
||||
|
||||
/** A one-point `cameras` vector tile (z0) with string attributes. */
|
||||
function cameraTile(properties) {
|
||||
const tile = new PbfWriter();
|
||||
tile.writeMessage(3, (_, layer) => {
|
||||
layer.writeVarintField(15, 2);
|
||||
layer.writeStringField(1, 'cameras');
|
||||
layer.writeVarintField(5, 4096);
|
||||
const keys = Object.keys(properties);
|
||||
layer.writeMessage(2, (_, feature) => {
|
||||
feature.writePackedVarint(
|
||||
2,
|
||||
keys.flatMap((_, i) => [i, i]),
|
||||
);
|
||||
feature.writeVarintField(3, 1); // Point
|
||||
feature.writePackedVarint(4, [9, 4096, 4096]); // MoveTo(2048, 2048)
|
||||
});
|
||||
for (const key of keys) layer.writeStringField(3, key);
|
||||
for (const key of keys)
|
||||
layer.writeMessage(4, (_, value) =>
|
||||
value.writeStringField(1, String(properties[key])),
|
||||
);
|
||||
});
|
||||
return tile.finish();
|
||||
}
|
||||
|
||||
test('extract tiles carry the model tag to the card', () => {
|
||||
const [record] = decodeAlprTile(
|
||||
cameraTile({
|
||||
osmType: 'node',
|
||||
osmId: '7',
|
||||
brand: 'Motorola Solutions',
|
||||
model: 'L6Q',
|
||||
}),
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
);
|
||||
assert.equal(record.model, 'L6Q');
|
||||
assert.ok(
|
||||
alprLabelDetails(record, OSM).includes('SPEC VIGILANT L6Q · 1080P · IR'),
|
||||
);
|
||||
});
|
||||
|
||||
test('the card adds the datasheet line only for a resolved model', () => {
|
||||
const details = alprLabelDetails(
|
||||
{ manufacturer: 'Motorola Solutions', model: 'L6Q' },
|
||||
OSM,
|
||||
);
|
||||
assert.deepEqual(details, [
|
||||
'OSM MAPPED',
|
||||
'MOTOROLA SOLUTIONS',
|
||||
'SPEC VIGILANT L6Q · 1080P · IR',
|
||||
'PUBLIC MAP DATA',
|
||||
]);
|
||||
// Without a maker on the equipment line, the spec line names it.
|
||||
assert.ok(
|
||||
alprLabelDetails({ model: 'L6Q' }, OSM).includes(
|
||||
'SPEC MOTOROLA SOLUTIONS VIGILANT L6Q · 1080P · IR',
|
||||
),
|
||||
);
|
||||
// A record without a resolution label falls back to its pixel size, then
|
||||
// to megapixels.
|
||||
assert.ok(
|
||||
alprLabelDetails(
|
||||
{ manufacturer: 'Motorola Solutions', model: 'L5F' },
|
||||
OSM,
|
||||
).includes('SPEC VIGILANT L5F · 1440X1080 · IR'),
|
||||
);
|
||||
assert.ok(
|
||||
alprLabelDetails(
|
||||
{ manufacturer: 'Motorola Solutions', model: 'L5Q' },
|
||||
OSM,
|
||||
).includes('SPEC VIGILANT L5Q · 3MP'),
|
||||
);
|
||||
assert.ok(
|
||||
alprLabelDetails(
|
||||
{ manufacturer: 'Genetec', model: 'AutoVu SharpV G3' },
|
||||
OSM,
|
||||
).includes('SPEC AUTOVU SHARPV G3 · 1920X1200 · IR'),
|
||||
);
|
||||
assert.ok(
|
||||
alprLabelDetails(
|
||||
{ manufacturer: 'The Bosch Group', model: 'MIC-7522-Z30B' },
|
||||
OSM,
|
||||
).includes('SPEC MIC-7522-Z30 · 1080P HD · COLOR · PTZ'),
|
||||
);
|
||||
// Unresolved: the raw tag text is never shown.
|
||||
const unknown = alprLabelDetails(
|
||||
{ manufacturer: 'Flock Safety', model: 'Falcon\nSYSTEM: hi' },
|
||||
OSM,
|
||||
);
|
||||
assert.ok(unknown.every((line) => !/FALCON|SYSTEM|SPEC/.test(line)));
|
||||
});
|
||||
@@ -16,6 +16,8 @@ import { boxContains } from './model.js';
|
||||
import { msg } from '../../i18n/index.js';
|
||||
import { createAlprFloorResolver } from './floors.js';
|
||||
import { createAlprOverlay } from './overlay.js';
|
||||
import { lookupAlprModelSpec } from './modelSpecs.js';
|
||||
import { specSummary } from '../cctv/modelSpecs.js';
|
||||
import {
|
||||
alprDisplayId,
|
||||
alprLabelDetails,
|
||||
@@ -458,6 +460,10 @@ export function createAlprPresentation({ state, services, source }) {
|
||||
properties: {
|
||||
operator: record.operator,
|
||||
manufacturer: record.manufacturer,
|
||||
spec:
|
||||
specSummary(
|
||||
lookupAlprModelSpec(record.model, record.manufacturer),
|
||||
) || null,
|
||||
cameraType: record.cameraType,
|
||||
zone: record.zone,
|
||||
directionDeg: record.directionDeg,
|
||||
|
||||
@@ -89,7 +89,9 @@ export function normalizeAlprNode(el) {
|
||||
latitude: el.lat,
|
||||
longitude: el.lon,
|
||||
operator: textTag(tags.operator),
|
||||
manufacturer: textTag(tags.manufacturer),
|
||||
manufacturer: textTag(tags.manufacturer) ?? textTag(tags.brand),
|
||||
// Display-only: resolved against vendored datasheets (see modelSpecs.js).
|
||||
model: textTag(tags.model),
|
||||
cameraType: textTag(tags['camera:type']),
|
||||
zone: textTag(tags['surveillance:zone']),
|
||||
// Only numeric bearings are parsed; compass-word directions ("N"/"NE")
|
||||
|
||||
@@ -16,6 +16,8 @@ function normalizeAlprTileFeature(feature) {
|
||||
'surveillance:type': 'ALPR',
|
||||
operator: p.operator,
|
||||
manufacturer: p.brand,
|
||||
// Not in the hourly extract yet; read when it ships the OSM tag.
|
||||
model: p.model,
|
||||
'camera:type': p.cameraType,
|
||||
'camera:direction': p.direction,
|
||||
'surveillance:zone': p.surveillanceZone,
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
import { fromDegrees } from '../../geo/index.js';
|
||||
import { destinationPointDeg } from './model.js';
|
||||
import { lookupAlprModelSpec } from './modelSpecs.js';
|
||||
import { specSummary } from '../cctv/modelSpecs.js';
|
||||
import {
|
||||
DIRECTION_CONE_M,
|
||||
DIRECTION_CONE_HALF_ANGLE_DEG,
|
||||
@@ -64,6 +66,15 @@ export function alprLabelDetails(record, source) {
|
||||
),
|
||||
];
|
||||
if (equipment.length) details.push(equipment.join(' · '));
|
||||
// Only a datasheet match is shown: the raw OSM `model` text never is.
|
||||
const spec = lookupAlprModelSpec(record.model, record.manufacturer);
|
||||
if (spec) {
|
||||
// The equipment line usually names the maker already; the card does not wrap.
|
||||
const withBrand = !equipment.some((line) =>
|
||||
line.includes(spec.brand.toUpperCase()),
|
||||
);
|
||||
details.push(`SPEC ${specSummary(spec, { withBrand }).toUpperCase()}`);
|
||||
}
|
||||
if (sourceName === 'OpenStreetMap') details.push('PUBLIC MAP DATA');
|
||||
return details;
|
||||
}
|
||||
|
||||
@@ -456,33 +456,60 @@ const BRAND_ALIASES = {
|
||||
Hikvision: ['hiwatch'],
|
||||
};
|
||||
|
||||
function brandWords(record) {
|
||||
return new Set([
|
||||
...words(record.brand),
|
||||
...(BRAND_ALIASES[record.brand] || []),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* One entry per record name (model + aliases). `required` is every
|
||||
* non-generic word of the name — all must appear in a query, so a generation
|
||||
* or suffix ("Mk II", "7000i" vs "7000") is never dropped. `hasCode` marks
|
||||
* names carrying a letter+digit model code; names without one ("3950",
|
||||
* "01959-004", "AUTODOME 800 Series") match only when the brand does.
|
||||
*/
|
||||
const NAME_INDEX = RECORDS.flatMap((record) => {
|
||||
const brand = brandWords(record);
|
||||
return [record.model, ...(record.aliases || [])].map((name) => {
|
||||
const nameWords = words(name);
|
||||
return {
|
||||
record,
|
||||
brand,
|
||||
allowed: new Set(nameWords),
|
||||
required: nameWords.filter((word) => !isGeneric(word)),
|
||||
hasCode: nameWords.some(isModelCode),
|
||||
};
|
||||
/** A model lookup over `records` with this module's matching rules (see lookupModelSpec). */
|
||||
export function createModelSpecLookup(records) {
|
||||
/**
|
||||
* One entry per record name (model + aliases). `required` is every
|
||||
* non-generic word of the name — all must appear in a query, so a
|
||||
* generation or suffix ("Mk II", "7000i" vs "7000") is never dropped.
|
||||
* `hasCode` marks names carrying a letter+digit model code; names without
|
||||
* one ("3950", "01959-004", "AUTODOME 800 Series") match only when the
|
||||
* brand does.
|
||||
*/
|
||||
const nameIndex = records.flatMap((record) => {
|
||||
const brand = new Set([
|
||||
...words(record.brand),
|
||||
...(BRAND_ALIASES[record.brand] || []),
|
||||
]);
|
||||
return [record.model, ...(record.aliases || [])].map((name) => {
|
||||
const nameWords = words(name);
|
||||
return {
|
||||
record,
|
||||
brand,
|
||||
allowed: new Set(nameWords),
|
||||
required: nameWords.filter((word) => !isGeneric(word)),
|
||||
hasCode: nameWords.some(isModelCode),
|
||||
};
|
||||
});
|
||||
});
|
||||
});
|
||||
return (model, manufacturer) => {
|
||||
const query = words(model).map(stripVariantSuffix);
|
||||
if (!query.length) return null;
|
||||
const maker = words(manufacturer);
|
||||
const found = new Set();
|
||||
for (const entry of nameIndex) {
|
||||
const makerMatches = maker.some((word) => entry.brand.has(word));
|
||||
if (maker.length && !makerMatches) continue;
|
||||
if (
|
||||
!entry.hasCode &&
|
||||
!makerMatches &&
|
||||
!query.some((word) => entry.brand.has(word))
|
||||
)
|
||||
continue;
|
||||
if (!entry.required.length) continue;
|
||||
if (!entry.required.every((word) => query.includes(word))) continue;
|
||||
if (
|
||||
!query.every(
|
||||
(word) =>
|
||||
entry.allowed.has(word) || entry.brand.has(word) || isGeneric(word),
|
||||
)
|
||||
)
|
||||
continue;
|
||||
found.add(entry.record);
|
||||
}
|
||||
return found.size === 1 ? [...found][0] : null;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Looks up a spec record for a hardware-model string.
|
||||
@@ -502,33 +529,7 @@ const NAME_INDEX = RECORDS.flatMap((record) => {
|
||||
* @param {string} [manufacturer] - The source's manufacturer, when published.
|
||||
* @returns {Object|null} The matched spec record, or null when unknown.
|
||||
*/
|
||||
export function lookupModelSpec(model, manufacturer) {
|
||||
const query = words(model).map(stripVariantSuffix);
|
||||
if (!query.length) return null;
|
||||
const maker = words(manufacturer);
|
||||
const found = new Set();
|
||||
for (const entry of NAME_INDEX) {
|
||||
const makerMatches = maker.some((word) => entry.brand.has(word));
|
||||
if (maker.length && !makerMatches) continue;
|
||||
if (
|
||||
!entry.hasCode &&
|
||||
!makerMatches &&
|
||||
!query.some((word) => entry.brand.has(word))
|
||||
)
|
||||
continue;
|
||||
if (!entry.required.length) continue;
|
||||
if (!entry.required.every((word) => query.includes(word))) continue;
|
||||
if (
|
||||
!query.every(
|
||||
(word) =>
|
||||
entry.allowed.has(word) || entry.brand.has(word) || isGeneric(word),
|
||||
)
|
||||
)
|
||||
continue;
|
||||
found.add(entry.record);
|
||||
}
|
||||
return found.size === 1 ? [...found][0] : null;
|
||||
}
|
||||
export const lookupModelSpec = createModelSpecLookup(RECORDS);
|
||||
|
||||
/**
|
||||
* Datasheet horizontal FOV as the range the hardware can cover. Only
|
||||
@@ -602,13 +603,19 @@ export function specFovCapabilityToken(
|
||||
|
||||
/**
|
||||
* One-line spec summary for HUD meta text.
|
||||
* @param {Object} spec - A record from this module.
|
||||
* @param {Object} spec - A spec record (CCTV or ALPR). Resolution falls back
|
||||
* from label to WxH to NMP.
|
||||
* @param {{withBrand?: boolean}} [options] - `withBrand: false` leaves the
|
||||
* brand out.
|
||||
* @returns {string} e.g. 'Axis Q6135-LE · 1080p · IR 250m · PTZ' ('' for null).
|
||||
*/
|
||||
export function specSummary(spec) {
|
||||
export function specSummary(spec, { withBrand = true } = {}) {
|
||||
if (!spec) return '';
|
||||
const parts = [`${spec.brand} ${spec.model}`];
|
||||
if (spec.resolution?.label) parts.push(spec.resolution.label);
|
||||
const parts = [withBrand ? `${spec.brand} ${spec.model}` : spec.model];
|
||||
const { label, w, h, mp } = spec.resolution || {};
|
||||
if (label) parts.push(label);
|
||||
else if (w && h) parts.push(`${w}x${h}`);
|
||||
else if (mp) parts.push(`${mp}MP`);
|
||||
if (spec.night_vision) {
|
||||
parts.push(
|
||||
`${spec.night_vision.type.toUpperCase()}${spec.night_vision.range_m ? ` ${spec.night_vision.range_m}m` : ''}`,
|
||||
|
||||
@@ -4,8 +4,14 @@ import {
|
||||
wrapDegrees,
|
||||
} from './panorama.js';
|
||||
import { imageSize } from './imageSize.js';
|
||||
import { nextViewKey } from './photoViewers.js';
|
||||
import { msg, t } from '../../i18n/index.js';
|
||||
|
||||
// Each mount's and each canvas's key comes from `nextViewKey()`, 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).
|
||||
|
||||
/**
|
||||
* EXPAND puts the viewer's wrap full screen, or (iPhone) opens it as a
|
||||
* popover. Each is tried on its own: a browser that cannot parse one
|
||||
@@ -36,14 +42,6 @@ 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]);
|
||||
@@ -159,6 +157,16 @@ async function loadBitmap(url, timeoutMs, resize, failure, signal) {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Unmounted, superseded or left without a view before the view drew what
|
||||
* was waiting on it (`undrawn`): a mount or an open that fails as closed.
|
||||
*/
|
||||
function undrawn() {
|
||||
return Object.assign(new Error('Street-level viewer was closed'), {
|
||||
undrawn: true,
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* A picture refused for its size (`tooLarge`): the viewer will not load
|
||||
* that file another way, an <img> having no size limit at all.
|
||||
@@ -228,13 +236,20 @@ export function panoSizeFor(canvasPixels, fovDeg) {
|
||||
* 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. 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.
|
||||
* element back through `actions.attach(name, element)` on every draw: the
|
||||
* panorama canvas to draw on, the frame and picture to measure, the root
|
||||
* and buttons to keep focus. A view may draw at once, inside its listener,
|
||||
* or later (Vue draws on a microtask). 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, and a mount or
|
||||
* a panorama's open waiting on a draw when the last view leaves fails as
|
||||
* closed (the mount unmounts; the open leaves no photo shown). A view
|
||||
* handing back another canvas element than the renderer's (one subscribed
|
||||
* since) gets the panorama drawn again on it, as after a lost context;
|
||||
* while several views draw, a canvas the panorama moved off is not moved
|
||||
* back to, and once one view is left its canvas is taken back.
|
||||
*
|
||||
* Photos are normalized by the provider: `{id, sequenceId, lon, lat,
|
||||
* heading, isPano, capturedAt}` plus whatever its `sources` read.
|
||||
@@ -284,6 +299,17 @@ export function createPhotoViewer({
|
||||
/** 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();
|
||||
/** How many times the view has handed the canvas back (once per draw). */
|
||||
let canvasDraws = 0;
|
||||
/**
|
||||
* Canvases the panorama moved off while more than one view draws: one
|
||||
* handed back again (a second view drawing its own) is not moved back to,
|
||||
* so two views cannot take turns. Forgotten once a single view is left,
|
||||
* so its canvas is taken back.
|
||||
*/
|
||||
let retiredCanvases = new WeakSet();
|
||||
/** The canvas moved while an open waited on its draw. */
|
||||
let canvasMoved = false;
|
||||
let renderer = null;
|
||||
/** WebGL failed once: 360° photos show their front view instead. */
|
||||
let rendererFailed = false;
|
||||
@@ -396,22 +422,28 @@ export function createPhotoViewer({
|
||||
waiters.clear();
|
||||
}
|
||||
|
||||
/** The panorama canvas once the view has drawn it, or the open stopped. */
|
||||
function canvasReady(signal) {
|
||||
if (canvas || !presented || signal?.aborted) return Promise.resolve();
|
||||
/** A draw of the canvas, or none coming: unmounted, no view, or the open stopped. */
|
||||
function canvasDrawn(drawn, signal) {
|
||||
if (drawn() || !presented || !viewListeners.size || signal?.aborted)
|
||||
return Promise.resolve();
|
||||
return new Promise((resolve) => {
|
||||
canvasWaiters.add(resolve);
|
||||
signal?.addEventListener?.('abort', resolve, { once: true });
|
||||
});
|
||||
}
|
||||
|
||||
/** 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 });
|
||||
});
|
||||
/** The panorama canvas once the view has drawn it, or the open stopped. */
|
||||
function canvasReady(signal) {
|
||||
return canvasDrawn(() => Boolean(canvas), signal);
|
||||
}
|
||||
|
||||
/**
|
||||
* A draw of the canvas after the `count`th (it hands the canvas back each
|
||||
* time), or none coming. A view may draw synchronously, inside the
|
||||
* presentation, before anything waits: counted, that draw is not missed.
|
||||
*/
|
||||
function drawnAfter(count, signal) {
|
||||
return canvasDrawn(() => canvasDraws > count, signal);
|
||||
}
|
||||
|
||||
function syncButtons() {
|
||||
@@ -469,6 +501,30 @@ export function createPhotoViewer({
|
||||
const from = event?.currentTarget ?? event?.target;
|
||||
if (from && from !== canvas) return;
|
||||
event.preventDefault?.();
|
||||
dropRenderer();
|
||||
// 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: nextViewKey() });
|
||||
reopen();
|
||||
}
|
||||
|
||||
/**
|
||||
* The view drew the panorama on another canvas element than the
|
||||
* renderer's (a view that subscribed since): the renderer cannot follow
|
||||
* it, so the photo is drawn again on a new one, as after a lost context.
|
||||
*/
|
||||
function moveCanvas(element) {
|
||||
dropRenderer();
|
||||
retiredCanvases.add(canvas);
|
||||
canvas = element;
|
||||
// An open waiting on the draw goes on, on the new canvas.
|
||||
if (openingId !== null) canvasMoved = true;
|
||||
else reopen();
|
||||
}
|
||||
|
||||
/** Let the renderer go, keeping the user's look for the photo's reopen. */
|
||||
function dropRenderer() {
|
||||
const lost = renderer;
|
||||
renderer = null;
|
||||
// The new renderer starts facing the photo's heading: keep the user's look.
|
||||
@@ -479,11 +535,10 @@ export function createPhotoViewer({
|
||||
} catch {
|
||||
/* already gone */
|
||||
}
|
||||
// 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: ++viewKeys });
|
||||
// Show the photo again through the core, unless a newer one is opening.
|
||||
}
|
||||
|
||||
/** Show the panorama again through the core, unless a newer photo is opening. */
|
||||
function reopen() {
|
||||
if (showing === 'pano' && photo && openingId === null) {
|
||||
const again = photo.id;
|
||||
show(null);
|
||||
@@ -665,9 +720,23 @@ export function createPhotoViewer({
|
||||
const actions = {
|
||||
attach(name, element) {
|
||||
if (presented?.actions !== actions || !element) return;
|
||||
ELEMENTS[name]?.(element);
|
||||
const retired = name === 'canvas' && retiredCanvases.has(element);
|
||||
if (retired) {
|
||||
// Another view still draws the panorama: this draw is counted, so
|
||||
// an open waiting on it goes on, but the panorama stays put.
|
||||
} else if (
|
||||
name === 'canvas' &&
|
||||
renderer &&
|
||||
canvas &&
|
||||
element !== canvas
|
||||
)
|
||||
moveCanvas(element);
|
||||
else ELEMENTS[name]?.(element);
|
||||
if (name === 'root') wake(rootWaiters);
|
||||
if (name === 'canvas') wake(canvasWaiters);
|
||||
if (name === 'canvas') {
|
||||
canvasDraws++;
|
||||
wake(canvasWaiters);
|
||||
}
|
||||
},
|
||||
keydown: current(onKeyDown),
|
||||
pointerdown: current(onPointerDown),
|
||||
@@ -683,7 +752,7 @@ export function createPhotoViewer({
|
||||
};
|
||||
presented = {
|
||||
host,
|
||||
mountKey: ++viewKeys,
|
||||
mountKey: nextViewKey(),
|
||||
providerId,
|
||||
label: t(
|
||||
'{name} photo. ← and → step along the sequence. On a 360° photo, drag or use Shift+← →, ↑ ↓ to look around, and + − to zoom',
|
||||
@@ -693,7 +762,7 @@ export function createPhotoViewer({
|
||||
kind: undefined,
|
||||
src: null,
|
||||
fit: renderMode === 'fill' ? 'cover' : 'contain',
|
||||
canvasKey: ++viewKeys,
|
||||
canvasKey: nextViewKey(),
|
||||
dragging: false,
|
||||
prevLabel: t('Previous photo in this sequence (←)'),
|
||||
prevDisabled: true,
|
||||
@@ -865,9 +934,27 @@ export function createPhotoViewer({
|
||||
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. The
|
||||
// view draws it shown on a microtask, and hands the canvas back then.
|
||||
// view draws it shown, at once or on a microtask, and hands the canvas
|
||||
// back then.
|
||||
const draws = canvasDraws;
|
||||
canvasMoved = false;
|
||||
show('pano');
|
||||
await nextDraw(signal);
|
||||
await drawnAfter(draws, signal);
|
||||
// The view drew it on another canvas: the panorama goes there.
|
||||
if (canvasMoved && seq === openSeq && !renderer && ensureRenderer()) {
|
||||
const moved = restoreView?.id === target.id ? restoreView.view : null;
|
||||
restoreView = null;
|
||||
if (moved) renderer.setView(moved);
|
||||
}
|
||||
canvasMoved = false;
|
||||
// The last view left before drawing it: the canvas was never shown, and
|
||||
// nothing is drawn until a view is back.
|
||||
if (seq === openSeq && canvasDraws === draws && !viewListeners.size) {
|
||||
image.close?.();
|
||||
photo = null;
|
||||
show(null);
|
||||
throw undrawn();
|
||||
}
|
||||
if (seq !== openSeq || !renderer) {
|
||||
image.close?.();
|
||||
if (seq !== openSeq) return false;
|
||||
@@ -916,7 +1003,8 @@ export function createPhotoViewer({
|
||||
if (sources.hasPano(target) && ensureRenderer()) {
|
||||
try {
|
||||
shown = await showPano(target, seq, keepView, signal);
|
||||
} catch {
|
||||
} catch (error) {
|
||||
if (error?.undrawn) throw error;
|
||||
// The panorama failed: its front view is better than nothing.
|
||||
if (seq !== openSeq) return;
|
||||
shown = await showFlat(target, seq, signal);
|
||||
@@ -937,12 +1025,31 @@ export function createPhotoViewer({
|
||||
rendererFailed = false;
|
||||
if (!viewListeners.size)
|
||||
throw new Error('Street Level photo viewer has no view to draw it');
|
||||
// Resolves once the view drew the viewer, or it was unmounted meanwhile.
|
||||
const drawn = new Promise((resolve) => rootWaiters.add(resolve));
|
||||
// Resolves once the view drew the viewer, or it was unmounted, or its
|
||||
// last view left, meanwhile.
|
||||
const waitRoot = () => new Promise((resolve) => rootWaiters.add(resolve));
|
||||
let drawn = waitRoot();
|
||||
build(host);
|
||||
if (!root) await drawn;
|
||||
// Unmounted or superseded before the view drew it.
|
||||
if (!root) throw new Error('Street-level viewer was closed');
|
||||
// This mount's presentation: a later mount presents its own. A view
|
||||
// may have unmounted it already, inside its first draw.
|
||||
const mine = presented?.actions;
|
||||
if (!mine) throw undrawn();
|
||||
for (;;) {
|
||||
if (!root) await drawn;
|
||||
const current = presented?.actions === mine;
|
||||
if (root && current) return;
|
||||
// Unmounted, superseded or mounted again: that is not this mount.
|
||||
if (!current) throw undrawn();
|
||||
// Its last view left, and another subscribed since: it draws.
|
||||
if (viewListeners.size) {
|
||||
drawn = waitRoot();
|
||||
continue;
|
||||
}
|
||||
// No view will draw it. Unmounted, as when superseded, so a view
|
||||
// subscribing later draws nothing.
|
||||
adapter.unmount();
|
||||
throw undrawn();
|
||||
}
|
||||
},
|
||||
|
||||
/** Resolves once the photo is on screen and its pose was emitted. */
|
||||
@@ -1029,7 +1136,17 @@ export function createPhotoViewer({
|
||||
subscribeView(listener) {
|
||||
viewListeners.add(listener);
|
||||
listener(presented);
|
||||
return () => viewListeners.delete(listener);
|
||||
return () => {
|
||||
// The last view went: a mount or an open waiting on its draw fails
|
||||
// as closed.
|
||||
if (!viewListeners.delete(listener)) return;
|
||||
// One view left: the canvases moved off are its to hand back again.
|
||||
if (viewListeners.size <= 1) retiredCanvases = new WeakSet();
|
||||
if (!viewListeners.size) {
|
||||
wake(rootWaiters);
|
||||
wake(canvasWaiters);
|
||||
}
|
||||
};
|
||||
},
|
||||
};
|
||||
return adapter;
|
||||
|
||||
@@ -1,7 +1,14 @@
|
||||
import assert from 'node:assert/strict';
|
||||
import test from 'node:test';
|
||||
import { createPhotoViewer } from './photoViewer.js';
|
||||
import { fakeHost, until } from '../../testSupport/kartaViewFakes.mjs';
|
||||
import {
|
||||
fakeHost,
|
||||
until,
|
||||
resetPhotoView,
|
||||
} from '../../testSupport/kartaViewFakes.mjs';
|
||||
|
||||
// Each test draws its own viewers only.
|
||||
test.beforeEach(resetPhotoView);
|
||||
|
||||
const PHOTOS = {
|
||||
a: { id: 'a', sequenceId: 's', lon: 1, lat: 2, heading: 0, isPano: true },
|
||||
@@ -636,3 +643,356 @@ test('a panorama reopened after a lost context waits for the view’s fresh canv
|
||||
);
|
||||
assert.equal(shown.kind, 'pano', 'not the front view');
|
||||
});
|
||||
|
||||
/**
|
||||
* A panorama viewer whose sequence goes on both ways (its ‹ › present no
|
||||
* change once shown), drawn by `view` alone, not by the shared fake view.
|
||||
*/
|
||||
function panoramaViewer() {
|
||||
const images = [];
|
||||
const viewer = createPhotoViewer({
|
||||
providerId: 'test',
|
||||
name: 'Test',
|
||||
sources: SOURCES,
|
||||
getPhoto: async (id) => PHOTOS[id],
|
||||
step: async () => 'b',
|
||||
onStep() {},
|
||||
load: async (url) => ({ src: url }),
|
||||
createRenderer: () => ({
|
||||
setImage: (image) => images.push(image.src),
|
||||
setView() {},
|
||||
getView: () => ({ yaw: 0, pitch: 0, fov: 80 }),
|
||||
resize() {},
|
||||
clear() {},
|
||||
destroy() {},
|
||||
}),
|
||||
});
|
||||
return { viewer, images };
|
||||
}
|
||||
|
||||
/** Elements a view hands back: what the viewer measures and focuses. */
|
||||
const elements = () =>
|
||||
Object.fromEntries(
|
||||
['root', 'picture', 'canvas', 'prev', 'next'].map((name) => [
|
||||
name,
|
||||
{ clientWidth: 400, clientHeight: 250 },
|
||||
]),
|
||||
);
|
||||
|
||||
/** Whether `promise` settles within a few macrotasks. */
|
||||
const settles = (promise) =>
|
||||
Promise.race([
|
||||
promise.then(
|
||||
() => true,
|
||||
() => true,
|
||||
),
|
||||
new Promise((resolve) => setTimeout(() => resolve(false), 50)),
|
||||
]);
|
||||
|
||||
test('a view that draws at once, inside its listener, opens a panorama', async () => {
|
||||
const { viewer, images } = panoramaViewer();
|
||||
const parts = elements();
|
||||
const unsubscribe = viewer.subscribeView((view) => {
|
||||
if (!view) return;
|
||||
for (const [name, element] of Object.entries(parts))
|
||||
view.actions.attach(name, element);
|
||||
});
|
||||
await viewer.mount(fakeHost());
|
||||
assert.equal(await settles(viewer.open('a')), true, 'not left pending');
|
||||
assert.deepEqual(images, ['a-first']);
|
||||
viewer.unmount();
|
||||
unsubscribe();
|
||||
});
|
||||
|
||||
test('a panorama’s open waiting on the view’s draw fails as closed when the view leaves', async () => {
|
||||
const { viewer, images } = panoramaViewer();
|
||||
const parts = elements();
|
||||
const kinds = [];
|
||||
const presented = [];
|
||||
// Draws the mount, then nothing more.
|
||||
const unsubscribe = viewer.subscribeView((view) => {
|
||||
presented.push(view);
|
||||
if (!view) return;
|
||||
kinds.push(view.kind);
|
||||
if (kinds.length > 1) return;
|
||||
for (const [name, element] of Object.entries(parts))
|
||||
view.actions.attach(name, element);
|
||||
});
|
||||
await viewer.mount(fakeHost());
|
||||
const opening = viewer.open('a');
|
||||
assert.ok(await until(() => kinds.includes('pano')), 'waiting on a draw');
|
||||
unsubscribe();
|
||||
assert.equal(await settles(opening), true, 'not left pending');
|
||||
await assert.rejects(opening, /viewer was closed/);
|
||||
assert.deepEqual(images, [], 'nothing uploaded to a canvas never shown');
|
||||
// A view back draws the viewer mounted, with no photo shown.
|
||||
let again = null;
|
||||
const resubscribe = viewer.subscribeView((view) => (again = view));
|
||||
assert.ok(again, 'still mounted');
|
||||
assert.equal(again.kind, '');
|
||||
resubscribe();
|
||||
viewer.unmount();
|
||||
});
|
||||
|
||||
test('a mount waiting on the view’s draw fails as closed when the view leaves, and is unmounted', async () => {
|
||||
const { viewer } = panoramaViewer();
|
||||
const unsubscribe = viewer.subscribeView(() => {});
|
||||
const mounting = viewer.mount(fakeHost());
|
||||
unsubscribe();
|
||||
assert.equal(await settles(mounting), true, 'not left pending');
|
||||
await assert.rejects(mounting, /viewer was closed/);
|
||||
let current;
|
||||
const resubscribe = viewer.subscribeView((view) => (current = view));
|
||||
assert.equal(current, null, 'nothing presented');
|
||||
await assert.rejects(viewer.open('a'), /not mounted/);
|
||||
resubscribe();
|
||||
});
|
||||
|
||||
test('a view handing back another canvas gets the panorama drawn again on it', async () => {
|
||||
const renderers = [];
|
||||
const reopened = [];
|
||||
const viewer = createPhotoViewer({
|
||||
providerId: 'test',
|
||||
name: 'Test',
|
||||
sources: SOURCES,
|
||||
getPhoto: async (id) => PHOTOS[id],
|
||||
step: async () => 'b',
|
||||
onStep() {},
|
||||
onReopen: (id) => {
|
||||
reopened.push(id);
|
||||
viewer.open(id);
|
||||
},
|
||||
load: async (url) => ({ src: url }),
|
||||
createRenderer: (canvas) => {
|
||||
const renderer = {
|
||||
canvas,
|
||||
images: [],
|
||||
destroyed: false,
|
||||
setImage: (image) => renderer.images.push(image.src),
|
||||
setView() {},
|
||||
getView: () => ({ yaw: 10, pitch: 0, fov: 80 }),
|
||||
resize() {},
|
||||
clear() {},
|
||||
destroy: () => (renderer.destroyed = true),
|
||||
};
|
||||
renderers.push(renderer);
|
||||
return renderer;
|
||||
},
|
||||
});
|
||||
const drawWith = (parts) => (view) => {
|
||||
if (!view) return;
|
||||
for (const [name, element] of Object.entries(parts))
|
||||
view.actions.attach(name, element);
|
||||
};
|
||||
const first = elements();
|
||||
const unsubscribe = viewer.subscribeView(drawWith(first));
|
||||
await viewer.mount(fakeHost());
|
||||
await viewer.open('a');
|
||||
assert.equal(renderers[0].canvas, first.canvas);
|
||||
// Another view takes over, with elements of its own.
|
||||
const second = elements();
|
||||
const resubscribe = viewer.subscribeView(drawWith(second));
|
||||
unsubscribe();
|
||||
assert.ok(await until(() => renderers[1]?.images.length > 0));
|
||||
assert.deepEqual(reopened, ['a']);
|
||||
assert.equal(renderers[0].destroyed, true);
|
||||
assert.equal(renderers[1].canvas, second.canvas);
|
||||
assert.deepEqual(renderers[1].images, ['a-first']);
|
||||
viewer.unmount();
|
||||
resubscribe();
|
||||
});
|
||||
|
||||
test('the shared fake view draws only the later of two mounts, as the app does', async () => {
|
||||
const one = setup();
|
||||
const two = setup();
|
||||
await one.viewer.mount(one.host);
|
||||
assert.equal(one.host.children.length, 1);
|
||||
await two.viewer.mount(one.host);
|
||||
assert.equal(one.host.children.length, 1, 'one root, not one per viewer');
|
||||
await assert.rejects(one.viewer.open('f'), /not mounted/, 'superseded');
|
||||
two.viewer.unmount();
|
||||
});
|
||||
|
||||
test('a viewer left by an earlier test is not superseded by a later test’s', async () => {
|
||||
const earlier = setup();
|
||||
await earlier.viewer.mount(earlier.host);
|
||||
// As before each test.
|
||||
resetPhotoView();
|
||||
const later = setup();
|
||||
await later.viewer.mount(later.host);
|
||||
await tick();
|
||||
const opening = earlier.viewer.open('f');
|
||||
await until(() => earlier.loads.includes('f-first'));
|
||||
earlier.release('f-first');
|
||||
await opening;
|
||||
earlier.viewer.unmount();
|
||||
later.viewer.unmount();
|
||||
});
|
||||
|
||||
/** A panorama viewer whose renderers and flat loads the test reads. */
|
||||
function trackedViewer(options = {}) {
|
||||
const renderers = [];
|
||||
const loads = [];
|
||||
const viewer = createPhotoViewer({
|
||||
providerId: 'test',
|
||||
name: 'Test',
|
||||
sources: SOURCES,
|
||||
getPhoto: async (id) => PHOTOS[id],
|
||||
step: async () => null,
|
||||
onStep() {},
|
||||
load: async (url) => {
|
||||
loads.push(url);
|
||||
return { src: url };
|
||||
},
|
||||
createRenderer: (canvas) => {
|
||||
const renderer = {
|
||||
canvas,
|
||||
images: [],
|
||||
setImage: (image) => renderer.images.push(image.src),
|
||||
setView() {},
|
||||
getView: () => ({ yaw: 0, pitch: 0, fov: 80 }),
|
||||
resize() {},
|
||||
clear() {},
|
||||
destroy() {},
|
||||
};
|
||||
renderers.push(renderer);
|
||||
return renderer;
|
||||
},
|
||||
...options,
|
||||
});
|
||||
return { viewer, renderers, loads };
|
||||
}
|
||||
|
||||
/** Hand `parts` back to the viewer that presented `view`. */
|
||||
const attachAll = (view, parts) => {
|
||||
for (const [name, element] of Object.entries(parts))
|
||||
view.actions.attach(name, element);
|
||||
};
|
||||
|
||||
test('a mount overtaken by a second mount of the same viewer closes, and the second stays', async () => {
|
||||
const { viewer } = trackedViewer();
|
||||
let latest = null;
|
||||
const unsubscribe = viewer.subscribeView((view) => (latest = view));
|
||||
const first = viewer.mount({ id: 1 });
|
||||
const hostTwo = { id: 2 };
|
||||
const second = viewer.mount(hostTwo);
|
||||
// The view draws on a later macrotask.
|
||||
setTimeout(() => latest && attachAll(latest, elements()), 0);
|
||||
await assert.rejects(first, /viewer was closed/);
|
||||
await second;
|
||||
assert.equal(latest?.host, hostTwo, 'the second mount is presented');
|
||||
viewer.unmount();
|
||||
unsubscribe();
|
||||
});
|
||||
|
||||
test('a mount whose view subscribes again in the same tick is drawn', async () => {
|
||||
const { viewer } = trackedViewer();
|
||||
let latest = null;
|
||||
const view = (next) => (latest = next);
|
||||
let unsubscribe = viewer.subscribeView(view);
|
||||
const mounting = viewer.mount({});
|
||||
// The view is replaced at once, and draws on a later macrotask.
|
||||
unsubscribe();
|
||||
unsubscribe = viewer.subscribeView(view);
|
||||
setTimeout(() => latest && attachAll(latest, elements()), 0);
|
||||
await mounting;
|
||||
assert.ok(latest, 'still presented');
|
||||
viewer.unmount();
|
||||
unsubscribe();
|
||||
});
|
||||
|
||||
test('a canvas handed back while a panorama’s open waits on its draw takes the panorama', async () => {
|
||||
const { viewer, renderers, loads } = trackedViewer();
|
||||
const first = elements();
|
||||
const second = elements();
|
||||
let latest = null;
|
||||
let hold = false;
|
||||
const unsubscribe = viewer.subscribeView((view) => {
|
||||
latest = view;
|
||||
if (view && !hold) attachAll(view, first);
|
||||
});
|
||||
await viewer.mount({});
|
||||
hold = true;
|
||||
const opening = viewer.open('a');
|
||||
assert.ok(await until(() => latest?.kind === 'pano'), 'waiting on a draw');
|
||||
// The view draws it with a canvas of its own.
|
||||
attachAll(latest, second);
|
||||
await opening;
|
||||
assert.equal(latest.kind, 'pano', 'not the front view');
|
||||
assert.deepEqual(loads, ['a-first'], 'no flat version loaded');
|
||||
assert.equal(renderers.at(-1).canvas, second.canvas);
|
||||
assert.deepEqual(renderers.at(-1).images, ['a-first']);
|
||||
viewer.unmount();
|
||||
unsubscribe();
|
||||
});
|
||||
|
||||
test('two views handing back canvases of their own do not take the panorama in turns', async () => {
|
||||
let reopened = 0;
|
||||
const { viewer, renderers } = trackedViewer({
|
||||
onReopen: (id) => {
|
||||
reopened++;
|
||||
if (reopened < 20) viewer.open(id).catch(() => {});
|
||||
},
|
||||
});
|
||||
const one = elements();
|
||||
const two = elements();
|
||||
const unsubscribes = [
|
||||
viewer.subscribeView((view) => view && attachAll(view, one)),
|
||||
viewer.subscribeView((view) => view && attachAll(view, two)),
|
||||
];
|
||||
await viewer.mount({});
|
||||
await viewer.open('a');
|
||||
const built = renderers.length;
|
||||
// Each presentation is drawn by both views.
|
||||
for (const mode of ['fill', 'letterbox', 'fill']) {
|
||||
viewer.setRenderMode(mode);
|
||||
for (let i = 0; i < 5; i++) await tick();
|
||||
}
|
||||
assert.equal(reopened, 0);
|
||||
assert.equal(renderers.length, built, 'the panorama stays where it is');
|
||||
assert.deepEqual(renderers.at(-1).images, ['a-first']);
|
||||
viewer.unmount();
|
||||
for (const unsubscribe of unsubscribes) unsubscribe();
|
||||
});
|
||||
|
||||
test('the panorama goes back to the one view left once a second view leaves', async () => {
|
||||
const { viewer, renderers } = trackedViewer({
|
||||
onReopen: (id) => viewer.open(id).catch(() => {}),
|
||||
});
|
||||
const one = elements();
|
||||
const two = elements();
|
||||
const unsubscribeOne = viewer.subscribeView(
|
||||
(view) => view && attachAll(view, one),
|
||||
);
|
||||
await viewer.mount({});
|
||||
await viewer.open('a');
|
||||
// A second view subscribes and draws its own canvas: the panorama moves.
|
||||
const unsubscribeTwo = viewer.subscribeView(
|
||||
(view) => view && attachAll(view, two),
|
||||
);
|
||||
for (let i = 0; i < 5; i++) await tick();
|
||||
assert.equal(renderers.at(-1).canvas, two.canvas);
|
||||
unsubscribeTwo();
|
||||
assert.ok(
|
||||
await settles(viewer.open('b')),
|
||||
'the next open is not left waiting',
|
||||
);
|
||||
for (let i = 0; i < 5; i++) await tick();
|
||||
assert.equal(
|
||||
renderers.at(-1).canvas,
|
||||
one.canvas,
|
||||
'the panorama is back on the view left',
|
||||
);
|
||||
assert.equal(renderers.at(-1).images.at(-1), 'b-first');
|
||||
viewer.unmount();
|
||||
unsubscribeOne();
|
||||
});
|
||||
|
||||
test('a mount its view unmounts inside the first draw fails as closed', async () => {
|
||||
const { viewer } = trackedViewer();
|
||||
const unsubscribe = viewer.subscribeView((view) => {
|
||||
if (view) viewer.unmount();
|
||||
});
|
||||
await assert.rejects(viewer.mount({}), /Street-level viewer was closed/);
|
||||
unsubscribe();
|
||||
});
|
||||
@@ -1,37 +1,93 @@
|
||||
/** 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, 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 (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.
|
||||
// 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 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();
|
||||
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 (view) shown.set(viewer, view);
|
||||
else shown.delete(viewer);
|
||||
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);
|
||||
@@ -40,6 +96,7 @@ export function subscribePhotoViewers(viewers, listener) {
|
||||
publish();
|
||||
if (last === undefined) listener((last = null));
|
||||
return () => {
|
||||
closed = true;
|
||||
for (const unsubscribe of unsubscribes.splice(0)) unsubscribe();
|
||||
};
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
import assert from 'node:assert/strict';
|
||||
import test from 'node:test';
|
||||
import { subscribePhotoViewers } from './photoViewers.js';
|
||||
import { createPhotoViewer } from './photoViewer.js';
|
||||
import { nextViewKey, subscribePhotoViewers } from './photoViewers.js';
|
||||
|
||||
/**
|
||||
* A photo viewer whose presentations the test sends; `superseded` counts.
|
||||
@@ -9,6 +10,7 @@ import { subscribePhotoViewers } from './photoViewers.js';
|
||||
*/
|
||||
function fakeViewer() {
|
||||
const listeners = new Set();
|
||||
let current = null;
|
||||
const viewer = {
|
||||
superseded: 0,
|
||||
view(mountKey) {
|
||||
@@ -18,18 +20,19 @@ function fakeViewer() {
|
||||
};
|
||||
},
|
||||
present(view) {
|
||||
current = view;
|
||||
for (const listener of [...listeners]) listener(view);
|
||||
},
|
||||
subscribeView(listener) {
|
||||
listeners.add(listener);
|
||||
listener(null);
|
||||
listener(current);
|
||||
return () => listeners.delete(listener);
|
||||
},
|
||||
};
|
||||
return viewer;
|
||||
}
|
||||
|
||||
test('the later mount is drawn, whatever order the viewers present in', () => {
|
||||
test('the later mount is drawn, whatever order the viewers present in', async () => {
|
||||
const a = fakeViewer();
|
||||
const b = fakeViewer();
|
||||
const shown = [];
|
||||
@@ -40,6 +43,8 @@ test('the later mount is drawn, whatever order the viewers present in', () => {
|
||||
const later = b.view(2);
|
||||
b.present(later);
|
||||
assert.equal(shown.at(-1), later);
|
||||
// Once every subscriber has the later one.
|
||||
await Promise.resolve();
|
||||
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' });
|
||||
@@ -53,3 +58,158 @@ test('the later mount is drawn, whatever order the viewers present in', () => {
|
||||
a.present(first);
|
||||
assert.equal(shown.length, 5, 'unsubscribed');
|
||||
});
|
||||
|
||||
/** A viewer that, superseded, unmounts as photoViewer.js does: it presents null. */
|
||||
function unmountingViewer() {
|
||||
const viewer = fakeViewer();
|
||||
const view = viewer.view;
|
||||
viewer.view = (mountKey) => {
|
||||
const presented = view(mountKey);
|
||||
const supersede = presented.actions.supersede;
|
||||
presented.actions.supersede = () => {
|
||||
supersede();
|
||||
viewer.present(null);
|
||||
};
|
||||
return presented;
|
||||
};
|
||||
return viewer;
|
||||
}
|
||||
|
||||
test('every subscriber sees the earlier mount, then the later one, with no gap', async () => {
|
||||
const a = unmountingViewer();
|
||||
const b = unmountingViewer();
|
||||
const first = [];
|
||||
const second = [];
|
||||
const unsubscribes = [
|
||||
subscribePhotoViewers([a, b], (view) => first.push(view)),
|
||||
subscribePhotoViewers([a, b], (view) => second.push(view)),
|
||||
];
|
||||
const earlier = a.view(1);
|
||||
a.present(earlier);
|
||||
const later = b.view(2);
|
||||
b.present(later);
|
||||
await Promise.resolve();
|
||||
assert.equal(a.superseded, 1, 'superseded once, by both');
|
||||
assert.deepEqual(first, [null, earlier, later]);
|
||||
assert.deepEqual(second, [null, earlier, later], 'never null between');
|
||||
for (const unsubscribe of unsubscribes) unsubscribe();
|
||||
});
|
||||
|
||||
test('a presentation without a mount key counts as mounted when it began presenting', async () => {
|
||||
const a = fakeViewer();
|
||||
const b = fakeViewer();
|
||||
const c = fakeViewer();
|
||||
const shown = [];
|
||||
const unsubscribe = subscribePhotoViewers([a, b, c], (view) =>
|
||||
shown.push(view),
|
||||
);
|
||||
a.present(a.view(nextViewKey()));
|
||||
const keyless = b.view(undefined);
|
||||
b.present(keyless);
|
||||
assert.equal(shown.at(-1), keyless, 'after the keyed one');
|
||||
// Of two without, the one that began presenting last.
|
||||
const newer = c.view(undefined);
|
||||
c.present(newer);
|
||||
assert.equal(shown.at(-1), newer);
|
||||
b.present({ ...keyless, kind: 'flat' });
|
||||
assert.equal(shown.at(-1), newer, 'presenting again is not a new mount');
|
||||
// A keyed mount made after them wins.
|
||||
const latest = a.view(nextViewKey());
|
||||
a.present(latest);
|
||||
assert.equal(shown.at(-1), latest);
|
||||
await Promise.resolve();
|
||||
assert.ok(b.superseded > 0 && c.superseded > 0, 'the keyless ones are told');
|
||||
unsubscribe();
|
||||
});
|
||||
|
||||
test('a presentation that is not drawn is superseded even when the pick stays', async () => {
|
||||
const a = fakeViewer();
|
||||
const b = fakeViewer();
|
||||
const shown = [];
|
||||
const unsubscribe = subscribePhotoViewers([a, b], (view) => shown.push(view));
|
||||
const later = b.view(2);
|
||||
b.present(later);
|
||||
// An earlier mount presents once the later one is drawn.
|
||||
a.present(a.view(1));
|
||||
assert.deepEqual(shown, [null, later]);
|
||||
await Promise.resolve();
|
||||
assert.equal(a.superseded, 1);
|
||||
unsubscribe();
|
||||
});
|
||||
|
||||
test('a photo viewer mounting while a keyless presentation shows is drawn, and the keyless one superseded', async () => {
|
||||
let superseded = 0;
|
||||
const keyless = {
|
||||
subscribeView(listener) {
|
||||
listener({ actions: { supersede: () => superseded++ } });
|
||||
return () => {};
|
||||
},
|
||||
};
|
||||
const viewer = createPhotoViewer({
|
||||
providerId: 'test',
|
||||
name: 'Test',
|
||||
sources: {},
|
||||
getPhoto: async () => null,
|
||||
step: async () => null,
|
||||
onStep() {},
|
||||
});
|
||||
const shown = [];
|
||||
const element = { clientWidth: 400, clientHeight: 250 };
|
||||
const unsubscribe = subscribePhotoViewers([keyless, viewer], (view) => {
|
||||
shown.push(view);
|
||||
if (!view?.mountKey) return;
|
||||
for (const name of ['root', 'picture', 'canvas', 'prev', 'next'])
|
||||
view.actions.attach(name, element);
|
||||
});
|
||||
const mounting = viewer.mount({});
|
||||
const outcome = await Promise.race([
|
||||
mounting.then(() => 'mounted'),
|
||||
new Promise((resolve) => setTimeout(() => resolve('pending'), 50)),
|
||||
]);
|
||||
assert.equal(outcome, 'mounted');
|
||||
assert.ok(Number.isFinite(shown.at(-1)?.mountKey), 'the mount is drawn');
|
||||
assert.ok(superseded >= 1, 'the keyless one is told');
|
||||
viewer.unmount();
|
||||
unsubscribe();
|
||||
});
|
||||
|
||||
test('a presentation without actions or supersede is drawn and passed over without throwing', async () => {
|
||||
const a = fakeViewer();
|
||||
const b = fakeViewer();
|
||||
const shown = [];
|
||||
const unsubscribe = subscribePhotoViewers([a, b], (view) => shown.push(view));
|
||||
a.present({ mountKey: 1 });
|
||||
const later = { mountKey: 2, actions: {} };
|
||||
assert.doesNotThrow(() => b.present(later));
|
||||
await Promise.resolve();
|
||||
assert.equal(shown.at(-1), later);
|
||||
a.present({ mountKey: 3 });
|
||||
await Promise.resolve();
|
||||
assert.equal(shown.at(-1).mountKey, 3);
|
||||
unsubscribe();
|
||||
});
|
||||
|
||||
test('a keyless viewer on screen keeps its place when the picker is made again', async () => {
|
||||
const keyed = fakeViewer();
|
||||
const keyless = fakeViewer();
|
||||
keyed.present(keyed.view(nextViewKey()));
|
||||
const shown = [];
|
||||
let unsubscribe = subscribePhotoViewers([keyless, keyed], (view) =>
|
||||
shown.push(view),
|
||||
);
|
||||
const mounted = keyless.view(undefined);
|
||||
keyless.present(mounted);
|
||||
assert.equal(shown.at(-1), mounted);
|
||||
await Promise.resolve();
|
||||
unsubscribe();
|
||||
// A new picker, the keyless viewer first: it is still the later mount.
|
||||
const again = [];
|
||||
unsubscribe = subscribePhotoViewers([keyless, keyed], (view) =>
|
||||
again.push(view),
|
||||
);
|
||||
assert.equal(again.at(-1), mounted);
|
||||
await Promise.resolve();
|
||||
assert.equal(keyless.superseded, 0, 'never told it is not drawn');
|
||||
assert.ok(keyed.superseded > 0);
|
||||
unsubscribe();
|
||||
});
|
||||
@@ -17,6 +17,7 @@ import {
|
||||
drawn,
|
||||
fakeHost,
|
||||
until,
|
||||
resetPhotoView,
|
||||
} from '../../../../testSupport/kartaViewFakes.mjs';
|
||||
import {
|
||||
bindStreetViewer,
|
||||
@@ -26,6 +27,9 @@ import {
|
||||
} from '../../../../testSupport/streetLevelFakes.mjs';
|
||||
import { fakeDrawPort } from '../../../../testSupport/fakeDrawPort.mjs';
|
||||
|
||||
// Each test draws its own viewers only.
|
||||
test.beforeEach(resetPhotoView);
|
||||
|
||||
/** A photo record `metres` east of (42.3314, -83.0458), in sequence `seq` at `index`. */
|
||||
function photoRecord(id, { metres = 0, seq = '14010', index = 0 } = {}) {
|
||||
const lon = -83.0458 + metres / 82_300;
|
||||
|
||||
@@ -5,8 +5,12 @@ import {
|
||||
drawn,
|
||||
fakeHost,
|
||||
until,
|
||||
resetPhotoView,
|
||||
} from '../../../../testSupport/kartaViewFakes.mjs';
|
||||
|
||||
// Each test draws its own viewers only.
|
||||
test.beforeEach(resetPhotoView);
|
||||
|
||||
const PHOTOS = {
|
||||
flat1: {
|
||||
id: 'flat1',
|
||||
|
||||
@@ -18,6 +18,7 @@ import {
|
||||
drawn,
|
||||
fakeHost,
|
||||
until,
|
||||
resetPhotoView,
|
||||
} from '../../../../testSupport/kartaViewFakes.mjs';
|
||||
import {
|
||||
bindStreetViewer,
|
||||
@@ -31,6 +32,9 @@ import {
|
||||
stacItem,
|
||||
} from '../../../../testSupport/panoramaxFixtures.mjs';
|
||||
|
||||
// Each test draws its own viewers only.
|
||||
test.beforeEach(resetPhotoView);
|
||||
|
||||
const ORIGIN = { lon: 2.3495, lat: 48.8874 };
|
||||
/** A point `metres` east of ORIGIN. */
|
||||
const east = (metres) => ({
|
||||
|
||||
@@ -2,9 +2,16 @@ import assert from 'node:assert/strict';
|
||||
import test from 'node:test';
|
||||
import { PANORAMAX_SOURCES, createPanoramaxViewer } from './viewer.js';
|
||||
import { normalizeItem } from './policy.js';
|
||||
import { fakeHost, until } from '../../../../testSupport/kartaViewFakes.mjs';
|
||||
import {
|
||||
fakeHost,
|
||||
until,
|
||||
resetPhotoView,
|
||||
} from '../../../../testSupport/kartaViewFakes.mjs';
|
||||
import { stacItem } from '../../../../testSupport/panoramaxFixtures.mjs';
|
||||
|
||||
// Each test draws its own viewers only.
|
||||
test.beforeEach(resetPhotoView);
|
||||
|
||||
const photo = (options) =>
|
||||
normalizeItem(stacItem({ lon: 2.35, lat: 48.85, ...options }));
|
||||
|
||||
|
||||
@@ -65,7 +65,14 @@ export function filtersOf(provider) {
|
||||
* @property {(listener: (pose: StreetPose) => void) => () => void} onPose
|
||||
* @property {(listener: (view: ?object) => void) => () => void} [subscribeView]
|
||||
* An adapter that draws nothing itself presents its viewer here, for the
|
||||
* layer's `subscribePhotoViewer()` (see photoViewer.js).
|
||||
* layer's `subscribePhotoViewer()` (see photoViewer.js): its current
|
||||
* presentation at once, then each new one, null while unmounted. One view
|
||||
* draws one viewer (photoViewers.js picks it), reading two fields:
|
||||
* `mountKey`, a number larger for each later mount (the later mount is
|
||||
* drawn; a presentation without one counts as mounted when it began
|
||||
* presenting), and `actions.supersede()`, called on a mount that is not
|
||||
* drawn because a later one is (it should unmount, failing a mount still
|
||||
* waiting to be drawn; an adapter without it is simply not drawn).
|
||||
*
|
||||
* @typedef {object} StreetPose
|
||||
* @property {string} providerId
|
||||
|
||||
Reference in new issue
Block a user