1
0
mirror of https://github.com/nicoverbruggen/phpmon.git synced 2025-08-07 20:10:08 +02:00

️ Fix performance issue with async

This commit is contained in:
2022-03-17 19:18:48 +01:00
parent af9f30a123
commit c2dc6302c0
2 changed files with 20 additions and 16 deletions

View File

@ -20,16 +20,19 @@ struct HomebrewService: Decodable, Equatable {
let error_log_path: String?
public static func loadAll(
filter: [String] = [PhpEnv.phpInstall.formula, "nginx", "dnsmasq"]
) async -> [HomebrewService] {
return try! JSONDecoder().decode(
[HomebrewService].self,
from: Shell.pipe(
"sudo \(Paths.brew) services info --all --json",
requiresPath: true
).data(using: .utf8)!
).filter({ service in
return filter.contains(service.name)
})
filter: [String] = [PhpEnv.phpInstall.formula, "nginx", "dnsmasq"],
completion: @escaping ([HomebrewService]) -> Void
) {
DispatchQueue.global(qos: .background).async {
let data = Shell
.pipe("sudo \(Paths.brew) services info --all --json", requiresPath: true)
.data(using: .utf8)!
let services = try! JSONDecoder()
.decode([HomebrewService].self, from: data)
.filter({ return filter.contains($0.name) })
completion(services)
}
}
}

View File

@ -46,15 +46,16 @@ class ServicesView: NSView, XibLoadable {
}
@objc func updateInformation() {
Task { await self.loadData() }
self.loadData()
}
func loadData() async {
func loadData() {
self.applyAllInfoFieldsFromCachedValue()
let services = await HomebrewService.loadAll()
HomebrewService.loadAll { services in
ServicesView.services = Dictionary(uniqueKeysWithValues: services.map{ ($0.name, $0) })
self.applyAllInfoFieldsFromCachedValue()
}
}
func applyAllInfoFieldsFromCachedValue() {
if ServicesView.services.keys.isEmpty {