From e8c9633a4eb32b2ff2128f511edf931b7d3e566e Mon Sep 17 00:00:00 2001 From: cha0s Date: Mon, 4 Nov 2024 22:13:51 -0600 Subject: [PATCH] refactor: move top-level await with glob imports --- app/lib/resources.js | 7 +++++-- app/lib/script.js | 16 +++++++--------- app/silphius/client/predictor.js | 8 ++++---- app/silphius/server/engine.js | 2 ++ 4 files changed, 18 insertions(+), 15 deletions(-) diff --git a/app/lib/resources.js b/app/lib/resources.js index 04f22c7..689c25e 100644 --- a/app/lib/resources.js +++ b/app/lib/resources.js @@ -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); } diff --git a/app/lib/script.js b/app/lib/script.js index fc2d125..f625a2f 100644 --- a/app/lib/script.js +++ b/app/lib/script.js @@ -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); }); } diff --git a/app/silphius/client/predictor.js b/app/silphius/client/predictor.js index 0edd31e..5412608 100644 --- a/app/silphius/client/predictor.js +++ b/app/silphius/client/predictor.js @@ -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); +}); diff --git a/app/silphius/server/engine.js b/app/silphius/server/engine.js index 5e60a4b..e407dfb 100644 --- a/app/silphius/server/engine.js +++ b/app/silphius/server/engine.js @@ -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 {