Skip to content

Instantly share code, notes, and snippets.

@mdb1
Created August 12, 2023 23:18
Show Gist options
  • Save mdb1/623b7bc22ae2136dcdfae8ba56e39bc2 to your computer and use it in GitHub Desktop.
Save mdb1/623b7bc22ae2136dcdfae8ba56e39bc2 to your computer and use it in GitHub Desktop.
Notification Center Publisher protocol
/// Reflects notification publisher functionality of `NotificationCenter`.
public protocol NotificationPublisherProtocol: AnyObject {
/// Returns a publisher that emits events when broadcasting notifications.
/// - Parameters:
/// - name: The name of the notification to publish.
/// - object: The object posting the named notification. If `nil`, the publisher emits elements for any object
/// producing a notification with the given name.
/// - Returns: A publisher that emits events when broadcasting notifications.
func publisher(for name: Notification.Name, object: AnyObject?) -> NotificationCenter.Publisher
}
public extension NotificationPublisherProtocol {
/// Returns a publisher that emits events when broadcasting notifications.
/// - Parameters:
/// - name: The name of the notification to publish.
/// - Returns: A publisher that emits events when broadcasting notifications.
func publisher(for name: Notification.Name) -> NotificationCenter.Publisher {
publisher(for: name, object: nil)
}
}
extension NotificationCenter: NotificationPublisherProtocol {}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment