leaflet-vector-offline
Version:
Plugin for Leaflet.js that supports offline vector map tiles.
35 lines (34 loc) • 1.14 kB
JavaScript
import { getBlobByKey, hasTile } from "leaflet.offline";
export const reflect = (promise) => {
return promise.then((status) => {
return { status: "fulfilled", value: status.value };
}, (error) => {
return { status: "rejected", reason: error };
});
};
export const timer = (duration) => {
return new Promise((resolve) => {
setTimeout(() => {
resolve();
}, duration);
});
};
/**
* Fetches the tile image source from the cache if it exists, otherwise fetches
* it from the online url.
*
* This method is from leaflet.offline TileManager but it is not exported.
*
* @param key The key of the tile
* @param url The online url of the tile
* @returns The url of the tile image source and boolean where if `true`, means
* the tile was fetched from the URL. If `false`, tile was fetched from cache.
*/
export const getTileImageSource = async (key, url) => {
const shouldUseUrl = !(await hasTile(key));
if (shouldUseUrl) {
return [url, shouldUseUrl];
}
const blob = await getBlobByKey(key);
return [URL.createObjectURL(blob), shouldUseUrl];
};