Files
openlayers/src/ol/render/canvas/LabelCache.js
2019-01-23 21:52:22 +01:00

70 lines
1.5 KiB
JavaScript

import {getUid} from '../../util.js';
import LRUCache from '../../structs/LRUCache.js';
/**
* @module ol/render/canvas/LabelCache
*/
/**
* @classdesc
* Cache of pre-rendered labels.
* @fires import("../events/Event.js").Event
*/
class LabelCache extends LRUCache {
/**
* @inheritDoc
*/
constructor(opt_highWaterMark) {
super(opt_highWaterMark);
this.consumers = {};
}
clear() {
super.clear();
this.consumers = {};
}
/**
* @override
* @param {string} key Label key.
* @param {import("./Executor.js").default} consumer Label consumer.
* @return {HTMLCanvasElement} Label.
*/
get(key, consumer) {
const canvas = super.get(key);
const consumerId = getUid(consumer);
if (!(consumerId in this.consumers)) {
this.consumers[consumerId] = {};
}
this.consumers[consumerId][key] = true;
return canvas;
}
prune() {
outer:
while (this.canExpireCache()) {
const key = this.peekLastKey();
for (const consumerId in this.consumers) {
if (key in this.consumers[consumerId]) {
break outer;
}
}
const canvas = this.pop();
canvas.width = canvas.height = 0;
for (const consumerId in this.consumers) {
delete this.consumers[consumerId][key];
}
}
}
/**
* @param {import("./Executor.js").default} consumer Label consumer.
*/
release(consumer) {
delete this.consumers[getUid(consumer)];
}
}
export default LabelCache;