From 638c5942c06a66a69ab44ca5ab4e2bd77e5a2297 Mon Sep 17 00:00:00 2001 From: Sami Samhuri Date: Sat, 19 Apr 2025 13:25:59 -0700 Subject: [PATCH] Add Hashable conformance --- .../NotificationTask/NotificationTask.swift | 23 +++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/Sources/NotificationTask/NotificationTask.swift b/Sources/NotificationTask/NotificationTask.swift index 2cc2e36..9daad1d 100644 --- a/Sources/NotificationTask/NotificationTask.swift +++ b/Sources/NotificationTask/NotificationTask.swift @@ -14,8 +14,8 @@ extension Notification: @unchecked @retroactive Sendable {} /// always receives a strong reference. This one is called ``init(name:context:center:performing:)``. /// /// ``NotificationTask`` is bound to the main actor and is intended to be used in your view layer. This keeps it simple -@MainActor public final class NotificationTask { - var task: Task? +@MainActor public final class NotificationTask: Hashable { + let task: Task init(task: Task) { self.task = task @@ -49,7 +49,22 @@ extension Notification: @unchecked @retroactive Sendable {} } deinit { - task?.cancel() - task = nil + task.cancel() + } + + public func store(in set: inout Set) { + set.insert(self) + } +} + +// MARK: - Hashable conformance + +public extension NotificationTask { + nonisolated static func == (lhs: NotificationTask, rhs: NotificationTask) -> Bool { + ObjectIdentifier(lhs) == ObjectIdentifier(rhs) + } + + nonisolated func hash(into hasher: inout Hasher) { + hasher.combine(task) } }