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

34 lines
806 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);
[
].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);
fetch(new URL(uri, window.location.origin))
.then(async (response) => {
resolve(response.ok ? response.arrayBuffer() : new ArrayBuffer(0));
})
.catch(reject);
}
return cache.get(uri);
}
}