From 4936526d1921d29a4e0e044a5e39f476ea337baa Mon Sep 17 00:00:00 2001 From: Brandon Liu Date: Tue, 8 Jun 2021 18:21:00 +0800 Subject: [PATCH] implement cancellation for maplibre fetch [#1] --- js/index.src.mjs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/js/index.src.mjs b/js/index.src.mjs index d62abda..b65d9c3 100644 --- a/js/index.src.mjs +++ b/js/index.src.mjs @@ -206,18 +206,22 @@ export const addProtocol = maplibre_instance => { let z = result[2] let x = result[3] let y = result[4] + var cancel = () => {} instance.getZxy(+z,+x,+y).then(val => { if (val) { let headers = {'Range':'bytes=' + val[0] + '-' + (val[0]+val[1]-1)} - fetch(pmtiles_url,{headers:headers}).then(resp => { + const controller = new AbortController() + const signal = controller.signal + fetch(pmtiles_url,{signal:signal,headers:headers}).then(resp => { return resp.arrayBuffer() }).then(arr => { callback(null,arr,null,null) }) + cancel = controller.abort } else { callback(null,new Uint8Array(),null,null) } }) - return { cancel: () => { console.log("Cancel not implemented") } } + return { cancel: cancel } }) }