CustomNotification.swift 935 B

1234567891011121314151617181920212223242526
  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. static let didUpdateCobIob = Notification.Name("didUpdateCobIob")
  7. }
  8. func awaitNotification(_ name: Notification.Name) async {
  9. await withCheckedContinuation { continuation in
  10. var cancellable: AnyCancellable?
  11. // Create a Combine publisher that listens for notifications
  12. cancellable = Foundation.NotificationCenter.default
  13. .publisher(for: name)
  14. .sink { _ in
  15. // When the notification is received, resume the awaiting task
  16. continuation.resume()
  17. // Cancel the subscription after the continuation has resumed
  18. cancellable?.cancel()
  19. }
  20. }
  21. }