File size: 3,732 Bytes
bc20498 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
import Map from '../../../map';
import Set from'../../../set';
import * as util from '../../../util';
// Allows lookups for (ele, lvl) => cache.
// Uses keys so elements may share the same cache.
class ElementTextureCacheLookup {
constructor(getKey, doesEleInvalidateKey = util.falsify){
this.idsByKey = new Map();
this.keyForId = new Map();
this.cachesByLvl = new Map();
this.lvls = [];
this.getKey = getKey;
this.doesEleInvalidateKey = doesEleInvalidateKey;
}
getIdsFor(key){
if( key == null ){
util.error(`Can not get id list for null key`);
}
let { idsByKey } = this;
let ids = this.idsByKey.get(key);
if( !ids ){
ids = new Set();
idsByKey.set(key, ids);
}
return ids;
}
addIdForKey(key, id){
if( key != null ){
this.getIdsFor(key).add(id);
}
}
deleteIdForKey(key, id){
if( key != null ){
this.getIdsFor(key).delete(id);
}
}
getNumberOfIdsForKey(key){
if( key == null ){
return 0;
} else {
return this.getIdsFor(key).size;
}
}
updateKeyMappingFor(ele){
let id = ele.id();
let prevKey = this.keyForId.get(id);
let currKey = this.getKey(ele);
this.deleteIdForKey(prevKey, id);
this.addIdForKey(currKey, id);
this.keyForId.set(id, currKey);
}
deleteKeyMappingFor(ele){
let id = ele.id();
let prevKey = this.keyForId.get(id);
this.deleteIdForKey(prevKey, id);
this.keyForId.delete(id);
}
keyHasChangedFor(ele){
let id = ele.id();
let prevKey = this.keyForId.get(id);
let newKey = this.getKey(ele);
return prevKey !== newKey;
}
isInvalid(ele){
return this.keyHasChangedFor(ele) || this.doesEleInvalidateKey(ele);
}
getCachesAt(lvl){
let { cachesByLvl, lvls } = this;
let caches = cachesByLvl.get(lvl);
if( !caches ){
caches = new Map();
cachesByLvl.set(lvl, caches);
lvls.push(lvl);
}
return caches;
}
getCache(key, lvl){
return this.getCachesAt(lvl).get(key);
}
get(ele, lvl){
let key = this.getKey(ele);
let cache = this.getCache(key, lvl);
// getting for an element may need to add to the id list b/c eles can share keys
if( cache != null ){
this.updateKeyMappingFor(ele);
}
return cache;
}
getForCachedKey(ele, lvl){
let key = this.keyForId.get(ele.id()); // n.b. use cached key, not newly computed key
let cache = this.getCache(key, lvl);
return cache;
}
hasCache(key, lvl){
return this.getCachesAt(lvl).has(key);
}
has(ele, lvl){
let key = this.getKey(ele);
return this.hasCache(key, lvl);
}
setCache(key, lvl, cache){
cache.key = key;
this.getCachesAt(lvl).set(key, cache);
}
set(ele, lvl, cache){
let key = this.getKey(ele);
this.setCache(key, lvl, cache);
this.updateKeyMappingFor(ele);
}
deleteCache(key, lvl){
this.getCachesAt(lvl).delete(key);
}
delete(ele, lvl){
let key = this.getKey(ele);
this.deleteCache(key, lvl);
}
invalidateKey(key){
this.lvls.forEach( lvl => this.deleteCache(key, lvl) );
}
// returns true if no other eles reference the invalidated cache (n.b. other eles may need the cache with the same key)
invalidate(ele){
let id = ele.id();
let key = this.keyForId.get(id); // n.b. use stored key rather than current (potential key)
this.deleteKeyMappingFor(ele);
let entireKeyInvalidated = this.doesEleInvalidateKey(ele);
if( entireKeyInvalidated ){ // clear mapping for current key
this.invalidateKey(key);
}
return entireKeyInvalidated || this.getNumberOfIdsForKey(key) === 0;
}
}
export default ElementTextureCacheLookup;
|