Skip to content

Commit

Permalink
tile fetch add LRU cache support
Browse files Browse the repository at this point in the history
  • Loading branch information
deyihu committed Nov 18, 2024
1 parent b6ee3ff commit abb2ed1
Show file tree
Hide file tree
Showing 2 changed files with 143 additions and 6 deletions.
108 changes: 108 additions & 0 deletions src/LRUCache.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@

const nullOnRemove = () => { };

class LRUCache {
constructor(max, onRemove) {
this.max = max;
this.onRemove = onRemove || nullOnRemove;
this.reset();
}

reset() {
if (this.data) {
const values = this.data.values();
for (const p of values) {
this.onRemove(p);
}
}

this.data = new Map();
return this;
}

clear() {
this.reset();
delete this.onRemove;
}

add(key, data) {
if (!data) {
return this;
}
if (this.has(key)) {
this.data.delete(key);
this.data.set(key, data);
if (this.data.size > this.max) {
this.shrink();
}
} else {
this.data.set(key, data);
if (this.data.size > this.max) {
this.shrink();
}
}

return this;
}

keys() {
const keys = new Array(this.data.size);
let i = 0;
const iterator = this.data.keys();
for (const k of iterator) {
keys[i++] = k;
}
return keys;
}

shrink() {
const iterator = this.data.keys();
let item = iterator.next();
while (this.data.size > this.max) {
const removedData = this.getAndRemove(item.value);
if (removedData) {
this.onRemove(removedData);
}
item = iterator.next();
}
}

has(key) {
return this.data.has(key);
}

getAndRemove(key) {
if (!this.has(key)) { return null; }

const data = this.data.get(key);
this.data.delete(key);
return data;
}

get(key) {
if (!this.has(key)) { return null; }

const data = this.data.get(key);
return data;
}

remove(key) {
if (!this.has(key)) { return this; }

const data = this.data.get(key);
this.data.delete(key);
this.onRemove(data);

return this;
}

setMaxSize(max) {
this.max = max;
if (this.data.size > this.max) {
this.shrink();
}
return this;
}
};

export default LRUCache;
41 changes: 35 additions & 6 deletions src/tileget.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { getCanvas, imageFilter, imageTileScale } from './canvas';
import LRUCache from './LRUCache';

const CANVAS_ERROR_MESSAGE = new Error('not find canvas.The current environment does not support OffscreenCanvas');

Expand All @@ -11,16 +12,45 @@ function isNumber(value) {
return typeof value === 'number';
}

const tileCache = new LRUCache(200, (image) => {
if (image && image.close) {
image.close();
}
});

function fetchTile(url, headers = {}) {
return new Promise((resolve, reject) => {
const copyImageBitMap = (image) => {
createImageBitmap(image).then(imagebit => {
resolve(imagebit);
}).catch(error => {
reject(error);
});
};
const image = tileCache.get(url);
if (image) {
copyImageBitMap(image);
} else {
fetch(url, {
headers
}).then(res => res.blob()).then(blob => createImageBitmap(blob)).then(image => {
tileCache.add(url, image);
copyImageBitMap(image);
}).catch(error => {
reject(error);
});
}
});
}

export function getTile(url, options = {}) {
return new Promise((resolve, reject) => {
if (!url) {
reject(new Error('url is null'));
return;
}
const headers = Object.assign({}, HEADERS, options.headers || {});
fetch(url, {
headers
}).then(res => res.blob()).then(blob => createImageBitmap(blob)).then(imagebit => {
fetchTile(url, headers).then(imagebit => {
const filter = options.filter;
if (filter) {
const canvas = getCanvas();
Expand Down Expand Up @@ -92,9 +122,8 @@ export function getTileWithMaxZoom(options = {}) {
}
const url = urlTemplate.replace('{x}', tileX).replace('{y}', tileY).replace('{z}', tileZ);
const headers = Object.assign({}, HEADERS, options.headers || {});
fetch(url, {
headers
}).then(res => res.blob()).then(blob => createImageBitmap(blob)).then(imagebit => {

fetchTile(url, headers).then(imagebit => {
let image;
const filter = options.filter;
if (filter) {
Expand Down

0 comments on commit abb2ed1

Please sign in to comment.