luci: Using new function promiseAllDict

This commit is contained in:
remittor
2026-01-28 09:27:34 +03:00
parent 74836c93af
commit 7a6de4434f
2 changed files with 39 additions and 33 deletions

View File

@@ -48,16 +48,16 @@ return view.extend({
getAppStatus: function() getAppStatus: function()
{ {
return Promise.all([ return tools.promiseAllDict({
tools.getInitState(tools.appName), // svc_boot svc_boot : tools.getInitState(tools.appName),
fs.exec(tools.execPath, [ 'enabled' ]), // svc_en svc_en : fs.exec(tools.execPath, [ 'enabled' ]),
tools.getSvcInfo(), // svc_info svc_info : tools.getSvcInfo(),
fs.exec('/bin/busybox', [ 'ps' ]), // process list proc_list : fs.exec('/bin/busybox', [ 'ps' ]),
tools.getPackageDict(), // installed packages pkg_dict : tools.getPackageDict(),
tools.getStratList(), // nfqws strategy list strat_list : tools.getStratList(),
fs.exec('/bin/cat', [ '/etc/openwrt_release' ]), // CPU arch sys_info : fs.exec('/bin/cat', [ '/etc/openwrt_release' ]),
uci.load(tools.appName), // config uci_data : uci.load(tools.appName),
]).catch(e => { }).catch(e => {
ui.addNotification(null, E('p', _('Unable to execute or read contents') ui.addNotification(null, E('p', _('Unable to execute or read contents')
+ ': %s [ %s | %s | %s ]'.format( + ': %s [ %s | %s | %s ]'.format(
e.message, tools.execPath, 'tools.getInitState', 'uci.'+tools.appName e.message, tools.execPath, 'tools.getInitState', 'uci.'+tools.appName
@@ -65,42 +65,34 @@ return view.extend({
}); });
}, },
setAppStatus: function(status_array, elems = { }, force_app_status = 0) setAppStatus: function(data, elems = { }, force_app_status = 0)
{ {
tools.execDefferedAction(); tools.execDefferedAction();
let cfg = uci.get(tools.appName, 'config'); let cfg = uci.get(tools.appName, 'config');
if (!status_array || cfg == null || typeof(cfg) !== 'object') { if (!data || cfg == null || typeof(cfg) !== 'object') {
let elem_status = elems.status || document.getElementById("status"); let elem_status = elems.status || document.getElementById("status");
elem_status.innerHTML = tools.makeStatusString(null, '', ''); elem_status.innerHTML = tools.makeStatusString(null, '', '');
ui.addNotification(null, E('p', _('Unable to read the contents') + ': setAppStatus()')); ui.addNotification(null, E('p', _('Unable to read the contents') + ': setAppStatus()'));
this.disableButtons(true, -1, elems); this.disableButtons(true, -1, elems);
return; return;
} }
let svc_boot = status_array[0] ? true : false; let svc_boot = data.svc_boot ? true : false;
let svc_en = status_array[1]; // stdout: empty or error text this.nfqws_strat_list = data.strat_list;
let svc_info = status_array[2]; // dict for services this.pkg_arch = tools.getConfigPar(data.sys_info.stdout, 'DISTRIB_ARCH', 'unknown');
let proc_list = status_array[3]; // stdout: multiline text //console.log('svc_en: ' + data.svc_en.code + ' poll.running = ' + this.POLL.running);
let pkg_dict = status_array[4]; // stdout: installed packages let svc_en = (data.svc_en.code == 0) ? true : false;
let stratlist = status_array[5]; // array of strat names
let sys_info = status_array[6]; // stdout: openwrt distrib info
this.nfqws_strat_list = stratlist; if (typeof(data.svc_info) !== 'object') {
this.pkg_arch = tools.getConfigPar(sys_info.stdout, 'DISTRIB_ARCH', 'unknown');
//console.log('svc_en: ' + svc_en.code + ' poll.running = ' + this.POLL.running);
svc_en = (svc_en.code == 0) ? true : false;
if (typeof(svc_info) !== 'object') {
ui.addNotification(null, E('p', _('Unable to read the service info') + ': setAppStatus()')); ui.addNotification(null, E('p', _('Unable to read the service info') + ': setAppStatus()'));
this.disableButtons(true, -1, elems); this.disableButtons(true, -1, elems);
return; return;
} }
if (proc_list.code != 0) { if (data.proc_list.code != 0) {
ui.addNotification(null, E('p', _('Unable to read process list') + ': setAppStatus()')); ui.addNotification(null, E('p', _('Unable to read process list') + ': setAppStatus()'));
this.disableButtons(true, -1, elems); this.disableButtons(true, -1, elems);
return; return;
} }
if (!pkg_dict) { if (!data.pkg_dict) {
ui.addNotification(null, E('p', _('Unable to enumerate installed packages') + ': getPackageDict()')); ui.addNotification(null, E('p', _('Unable to enumerate installed packages') + ': getPackageDict()'));
this.disableButtons(true, -1, elems); this.disableButtons(true, -1, elems);
return; return;
@@ -109,7 +101,7 @@ return view.extend({
if (force_app_status) { if (force_app_status) {
svcinfo = force_app_status; svcinfo = force_app_status;
} else { } else {
svcinfo = tools.decode_svc_info(svc_en, svc_info, proc_list, cfg); svcinfo = tools.decode_svc_info(svc_en, data.svc_info, data.proc_list, cfg);
} }
let btn = this.get_svc_buttons(elems); let btn = this.get_svc_buttons(elems);
btn.reset.disabled = false; btn.reset.disabled = false;
@@ -302,14 +294,14 @@ return view.extend({
}); });
}, },
render: function(status_array) render: function(data)
{ {
if (!status_array) { if (!data) {
return; return;
} }
let cfg = uci.get(tools.appName, 'config'); let cfg = uci.get(tools.appName, 'config');
let pkgdict = status_array[4]; let pkgdict = data.pkg_dict;
if (pkgdict == null) { if (pkgdict == null) {
ui.addNotification(null, E('p', _('Unable to enumerate installed packages') + ': render()')); ui.addNotification(null, E('p', _('Unable to enumerate installed packages') + ': render()'));
return; return;
@@ -393,7 +385,7 @@ return view.extend({
"btn_diag": btn_diag, "btn_diag": btn_diag,
"btn_update": btn_update, "btn_update": btn_update,
}; };
this.setAppStatus(status_array, elems); this.setAppStatus(data, elems);
this.POLL.mode = 1; this.POLL.mode = 1;
this.POLL.init( L.bind(this.statusPoll, this), 2000 ); // interval 2 sec this.POLL.init( L.bind(this.statusPoll, this), 2000 ); // interval 2 sec

View File

@@ -193,6 +193,20 @@ return baseclass.extend({
} }
} }
}, },
promiseAllDict: function(promisesDict)
{
const keys = Object.keys(promisesDict);
const promises = keys.map(key => promisesDict[key]);
return Promise.all(promises)
.then(results => {
const resultDict = { };
keys.forEach((key, index) => {
resultDict[key] = results[index];
});
return resultDict;
});
},
baseLoad: function(ctx, callback) baseLoad: function(ctx, callback)
{ {