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