Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Sources/PlexBar/App/PlexBarApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ struct PlexBarApp: App {
@State private var historyStore: PlexHistoryStore
@State private var libraryStore: PlexLibraryStore
@State private var serverPreviewStore: PlexServerPreviewStore
private let systemLifecycleObserver: PlexSystemLifecycleObserver
private let updateService: PlexUpdateService

init() {
Expand Down Expand Up @@ -52,6 +53,9 @@ struct PlexBarApp: App {
libraryStore: libraryStore,
client: runtime.authClient
))
systemLifecycleObserver = PlexSystemLifecycleObserver {
sessionStore.refreshNow()
}
updateService = PlexUpdateService()
}

Expand Down
26 changes: 26 additions & 0 deletions Sources/PlexBar/Support/PlexSystemLifecycleObserver.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import AppKit

final class PlexSystemLifecycleObserver {
private let notificationCenter: NotificationCenter
private let didWakeObserver: NSObjectProtocol

init(
notificationCenter: NotificationCenter = NSWorkspace.shared.notificationCenter,
onDidWake: @escaping @MainActor () -> Void
) {
self.notificationCenter = notificationCenter
didWakeObserver = notificationCenter.addObserver(
forName: NSWorkspace.didWakeNotification,
object: nil,
queue: .main
) { _ in
Task { @MainActor in
onDidWake()
}
}
}

deinit {
notificationCenter.removeObserver(didWakeObserver)
}
}
56 changes: 56 additions & 0 deletions Tests/PlexBarTests/PlexSystemLifecycleObserverTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import AppKit
import Foundation
import Testing
@testable import PlexBar

@MainActor
@Test func systemWakeNotificationRunsWakeHandler() async throws {
let notificationCenter = NotificationCenter()
let counter = LifecycleObserverCounter()
do {
let observer = PlexSystemLifecycleObserver(
notificationCenter: notificationCenter
) {
counter.increment()
}

notificationCenter.post(name: NSWorkspace.didWakeNotification, object: nil)

await waitForLifecycleObserver {
counter.value == 1
}

#expect(counter.value == 1)
withExtendedLifetime(observer) {}
}

notificationCenter.post(name: NSWorkspace.didWakeNotification, object: nil)
try await Task.sleep(for: .milliseconds(50))

#expect(counter.value == 1)
}

@MainActor
private func waitForLifecycleObserver(
timeoutNanoseconds: UInt64 = 2_000_000_000,
condition: @escaping @MainActor () -> Bool
) async {
let deadline = DispatchTime.now().uptimeNanoseconds + timeoutNanoseconds

while DispatchTime.now().uptimeNanoseconds < deadline {
if condition() {
return
}

try? await Task.sleep(nanoseconds: 10_000_000)
}
}

@MainActor
private final class LifecycleObserverCounter {
private(set) var value = 0

func increment() {
value += 1
}
}
Loading