Skip to content

Commit

Permalink
Merge pull request #387 from Planetable/fixes-vapor-api-sleep
Browse files Browse the repository at this point in the history
Monitor system sleep/awake event.
  • Loading branch information
livid authored Oct 15, 2024
2 parents d9b133c + f4cf342 commit a7f32ab
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
6 changes: 6 additions & 0 deletions Planet/API/PlanetAPIController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,12 @@ class PlanetAPIController: NSObject, ObservableObject {
}
}
}

func pauseServerForSleep() {
globalApp?.shutdown()
globalApp = nil
stopBonjourService()
}

func startBonjourService() {
if bonjourService == nil {
Expand Down
19 changes: 19 additions & 0 deletions Planet/PlanetAppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,25 @@ class PlanetAppDelegate: NSObject, NSApplicationDelegate {
await PlanetStore.shared.aggregate()
}
}

// Notify API server if system is going to sleep / awake
NotificationCenter.default.addObserver(forName: NSWorkspace.willSleepNotification, object: nil, queue: nil) { _ in
if UserDefaults.standard.bool(forKey: .settingsAPIEnabled) {
Task { @MainActor in
PlanetAPIController.shared.pauseServerForSleep()
}
}
}
NotificationCenter.default.addObserver(forName: NSWorkspace.didWakeNotification, object: nil, queue: nil) { _ in
if UserDefaults.standard.bool(forKey: .settingsAPIEnabled) {
Task.detached(priority: .utility) {
try? await Task.sleep(nanoseconds: 1_000_000_000)
Task { @MainActor in
PlanetAPIController.shared.startServer()
}
}
}
}
}

func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool {
Expand Down
2 changes: 1 addition & 1 deletion Planet/versioning.xcconfig
Original file line number Diff line number Diff line change
@@ -1 +1 @@
CURRENT_PROJECT_VERSION = 2276
CURRENT_PROJECT_VERSION = 2277

0 comments on commit a7f32ab

Please sign in to comment.