fix: fixed a race condition when switching the Unknown sensors (#1241)

This commit is contained in:
Serhiy Mytrovtsiy
2022-12-22 18:13:34 +01:00
parent 3bd0160cf2
commit 1680b9331b
5 changed files with 27 additions and 32 deletions

View File

@@ -40,9 +40,6 @@ public class Sensors: Module {
self.sensorsReader.setInterval(value)
}
self.settingsView.HIDcallback = { [unowned self] in
self.sensorsReader.HIDCallback()
self.popupView.setup(self.sensorsReader.list)
self.settingsView.setList(list: self.sensorsReader.list)
DispatchQueue.global(qos: .background).async {
self.sensorsReader.HIDCallback()
DispatchQueue.main.async {
@@ -52,9 +49,6 @@ public class Sensors: Module {
}
}
self.settingsView.unknownCallback = { [unowned self] in
self.sensorsReader.unknownCallback()
self.popupView.setup(self.sensorsReader.list)
self.settingsView.setList(list: self.sensorsReader.list)
DispatchQueue.global(qos: .background).async {
self.sensorsReader.unknownCallback()
DispatchQueue.main.async {

View File

@@ -17,6 +17,10 @@ internal class Popup: NSStackView, Popup_p {
public var sizeCallback: ((NSSize) -> Void)? = nil
private var unknownSensorsState: Bool {
return Store.shared.bool(key: "Sensors_unknown", defaultValue: false)
}
public init() {
super.init(frame: NSRect( x: 0, y: 0, width: Constants.Popup.width, height: 0))
@@ -30,9 +34,12 @@ internal class Popup: NSStackView, Popup_p {
internal func setup(_ values: [Sensor_p]?) {
guard let fans = values?.filter({ $0.type == .fan }),
let sensors = values?.filter({ $0.type != .fan }) else {
var sensors = values?.filter({ $0.type != .fan }) else {
return
}
if !self.unknownSensorsState {
sensors = sensors.filter({ $0.group != .unknown })
}
self.subviews.forEach { (v: NSView) in
v.removeFromSuperview()

View File

@@ -34,11 +34,10 @@ internal class SensorsReader: Reader<[Sensor_p]> {
private var HIDState: Bool {
return Store.shared.bool(key: "Sensors_hid", defaultValue: false)
}
private var unknownSensorsState: Bool {
return Store.shared.bool(key: "Sensors_unknown", defaultValue: false)
}
private var unknownSensorsState: Bool
init() {
self.unknownSensorsState = Store.shared.bool(key: "Sensors_unknown", defaultValue: false)
super.init()
self.list = self.sensors()
}
@@ -84,19 +83,17 @@ internal class SensorsReader: Reader<[Sensor_p]> {
}
}
}
if self.unknownSensorsState {
available.forEach { (key: String) in
var type: SensorType? = nil
switch key.prefix(1) {
case "T": type = .temperature
case "V": type = .voltage
case "P": type = .power
case "I": type = .current
default: type = nil
}
if let t = type {
list.append(Sensor(key: key, name: key, group: .unknown, type: t, platforms: []))
}
available.forEach { (key: String) in
var type: SensorType? = nil
switch key.prefix(1) {
case "T": type = .temperature
case "V": type = .voltage
case "P": type = .power
case "I": type = .current
default: type = nil
}
if let t = type {
list.append(Sensor(key: key, name: key, group: .unknown, type: t, platforms: []))
}
}
@@ -129,12 +126,9 @@ internal class SensorsReader: Reader<[Sensor_p]> {
}
public override func read() {
let sensorsCounter = self.list.count
for (i, s) in self.list.enumerated() {
guard self.list.count == sensorsCounter else { return }
if s.group == .hid || s.isComputed {
continue
}
for i in self.list.indices {
guard self.list[i].group != .hid && !self.list[i].isComputed else { continue }
if !self.unknownSensorsState && self.list[i].group == .unknown { continue }
self.list[i].value = SMC.shared.getValue(self.list[i].key) ?? 0
}
@@ -274,7 +268,7 @@ internal class SensorsReader: Reader<[Sensor_p]> {
}
public func unknownCallback() {
self.list = self.sensors()
self.unknownSensorsState = Store.shared.bool(key: "Sensors_unknown", defaultValue: false)
}
}

View File

@@ -166,7 +166,7 @@ internal class Settings: NSStackView, Settings_v {
}
public func setList(list: [Sensor_p]) {
self.list = list
self.list = self.unknownSensorsState ? list : list.filter({ $0.group != .unknown })
self.load(widgets: self.widgets)
}

View File

@@ -17,7 +17,7 @@
<key>CFBundleShortVersionString</key>
<string>$(MARKETING_VERSION)</string>
<key>CFBundleVersion</key>
<string>448</string>
<string>449</string>
<key>Description</key>
<string>Simple macOS system monitor in your menu bar</string>
<key>LSApplicationCategoryType</key>