refactor: move top-level await with glob imports

This commit is contained in:
cha0s 2024-11-04 22:13:51 -06:00
parent 0e8e58b365
commit e8c9633a4e
4 changed files with 18 additions and 15 deletions

View File

@ -1,3 +1,5 @@
import Script from '@/lib/script.js';
export async function computeMissing(current, manifest) {
const missing = [];
for (const path in manifest) {
@ -30,7 +32,7 @@ export async function fetchMissingResources(manifest, signal) {
}
}
}
loadResources(current);
await loadResources(current);
}
export async function fetchResources(paths, {signal} = {}) {
@ -53,7 +55,8 @@ export async function get() {
const cache = new Map();
export function loadResources(resources) {
export async function loadResources(resources) {
Script.registerScriptsModule(await import('@/lib/scripts.js'));
for (const path in resources) {
cache.set(path, resources[path].asset);
}

View File

@ -45,6 +45,12 @@ export default class Script {
this.registered[path] = fn;
}
static registerScriptsModule({default: scripts}) {
for (const path in scripts) {
Script.register(path, scripts[path]);
}
}
reset() {
this.iterator = null;
this.$$ticker = null;
@ -100,16 +106,8 @@ export default class Script {
}
function register({default: scripts}) {
for (const path in scripts) {
Script.register(path, scripts[path]);
}
}
register(await import('./scripts.js'));
if (import.meta.hot) {
import.meta.hot.accept('./scripts.js', (M) => {
register(M);
Script.registerScriptsModule(M);
});
}

View File

@ -14,8 +14,6 @@ const Flow = {
DOWN: 1,
};
await loadResources(await get());
const actions = new Map();
let ecs = new PredictionEcs({Components, Systems});
let mainEntityId = 0;
@ -162,5 +160,7 @@ onmessage = (event) => {
}
};
// sync with parent
postMessage(null);
loadResources(await get()).then(() => {
// sync with parent
postMessage(null);
});

View File

@ -6,6 +6,7 @@ import {
TPS,
UPS,
} from '@/lib/constants.js';
import Script from '@/lib/script.js';
import createEcs from './create/ecs.js';
import createForest from './create/forest.js';
@ -358,6 +359,7 @@ export default class Engine {
}
async load() {
Script.registerScriptsModule(await import('@/lib/scripts.js'));
await this.server.load();
let townData;
try {