Skip to content

Commit

Permalink
fix(data): connect source when config is update
Browse files Browse the repository at this point in the history
fix #7
  • Loading branch information
orblazer committed Oct 24, 2021
1 parent c636423 commit 0e89d8d
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 37 deletions.
18 changes: 18 additions & 0 deletions package/contents/ui/components/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,24 @@ function addGraphData(model, graphItemPercent, graphGranularity) {
}


/**
* Rate limit call of function
* @param {function} func The function want rate limit
* @param {number} [limit] The rate limit be seconds
* @returns The caller of 'func'
*/
function rateLimit(func, limit = 1) {
var limitStartTime = Date.now()

return function (...args) {
// magic to limit to x frames per second
if (Date.now() - limitStartTime >= limit) {
limitStartTime = limitStartTime + limit
func.apply(this, args)
}
}
}

/**
* Get the usage int percent
* @param {number} current The current usage
Expand Down
85 changes: 48 additions & 37 deletions package/contents/ui/main.qml
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,20 @@ Item {
}
}

onShowCpuMonitorChanged: dataSourceChanged()
onShowClockChanged: {
dataSourceChanged()
cpuMonitor.secondLineValueLabel.visible = showClock
}

onShowRamMonitorChanged: dataSourceChanged()
onShowSwapGraphChanged: {
dataSourceChanged()
ramMonitor.secondLineValueLabel.visible = showSwapGraph
}
onShowNetMonitorChanged: dataSourceChanged()
property var dataSourceChanged: Functions.rateLimit(function () {
dataSource.sources.forEach(refreshSource)
}, 1)

// Graph data

Expand Down Expand Up @@ -187,48 +194,52 @@ Item {
}
}
onSourceAdded: {
var needConnect = false
refreshSource(source)
}
}

if (source === dataSource.totalLoad) {
needConnect = showCpuMonitor
}
else if (source === dataSource.averageClock) {
needConnect = showClock
}
else if (source === dataSource.memFree || source === dataSource.memUsed || source === dataSource.memApplication) {
needConnect = showRamMonitor
}
else if (source === dataSource.swapUsed || source === dataSource.swapFree) {
needConnect = showSwapGraph
}
else if (dataSource.networkRegex.test(source)) {
// Match network sources
var match
if (plasmoid.configuration.networkSensorInterface === '') {
match = source.match(dataSource.networkRegex)
} else {
match = source.match(new RegExp('/^network\/interfaces\/' +
plasmoid.configuration.networkSensorInterface.replace(/[.*+?^${}()|[\]\\]/g, '\\$&') +
'\/(transmitter|receiver)\/data$/'))
}
function refreshSource(source) {
var needConnect = false

if (match != null) {
if (match[1] === 'receiver') {
dataSource.downloadTotal = source
} else {
dataSource.uploadTotal = source
}
needConnect = showNetMonitor
}
if (source === dataSource.totalLoad) {
needConnect = showCpuMonitor
}
else if (source === dataSource.averageClock) {
needConnect = showClock
}
else if (source === dataSource.memFree || source === dataSource.memUsed || source === dataSource.memApplication) {
needConnect = showRamMonitor
}
else if (source === dataSource.swapUsed || source === dataSource.swapFree) {
needConnect = showSwapGraph
}
else if (dataSource.networkRegex.test(source)) {
// Match network sources
var match
if (plasmoid.configuration.networkSensorInterface === '') {
match = source.match(dataSource.networkRegex)
} else {
return
match = source.match(new RegExp('/^network\/interfaces\/' +
plasmoid.configuration.networkSensorInterface.replace(/[.*+?^${}()|[\]\\]/g, '\\$&') +
'\/(transmitter|receiver)\/data$/'))
}

if (needConnect) {
dataSource.connectSource(source)
} else {
dataSource.disconnectSource(source)
if (match != null) {
if (match[1] === 'receiver') {
dataSource.downloadTotal = source
} else {
dataSource.uploadTotal = source
}
needConnect = showNetMonitor
}
} else {
return
}

if (needConnect) {
dataSource.connectSource(source)
} else {
dataSource.disconnectSource(source)
}
}

Expand Down

0 comments on commit 0e89d8d

Please sign in to comment.