silphius/app/react/components/client-ecs.js

35 lines
829 B
JavaScript
Raw Normal View History

2024-07-03 11:17:36 -05:00
import {LRUCache} from 'lru-cache';
2024-07-21 03:01:12 -05:00
import Ecs from '@/ecs/ecs.js';
2024-07-22 00:13:03 -05:00
import {withResolvers} from '@/util/promise.js';
2024-07-21 03:01:12 -05:00
2024-07-03 11:17:36 -05:00
const cache = new LRUCache({
max: 128,
});
export default class ClientEcs extends Ecs {
2024-07-23 17:04:17 -05:00
constructor(specification) {
super(specification);
[
2024-07-28 18:40:58 -05:00
'MaintainColliderHash',
2024-07-23 17:04:17 -05:00
].forEach((defaultSystem) => {
const System = this.system(defaultSystem);
if (System) {
System.active = true;
}
});
}
2024-07-03 11:17:36 -05:00
async readAsset(uri) {
if (!cache.has(uri)) {
2024-07-22 00:13:03 -05:00
const {promise, resolve, reject} = withResolvers();
2024-07-03 11:17:36 -05:00
cache.set(uri, promise);
2024-07-30 09:56:53 -05:00
fetch(new URL(uri, location.origin))
2024-07-03 11:17:36 -05:00
.then(async (response) => {
resolve(response.ok ? response.arrayBuffer() : new ArrayBuffer(0));
})
.catch(reject);
}
return cache.get(uri);
}
}