luci-base: network.js: add ability to flush the internal cache

Signed-off-by: Jo-Philipp Wich <jo@mein.io>
This commit is contained in:
Jo-Philipp Wich 2019-07-31 08:33:52 +02:00
parent d1e9841e86
commit 3e5ef065df

View file

@ -84,11 +84,12 @@ var callNetworkDeviceStatus = rpc.declare({
});
var _cache = {},
_flush = true,
_state = null,
_protocols = {};
function getWifiState() {
if (_cache.wifi == null)
function getWifiState(flush) {
if (_cache.wifi == null || flush)
return callNetworkWirelessStatus().then(function(state) {
if (!L.isObject(state))
throw !1;
@ -100,8 +101,8 @@ function getWifiState() {
return Promise.resolve(_cache.wifi);
}
function getInterfaceState() {
if (_cache.interfacedump == null)
function getInterfaceState(flush) {
if (_cache.interfacedump == null || flush)
return callNetworkInterfaceStatus().then(function(state) {
if (!Array.isArray(state))
throw !1;
@ -113,8 +114,8 @@ function getInterfaceState() {
return Promise.resolve(_cache.interfacedump);
}
function getDeviceState() {
if (_cache.devicedump == null)
function getDeviceState(flush) {
if (_cache.devicedump == null || flush)
return callNetworkDeviceStatus().then(function(state) {
if (!L.isObject(state))
throw !1;
@ -126,8 +127,8 @@ function getDeviceState() {
return Promise.resolve(_cache.devicedump);
}
function getIfaddrState() {
if (_cache.ifaddrs == null)
function getIfaddrState(flush) {
if (_cache.ifaddrs == null || flush)
return callLuciIfaddrs().then(function(addrs) {
if (!Array.isArray(addrs))
throw !1;
@ -139,8 +140,8 @@ function getIfaddrState() {
return Promise.resolve(_cache.ifaddrs);
}
function getNetdevState() {
if (_cache.devices == null)
function getNetdevState(flush) {
if (_cache.devices == null || flush)
return callLuciNetdevs().then(function(state) {
if (!L.isObject(state))
throw !1;
@ -152,8 +153,8 @@ function getNetdevState() {
return Promise.resolve(_cache.devices);
}
function getBoardState() {
if (_cache.board == null)
function getBoardState(flush) {
if (_cache.board == null || flush)
return callLuciBoardjson().then(function(state) {
if (!L.isObject(state))
throw !1;
@ -414,10 +415,19 @@ function maskToPrefix(mask, v6) {
}
function initNetworkState() {
if (_state == null)
return (_state = Promise.all([
getInterfaceState(), getDeviceState(), getBoardState(),
getWifiState(), getIfaddrState(), getNetdevState(),
var flush = _flush;
_flush = false;
if (_state != null && !flush)
return Promise.resolve(_state);
if (_cache.pendingInit != null)
return Promise.resolve(_cache.pendingInit);
return (_cache.pendingInit = Promise.all([
getInterfaceState(flush), getDeviceState(flush), getBoardState(flush),
getWifiState(flush), getIfaddrState(flush), getNetdevState(flush), getProtocolHandlers(flush),
uci.load('network'), uci.load('wireless'), uci.load('luci')
]).finally(function() {
var ifaddrs = _cache.ifaddrs,
@ -544,10 +554,10 @@ function initNetworkState() {
}
}
delete _cache.pendingInit;
return (_state = s);
}));
return Promise.resolve(_state);
}
function ifnameOf(obj) {
@ -580,6 +590,12 @@ function deviceSort(a, b) {
var Network, Protocol, Device, WifiDevice, WifiNetwork;
Network = L.Class.extend({
flushCache: function() {
return Promise.resolve(_state).then(function() {
_flush = true;
});
},
getProtocol: function(protoname, netname) {
var v = _protocols[protoname];
if (v != null)