loading the root directory is lazy [#17]

This commit is contained in:
Brandon Liu
2021-11-15 17:11:03 +08:00
parent 2ebc616cbd
commit 99da15ff98

View File

@@ -39,6 +39,21 @@ const bytesToMap = dataview => {
export class PMTiles { export class PMTiles {
constructor(url) { constructor(url) {
this.url = url this.url = url
this.step = 0
this.leaves = new Map()
this.outstanding_requests = new Map()
}
metadata = func => {
return new Promise((resolve,reject) => {
this.getRoot().then(root => {
resolve(root.metadata)
})
})
}
getRoot = () => {
if (this.root) return this.root
const controller = new AbortController() const controller = new AbortController()
const signal = controller.signal const signal = controller.signal
this.root = fetch(this.url,{signal:signal, headers:{Range:'bytes=0-511999'}}).then(resp => { this.root = fetch(this.url,{signal:signal, headers:{Range:'bytes=0-511999'}}).then(resp => {
@@ -59,18 +74,7 @@ export class PMTiles {
dir:bytesToMap(new DataView(buf,10+header.json_size,17*header.root_entries)) dir:bytesToMap(new DataView(buf,10+header.json_size,17*header.root_entries))
} }
}) })
return this.root
this.step = 0
this.leaves = new Map()
this.outstanding_requests = new Map()
}
metadata = func => {
return new Promise((resolve,reject) => {
this.root.then(root => {
resolve(root.metadata)
})
})
} }
getLeaf = (offset, len) => { getLeaf = (offset, len) => {
@@ -109,7 +113,7 @@ export class PMTiles {
getZxy = (z,x,y) => { getZxy = (z,x,y) => {
var strid = z + '_' + x + '_' + y var strid = z + '_' + x + '_' + y
return this.root.then(root => { return this.getRoot().then(root => {
if (root.dir.has(strid) && root.dir.get(strid)[2] == 0) { if (root.dir.has(strid) && root.dir.get(strid)[2] == 0) {
return root.dir.get(strid) return root.dir.get(strid)
} else { } else {