|
@@ -18,8 +18,14 @@
|
|
|
*/
|
|
*/
|
|
|
export const WARMUP_LEVELS = [15]
|
|
export const WARMUP_LEVELS = [15]
|
|
|
|
|
|
|
|
-/** 单次预热的瓦片上限:按 ~200KB/片估算,160 片约 30MB,属于「进区本来也要下」的量。 */
|
|
|
|
|
-export const WARMUP_MAX_TILES = 160
|
|
|
|
|
|
|
+/**
|
|
|
|
|
+ * 单次预热的瓦片上限。
|
|
|
|
|
+ *
|
|
|
|
|
+ * 进区首屏不是只有主场景矩形:12km / -55° 的视野会同时看到 ring0、ring1 和一部分 ring2。
|
|
|
|
|
+ * 实测三层 z15 合计 754 片、约 68MB(本地 GIS 统计),全部捂热才不会进区时现下。
|
|
|
|
|
+ * 上限留一点余量,但绝不把 ring3 的 z14(2900 片 / 383MB)拖进来。
|
|
|
|
|
+ */
|
|
|
|
|
+export const WARMUP_MAX_TILES = 800
|
|
|
|
|
|
|
|
const MAX_MERCATOR_LATITUDE = 85.0511287798066
|
|
const MAX_MERCATOR_LATITUDE = 85.0511287798066
|
|
|
|
|
|
|
@@ -108,8 +114,34 @@ function insideXyz(x, y, level, bounds) {
|
|
|
return x >= xs.from && x <= xs.to && y >= ys.from && y <= ys.to
|
|
return x >= xs.from && x <= xs.to && y >= ys.from && y <= ys.to
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+/** 借 `Image` 的解码缓存再捂一层:Cesium 拿到瓦片后还要解码,只捂 HTTP 缓存仍会卡首帧。 */
|
|
|
|
|
+function warmDecodedImage(url, ImageCtor, signal) {
|
|
|
|
|
+ if (typeof ImageCtor !== 'function') return Promise.resolve(true)
|
|
|
|
|
+ if (signal && signal.aborted) return Promise.resolve(false)
|
|
|
|
|
+ return new Promise((resolve) => {
|
|
|
|
|
+ const image = new ImageCtor()
|
|
|
|
|
+ let settled = false
|
|
|
|
|
+ const finish = (ok) => {
|
|
|
|
|
+ if (settled) return
|
|
|
|
|
+ settled = true
|
|
|
|
|
+ image.onload = null
|
|
|
|
|
+ image.onerror = null
|
|
|
|
|
+ resolve(ok)
|
|
|
|
|
+ }
|
|
|
|
|
+ image.onload = () => finish(true)
|
|
|
|
|
+ image.onerror = () => finish(false)
|
|
|
|
|
+ try {
|
|
|
|
|
+ const href = globalThis.location && globalThis.location.href
|
|
|
|
|
+ if (href && new URL(url, href).origin !== new URL(href).origin) image.crossOrigin = 'anonymous'
|
|
|
|
|
+ image.src = url
|
|
|
|
|
+ } catch {
|
|
|
|
|
+ finish(false)
|
|
|
|
|
+ }
|
|
|
|
|
+ })
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
- * 执行预热:有限并发取回 URL,借浏览器 HTTP 缓存供 Cesium 后续命中。
|
|
|
|
|
|
|
+ * 执行预热:有限并发取回 URL,借浏览器 HTTP 缓存 + Image 解码缓存供 Cesium 后续命中。
|
|
|
*
|
|
*
|
|
|
* 关键纪律:一个瓦片失败(网络抖动 / 404)只算这一片失败,绝不能让 worker 退出——
|
|
* 关键纪律:一个瓦片失败(网络抖动 / 404)只算这一片失败,绝不能让 worker 退出——
|
|
|
* 否则偶发抖动会让整批预热静默少一大半,进区时就概率性看到糊图。
|
|
* 否则偶发抖动会让整批预热静默少一大半,进区时就概率性看到糊图。
|
|
@@ -117,6 +149,7 @@ function insideXyz(x, y, level, bounds) {
|
|
|
* @param {string[]} urls `planTileWarmup()` 的输出
|
|
* @param {string[]} urls `planTileWarmup()` 的输出
|
|
|
* @param {{
|
|
* @param {{
|
|
|
* fetchImpl?: typeof fetch,
|
|
* fetchImpl?: typeof fetch,
|
|
|
|
|
+ * ImageCtor?: typeof Image,
|
|
|
* concurrency?: number,
|
|
* concurrency?: number,
|
|
|
* retries?: number,
|
|
* retries?: number,
|
|
|
* signal?: AbortSignal,
|
|
* signal?: AbortSignal,
|
|
@@ -126,6 +159,7 @@ function insideXyz(x, y, level, bounds) {
|
|
|
*/
|
|
*/
|
|
|
export async function runTileWarmup(urls, options = {}) {
|
|
export async function runTileWarmup(urls, options = {}) {
|
|
|
const fetchImpl = options.fetchImpl || globalThis.fetch
|
|
const fetchImpl = options.fetchImpl || globalThis.fetch
|
|
|
|
|
+ const ImageCtor = options.ImageCtor || globalThis.Image
|
|
|
const concurrency = Number.isInteger(options.concurrency) && options.concurrency > 0 ? options.concurrency : 4
|
|
const concurrency = Number.isInteger(options.concurrency) && options.concurrency > 0 ? options.concurrency : 4
|
|
|
const retries = Number.isInteger(options.retries) && options.retries >= 0 ? options.retries : 1
|
|
const retries = Number.isInteger(options.retries) && options.retries >= 0 ? options.retries : 1
|
|
|
const signal = options.signal
|
|
const signal = options.signal
|
|
@@ -146,7 +180,7 @@ export async function runTileWarmup(urls, options = {}) {
|
|
|
if (response && typeof response.arrayBuffer === 'function') await response.arrayBuffer()
|
|
if (response && typeof response.arrayBuffer === 'function') await response.arrayBuffer()
|
|
|
// 4xx/5xx 立刻记失败:同一 URL 短时间重试不会变好,只是白占带宽。
|
|
// 4xx/5xx 立刻记失败:同一 URL 短时间重试不会变好,只是白占带宽。
|
|
|
if (response && response.ok === false) return false
|
|
if (response && response.ok === false) return false
|
|
|
- return true
|
|
|
|
|
|
|
+ return warmDecodedImage(url, ImageCtor, signal)
|
|
|
} catch (error) {
|
|
} catch (error) {
|
|
|
if (signal && signal.aborted) return false
|
|
if (signal && signal.aborted) return false
|
|
|
if (attempt === retries) return false
|
|
if (attempt === retries) return false
|