playcanvas
Version:
PlayCanvas WebGL game engine
32 lines (30 loc) • 648 B
JavaScript
class RefCountedCache {
destroy() {
this.cache.forEach((refCount, object)=>{
object.destroy();
});
this.cache.clear();
}
incRef(object) {
const refCount = (this.cache.get(object) || 0) + 1;
this.cache.set(object, refCount);
}
decRef(object) {
if (object) {
let refCount = this.cache.get(object);
if (refCount) {
refCount--;
if (refCount === 0) {
this.cache.delete(object);
object.destroy();
} else {
this.cache.set(object, refCount);
}
}
}
}
constructor(){
this.cache = new Map();
}
}
export { RefCountedCache };