asyncify init_*

This commit is contained in:
Stewart Allen 2025-12-25 00:50:59 -05:00
commit 2bb67ab457
2 changed files with 46 additions and 48 deletions

View file

@ -33,9 +33,9 @@ import STACKS from './stacks.js';
let { LOCAL, SETUP } = api, let { LOCAL, SETUP } = api,
{ CAM, SLA, FDM, LASER, DRAG, WJET, WEDM } = MODES, { CAM, SLA, FDM, LASER, DRAG, WJET, WEDM } = MODES,
{ client, catalog, platform, selection, stats } = api, { client, catalog, platform, selection, stats } = api,
LANG = api.language.current,
WIN = self.window,
DOC = self.document, DOC = self.document,
WIN = self.window,
LANG = api.language.current,
STARTMODE = SETUP.sm && SETUP.sm.length === 1 ? SETUP.sm[0] : null, STARTMODE = SETUP.sm && SETUP.sm.length === 1 ? SETUP.sm[0] : null,
TWOD = [ LASER, DRAG, WJET, WEDM ], TWOD = [ LASER, DRAG, WJET, WEDM ],
TWONED = [ LASER, DRAG, WJET ], TWONED = [ LASER, DRAG, WJET ],
@ -46,8 +46,6 @@ let { LOCAL, SETUP } = api,
NO_WEDM = [ FDM, CAM, SLA, LASER, DRAG, WJET ], NO_WEDM = [ FDM, CAM, SLA, LASER, DRAG, WJET ],
FDM_CAM = [ FDM, CAM ], FDM_CAM = [ FDM, CAM ],
proto = location.protocol, proto = location.protocol,
platformColor,
statsTimer,
inline = true, inline = true,
driven = true, driven = true,
trigger = true, trigger = true,
@ -120,17 +118,11 @@ function onButtonClick(ev) {
api.event.on('click.button', onButtonClick); api.event.on('click.button', onButtonClick);
function keys(o) { // function keys(o) {
let key, list = []; // let key, list = [];
for (key in o) { if (o.hasOwnProperty(key)) list.push(key) } // for (key in o) { if (o.hasOwnProperty(key)) list.push(key) }
return list.sort(); // return list.sort();
} // }
function clearSelected(children) {
for (let i=0; i<children.length; i++) {
children[i].setAttribute('class','');
}
}
function isBelt() { function isBelt() {
return api.device.isBelt(); return api.device.isBelt();
@ -690,11 +682,13 @@ function init_one() {
} }
}; };
api.space.restore(init_two) || checkSeed(init_two) || init_two(); return new Promise(resolve => {
api.space.restore(resolve) || checkSeed(resolve) || resolve();
});
}; };
// SECOND STAGE INIT AFTER UI RESTORED // SECOND STAGE INIT AFTER UI RESTORED
function init_two() { async function init_two() {
api.event.emit('init.two'); api.event.emit('init.two');
// load script extensions // load script extensions
@ -906,40 +900,44 @@ function lang_rewrite() {
// init lang must happen before all other init functions // init lang must happen before all other init functions
function init_lang() { function init_lang() {
// if a language needs to load, the script is injected and loaded return new Promise(resolve => {
// first. once this loads, or doesn't, the initialization begins // if a language needs to load, the script is injected and loaded
let lang = SETUP.ln ? SETUP.ln[0] : sdb.getItem('kiri-lang') || api.language.get(); // first. once this loads, or doesn't, the initialization begins
api.event.emit('init.lang', lang); let lang = SETUP.ln ? SETUP.ln[0] : sdb.getItem('kiri-lang') || api.language.get();
// inject language script if not english api.event.emit('init.lang', lang);
if (lang && lang !== 'en' && lang !== 'en-us') { // inject language script if not english
let map = api.language.map(lang); if (lang && lang !== 'en' && lang !== 'en-us') {
let scr = DOC.createElement('script'); let map = api.language.map(lang);
// scr.setAttribute('defer',true); let scr = DOC.createElement('script');
scr.setAttribute('src',`/kiri/lang/${map}.js`); // scr.setAttribute('defer',true);
(DOC.body || DOC.head).appendChild(scr); scr.setAttribute('src',`/kiri/lang/${map}.js`);
stats.set('ll',lang); (DOC.body || DOC.head).appendChild(scr);
scr.onload = function() { stats.set('ll',lang);
api.language.set(map); scr.onload = function() {
lang_rewrite(); api.language.set(map);
init_one(); lang_rewrite();
}; resolve();
scr.onerror = function(err) { };
console.log({language_load_error: err, lang}) scr.onerror = function(err) {
console.log({language_load_error: err, lang})
api.language.set();
lang_rewrite();
resolve();
}
} else {
// set to browser default which will be overridden
// by any future script loads (above)
api.language.set(); api.language.set();
lang_rewrite(); lang_rewrite();
init_one(); resolve();
} }
} else { });
// set to browser default which will be overridden
// by any future script loads (above)
api.language.set();
lang_rewrite();
init_one();
}
} }
export function run() { export async function run() {
client.start(); client.start();
init_lang(); await init_lang();
await init_one();
await init_two();
return api; return api;
} }

View file

@ -35,11 +35,11 @@ function safeExec(fn) {
} }
} }
function checkReady() { async function checkReady() {
if (document.readyState === 'complete') { if (document.readyState === 'complete') {
let bootctrl = navigator.serviceWorker.controller; let bootctrl = navigator.serviceWorker.controller;
console.log(`kiri | boot ctrl | ` + (bootctrl ? true : false)); console.log(`kiri | boot ctrl | ` + (bootctrl ? true : false));
let api = kiri.api = run(); let api = kiri.api = await run();
self.$ = api.web.$; self.$ = api.web.$;
for (let fn of load) { for (let fn of load) {
safeExec(fn); safeExec(fn);