CustomNotification.swift 865 B

12345678910111213141516171819202122232425
  1. import Combine
  2. import Foundation
  3. extension Notification.Name {
  4. static let willUpdateOverrideConfiguration = Notification.Name("willUpdateOverrideConfiguration")
  5. static let didUpdateOverrideConfiguration = Notification.Name("didUpdateOverrideConfiguration")
  6. }
  7. func awaitNotification(_ name: Notification.Name) async {
  8. await withCheckedContinuation { continuation in
  9. var cancellable: AnyCancellable?
  10. // Create a Combine publisher that listens for notifications
  11. cancellable = Foundation.NotificationCenter.default
  12. .publisher(for: name)
  13. .sink { _ in
  14. // When the notification is received, resume the awaiting task
  15. continuation.resume()
  16. // Cancel the subscription after the continuation has resumed
  17. cancellable?.cancel()
  18. }
  19. }
  20. }