From b108831159ef5264e3c3afb04d2d2220b5a87164 Mon Sep 17 00:00:00 2001 From: Brandon Liu Date: Wed, 5 Oct 2022 17:22:37 +0800 Subject: [PATCH] js v2 compatibility shim supports tile cancellation --- js/adapters.ts | 11 ++++++----- js/index.ts | 2 +- js/v2.ts | 7 ++++--- 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/js/adapters.ts b/js/adapters.ts index 4b1448a..d335b21 100644 --- a/js/adapters.ts +++ b/js/adapters.ts @@ -81,15 +81,16 @@ export class Protocol { controller.abort(); }; - instance.getZxy(+z, +x, +y, signal).then((arr) => { - if (arr) { - let data = new Uint8Array(arr.data); - callback(null, data, null, null); + instance.getZxy(+z, +x, +y, signal).then((resp) => { + if (resp) { + callback(null, new Uint8Array(resp.data), resp.cacheControl, resp.expires); } else { callback(null, new Uint8Array(), null, null); } }).catch((e) => { - console.log(e); + if (e.name !== "AbortError") { + throw e; + } }); return { cancel: cancel diff --git a/js/index.ts b/js/index.ts index 774e8fd..d44de89 100644 --- a/js/index.ts +++ b/js/index.ts @@ -780,7 +780,7 @@ export class PMTiles { // V2 COMPATIBILITY if (header.specVersion < 3) { - return v2.getZxy(header, this.source, this.cache, z, x, y); + return v2.getZxy(header, this.source, this.cache, z, x, y, signal); } if (z < header.minZoom || z > header.maxZoom) { diff --git a/js/v2.ts b/js/v2.ts index 5035ff7..d40afb6 100644 --- a/js/v2.ts +++ b/js/v2.ts @@ -261,7 +261,8 @@ async function getZxy( cache: Cache, z: number, x: number, - y: number + y: number, + signal?: AbortSignal ): Promise { let root_dir = await cache.getArrayBuffer( source, @@ -275,7 +276,7 @@ async function getZxy( const entry = queryTile(new DataView(root_dir), z, x, y); if (entry) { - let resp = await source.getBytes(entry.offset, entry.length); // TODO signal + let resp = await source.getBytes(entry.offset, entry.length, signal); let tile_data = resp.data; let view = new DataView(tile_data); @@ -309,7 +310,7 @@ async function getZxy( } let tile_entry = queryTile(new DataView(leaf_dir), z, x, y); if (tile_entry) { - let resp = await source.getBytes(tile_entry.offset, tile_entry.length); // TODO signal + let resp = await source.getBytes(tile_entry.offset, tile_entry.length, signal); let tile_data = resp.data; let view = new DataView(tile_data);