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

View File

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