silphius/app/react/components/client-ecs.js
2024-07-30 09:56:53 -05:00

35 lines
829 B
JavaScript

import {LRUCache} from 'lru-cache';
import Ecs from '@/ecs/ecs.js';
import {withResolvers} from '@/util/promise.js';
const cache = new LRUCache({
max: 128,
});
export default class ClientEcs extends Ecs {
constructor(specification) {
super(specification);
[
'MaintainColliderHash',
].forEach((defaultSystem) => {
const System = this.system(defaultSystem);
if (System) {
System.active = true;
}
});
}
async readAsset(uri) {
if (!cache.has(uri)) {
const {promise, resolve, reject} = withResolvers();
cache.set(uri, promise);
fetch(new URL(uri, location.origin))
.then(async (response) => {
resolve(response.ok ? response.arrayBuffer() : new ArrayBuffer(0));
})
.catch(reject);
}
return cache.get(uri);
}
}