Created
June 23, 2024 00:49
-
-
Save mattmassicotte/5adf94b59594d8557828bbba4800aab9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
extension NotificationCenter { | |
public func addOIsolatedObserver<Payload>( | |
forName name: Notification.Name, | |
object obj: Any? = nil, | |
isolation: (any Actor)? = #isolation, | |
process: @Sendable @escaping (Notification) -> Payload, | |
using block: @Sendable @escaping (Payload) -> Void | |
) -> any NSObjectProtocol where Payload : Sendable { | |
return self.addObserver(forName: name, object: obj, queue: .main) { note in | |
let payload = process(note) | |
isolation?.assumeIsolated { _ in | |
block(payload) | |
} | |
} | |
} | |
} |
I wrote this when I was tried and it is very wrong.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Optional is clearly a bug…