UNPKG

playcanvas

Version:

PlayCanvas WebGL game engine

47 lines (45 loc) 1.96 kB
/** * @import { GraphicsDevice } from './graphics-device.js' */ /** * A cache storing shared resources associated with a device. The resources are removed * from the cache when the device is destroyed. */ class DeviceCache { /** * Returns the resources for the supplied device. * * @param {GraphicsDevice} device - The graphics device. * @param {() => any} onCreate - A function that creates the resource for the device. * @returns {any} The resource for the device. */ get(device, onCreate) { if (!this._cache.has(device)) { this._cache.set(device, onCreate()); // when the device is destroyed, destroy and remove its entry device.on('destroy', ()=>{ this.remove(device); }); // when the context is lost, call optional loseContext on its entry device.on('devicelost', ()=>{ var _this__cache_get_loseContext, _this__cache_get; (_this__cache_get = this._cache.get(device)) == null ? void 0 : (_this__cache_get_loseContext = _this__cache_get.loseContext) == null ? void 0 : _this__cache_get_loseContext.call(_this__cache_get, device); }); } return this._cache.get(device); } /** * Destroys and removes the content of the cache associated with the device * * @param {GraphicsDevice} device - The graphics device. */ remove(device) { var _this__cache_get_destroy, _this__cache_get; (_this__cache_get = this._cache.get(device)) == null ? void 0 : (_this__cache_get_destroy = _this__cache_get.destroy) == null ? void 0 : _this__cache_get_destroy.call(_this__cache_get, device); this._cache.delete(device); } constructor(){ /** * Cache storing the resource for each GraphicsDevice * * @type {Map<GraphicsDevice, any>} */ this._cache = new Map(); } } export { DeviceCache };