@animech-public/playcanvas
Version:
PlayCanvas WebGL game engine
26 lines (24 loc) • 711 B
JavaScript
class DeviceCache {
constructor() {
this._cache = new Map();
}
get(device, onCreate) {
if (!this._cache.has(device)) {
this._cache.set(device, onCreate());
device.on('destroy', () => {
this.remove(device);
});
device.on('devicelost', () => {
var _this$_cache$get;
(_this$_cache$get = this._cache.get(device)) == null || _this$_cache$get.loseContext == null || _this$_cache$get.loseContext(device);
});
}
return this._cache.get(device);
}
remove(device) {
var _this$_cache$get2;
(_this$_cache$get2 = this._cache.get(device)) == null || _this$_cache$get2.destroy == null || _this$_cache$get2.destroy(device);
this._cache.delete(device);
}
}
export { DeviceCache };