cloudcmd/client/dom/load-remote.js
2026-07-15 23:26:28 +03:00

58 lines
1.3 KiB
JavaScript

/* global CloudCmd */
import {callbackify} from 'node:util';
import {rendy} from 'rendy';
import itype from 'itype';
import * as load from 'load.js';
import {tryToCatch} from 'try-to-catch';
import {findObjByNameInArr} from '#common/util';
import * as Files from '#dom/files';
export const loadRemote = callbackify(async (name, options) => {
const {prefix, config} = CloudCmd;
const o = options;
if (o.name && window[o.name])
return;
const modules = await Files.get('modules');
const online = config('online') && navigator.onLine;
const module = findObjByNameInArr(modules.remote, name);
const isArray = itype.array(module.local);
const {version} = module;
let remoteTmpls;
let local;
if (isArray) {
remoteTmpls = module.remote;
({local} = module);
} else {
remoteTmpls = [module.remote];
local = [module.local];
}
const localURL = [];
for (const url of local) {
localURL.push(prefix + url);
}
const remoteURL = [];
for (const tmpl of remoteTmpls) {
remoteURL.push(rendy(tmpl, {
version,
}));
}
if (online) {
const [e] = await tryToCatch(load.parallel, remoteURL);
if (!e)
return;
}
await load.parallel(localURL);
});