UNPKG

playcanvas

Version:

Open-source WebGL/WebGPU 3D engine for the web

88 lines (87 loc) 2.68 kB
var __defProp = Object.defineProperty; var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value); import { Debug } from "./debug.js"; import { RefCountedObject } from "./ref-counted-object.js"; class Entry extends RefCountedObject { constructor(obj) { super(); __publicField(this, "object"); this.object = obj; this.incRefCount(); } } class RefCountedKeyCache { constructor() { /** * Map storing the cache. They key is a look up key for the object, the value is an instance * of the Entry class, which wraps the object with a reference count. * * {@type <object, Entry>} * @private */ __publicField(this, "cache", /* @__PURE__ */ new Map()); } /** * Destroy all stored objects. */ destroy() { this.cache.forEach((entry) => { entry.object?.destroy(); }); this.cache.clear(); } /** * Clear the cache, without destroying the objects. */ clear() { this.cache.clear(); } /** * Get the object from the cache with the given key, while incrementing the reference count. If * the object is not in the cache, returns null. * * @param {object} key - The key to look up the object. * @returns {object} The object, or null if not found. */ get(key) { const entry = this.cache.get(key); if (entry) { entry.incRefCount(); return entry.object; } return null; } /** * Put the object in the cache with the given key. The object cannot be in the cache already. * This sets its reference count to 1. * * @param {object} key - The key to store the object under. * @param {object} object - The object to store. */ set(key, object) { Debug.assert(!this.cache.has(key), "RefCountedKeyCache: Trying to put object with key that already exists in the cache", { key, object }); this.cache.set(key, new Entry(object)); } /** * Remove the object reference from the cache with the given key. If the reference count reaches * zero, the object is destroyed. * * @param {object} key - The key to remove the object under. */ release(key) { const entry = this.cache.get(key); if (entry) { entry.decRefCount(); if (entry.refCount === 0) { this.cache.delete(key); entry.object?.destroy(); } } else { Debug.warn("RefCountedKeyCache: Trying to release object that is not in the cache", { key }); } } } export { RefCountedKeyCache };