1
0
mirror of https://github.com/nicoverbruggen/phpmon.git synced 2025-11-07 13:20:05 +01:00

👌 Improve async PHP Doctor

This commit is contained in:
2023-02-25 15:48:01 +01:00
parent e1adcbcde6
commit eb664477f9
2 changed files with 28 additions and 18 deletions

View File

@@ -13,7 +13,7 @@ struct WarningListView: View {
init(empty: Bool = false) { init(empty: Bool = false) {
if empty { if empty {
WarningManager.shared.warnings = [] WarningManager.shared.clearWarnings()
} }
warningManager = WarningManager.shared warningManager = WarningManager.shared
@@ -54,7 +54,7 @@ struct WarningListView: View {
List { List {
VStack(alignment: .leading, spacing: 0) { VStack(alignment: .leading, spacing: 0) {
if warningManager.warnings.isEmpty { if !warningManager.hasWarnings() {
NoWarningsView() NoWarningsView()
} else { } else {
ForEach(warningManager.warnings) { warning in ForEach(warningManager.warnings) { warning in

View File

@@ -13,6 +13,14 @@ class WarningManager: ObservableObject {
static var shared: WarningManager = WarningManager() static var shared: WarningManager = WarningManager()
/// These warnings are the ones that are ready to be displayed.
@Published public var warnings: [Warning] = []
/// This variable is thread-safe and may be modified at any time.
/// When all temporary warnings are set, you may broadcast these changes
/// and they will be sent to the @Published variable via the main thread.
private var temporaryWarnings: [Warning] = []
init() { init() {
if isRunningSwiftUIPreview { if isRunningSwiftUIPreview {
self.warnings = self.evaluations self.warnings = self.evaluations
@@ -60,8 +68,6 @@ class WarningManager: ObservableObject {
) )
] ]
@Published public var warnings: [Warning] = []
public func hasWarnings() -> Bool { public func hasWarnings() -> Bool {
return !warnings.isEmpty return !warnings.isEmpty
} }
@@ -70,33 +76,37 @@ class WarningManager: ObservableObject {
Task { await WarningManager.shared.checkEnvironment() } Task { await WarningManager.shared.checkEnvironment() }
} }
@MainActor func clearWarnings() {
self.warnings = []
}
@MainActor func broadcastWarnings() {
self.warnings = temporaryWarnings
}
/** /**
Checks the user's environment and checks if any special warnings apply. Checks the user's environment and checks if any special warnings apply.
*/ */
func checkEnvironment() async { func checkEnvironment() async {
if ProcessInfo.processInfo.environment["EXTREME_DOCTOR_MODE"] != nil { if ProcessInfo.processInfo.environment["EXTREME_DOCTOR_MODE"] != nil {
// For debugging purposes, we may wish to see all possible evaluations listed self.temporaryWarnings = self.evaluations
Task { @MainActor in await self.broadcastWarnings()
self.warnings = self.evaluations return
}
} else {
// Otherwise, loop over the actual evaluations and list the warnings
await loopOverEvaluations()
} }
await evaluate()
await MainMenu.shared.rebuild() await MainMenu.shared.rebuild()
} }
private func loopOverEvaluations() async { private func evaluate() async {
Task { @MainActor in self.temporaryWarnings = []
self.warnings = []
}
for check in self.evaluations where await check.applies() { for check in self.evaluations where await check.applies() {
Log.info("[DOCTOR] \(check.name) (!)") Log.info("[DOCTOR] \(check.name) (!)")
Task { @MainActor in self.temporaryWarnings.append(check)
self.warnings.append(check)
}
continue continue
} }
await self.broadcastWarnings()
} }
} }