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) { export async function computeMissing(current, manifest) {
const missing = []; const missing = [];
for (const path in manifest) { 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} = {}) { export async function fetchResources(paths, {signal} = {}) {
@ -53,7 +55,8 @@ export async function get() {
const cache = new Map(); 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) { for (const path in resources) {
cache.set(path, resources[path].asset); cache.set(path, resources[path].asset);
} }

View File

@ -45,6 +45,12 @@ export default class Script {
this.registered[path] = fn; this.registered[path] = fn;
} }
static registerScriptsModule({default: scripts}) {
for (const path in scripts) {
Script.register(path, scripts[path]);
}
}
reset() { reset() {
this.iterator = null; this.iterator = null;
this.$$ticker = 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) { if (import.meta.hot) {
import.meta.hot.accept('./scripts.js', (M) => { import.meta.hot.accept('./scripts.js', (M) => {
register(M); Script.registerScriptsModule(M);
}); });
} }

View File

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

View File

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