js library has better organization

This commit is contained in:
Brandon Liu
2022-10-04 23:40:38 +08:00
parent 98311c1f12
commit 41b53c2c12
12 changed files with 136 additions and 39 deletions

98
js/adapters.ts Normal file
View File

@@ -0,0 +1,98 @@
declare const L: any;
import { PMTiles, Source } from "./index";
export const leafletRasterLayer = (source: PMTiles, options: any) => {
const cls = L.GridLayer.extend({
createTile: function (coord: any, done: any) {
const tile: any = document.createElement("img");
const controller = new AbortController();
const signal = controller.signal;
tile.cancel = () => {
controller.abort();
};
source.getZxy(coord.z, coord.x, coord.y).then((arr) => {
if (arr) {
const blob = new Blob([arr.data], { type: "image/png" });
const imageUrl = window.URL.createObjectURL(blob);
tile.src = imageUrl;
tile.cancel = null;
done(null, tile);
} else {
// return an empty image
}
});
return tile;
},
_removeTile: function (key: string) {
const tile = this._tiles[key];
if (!tile) {
return;
}
if (tile.el.cancel) tile.el.cancel();
tile.el.width = 0;
tile.el.height = 0;
tile.el.deleted = true;
L.DomUtil.remove(tile.el);
delete this._tiles[key];
this.fire("tileunload", {
tile: tile.el,
coords: this._keyToTileCoords(key),
});
},
});
return new cls(options);
};
export class Protocol {
tiles: Map<string, PMTiles>;
constructor() {
this.tiles = new Map<string, PMTiles>();
}
add(p: PMTiles) {
this.tiles.set(p.source.getKey(), p);
}
get(url: string) {
return this.tiles.get(url);
}
tileFunc = (params: any, callback: any) => {
const re = new RegExp(/pmtiles:\/\/(.+)\/(\d+)\/(\d+)\/(\d+)/);
const result = params.url.match(re);
const pmtiles_url = result[1];
let instance = this.tiles.get(pmtiles_url);
if (!instance) {
instance = new PMTiles(pmtiles_url);
this.tiles.set(pmtiles_url, instance);
}
const z = result[2];
const x = result[3];
const y = result[4];
const controller = new AbortController();
const signal = controller.signal;
let cancel = () => {
controller.abort();
};
instance.getZxy(+z, +x, +y, signal).then((arr) => {
if (arr) {
let data = new Uint8Array(arr.data);
callback(null, data, null, null);
} else {
callback(null, new Uint8Array(), null, null);
}
}).catch((e) => {
console.log(e);
});
return {
cancel: cancel
};
};
}