@johnmorrisdotca/chikyu-earth 0.29.0 → 0.31.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +8 -0
- package/NOTICE.md +4 -0
- package/README.md +55 -2
- package/dist/data-door.d.ts +357 -0
- package/dist/data-door.js +393 -0
- package/dist/door/book.d.ts +118 -0
- package/dist/door/book.js +211 -0
- package/dist/door/elements.d.ts +96 -0
- package/dist/door/elements.js +108 -0
- package/dist/door/host.d.ts +30 -0
- package/dist/door/host.js +44 -0
- package/dist/door/index.d.ts +6 -0
- package/dist/door/index.js +10 -0
- package/dist/door/map.d.ts +165 -0
- package/dist/door/map.js +182 -0
- package/dist/door/own.d.ts +36 -0
- package/dist/door/own.js +75 -0
- package/dist/meteors/index.d.ts +1 -0
- package/dist/meteors/index.js +1 -0
- package/dist/meteors/layer.d.ts +6 -2
- package/dist/meteors/layer.js +26 -9
- package/dist/meteors/strength.d.ts +26 -0
- package/dist/meteors/strength.js +35 -0
- package/dist/passes/element.d.ts +18 -0
- package/dist/passes/element.js +67 -5
- package/dist/realistic/earth.d.ts +5 -3
- package/dist/realistic/earth.js +2 -1
- package/dist/realistic/element.d.ts +27 -0
- package/dist/realistic/element.js +81 -5
- package/dist/realistic/tile-set.d.ts +27 -2
- package/dist/realistic/tile-set.js +155 -49
- package/dist/space/element.d.ts +8 -0
- package/dist/space/element.js +19 -2
- package/package.json +6 -1
|
@@ -0,0 +1,393 @@
|
|
|
1
|
+
// data-door.ts: the family's data door, as one file of types and four small functions. The same file in every package that takes outside data (src/data-door.ts), copied unchanged from
|
|
2
|
+
// kazoku-ui's templates/; a repository never edits it, and scripts/check-standard.mjs fails when a copy differs. A copy, not an import, so that a package with no runtime dependencies keeps none.
|
|
3
|
+
// No DOM, no three.js, no clock, no network of its own: a source does its own fetching, and this file only checks, counts, budgets and words what a source says.
|
|
4
|
+
//
|
|
5
|
+
// The idea: a package draws, and the caller supplies the data. Outside data comes in through a source, either records (a list that fits in memory) or tiles (data cut by place and by detail,
|
|
6
|
+
// fetched only for what the view shows). Every source carries its licence and credit in code, so a view can always say whose data is on screen. With no source a package draws what it drew before.
|
|
7
|
+
//
|
|
8
|
+
// validateSource(source, taken) throws a TypeError for an empty credit, no licence name or link, a bad id, a clashing id, a missing function
|
|
9
|
+
// creditLine(credits, language) "Yale Bright Star Catalogue (Public domain); UCAC4 (CC0 1.0)." with "non-commercial" or "share-alike" whenever the source says so
|
|
10
|
+
// loadRecords(source, signal) reads a record list once, freezes it, refuses more than MAX_RECORDS, skips and counts records with a non-finite number
|
|
11
|
+
// createTileStore(options) loads tiles by key under a byte budget: at most 6 in flight, least recently used out first, never a tile the view needs, failures counted
|
|
12
|
+
//
|
|
13
|
+
// The shapes are in DESIGN-831 (MODULES-831). The licence terms of a caller's source may be non-commercial or share-alike; a package's own (bundled) sources never are.
|
|
14
|
+
/**
|
|
15
|
+
* The most records a record source may hold. Bigger data must be tiles. *
|
|
16
|
+
* @example
|
|
17
|
+
* ```ts no-check
|
|
18
|
+
* import { MAX_RECORDS } from "./data-door.ts";
|
|
19
|
+
*
|
|
20
|
+
* console.log(MAX_RECORDS); // 200000
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
23
|
+
export const MAX_RECORDS = 200_000;
|
|
24
|
+
/**
|
|
25
|
+
* The default budget for decoded tiles in a view, all sources together: 48 MB (a picture counts with its mipmaps, 4/3 of its bytes). *
|
|
26
|
+
* @example
|
|
27
|
+
* ```ts no-check
|
|
28
|
+
* import { TILE_BUDGET_BYTES } from "./data-door.ts";
|
|
29
|
+
*
|
|
30
|
+
* console.log(TILE_BUDGET_BYTES / 1024 / 1024); // 48
|
|
31
|
+
* ```
|
|
32
|
+
*/
|
|
33
|
+
export const TILE_BUDGET_BYTES = 48 * 1024 * 1024;
|
|
34
|
+
/**
|
|
35
|
+
* The most loads in flight at once for one source. *
|
|
36
|
+
* @example
|
|
37
|
+
* ```ts no-check
|
|
38
|
+
* import { MAX_IN_FLIGHT } from "./data-door.ts";
|
|
39
|
+
*
|
|
40
|
+
* console.log(MAX_IN_FLIGHT); // 6
|
|
41
|
+
* ```
|
|
42
|
+
*/
|
|
43
|
+
export const MAX_IN_FLIGHT = 6;
|
|
44
|
+
const ID = /^[a-z0-9][a-z0-9-]{0,63}$/;
|
|
45
|
+
const DAY = /^(\d{4})-(\d{2})-(\d{2})$/;
|
|
46
|
+
const WEB = /^https?:\/\/[^\s/]+/i;
|
|
47
|
+
const text = (value) => typeof value === "string" && value.trim() !== "";
|
|
48
|
+
const object = (value) => typeof value === "object" && value !== null;
|
|
49
|
+
function isDay(value) {
|
|
50
|
+
if (typeof value !== "string")
|
|
51
|
+
return false;
|
|
52
|
+
const parts = DAY.exec(value);
|
|
53
|
+
if (parts === null)
|
|
54
|
+
return false;
|
|
55
|
+
const date = new Date(Date.UTC(Number(parts[1]), Number(parts[2]) - 1, Number(parts[3])));
|
|
56
|
+
return date.toISOString().slice(0, 10) === value;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Refuses a source that cannot be shown honestly: it throws a `TypeError` at once, when a source is added, not at the first draw. An empty credit (either language), a licence with no name or no
|
|
60
|
+
* link, a bad or already used id, a missing name, page or day, or a missing function is refused.
|
|
61
|
+
* @param source What the caller handed in.
|
|
62
|
+
* @param taken The ids already in the view, so that a clash is refused.
|
|
63
|
+
* @example
|
|
64
|
+
* ```ts no-check
|
|
65
|
+
* import { validateSource } from "./data-door.ts";
|
|
66
|
+
*
|
|
67
|
+
* validateSource(mySource, view.sources.map((one) => one.info.id)); // throws TypeError: source "mine": the credit is empty (en)
|
|
68
|
+
* ```
|
|
69
|
+
*/
|
|
70
|
+
export function validateSource(source, taken = []) {
|
|
71
|
+
if (!object(source))
|
|
72
|
+
throw new TypeError("a source must be an object");
|
|
73
|
+
const info = source["info"];
|
|
74
|
+
if (!object(info))
|
|
75
|
+
throw new TypeError("a source needs an info object");
|
|
76
|
+
const id = info["id"];
|
|
77
|
+
if (typeof id !== "string" || !ID.test(id))
|
|
78
|
+
throw new TypeError(`a source's id must be lower-case letters, digits and hyphens, up to 64 characters: ${JSON.stringify(id)}`);
|
|
79
|
+
const refuse = (why) => {
|
|
80
|
+
throw new TypeError(`source "${id}": ${why}`);
|
|
81
|
+
};
|
|
82
|
+
for (const used of taken)
|
|
83
|
+
if (used === id)
|
|
84
|
+
refuse("the id is already used in this view");
|
|
85
|
+
for (const field of ["name", "credit"]) {
|
|
86
|
+
const pair = info[field];
|
|
87
|
+
for (const language of ["en", "ja"])
|
|
88
|
+
if (!object(pair) || !text(pair[language]))
|
|
89
|
+
refuse(`the ${field} is empty (${language})`);
|
|
90
|
+
}
|
|
91
|
+
const licence = info["licence"];
|
|
92
|
+
if (!object(licence))
|
|
93
|
+
refuse("there is no licence");
|
|
94
|
+
const terms = licence;
|
|
95
|
+
if (!text(terms["name"]))
|
|
96
|
+
refuse("the licence has no name");
|
|
97
|
+
if (!text(terms["url"]) || !WEB.test(terms["url"]))
|
|
98
|
+
refuse("the licence has no link (an http or https address)");
|
|
99
|
+
for (const flag of ["nonCommercial", "shareAlike"])
|
|
100
|
+
if (terms[flag] !== undefined && typeof terms[flag] !== "boolean")
|
|
101
|
+
refuse(`the licence's ${flag} must be true or false`);
|
|
102
|
+
if (!text(info["url"]) || !WEB.test(info["url"]))
|
|
103
|
+
refuse("the source has no page (an http or https address)");
|
|
104
|
+
if (!isDay(info["retrieved"]))
|
|
105
|
+
refuse("retrieved must be a real day, yyyy-mm-dd");
|
|
106
|
+
const kind = source["kind"];
|
|
107
|
+
if (kind === "records") {
|
|
108
|
+
if (typeof source["records"] !== "function")
|
|
109
|
+
refuse("a records source needs records()");
|
|
110
|
+
}
|
|
111
|
+
else if (kind === "tiles") {
|
|
112
|
+
for (const method of ["need", "load", "bytes"])
|
|
113
|
+
if (typeof source[method] !== "function")
|
|
114
|
+
refuse(`a tiles source needs ${method}()`);
|
|
115
|
+
}
|
|
116
|
+
else
|
|
117
|
+
refuse(`kind must be "records" or "tiles", not ${JSON.stringify(kind)}`);
|
|
118
|
+
}
|
|
119
|
+
/** The words for a licence's terms. */
|
|
120
|
+
const TERMS = {
|
|
121
|
+
en: { nonCommercial: "non-commercial", shareAlike: "share-alike" },
|
|
122
|
+
ja: { nonCommercial: "非営利のみ", shareAlike: "同一条件で共有" },
|
|
123
|
+
};
|
|
124
|
+
/** Licence names that are plain phrases, not identifiers, and so are translated. An identifier such as "CC BY-NC 3.0 IGO" is a name and stays. */
|
|
125
|
+
const JA_NAMES = { "public domain": "パブリックドメイン" };
|
|
126
|
+
/**
|
|
127
|
+
* The credit line for what a view drew: each source's credit with its licence name, and "non-commercial" or "share-alike" whenever the source says so, so a page cannot hide the terms of the data it
|
|
128
|
+
* uses. The package's own sources come first, then the rest, each group in the order given; a source listed twice is written once. English: joined with semicolons, ending in a full stop. Japanese: the
|
|
129
|
+
* licence in full-width parentheses, joined with /, no full stop. An empty list is an empty string.
|
|
130
|
+
* @param credits What the view drew, from its `credits()`.
|
|
131
|
+
* @param language `"en"` or `"ja"`.
|
|
132
|
+
* @returns The line, or an empty string for no credits.
|
|
133
|
+
* @example
|
|
134
|
+
* ```ts no-check
|
|
135
|
+
* import { creditLine } from "./data-door.ts";
|
|
136
|
+
*
|
|
137
|
+
* creditLine(view.credits(), "en"); // "Yale Bright Star Catalogue (Public domain); UCAC4 (CC0 1.0)."
|
|
138
|
+
* creditLine(view.credits(), "ja"); // "イェール輝星カタログ(パブリックドメイン)/UCAC4(CC0 1.0)"
|
|
139
|
+
* ```
|
|
140
|
+
*/
|
|
141
|
+
export function creditLine(credits, language) {
|
|
142
|
+
const seen = new Set();
|
|
143
|
+
const once = credits.filter((credit) => !seen.has(credit.source.id) && (seen.add(credit.source.id), true));
|
|
144
|
+
const ordered = [...once.filter((credit) => credit.own), ...once.filter((credit) => !credit.own)];
|
|
145
|
+
const words = TERMS[language];
|
|
146
|
+
const parts = ordered.map((credit) => {
|
|
147
|
+
const { source } = credit;
|
|
148
|
+
const line = (source.credit[language] || source.credit.en).trim();
|
|
149
|
+
const name = language === "ja" ? (JA_NAMES[source.licence.name.trim().toLowerCase()] ?? source.licence.name) : source.licence.name;
|
|
150
|
+
const terms = [source.licence.nonCommercial === true ? words.nonCommercial : "", source.licence.shareAlike === true ? words.shareAlike : ""].filter((term) => term !== "");
|
|
151
|
+
if (language === "ja")
|
|
152
|
+
return `${line}(${[name, ...terms].join("、")})`;
|
|
153
|
+
return terms.length === 0 ? `${line} (${name})` : `${line} (${name}; ${terms.join(", ")})`;
|
|
154
|
+
});
|
|
155
|
+
if (parts.length === 0)
|
|
156
|
+
return "";
|
|
157
|
+
return language === "ja" ? parts.join("/") : `${parts.join("; ")}.`;
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* Reads a record source once, when it is added: the list is frozen, a record with a non-finite number (a `NaN` or an infinity in any number `finite` names) is skipped and counted, and a list over
|
|
161
|
+
* `MAX_RECORDS` is refused whole (bigger data must be tiles). It never throws: a source that throws or rejects is a failed read, and a read the signal aborted is `aborted`.
|
|
162
|
+
* @param source The record source.
|
|
163
|
+
* @param signal Aborts the read (the source is removed or switched off).
|
|
164
|
+
* @param finite The numbers of one record that must be finite, if any: `(star) => [star.ra, star.dec, star.mag]`.
|
|
165
|
+
* @returns What was found: the status, the frozen records, how many were skipped and, for a failed read, why.
|
|
166
|
+
* @example
|
|
167
|
+
* ```ts no-check
|
|
168
|
+
* import { loadRecords } from "./data-door.ts";
|
|
169
|
+
*
|
|
170
|
+
* const read = await loadRecords(source, controller.signal, (star) => [star.ra, star.dec]); // read.records is frozen; read.skipped counts the NaNs
|
|
171
|
+
* ```
|
|
172
|
+
*/
|
|
173
|
+
export async function loadRecords(source, signal, finite) {
|
|
174
|
+
const none = (status, reason) => ({ status, records: Object.freeze([]), skipped: 0, ...(reason === undefined ? {} : { reason }) });
|
|
175
|
+
if (signal.aborted)
|
|
176
|
+
return none("aborted");
|
|
177
|
+
let list;
|
|
178
|
+
try {
|
|
179
|
+
list = await source.records(signal);
|
|
180
|
+
}
|
|
181
|
+
catch (error) {
|
|
182
|
+
return signal.aborted ? none("aborted") : none("failed", error instanceof Error ? error.message : String(error));
|
|
183
|
+
}
|
|
184
|
+
if (signal.aborted)
|
|
185
|
+
return none("aborted");
|
|
186
|
+
if (!Array.isArray(list))
|
|
187
|
+
return none("failed", "records() did not return a list");
|
|
188
|
+
if (list.length > MAX_RECORDS)
|
|
189
|
+
return none("failed", `${list.length} records is over the limit of ${MAX_RECORDS}: bigger data must be tiles`);
|
|
190
|
+
const kept = [];
|
|
191
|
+
let skipped = 0;
|
|
192
|
+
for (const record of list) {
|
|
193
|
+
let ok = record !== null && record !== undefined;
|
|
194
|
+
if (ok && finite !== undefined) {
|
|
195
|
+
try {
|
|
196
|
+
ok = finite(record).every((value) => typeof value === "number" && Number.isFinite(value));
|
|
197
|
+
}
|
|
198
|
+
catch {
|
|
199
|
+
ok = false;
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
if (ok)
|
|
203
|
+
kept.push(record);
|
|
204
|
+
else
|
|
205
|
+
skipped += 1;
|
|
206
|
+
}
|
|
207
|
+
return { status: "ok", records: Object.freeze(kept), skipped };
|
|
208
|
+
}
|
|
209
|
+
const joinKey = (id, key) => `${id}\u0000${key}`;
|
|
210
|
+
/**
|
|
211
|
+
* A tile store for a view: loads the tiles of every source by key and keeps them under one budget of bytes (default 48 MB, all sources together).
|
|
212
|
+
* - At most `maxInFlight` loads (6) run at once for a source, and a load starts only because the keys changed: at rest the store sends nothing, and a tile that failed waits for the next change.
|
|
213
|
+
* - The least recently used tile goes first when room is needed, and never a tile the view needs now. If a new tile does not fit even so, it is refused and counted: the held bytes never pass the budget (nor the count, if `maxTiles` is set).
|
|
214
|
+
* - A tile that throws, comes back empty or reports a size that is not a finite number is counted in `failed` and never held, so it is never drawn.
|
|
215
|
+
* - `drop(sourceId)` aborts the source's loads and frees its tiles.
|
|
216
|
+
* @param options Budget, loads in flight, and a callback for a redraw.
|
|
217
|
+
* @returns The store.
|
|
218
|
+
* @example
|
|
219
|
+
* ```ts no-check
|
|
220
|
+
* import { createTileStore } from "./data-door.ts";
|
|
221
|
+
*
|
|
222
|
+
* const store = createTileStore<Tile>({ onChange: draw });
|
|
223
|
+
* const held = store.want(source, source.need(place, detail)); // draws what is held now; the rest arrives through onChange
|
|
224
|
+
* store.drop(source.info.id); // the layer is switched off
|
|
225
|
+
* ```
|
|
226
|
+
*/
|
|
227
|
+
export function createTileStore(options = {}) {
|
|
228
|
+
const budget = options.budgetBytes ?? TILE_BUDGET_BYTES;
|
|
229
|
+
const maxInFlight = Math.max(1, Math.floor(options.maxInFlight ?? MAX_IN_FLIGHT));
|
|
230
|
+
const maxTiles = Math.max(1, options.maxTiles ?? Infinity);
|
|
231
|
+
const held = new Map(); // oldest first: a tile used again is moved to the end
|
|
232
|
+
const accounts = new Map();
|
|
233
|
+
const counts = { heldBytes: 0, requested: 0, failed: 0, refused: 0, evicted: 0 };
|
|
234
|
+
let waiting = [];
|
|
235
|
+
const busy = () => [...accounts.values()].some((account) => account.loading.size > 0 || account.queue.length > 0);
|
|
236
|
+
const settle = () => {
|
|
237
|
+
if (busy())
|
|
238
|
+
return;
|
|
239
|
+
const done = waiting;
|
|
240
|
+
waiting = [];
|
|
241
|
+
for (const resolve of done)
|
|
242
|
+
resolve();
|
|
243
|
+
};
|
|
244
|
+
const needed = (full) => {
|
|
245
|
+
const at = full.indexOf("\u0000");
|
|
246
|
+
return accounts.get(full.slice(0, at))?.wanted.has(full.slice(at + 1)) === true;
|
|
247
|
+
};
|
|
248
|
+
const release = (full) => {
|
|
249
|
+
const one = held.get(full);
|
|
250
|
+
if (one === undefined)
|
|
251
|
+
return;
|
|
252
|
+
held.delete(full);
|
|
253
|
+
counts.heldBytes -= one.bytes;
|
|
254
|
+
};
|
|
255
|
+
/** Keeps `tile` if it fits (in bytes and in count), letting go of the least recently used tiles nobody needs. */
|
|
256
|
+
function keep(full, tile, bytes) {
|
|
257
|
+
const victims = [];
|
|
258
|
+
let freed = 0;
|
|
259
|
+
let count = held.size;
|
|
260
|
+
const fits = () => counts.heldBytes - freed + bytes <= budget && count + 1 <= maxTiles;
|
|
261
|
+
if (!fits()) {
|
|
262
|
+
for (const [old, one] of held) {
|
|
263
|
+
if (needed(old))
|
|
264
|
+
continue;
|
|
265
|
+
victims.push(old);
|
|
266
|
+
freed += one.bytes;
|
|
267
|
+
count -= 1;
|
|
268
|
+
if (fits())
|
|
269
|
+
break;
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
if (!fits())
|
|
273
|
+
return false;
|
|
274
|
+
for (const old of victims) {
|
|
275
|
+
release(old);
|
|
276
|
+
counts.evicted += 1;
|
|
277
|
+
}
|
|
278
|
+
held.set(full, { tile, bytes });
|
|
279
|
+
counts.heldBytes += bytes;
|
|
280
|
+
return true;
|
|
281
|
+
}
|
|
282
|
+
function start(id, account, key) {
|
|
283
|
+
const controller = new AbortController();
|
|
284
|
+
account.loading.set(key, controller);
|
|
285
|
+
counts.requested += 1;
|
|
286
|
+
const finish = () => {
|
|
287
|
+
if (account.loading.get(key) === controller)
|
|
288
|
+
account.loading.delete(key);
|
|
289
|
+
if (accounts.get(id) === account)
|
|
290
|
+
pump(id, account);
|
|
291
|
+
settle();
|
|
292
|
+
};
|
|
293
|
+
const arrived = (tile) => {
|
|
294
|
+
if (controller.signal.aborted || accounts.get(id) !== account)
|
|
295
|
+
return;
|
|
296
|
+
let bytes = Number.NaN;
|
|
297
|
+
try {
|
|
298
|
+
bytes = tile === null || tile === undefined ? Number.NaN : account.source.bytes(tile);
|
|
299
|
+
}
|
|
300
|
+
catch {
|
|
301
|
+
bytes = Number.NaN;
|
|
302
|
+
}
|
|
303
|
+
if (!Number.isFinite(bytes) || bytes < 0)
|
|
304
|
+
counts.failed += 1;
|
|
305
|
+
else if (!keep(joinKey(id, key), tile, bytes))
|
|
306
|
+
counts.refused += 1;
|
|
307
|
+
options.onChange?.();
|
|
308
|
+
};
|
|
309
|
+
const failed = () => {
|
|
310
|
+
if (controller.signal.aborted || accounts.get(id) !== account)
|
|
311
|
+
return;
|
|
312
|
+
counts.failed += 1;
|
|
313
|
+
options.onChange?.();
|
|
314
|
+
};
|
|
315
|
+
let promise;
|
|
316
|
+
try {
|
|
317
|
+
promise = Promise.resolve(account.source.load(key, controller.signal));
|
|
318
|
+
}
|
|
319
|
+
catch (error) {
|
|
320
|
+
promise = Promise.reject(error instanceof Error ? error : new Error(String(error)));
|
|
321
|
+
}
|
|
322
|
+
promise.then(arrived, failed).then(finish, finish);
|
|
323
|
+
}
|
|
324
|
+
function pump(id, account) {
|
|
325
|
+
while (account.loading.size < maxInFlight && account.queue.length > 0) {
|
|
326
|
+
const key = account.queue.shift();
|
|
327
|
+
if (account.wanted.has(key) && !held.has(joinKey(id, key)) && !account.loading.has(key))
|
|
328
|
+
start(id, account, key);
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
return {
|
|
332
|
+
want(source, keys, again = false) {
|
|
333
|
+
const id = source.info.id;
|
|
334
|
+
const list = [...new Set(keys)];
|
|
335
|
+
let account = accounts.get(id);
|
|
336
|
+
if (account === undefined) {
|
|
337
|
+
account = { source, signature: "\u0001", wanted: new Set(), queue: [], loading: new Map() };
|
|
338
|
+
accounts.set(id, account);
|
|
339
|
+
}
|
|
340
|
+
account.source = source;
|
|
341
|
+
const signature = [...list].sort().join("\n");
|
|
342
|
+
if (signature !== account.signature || again) {
|
|
343
|
+
account.signature = signature;
|
|
344
|
+
account.wanted = new Set(list);
|
|
345
|
+
for (const [key, controller] of account.loading) {
|
|
346
|
+
if (account.wanted.has(key))
|
|
347
|
+
continue;
|
|
348
|
+
controller.abort();
|
|
349
|
+
account.loading.delete(key);
|
|
350
|
+
}
|
|
351
|
+
account.queue = list.filter((key) => !held.has(joinKey(id, key)) && !account.loading.has(key));
|
|
352
|
+
pump(id, account);
|
|
353
|
+
settle();
|
|
354
|
+
}
|
|
355
|
+
const found = new Map();
|
|
356
|
+
for (const key of list) {
|
|
357
|
+
const full = joinKey(id, key);
|
|
358
|
+
const one = held.get(full);
|
|
359
|
+
if (one === undefined)
|
|
360
|
+
continue;
|
|
361
|
+
held.delete(full);
|
|
362
|
+
held.set(full, one); // used now: last to go
|
|
363
|
+
found.set(key, one.tile);
|
|
364
|
+
}
|
|
365
|
+
return found;
|
|
366
|
+
},
|
|
367
|
+
get: (sourceId, key) => held.get(joinKey(sourceId, key))?.tile,
|
|
368
|
+
drop(sourceId) {
|
|
369
|
+
const account = accounts.get(sourceId);
|
|
370
|
+
if (account === undefined)
|
|
371
|
+
return;
|
|
372
|
+
accounts.delete(sourceId);
|
|
373
|
+
for (const controller of account.loading.values())
|
|
374
|
+
controller.abort();
|
|
375
|
+
account.loading.clear();
|
|
376
|
+
account.queue = [];
|
|
377
|
+
for (const full of [...held.keys()])
|
|
378
|
+
if (full.startsWith(`${sourceId}\u0000`))
|
|
379
|
+
release(full);
|
|
380
|
+
settle();
|
|
381
|
+
},
|
|
382
|
+
stats() {
|
|
383
|
+
let inFlight = 0;
|
|
384
|
+
let queued = 0;
|
|
385
|
+
for (const account of accounts.values()) {
|
|
386
|
+
inFlight += account.loading.size;
|
|
387
|
+
queued += account.queue.length;
|
|
388
|
+
}
|
|
389
|
+
return { held: held.size, heldBytes: counts.heldBytes, inFlight, queued, requested: counts.requested, failed: counts.failed, refused: counts.refused, evicted: counts.evicted };
|
|
390
|
+
},
|
|
391
|
+
idle: () => (busy() ? new Promise((resolve) => waiting.push(resolve)) : Promise.resolve()),
|
|
392
|
+
};
|
|
393
|
+
}
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import { type Credit, type DataSource } from "../data-door.ts";
|
|
2
|
+
import type { EarthMapSource, MapLayer } from "./map.ts";
|
|
3
|
+
import type { ElementSet } from "../satellites/types.ts";
|
|
4
|
+
/**
|
|
5
|
+
* Options of a view's source book.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```ts
|
|
9
|
+
* import type { SourceBookOptions } from "@johnmorrisdotca/chikyu-earth/door";
|
|
10
|
+
*
|
|
11
|
+
* const options: SourceBookOptions = { kinds: ["tiles"] };
|
|
12
|
+
* console.log(options.kinds);
|
|
13
|
+
* ```
|
|
14
|
+
*/
|
|
15
|
+
export interface SourceBookOptions {
|
|
16
|
+
/** The kinds of source this view can draw: `tiles` (a map of the Earth) and `records` (satellites' orbital elements). Both where left out. */
|
|
17
|
+
kinds?: readonly ("tiles" | "records")[];
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* What a view's sources have done: for each record source how many records it holds and how many were left out.
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* ```ts
|
|
24
|
+
* import type { SourceStats } from "@johnmorrisdotca/chikyu-earth/door";
|
|
25
|
+
*
|
|
26
|
+
* const kept = (stats: SourceStats): number => Object.keys(stats.records).length;
|
|
27
|
+
* console.log(typeof kept);
|
|
28
|
+
* ```
|
|
29
|
+
*/
|
|
30
|
+
export interface SourceStats {
|
|
31
|
+
records: Record<string, {
|
|
32
|
+
status: string;
|
|
33
|
+
kept: number;
|
|
34
|
+
skipped: number;
|
|
35
|
+
}>;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* A view's sources, as `<chikyu-earth-realistic>` and `<chikyu-passes>` keep them: `addSource`, `removeSource`, `enableSource` and `ownOnly` are the element's methods of the same names.
|
|
39
|
+
*
|
|
40
|
+
* @example
|
|
41
|
+
* ```ts
|
|
42
|
+
* import { createSourceBook } from "@johnmorrisdotca/chikyu-earth/door";
|
|
43
|
+
*
|
|
44
|
+
* const book = createSourceBook();
|
|
45
|
+
* console.log(book.sources.length, book.active().length);
|
|
46
|
+
* ```
|
|
47
|
+
*/
|
|
48
|
+
export interface SourceBook {
|
|
49
|
+
/** The sources now in the view, in the order added (the package's own folder of tiles is not among them unless a page added it). */
|
|
50
|
+
readonly sources: readonly DataSource[];
|
|
51
|
+
/** Adds a source. It throws a `TypeError` at once for a source `validateSource` refuses, an id already in the view, a kind this view cannot draw, or a map source with no layer. Returns the function that removes it. */
|
|
52
|
+
addSource(source: unknown): () => void;
|
|
53
|
+
/** Removes a source by id: aborts its reads, frees its records. Returns whether there was one. */
|
|
54
|
+
removeSource(id: string): boolean;
|
|
55
|
+
/** Switches a source off (`false`) or on: off aborts its read and frees its records, and the source stays in the view. */
|
|
56
|
+
enableSource(id: string, on: boolean): void;
|
|
57
|
+
/** True while a source is in the view and switched on. */
|
|
58
|
+
isEnabled(id: string): boolean;
|
|
59
|
+
/** `true` ignores every source that is not the package's own, at once (the attribute `data="own"`). */
|
|
60
|
+
ownOnly(on: boolean): void;
|
|
61
|
+
readonly onlyOwn: boolean;
|
|
62
|
+
/** The sources in use: in the view, switched on, and not ignored by `ownOnly`. */
|
|
63
|
+
active(): readonly DataSource[];
|
|
64
|
+
/** The map sources in use for a layer, the last one added first (it is the one the layer is drawn from). */
|
|
65
|
+
mapSources(layer: MapLayer): readonly EarthMapSource[];
|
|
66
|
+
/** The element sets of every record source in use, read so far, with the id of the source each came from, in the order added. */
|
|
67
|
+
elementLists(): readonly {
|
|
68
|
+
id: string;
|
|
69
|
+
sets: readonly ElementSet[];
|
|
70
|
+
}[];
|
|
71
|
+
/** True for a source the package made itself. */
|
|
72
|
+
isOwn(source: DataSource): boolean;
|
|
73
|
+
/** The source of this id, if it is in the view. */
|
|
74
|
+
get(id: string): DataSource | undefined;
|
|
75
|
+
/** The signal of a source: it aborts when the source is removed, switched off or ignored, so what a view started for it stops. */
|
|
76
|
+
signal(id: string): AbortSignal;
|
|
77
|
+
/** Calls `listener` when something changed that a view may want to draw again for: a source added, removed or switched, records read. Returns the function that stops it. */
|
|
78
|
+
onChange(listener: () => void): () => void;
|
|
79
|
+
stats(): SourceStats;
|
|
80
|
+
/** A number that changes whenever the sources in use change (a source read, switched, added or removed), so a view looks again only then. */
|
|
81
|
+
readonly version: number;
|
|
82
|
+
/** Resolves when every record source has been read. */
|
|
83
|
+
ready(): Promise<void>;
|
|
84
|
+
/** Frees everything: aborts every read and signal, frees every record. The book is not used again. */
|
|
85
|
+
dispose(): void;
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Makes the sources of one view.
|
|
89
|
+
*
|
|
90
|
+
* @param options - The kinds the view draws.
|
|
91
|
+
* @returns The book.
|
|
92
|
+
*
|
|
93
|
+
* @example
|
|
94
|
+
* ```ts
|
|
95
|
+
* import { createSourceBook } from "@johnmorrisdotca/chikyu-earth/door";
|
|
96
|
+
*
|
|
97
|
+
* const book = createSourceBook({ kinds: ["tiles"] });
|
|
98
|
+
* console.log(book.active().length);
|
|
99
|
+
* ```
|
|
100
|
+
*/
|
|
101
|
+
export declare function createSourceBook(options?: SourceBookOptions): SourceBook;
|
|
102
|
+
/**
|
|
103
|
+
* The credits of a view for the last drawn frame: the package's own first, then the sources a caller added, each with the marks or tiles it gave; a source that gave none is left out.
|
|
104
|
+
*
|
|
105
|
+
* @param own - The package's own credits (`ownCredit`), with how many marks each gave.
|
|
106
|
+
* @param book - The view's sources.
|
|
107
|
+
* @param used - How many marks or tiles each source of the book gave, by id.
|
|
108
|
+
* @returns The credits, own first, in a stable order.
|
|
109
|
+
*
|
|
110
|
+
* @example
|
|
111
|
+
* ```ts
|
|
112
|
+
* import { createSourceBook, creditsOf, ownCredit } from "@johnmorrisdotca/chikyu-earth/door";
|
|
113
|
+
*
|
|
114
|
+
* const book = createSourceBook();
|
|
115
|
+
* console.log(creditsOf([ownCredit("chikyu-pictures", 1)], book, new Map()).length);
|
|
116
|
+
* ```
|
|
117
|
+
*/
|
|
118
|
+
export declare function creditsOf(own: readonly Credit[], book: SourceBook, used: ReadonlyMap<string, number>): Credit[];
|