playcanvas
Version:
Open-source WebGL/WebGPU 3D engine for the web
23 lines (22 loc) • 479 B
JavaScript
class DeviceCache {
_cache = /* @__PURE__ */ new Map();
get(device, onCreate) {
if (!this._cache.has(device)) {
this._cache.set(device, onCreate());
device.on("destroy", () => {
this.remove(device);
});
device.on("devicelost", () => {
this._cache.get(device)?.loseContext?.(device);
});
}
return this._cache.get(device);
}
remove(device) {
this._cache.get(device)?.destroy?.(device);
this._cache.delete(device);
}
}
export {
DeviceCache
};