Skip to content

Commit

Permalink
Update NotificationCenter+Transformers.swift
Browse files Browse the repository at this point in the history
  • Loading branch information
rismay-luassat committed Jun 25, 2024
1 parent 8615946 commit ae3e587
Showing 1 changed file with 6 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import Foundation
#endif

extension Notification {

/// A structure that represents a transformer for notifications, allowing for type-safe handling.
///
/// It includes the notification name and a transformation function that converts a Notification
Expand All @@ -28,17 +27,17 @@ extension Notification {
name: Notification.Name,
transform: @escaping ((Notification) -> A) = {
(A.self == Void.self ? () : $0.object) as! A // swiftlint:disable:this force_cast
})
{
}
) {
self.name = name
self.transform = transform
}
}

/// A token that represents a subscription to a notification.
///
/// Automatically deregisters itself from the notification center upon deinitialization.
/// This helps manage the lifecycle of notification observers in a more controlled and automatic way.
/// Automatically deregisters itself from the notification center upon deinitialization. This
/// helps manage the lifecycle of notification observers in a more controlled and automatic way.
///
/// - Parameters:
/// - token: The observer token returned by the notification center.
Expand All @@ -59,7 +58,6 @@ extension Notification {
}

extension NotificationCenter {

/// Adds an observer for a given transformer and executes a block when the notification is posted.
///
/// The block receives the transformed notification content as defined in the transformer.
Expand All @@ -72,8 +70,8 @@ extension NotificationCenter {
public func addObserver<A>(
for transformer: Notification.Transformer<A>,
queue: OperationQueue? = .main,
using block: @escaping (A) -> Void) -> Notification.Token
{
using block: @escaping (A) -> Void
) -> Notification.Token {
let token = addObserver(forName: transformer.name, object: nil, queue: queue) { note in
block(transformer.transform(note))
}
Expand Down

0 comments on commit ae3e587

Please sign in to comment.